diff --git a/structures/models/owners.py b/structures/models/owners.py index 4677f78..9a39c9e 100644 --- a/structures/models/owners.py +++ b/structures/models/owners.py @@ -633,6 +633,11 @@ def _fetch_universe_infos_for_structures( self, token: Token, structures: List[dict] ) -> bool: count = 0 + existing_names = dict( + self.structures.filter( + id__in=[s["structure_id"] for s in structures] + ).values_list("id", "name") + ) for s in structures: try: structure_info_obj = ( @@ -654,7 +659,8 @@ def _fetch_universe_infos_for_structures( self._report_esi_issue( f"fetch structure #{s['structure_id']}", ex, token ) - s["name"] = "(no data)" + # Don't clobber a previously resolved name on a transient failure. + s["name"] = existing_names.get(s["structure_id"]) or "(no data)" else: structure_info = structure_info_obj.model_dump() count += 1 diff --git a/structures/tests/models/test_owners_2.py b/structures/tests/models/test_owners_2.py index cf9ce06..9aff168 100644 --- a/structures/tests/models/test_owners_2.py +++ b/structures/tests/models/test_owners_2.py @@ -343,6 +343,50 @@ def test_update_will_not_break_on_403_error_from_structure_info(self): structure = Structure.objects.get(id=structure_id) self.assertEqual(structure.name, "(no data)") + @pook.on + def test_should_keep_last_known_name_when_universe_endpoint_fails(self): + # given + owner = OwnerFactory(user=self.user, structures_last_update_at=None) + structure_id = 1000000000001 + solar_system = EveSolarSystemFactory() + structure_type = CitadelTypeFactory() + StructureFactory( + id=structure_id, + owner=owner, + eve_type=structure_type, + eve_solar_system=solar_system, + name="My Awesome Structure", + ) + pook.get( + f"https://esi.evetech.net/corporations/{self.corporation_id}/structures", + reply=200, + response_json=[ + { + "corporation_id": self.corporation_id, + "profile_id": 101853, + "reinforce_hour": 18, + "state": "shield_vulnerable", + "structure_id": structure_id, + "system_id": solar_system.id, + "type_id": structure_type.id, + }, + ], + ) + pook.get( + f"https://esi.evetech.net/universe/structures/{structure_id}", + reply=500, + response_json={"error": "internal_error"}, + ) + + # when + owner.update_structures_esi() + + # then + owner.refresh_from_db() + self.assertFalse(owner.is_structure_sync_fresh) + structure = Structure.objects.get(id=structure_id) + self.assertEqual(structure.name, "My Awesome Structure") + @pook.on def test_tags_are_not_modified_by_update(self): # given