from django.test import TestCase
from ..services.user_service import UserService

class TestUserAdd(TestCase):
    def setUp(self):
        # 初始化测试数据
        self.test_params = {
            "account": "testuser",
            "password": "testpassword",
            "fullname": "Test User",
            "nickname": "testnick",
            "user_type": "PERSON",
            "phone": "1234567890",
            "email": "test@example.com"
        }

    def test_user_add_with_valid_params(self):
        # 测试带有效参数的 user_add 方法
        # 确保手机号格式正确
        self.test_params["phone"] = "13800138000"  # 使用有效的手机号格式
        result, err = UserService.user_add(params=self.test_params)
        print("> TestUserAdd: test_user_add_with_valid_params:", result, err)
        self.assertEqual(err, None)
        self.assertIn('id', result)
        self.assertIn('username', result)
        self.assertIn('fullname', result)
        self.assertIn('nickname', result)
        self.assertIn('phone', result)
        self.assertIn('email', result)

    def test_user_add_with_missing_account(self):
        # 测试缺少 account 参数的 user_add 方法
        invalid_params = self.test_params.copy()
        invalid_params.pop("account")
        result, err = UserService.user_add(params=invalid_params)
        print("> TestUserAdd: test_user_add_with_missing_account:", result, err)
        self.assertNotEqual(err, None)
        self.assertEqual(result, None)

    def test_user_add_with_invalid_phone(self):
        # 测试带无效手机号格式的 user_add 方法
        invalid_params = self.test_params.copy()
        invalid_params["phone"] = "invalid_phone"
        result, err = UserService.user_add(params=invalid_params)
        print("> TestUserAdd: test_user_add_with_invalid_phone:", result, err)
        self.assertNotEqual(err, None)
        self.assertEqual(result, None)

    def test_user_add_with_invalid_email(self):
        # 测试带无效邮箱格式的 user_add 方法
        invalid_params = self.test_params.copy()
        invalid_params["email"] = "invalid_email"
        result, err = UserService.user_add(params=invalid_params)
        print("> TestUserAdd: test_user_add_with_invalid_email:", result, err)
        self.assertNotEqual(err, None)
        self.assertEqual(result, None)

    def test_user_add_with_existing_account(self):
        # 测试带已存在账号的 user_add 方法
        # 先创建一个用户
        UserService.user_add(params=self.test_params)
        # 再次尝试创建相同账号的用户
        result, err = UserService.user_add(params=self.test_params)
        print("> TestUserAdd: test_user_add_with_existing_account:", result, err)
        self.assertNotEqual(err, None)
        self.assertEqual(result, None)

    def test_user_add_with_default_values(self):
        # 测试带默认值的 user_add 方法
        # 只提供必填参数，其他参数使用默认值
        minimal_params = {
            "account": "testuser2",
            "password": "testpassword"
        }
        result, err = UserService.user_add(params=minimal_params)
        print("> TestUserAdd: test_user_add_with_default_values:", result, err)
        self.assertEqual(err, None)
        self.assertIn('id', result)
        self.assertIn('username', result)
        self.assertIn('fullname', result)
        self.assertIn('nickname', result)
        self.assertIn('phone', result)
        self.assertIn('email', result)