class TestLoadFileMakerDatabase(unittest.TestCase):
    def setUp(self):
        call_command('loaddata', 'language.json')
        call_command('loaddata', 'linkeddatatypes.json')
        self.parser = FMPDSOParser(DatabaseHandler())

    def test_parse_authority(self):
        self.parser.parse('authority', 'isisdata/tests/data/chunked/authority.xml', ['person'])
        authority = Authority.objects.get(pk='CBA000167135')
        self.assertEqual(authority.created_by_fm, 'admin')
        self.assertEqual(authority.name, 'Perugia: Edizioni Centro Stampa')
        self.assertNotEqual(authority.record_history, '')
        self.assertTrue(authority.public)
        self.assertEqual(authority.record_status_value, CuratedMixin.ACTIVE)
        self.assertEqual(authority.type_controlled, Authority.PUBLISHER)

    def test_parse_attribute(self):
        # Try to trip things up by creating back AttributeTypes ahead of time.
        AttributeType.objects.create(
            name='PublicationDate',
            display_name='Publication Date',
            value_content_type=ContentType.objects.get_for_model(DateValue)
        )
        AttributeType.objects.create(
            name='BirthToDeathDates',
            display_name='BirthToDeathDates',
            value_content_type=ContentType.objects.get_for_model(DateValue)
        )

        citations = [
            'CBB000111626', 'CBB000110001', 'CBB000110002', 'CBB000110003',
            'CBB000110004', 'CBB000110005' 'CBB000110006', 'CBB000110007',
            'CBB000110008', 'CBB000110009', 'CBB000110010', 'CBB000110011',
            'CBB000110012']
        authorities = [
            'CBA000073481', 'CBA000018872', 'CBA000019231', 'CBA000060556',
            'CBA000042082',
        ]
        for citation_id in citations:
            Citation.objects.create(
                id=citation_id,
                type_controlled=Citation.ARTICLE,
                title='A Test Citation'
            )
        for authority_id in authorities:
            Authority.objects.create(
                id=authority_id,
                type_controlled=Authority.PERSON,
                name='A Test Authority'
            )
        self.parser.parse('attribute', 'isisdata/tests/data/chunked/attribute.xml', [])
        attribute = Attribute.objects.get(pk='ATT000204037')
        self.assertEqual(attribute.source_instance_id, 'CBA000042082')
        self.assertIsInstance(attribute.value.get_child_class(), ISODateRangeValue)
        self.assertEqual(attribute.value.get_child_class().value, [[1916]])
        self.assertEqual(
            attribute.type_controlled.value_content_type.id,
            ContentType.objects.get_for_model(ISODateRangeValue).id)

        attribute = Attribute.objects.get(pk='ATT000204034')
        self.assertEqual(attribute.source_instance_id, 'CBA000018872')
        self.assertIsInstance(attribute.value.get_child_class(), ISODateRangeValue)
        self.assertEqual(attribute.value.get_child_class().value, [[1916], [1972]])
        self.assertEqual(
            attribute.type_controlled.value_content_type.id,
            ContentType.objects.get_for_model(ISODateRangeValue).id)

        attribute = Attribute.objects.get(pk='ATT000000021')
        self.assertEqual(attribute.source_instance_id, 'CBB000110012')
        self.assertIsInstance(attribute.value.get_child_class(), ISODateValue)
        self.assertEqual(attribute.value.get_child_class().value, [2000])
        self.assertEqual(
            attribute.type_controlled.value_content_type.id,
            ContentType.objects.get_for_model(ISODateValue).id)

        attribute = Attribute.objects.get(pk='ATT000000009')
        self.assertEqual(attribute.value.cvalue(), [2000, 5, 3])


    def tearDown(self):
        Value.objects.all().delete()
        DateTimeValue.objects.all().delete()
        DateRangeValue.objects.all().delete()
        AttributeType.objects.all().delete()
        Attribute.objects.all().delete()
        ACRelation.objects.all().delete()
        CCRelation.objects.all().delete()
        Authority.objects.all().delete()
        Citation.objects.all().delete()

        Language.objects.all().delete()
        LinkedDataType.objects.all().delete()
        LinkedData.objects.all().delete()
 def setUp(self):
     call_command('loaddata', 'language.json')
     call_command('loaddata', 'linkeddatatypes.json')
     self.parser = FMPDSOParser(DatabaseHandler())
 def test_parse_acrelation(self):
     parser = FMPDSOParser(self.handler)
     parser.parse('acrelation', 'isisdata/tests/data/chunked/acrelation.xml', [])
 def test_parse_attribute(self):
     parser = FMPDSOParser(self.handler)
     parser.parse('attribute', 'isisdata/tests/data/chunked/attribute.xml', [])
 def test_parse_citation(self):
     parser = FMPDSOParser(self.handler)
     parser.parse('citation', 'isisdata/tests/data/chunked/citation.xml', ['partdetails'])
 def test_parse_authority(self):
     parser = FMPDSOParser(self.handler)
     parser.parse('authority', 'isisdata/tests/data/chunked/authority.xml', ['person'])
 def test_handle_language(self):
     parser = FMPDSOParser(dict())
     result = parser._handle_language('authority', 'Language', 'English')
     self.assertEqual(result, 'en')
 def test_map_field_value(self):
     parser = FMPDSOParser(dict())
     result = dict(parser._map_field_value('authority', 'RecordStatus', 'Inactive'))
     self.assertFalse(result['public'])
     self.assertEqual(result['record_status_value'], 'Inactive')
 def test_handle_record_status(self):
     public, status, explanation = FMPDSOParser._handle_record_status('authority', 'record_status', 'Inactive')
     self.assertFalse(public)
     self.assertEqual(status, 'Inactive')
     self.assertEqual(explanation, '')