Changeset 521

Show
Ignore:
Timestamp:
02/01/10 19:36:22 (5 weeks ago)
Author:
olivier
Message:

fix unit tests for new location models

Location:
trunk/telemeta
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/telemeta/models/crem.py

    r520 r521  
    557557 
    558558    name             = CharField(_('name'), unique=True, max_length=150, required=True) 
    559     type             = IntegerField(_('type'), choices=TYPE_CHOICES, required=True, db_index=True) 
     559    type             = IntegerField(_('type'), choices=TYPE_CHOICES, default=OTHER_TYPE, db_index=True) 
    560560    complete_type    = ForeignKey('LocationType', related_name="locations", verbose_name=_('complete type')) 
    561561    current_location = WeakForeignKey('self', related_name="past_names",  
     
    577577        return Location.objects.filter(ancestor_relations__ancestor_location=self) 
    578578 
     579    def add_child(self, other): 
     580        LocationRelation.objects.create(location=other, ancestor_location=self, is_direct=True) 
     581        for location in self.ancestors(): 
     582            #FIXME: might raise Duplicate Entry 
     583            LocationRelation.objects.create(location=other, ancestor_location=location) 
     584             
     585    def add_parent(self, other): 
     586        LocationRelation.objects.create(location=self, ancestor_location=other, is_direct=True) 
     587        for location in self.descendants(): 
     588            #FIXME: might raise Duplicate Entry 
     589            LocationRelation.objects.create(location=location, ancestor_location=other) 
     590 
    579591    def countries(self): 
    580592        if self.type == self.COUNTRY: 
    581593            return Location.objects.filter(pk=self.id) 
    582594        return self.ancestors().filter(type=self.COUNTRY) 
     595 
     596    def continents(self): 
     597        if self.type == self.CONTINENT: 
     598            return Location.objects.filter(pk=self.id) 
     599        return self.ancestors().filter(type=self.CONTINENT) 
    583600 
    584601    class Meta(MetaCore): 
  • trunk/telemeta/models/cremquery.py

    r520 r521  
    181181        for lid in MediaItem.objects.filter(location__isnull=False).values_list('location', flat=True).distinct(): 
    182182            location = Location.objects.get(pk=lid) 
    183             if not only_continent or (only_continent in location.ancestors().filter(type=Location.CONTINENT)): 
     183            if not only_continent or (only_continent in location.continents()): 
    184184                for l in location.countries(): 
    185185                    if not l in countries: 
     
    190190        for country in countries: 
    191191            count = country.collections().count() 
    192             for continent in country.ancestors().filter(type=Location.CONTINENT): 
     192            for continent in country.continents(): 
    193193                if not stat.has_key(continent): 
    194194                    stat[continent] = {} 
  • trunk/telemeta/tests/model_tests.py

    r513 r521  
    4646        self.olivier = User.objects.create(username="olivier", level="admin")     
    4747 
    48         self.country = LocationType.objects.create(id="country", name="Country") 
    49         self.continent = LocationType.objects.create(id="continent", name="Continent") 
    50         self.city = LocationType.objects.create(id="city", name="City") 
    51  
    52         self.paris = Location.objects.create(name="Paris", type="other", complete_type=self.city) 
    53         self.france = Location.objects.create(name="France", type="country", complete_type=self.country) 
    54         self.europe = Location.objects.create(name="Europe", type="continent", complete_type=self.continent) 
    55         self.belgique = Location.objects.create(name="Belgique", type="country", complete_type=self.country) 
    56  
    57         LocationRelation.objects.create(location=self.paris, parent_location=self.france) 
    58         LocationRelation.objects.create(location=self.france, parent_location=self.europe) 
     48        self.country = LocationType.objects.create(code="country", name="Country") 
     49        self.continent = LocationType.objects.create(code="continent", name="Continent") 
     50        self.city = LocationType.objects.create(code="city", name="City") 
     51 
     52        self.paris = Location.objects.create(name="Paris", type=Location.OTHER_TYPE, complete_type=self.city) 
     53        self.france = Location.objects.create(name="France", type=Location.COUNTRY, complete_type=self.country) 
     54        self.europe = Location.objects.create(name="Europe", type=Location.CONTINENT, complete_type=self.continent) 
     55        self.belgique = Location.objects.create(name="Belgique", type=Location.COUNTRY, complete_type=self.country) 
     56 
     57        self.europe.add_child(self.france) 
     58        self.france.add_child(self.paris) 
     59        self.europe.add_child(self.belgique) 
    5960 
    6061        self.a = EthnicGroup.objects.create(name="a") 
     
    166167    def testLocationSearch(self): 
    167168        "Test by_country and by_continent properties of MediaCollection class" 
    168         self.assertEquals(self.collections.by_country("France")[0], self.persepolis) 
    169         self.assertEquals(self.collections.by_continent("Europe")[0], self.persepolis) 
    170         self.assertEquals(self.collections.by_country("Belgique").order_by("title")[0], self.nicolas) 
    171         self.assertEquals(self.collections.by_country("Belgique").order_by("title")[1], self.volonte) 
     169        self.assertEquals(self.collections.by_location(self.france)[0], self.persepolis) 
     170        self.assertEquals(self.collections.by_location(self.europe)[0], self.persepolis) 
     171        self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[0], self.nicolas) 
     172        self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[1], self.volonte) 
    172173 
    173174    def testRecordingYear(self):  
     
    270271    def testLocationRelation(self): 
    271272        "Test location country and continent resolving" 
    272         self.assertEquals(self.france, self.item_1.location.country()) 
    273         self.assertEquals(self.europe, self.item_1.location.continent()) 
    274         self.assertEquals(self.france, self.item_2.location.country()) 
    275         self.assertEquals(self.europe, self.item_2.location.continent()) 
     273        self.assertEquals(self.france, self.item_1.location.countries()[0]) 
     274        self.assertEquals(self.europe, self.item_1.location.continents()[0]) 
     275        self.assertEquals(self.france, self.item_2.location.countries()[0]) 
     276        self.assertEquals(self.europe, self.item_2.location.continents()[0]) 
    276277 
    277278    def testCollectionCountries(self):