]> git.parisson.com Git - telemeta.git/commitdiff
fix unit tests for new location models
authorolivier <>
Mon, 1 Feb 2010 18:36:22 +0000 (18:36 +0000)
committerolivier <>
Mon, 1 Feb 2010 18:36:22 +0000 (18:36 +0000)
telemeta/models/crem.py
telemeta/models/cremquery.py
telemeta/tests/model_tests.py

index 4797c6927510441ed3785b3c0f335d8c47459de2..b21b4982bd7867310e15255bf2dfbbe88e2b4c0c 100755 (executable)
@@ -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'
 
index fb8f0484419f28cb6616a89823af6a8a58609427..fd4d42e3e0b93fbbfdeeaa7b9824bf1776bcfb3b 100644 (file)
@@ -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] = {}
 
index c75318ed7dcf9bd328abb18be31523e36d9d7ce8..e8473d42f361a1c4132e5bc48490439908f421e0 100644 (file)
@@ -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"