Ejemplo n.º 1
0
    def test_manager(self):
        # start with empty database
        c1 = Country.objects.all()
        self.assertEquals(len(c1), 0)
        
        # create a country, save, fetch again from database and verify
        c1 = Country(name="Canada", iso_2="CA", iso_3="CAN", population = 100000)
        c1.save()
        q1 = Country.objects.get(name="Canada")
        self.assertNotEquals(q1, None)
        self.assertEquals(q1.name, "Canada")
        self.assertEquals(q1.iso_2, "CA")
        self.assertEquals(q1.full_name, "Canada")
        self.assertEquals(q1.slug, "canada")
        self.assertEquals(q1.population, 100000)

        # create second country, save, fetch and verify
        c2 = Country(name="United States", iso_2="US", iso_3="USA", population = 100000)
        c2.full_name = "United States of America"
        c2.save()
        c2 = Country.objects.get(name="United States")
        self.assertEquals(c2.name, "United States")
        self.assertEquals(c2.iso_2, "US")
        self.assertEquals(c2.slug, "united-states")
        self.assertEquals(c2.population, 100000)
        
        # add a continent to an existing country, save, fetch and verify
        c3 = Country.objects.get(name="United States")
        d = Continent(name="North America", code="NA", population=9999999)
        d.save()    
        c3.continent = d
        c3.save()
        c4 = Country.objects.get(continent__slug__exact="north-america")
        self.assertEquals(c4.name, "United States")
Ejemplo n.º 2
0
    def test_manager(self):
        # database has no city
        c = City.objects.all()
        self.assertEquals(len(c), 0)
        
        # create a city and verify
        City(name="New York City", population = 99999).save()
        c = City.objects.all()
        self.assertEquals(len(c), 1) 
        c = c[0]
        self.assertEquals(c.name, "New York City")
        self.assertEquals(c.slug, "new-york-city")
        self.assertEquals(c.population, 99999)
        
        # create two identical cities without states/countries, verify un33k uslug
        City(name="London", population = 10000).save()
        City(name="London", population = 9999).save()
        c = City.objects.filter(name="London")
        self.assertEquals(len(c), 2) 
        self.assertEquals(c[0].slug, "london")
        self.assertEquals(c[1].slug, "london-1")

        # createe two identical cities without state / countries first, verify un33k uslug
        o1 = City(name="Ottawa", population = 8888)
        o1.save()
        o2 = City(name="Ottawa", population = 9999)
        o2.save()
        c = City.objects.filter(name="Ottawa")
        self.assertEquals(len(c), 2) 
        self.assertEquals(c[0].slug, "ottawa")
        self.assertEquals(c[1].slug, "ottawa-1")

        # add different countries to two identical cities and verify un33k uslug
        c1 = Country(name="Canada", iso_2="CA", iso_3="CAN")
        c1.save()
        c2 = Country(name="United States", iso_2="US", iso_3="USA")
        c2.save()
        
        o1.country = c1
        o1.save()
        o2.country = c2
        o2.save()
        c = City.objects.filter(name="Ottawa")
        self.assertEquals(len(c), 2) 
        self.assertEquals(c[0].slug, "ottawa-canada")
        self.assertEquals(c[1].slug, "ottawa-united-states")
        
        # add different states to two identical cities and verify un33k uslug
        s1 = State(name="Ontario")
        s1.save()
        o1.state = s1
        o1.save()
        s2 = State(name="Illinois")
        s2.save()
        o2.state = s2
        o2.save()
        c = City.objects.filter(name="Ottawa")
        self.assertEquals(len(c), 2) 
        self.assertEquals(c[0].slug, "ottawa-ontario-canada")
        self.assertEquals(c[1].slug, "ottawa-illinois-united-states")