from django.test import TestCase
from ..services.extend_field_service import ExtendFieldService
from ..models import ExtendField

class TestExtendFieldService(TestCase):
    def setUp(self):
        # 初始化测试数据
        self.test_params = {
            "field_index": "field_1",
            "field": "test_field",
            "label": "Test Label",
            "type": "text",
            "unit": "unit",
            "default": "default_value",
            "config": "{}",
            "sort": 1,
            "description": "Test Description"
        }
        # 删除可能存在的旧数据
        ExtendField.objects.filter(field_index=self.test_params["field_index"]).delete()
        self.test_extend_field = ExtendField.objects.create(**self.test_params)

    def test_extend_field_list_with_params(self):
        # 测试带参数的 extend_field_list 方法
        result, err = ExtendFieldService.extend_field_list(params=self.test_params)
        print("> TestExtendFieldService: test_extend_field_list_with_params:", result, err)
        self.assertEqual(err, None)
        self.assertIn('list', result)
        self.assertGreater(len(result['list']), 0)

    def test_extend_field_list_without_params(self):
        # 测试不带参数的 extend_field_list 方法
        result, err = ExtendFieldService.extend_field_list()
        print("> TestExtendFieldService: test_extend_field_list_without_params:", result, err)
        self.assertEqual(err, None)
        self.assertIn('list', result)
        self.assertGreater(len(result['list']), 0)

    def test_extend_field_add(self):
        # 测试 extend_field_add 方法
        new_field_params = {
            "field_index": "field_4",  # 确保这个值是唯一的
            "field": "new_field",
            "label": "New Label",
            "type": "number",
            "unit": "new_unit",
            "default": "new_default",
            "config": "{}",
            "sort": 2,
            "description": "New Description"
        }
        result, err = ExtendFieldService.extend_field_add(new_field_params)
        print("> TestExtendFieldService: test_extend_field_add:", result, err)
        self.assertEqual(err, None)
        self.assertIsNotNone(result)
        self.assertEqual(result['field_index'], new_field_params['field_index'])

    def test_extend_field_add_with_duplicate_field_index(self):
        # 测试 extend_field_add 方法，使用重复的 field_index
        duplicate_field_params = {
            "field_index": "field_1",  # 重复的 field_index
            "field": "duplicate_field",
            "label": "Duplicate Label",
            "type": "text",
            "unit": "unit",
            "default": "default_value",
            "config": "{}",
            "sort": 3,
            "description": "Duplicate Description"
        }
        result, err = ExtendFieldService.extend_field_add(duplicate_field_params)
        print("> TestExtendFieldService: test_extend_field_add_with_duplicate_field_index:", result, err)
        self.assertIsNotNone(err)
        self.assertEqual(err, "field_index 已存在")

    def test_extend_field_edit(self):
        # 测试 extend_field_edit 方法
        edit_params = {
            "label": "Updated Label",
            "description": "Updated Description"
        }
        result, err = ExtendFieldService.extend_field_edit(edit_params, self.test_extend_field.id)
        print("> TestExtendFieldService: test_extend_field_edit:", result, err)
        self.assertEqual(err, None)
        self.assertIsNotNone(result)
        self.assertEqual(result['label'], edit_params['label'])
        self.assertEqual(result['description'], edit_params['description'])

    def test_extend_field_delete(self):
        # 测试 extend_field_delete 方法
        # 删除可能存在的旧数据
        ExtendField.objects.filter(field_index=self.test_params["field_index"]).delete()
        self.test_extend_field = ExtendField.objects.create(**self.test_params)