def test_relationships_from_gsx_entry(
        self,
        entry_original: Dict[str, Dict[str, str]],
        entry_translation: Dict[str, Dict[str, str]],
        entry_edition: Dict[str, Dict[str, str]],
    ):
        assert Resource.relationships_from_gsx_entry(None) is None

        Resource.from_gsx_entry(entry_original)
        resource = Resource.relationships_from_gsx_entry(entry_original)
        assert resource is not None
        assert resource.relationships.count() == 0

        Resource.from_gsx_entry(entry_translation)
        resource = Resource.relationships_from_gsx_entry(entry_translation)
        assert resource is not None
        assert resource.relationships.count() == 1

        entry = defaultdict(defaultdict)
        entry["gsx$title"]["$t"] = "Discours sur le gouvernement"
        Resource.from_gsx_entry(entry)
        entry = defaultdict(defaultdict)
        entry["gsx$title"]["$t"] = "Discourses concerning government"
        Resource.from_gsx_entry(entry)

        Resource.from_gsx_entry(entry_edition)
        resource = Resource.relationships_from_gsx_entry(entry_edition)
        assert resource is not None
        assert resource.relationships.count() == 2
    def test_is_translation(
        self,
        entry_original: Dict[str, Dict[str, str]],
        entry_translation: Dict[str, Dict[str, str]],
        entry_edition: Dict[str, Dict[str, str]],
    ):
        Resource.from_gsx_entry(entry_original)
        resource = Resource.relationships_from_gsx_entry(entry_original)
        assert resource.is_translation() is False

        Resource.from_gsx_entry(entry_translation)
        resource = Resource.relationships_from_gsx_entry(entry_translation)
        assert resource.is_translation() is True

        Resource.from_gsx_entry(entry_edition)
        resource = Resource.relationships_from_gsx_entry(entry_edition)
        assert resource.is_translation() is True
    def test_get_languages_source_text(
        self,
        entry_original: Dict[str, Dict[str, str]],
        entry_translation: Dict[str, Dict[str, str]],
    ):
        original = Resource.from_gsx_entry(entry_original)
        assert original.get_languages_source_text() is None

        Resource.from_gsx_entry(entry_translation)
        translation = Resource.relationships_from_gsx_entry(entry_translation)
        languages = translation.get_languages_source_text()
        assert languages is not None
        assert languages[0].label == "French"
    def test_get_source_texts(
        self,
        entry_original: Dict[str, Dict[str, str]],
        entry_translation: Dict[str, Dict[str, str]],
    ):
        original = Resource.from_gsx_entry(entry_original)
        assert original.get_source_texts() is None

        Resource.from_gsx_entry(entry_translation)
        translation = Resource.relationships_from_gsx_entry(entry_translation)
        source_texts = translation.get_source_texts()
        assert source_texts is not None
        assert "Les ruines" in source_texts[0].title.main_title
    def test_get_authors_source_text(
        self,
        entry_original: Dict[str, Dict[str, str]],
        entry_translation: Dict[str, Dict[str, str]],
    ):
        resource = Resource.from_gsx_entry(entry_original)
        assert resource.get_authors_source_text() is None

        Resource.from_gsx_entry(entry_translation)
        resource = Resource.relationships_from_gsx_entry(entry_translation)
        authors = resource.get_authors_source_text()
        assert authors is not None
        assert "Constantin" in authors[0].name
    def handle(self, *args, **options):
        url = options["url"][0]
        delete = options["delete"]

        with request.urlopen(url) as response:
            data = json.loads(response.read().decode())
            entries = data["feed"]["entry"]

            if delete:
                self.stdout.write(
                    self.style.WARNING("Deleting all resources..."))
                Resource.objects.all().delete()

            self.stdout.write("Importing resources...")
            for entry in entries:
                resource = Resource.from_gsx_entry(entry)
                if resource:
                    self.stdout.write(self.style.SUCCESS(f"- {resource}"))

            self.stdout.write("Importing resource relationships...")
            for entry in entries:
                resource = Resource.relationships_from_gsx_entry(entry)
                if resource:
                    self.stdout.write(self.style.SUCCESS(f"> {resource}"))