From be51b238e86244c66d3dbc6d1b9d667933a84250 Mon Sep 17 00:00:00 2001 From: olivier <> Date: Mon, 1 Feb 2010 18:36:22 +0000 Subject: [PATCH] fix unit tests for new location models --- telemeta/models/crem.py | 19 ++++++++++++++++++- telemeta/models/cremquery.py | 4 ++-- telemeta/tests/model_tests.py | 35 ++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/telemeta/models/crem.py b/telemeta/models/crem.py index 4797c692..b21b4982 100755 --- a/telemeta/models/crem.py +++ b/telemeta/models/crem.py @@ -556,7 +556,7 @@ class Location(ModelCore): TYPE_CHOICES = ((COUNTRY, _('country')), (CONTINENT, _('continent')), (OTHER_TYPE, _('other'))) name = CharField(_('name'), unique=True, max_length=150, required=True) - type = IntegerField(_('type'), choices=TYPE_CHOICES, required=True, db_index=True) + type = IntegerField(_('type'), choices=TYPE_CHOICES, default=OTHER_TYPE, db_index=True) complete_type = ForeignKey('LocationType', related_name="locations", verbose_name=_('complete type')) current_location = WeakForeignKey('self', related_name="past_names", verbose_name=_('current location')) @@ -576,11 +576,28 @@ class Location(ModelCore): def descendants(self): return Location.objects.filter(ancestor_relations__ancestor_location=self) + def add_child(self, other): + LocationRelation.objects.create(location=other, ancestor_location=self, is_direct=True) + for location in self.ancestors(): + #FIXME: might raise Duplicate Entry + LocationRelation.objects.create(location=other, ancestor_location=location) + + def add_parent(self, other): + LocationRelation.objects.create(location=self, ancestor_location=other, is_direct=True) + for location in self.descendants(): + #FIXME: might raise Duplicate Entry + LocationRelation.objects.create(location=location, ancestor_location=other) + def countries(self): if self.type == self.COUNTRY: return Location.objects.filter(pk=self.id) return self.ancestors().filter(type=self.COUNTRY) + def continents(self): + if self.type == self.CONTINENT: + return Location.objects.filter(pk=self.id) + return self.ancestors().filter(type=self.CONTINENT) + class Meta(MetaCore): db_table = 'locations' diff --git a/telemeta/models/cremquery.py b/telemeta/models/cremquery.py index fb8f0484..fd4d42e3 100644 --- a/telemeta/models/cremquery.py +++ b/telemeta/models/cremquery.py @@ -180,7 +180,7 @@ class MediaCollectionManager(CoreManager): countries = [] for lid in MediaItem.objects.filter(location__isnull=False).values_list('location', flat=True).distinct(): location = Location.objects.get(pk=lid) - if not only_continent or (only_continent in location.ancestors().filter(type=Location.CONTINENT)): + if not only_continent or (only_continent in location.continents()): for l in location.countries(): if not l in countries: countries.append(l) @@ -189,7 +189,7 @@ class MediaCollectionManager(CoreManager): for country in countries: count = country.collections().count() - for continent in country.ancestors().filter(type=Location.CONTINENT): + for continent in country.continents(): if not stat.has_key(continent): stat[continent] = {} diff --git a/telemeta/tests/model_tests.py b/telemeta/tests/model_tests.py index c75318ed..e8473d42 100644 --- a/telemeta/tests/model_tests.py +++ b/telemeta/tests/model_tests.py @@ -45,17 +45,18 @@ class CollectionItemTestCase(unittest.TestCase): self.david = User.objects.create(username="david", level="user") self.olivier = User.objects.create(username="olivier", level="admin") - self.country = LocationType.objects.create(id="country", name="Country") - self.continent = LocationType.objects.create(id="continent", name="Continent") - self.city = LocationType.objects.create(id="city", name="City") + self.country = LocationType.objects.create(code="country", name="Country") + self.continent = LocationType.objects.create(code="continent", name="Continent") + self.city = LocationType.objects.create(code="city", name="City") - self.paris = Location.objects.create(name="Paris", type="other", complete_type=self.city) - self.france = Location.objects.create(name="France", type="country", complete_type=self.country) - self.europe = Location.objects.create(name="Europe", type="continent", complete_type=self.continent) - self.belgique = Location.objects.create(name="Belgique", type="country", complete_type=self.country) + self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) + self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) + self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) + self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) - LocationRelation.objects.create(location=self.paris, parent_location=self.france) - LocationRelation.objects.create(location=self.france, parent_location=self.europe) + self.europe.add_child(self.france) + self.france.add_child(self.paris) + self.europe.add_child(self.belgique) self.a = EthnicGroup.objects.create(name="a") self.b = EthnicGroup.objects.create(name="b") @@ -165,10 +166,10 @@ class CollectionItemTestCase(unittest.TestCase): def testLocationSearch(self): "Test by_country and by_continent properties of MediaCollection class" - self.assertEquals(self.collections.by_country("France")[0], self.persepolis) - self.assertEquals(self.collections.by_continent("Europe")[0], self.persepolis) - self.assertEquals(self.collections.by_country("Belgique").order_by("title")[0], self.nicolas) - self.assertEquals(self.collections.by_country("Belgique").order_by("title")[1], self.volonte) + self.assertEquals(self.collections.by_location(self.france)[0], self.persepolis) + self.assertEquals(self.collections.by_location(self.europe)[0], self.persepolis) + self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[0], self.nicolas) + self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[1], self.volonte) def testRecordingYear(self): "Test by_recording_year property of MediaCollection class" @@ -269,10 +270,10 @@ class CollectionItemTestCase(unittest.TestCase): def testLocationRelation(self): "Test location country and continent resolving" - self.assertEquals(self.france, self.item_1.location.country()) - self.assertEquals(self.europe, self.item_1.location.continent()) - self.assertEquals(self.france, self.item_2.location.country()) - self.assertEquals(self.europe, self.item_2.location.continent()) + self.assertEquals(self.france, self.item_1.location.countries()[0]) + self.assertEquals(self.europe, self.item_1.location.continents()[0]) + self.assertEquals(self.france, self.item_2.location.countries()[0]) + self.assertEquals(self.europe, self.item_2.location.continents()[0]) def testCollectionCountries(self): "Test the MediaCollection.countries() method" -- 2.39.5