def test_foreign_keys_explicit(self): chef_gusteau = factory.make("tests.Chef", fields={"first_name": "Gusteau"}) self.assertEqual(Chef.objects.count(), 1) pizza = factory.make("tests.Pizza", fields={"chef_id": chef_gusteau.pk}) self.assertEqual(pizza.chef, chef_gusteau) self.assertEqual(Chef.objects.count(), 1)
def test_foreign_keys(self): chef_gusteau = factory.make('tests.Chef', fields={'first_name': 'Gusteau'}) self.assertEqual(Chef.objects.count(), 1) pizza = factory.make('tests.Pizza', fields={'chef': chef_gusteau}) self.assertEqual(pizza.chef, chef_gusteau) self.assertEqual(Chef.objects.count(), 1)
def test_manytomany(self): pizza = factory.make('tests.Pizza', ) self.assertEqual(pizza.toppings.count(), 0) pizza = factory.make( 'tests.Pizza', fields={'toppings': [factory.make('tests.Topping')]}) self.assertEqual(pizza.toppings.count(), 1) pizza = factory.make( 'tests.Pizza', fields={'toppings': factory.make('tests.Topping', quantity=5)}) self.assertEqual(pizza.toppings.count(), 5)
def test_manytomany(self): pizza = factory.make("tests.Pizza") self.assertEqual(pizza.toppings.count(), 0) pizza = factory.make( "tests.Pizza", fields={"toppings": [factory.make("tests.Topping")]} ) self.assertEqual(pizza.toppings.count(), 1) pizza = factory.make( "tests.Pizza", fields={"toppings": factory.make("tests.Topping", quantity=5)}, ) self.assertEqual(pizza.toppings.count(), 5)
def test_foreign_keys(self): chef_gusteau = factory.make( 'tests.Chef', fields={ 'first_name': 'Gusteau' } ) pizza = factory.make( 'tests.Pizza', fields={ 'chef': chef_gusteau } ) self.assertEqual(pizza.chef, chef_gusteau)
def test_lazy_field(self): chef_masters = factory.make('tests.Chef', fields={ 'first_name': 'Chef {}', 'last_name': Lazy('first_name') }, quantity=10) self.assertEqual(len(chef_masters), 10) self.assertEqual(chef_masters[0].first_name, 'Chef 0') self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) self.assertEqual(chef_masters[1].first_name, 'Chef 1') self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) margherita = factory.make( 'tests.pizza', fields={'price': Lazy('get_price', tax=0.07)}) self.assertEqual(margherita.price, Decimal('8.55'))
def test_sequence_callable(self): margheritas = factory.make('tests.Pizza', fields={'baked_on': timezone.now}, quantity=10) self.assertEqual(len(margheritas), 10) self.assertNotEqual(margheritas[0].baked_on, None) self.assertNotEqual(margheritas[0].baked_on, margheritas[1].baked_on)
def test_update_or_make(self): already_there = factory.make( "tests.Chef", fields={"first_name": "Auguste", "last_name": "Gusteau"} ) self.assertEqual(Chef.objects.count(), 1) chef_gusteau, created = factory.update_or_make( "tests.Chef", lookup={"last_name": "Gusteau"}, fields={"first_name": "Remi"} ) self.assertEqual(Chef.objects.count(), 1) self.assertEqual(already_there, chef_gusteau) self.assertEqual(chef_gusteau.first_name, "Remi") self.assertFalse(created) chef_linguini, created = factory.update_or_make( "tests.Chef", lookup={"last_name": "Linguini"}, fields={"first_name": "Alfredo"}, ) self.assertEqual(Chef.objects.count(), 2) self.assertTrue(created) self.assertEqual(chef_linguini.first_name, "Alfredo") self.assertEqual(chef_linguini.last_name, "Linguini")
def test_save_hooks(self): user = factory.make('auth.User', fields={ 'username': '******', }, pre_save=[lambda i: i.set_password('password')]) self.assertTrue(user.check_password('password'))
def test_field_inheritance(self): chef_gusteau = factory.make('tests.Chef', fields={'last_name': 'Gusteau'}) self.assertTrue('@' in chef_gusteau.email_address) self.assertTrue( chef_gusteau.twitter_profile.startswith('http://') or chef_gusteau.twitter_profile.startswith('https://'))
def test_fields(self): margherita = factory.make( 'tests.Pizza', fields={'name': 'four cheeses'} ) self.assertEqual(margherita.name, 'four cheeses') self.assertEqual(margherita.description, '')
def test_foreign_keys_fail(self): chef_gusteau = factory.make('tests.Chef', fields={'last_name': 'Gusteau'}) self.assertRaises( ForeignKeyError, factory.build, 'tests.Pizza', ) factory.build('tests.Pizza', fields={ 'chef': chef_gusteau, }) chef_skinner = factory.build('tests.Chef', fields={ 'last_name': 'Skinner', }) self.assertRaises(ForeignKeyError, factory.build, 'tests.Pizza', fields={ 'chef': chef_skinner, }) factory.build('tests.Pizza', fields={ 'chef': chef_gusteau, 'critic': None, })
def setUp(self): self.regularuser = factory.make('auth.User', fields={ 'username': '******', 'password': '******', 'is_staff': False, })
def test_blank(self): margherita = factory.make( 'tests.Pizza', fields={'name': 'four cheeses', 'description': lambda n, f: f.sentence()} ) self.assertEqual(margherita.name, 'four cheeses') self.assertNotEqual(margherita.description, '')
def test_g_m(self): already_there = factory.make('tests.Chef', fields={ 'first_name': 'Auguste', 'last_name': 'Gusteau', }) self.assertEqual(Chef.objects.count(), 1) chef_gusteau, created = factory.g_m( 'tests.Chef', lookup={'last_name': 'Gusteau'}, )(first_name='Remi') self.assertEqual(Chef.objects.count(), 1) self.assertEqual(already_there, chef_gusteau) self.assertFalse(created) chef_linguini, created = factory.g_m( 'tests.Chef', lookup={'last_name': 'Linguini'}, )(first_name='Alfredo') self.assertEqual(Chef.objects.count(), 2) self.assertTrue(created) self.assertEqual(chef_linguini.first_name, 'Alfredo') self.assertEqual(chef_linguini.last_name, 'Linguini')
def test_lazy_field(self): chef_masters = factory.make( "tests.Chef", fields={"first_name": "Chef {}", "last_name": Lazy("first_name")}, quantity=10, ) self.assertEqual(len(chef_masters), 10) self.assertEqual(chef_masters[0].first_name, "Chef 0") self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) self.assertEqual(chef_masters[1].first_name, "Chef 1") self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) margherita = factory.make( "tests.pizza", fields={"price": Lazy("get_price", tax=0.07)} ) self.assertEqual(margherita.price, Decimal("8.55"))
def test_blank(self): margherita = factory.make( "tests.Pizza", fields={"name": "four cheeses", "description": lambda n, f: f.sentence()}, ) self.assertEqual(margherita.name, "four cheeses") self.assertNotEqual(margherita.description, "")
def test_sequence(self): margheritas = factory.make( "tests.Pizza", fields={"name": "pizza {}"}, quantity=10 ) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, "pizza 0") self.assertEqual(margheritas[1].name, "pizza 1")
def test_password(self): user = factory.make('auth.User', fields={ 'username': '******', 'password': '******' }) self.assertTrue(user.check_password('password'))
def test_field_inheritance(self): chef_gusteau = factory.make("tests.Chef", fields={"last_name": "Gusteau"}) self.assertTrue("@" in chef_gusteau.email_address) self.assertTrue( chef_gusteau.twitter_profile.startswith("http://") or chef_gusteau.twitter_profile.startswith("https://") )
def test_sequence(self): margheritas = factory.make('tests.Pizza', fields={'name': 'pizza {}'}, quantity=10) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, 'pizza 0') self.assertEqual(margheritas[1].name, 'pizza 1')
def test_save_hooks(self): user = factory.make( "auth.User", fields={"username": "******"}, pre_save=[lambda i: i.set_password("password")], ) self.assertTrue(user.check_password("password"))
def test_save_hooks(self): user = factory.make( 'auth.User', fields={ 'username': '******', }, pre_save=[lambda i: i.set_password('password')]) self.assertTrue(user.check_password('password'))
def test_password(self): user = factory.make( 'auth.User', fields={ 'username': '******', 'password': '******' }) self.assertTrue(user.check_password('password'))
def test_lazy_field(self): chef_masters = factory.make( 'tests.Chef', fields={ 'first_name': 'Chef {}', 'last_name': Lazy('first_name') }, quantity=10) self.assertEqual(len(chef_masters), 10) self.assertEqual(chef_masters[0].first_name, 'Chef 0') self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) self.assertEqual(chef_masters[1].first_name, 'Chef 1') self.assertEqual(chef_masters[0].first_name, chef_masters[0].last_name) margherita = factory.make( 'tests.pizza', fields={'price': Lazy('get_price', tax=0.07)}) self.assertEqual(margherita.price, Decimal('8.55'))
def test_blank(self): margherita = factory.make('tests.Pizza', fields={ 'name': 'four cheeses', 'description': lambda n, f: f.sentence() }) self.assertEqual(margherita.name, 'four cheeses') self.assertNotEqual(margherita.description, '')
def test_sequence_callable_lambda(self): margheritas = factory.make( 'tests.Pizza', fields={'name': lambda n, f: 'pizza {}'.format(n)}, quantity=10 ) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, 'pizza 0') self.assertEqual(margheritas[1].name, 'pizza 1')
def test_field_inheritance(self): chef_gusteau = factory.make( 'tests.Chef', fields={ 'first_name': 'Gusteau' } ) self.assertTrue('@' in chef_gusteau.email_address) self.assertTrue('http://' in chef_gusteau.twitter_profile)
def test_sequence_callable(self): margheritas = factory.make( 'tests.Pizza', fields={'backed_on': timezone.now}, quantity=10 ) self.assertEqual(len(margheritas), 10) self.assertNotEqual(margheritas[0].backed_on, None) self.assertNotEqual(margheritas[0].backed_on, margheritas[1].backed_on)
def test_sequence(self): margheritas = factory.make( 'tests.Pizza', fields={'name': 'pizza {}'}, quantity=10 ) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, 'pizza 0') self.assertEqual(margheritas[1].name, 'pizza 1')
def test_sequence_callable_lambda(self): margheritas = factory.make( "tests.Pizza", fields={"name": lambda n, f: "pizza {}".format(n)}, quantity=10, ) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, "pizza 0") self.assertEqual(margheritas[1].name, "pizza 1")
def test_sequence_callable_lambda(self): margheritas = factory.make('tests.Pizza', fields={ 'name': lambda n, f: 'pizza {}'.format(n) }, quantity=10) self.assertEqual(len(margheritas), 10) self.assertEqual(margheritas[0].name, 'pizza 0') self.assertEqual(margheritas[1].name, 'pizza 1')
def test_manytomany(self): pizza = factory.make( 'tests.Pizza', ) self.assertEqual(pizza.toppings.count(), 0) pizza = factory.make( 'tests.Pizza', fields={ 'toppings': [factory.make('tests.Topping')] } ) self.assertEqual(pizza.toppings.count(), 1) pizza = factory.make( 'tests.Pizza', fields={ 'toppings': factory.make('tests.Topping', quantity=5) } ) self.assertEqual(pizza.toppings.count(), 5)
def test_foreign_keys_fail(self): chef_gusteau = factory.make("tests.Chef", fields={"last_name": "Gusteau"}) self.assertRaises(ForeignKeyError, factory.build, "tests.Pizza") factory.build("tests.Pizza", fields={"chef": chef_gusteau}) chef_skinner = factory.build("tests.Chef", fields={"last_name": "Skinner"}) self.assertRaises( ForeignKeyError, factory.build, "tests.Pizza", fields={"chef": chef_skinner} ) factory.build("tests.Pizza", fields={"chef": chef_gusteau, "critic": None})
def test_gis_fields(self): from django.contrib.gis import gdal, geos gigis = factory.make("tests.Pizzeria") self.assertTrue(isinstance(gigis.hq, geos.Point)) self.assertTrue(isinstance(gigis.directions, geos.LineString)) self.assertTrue(isinstance(gigis.floor_plan, geos.Polygon)) self.assertTrue(isinstance(gigis.locations, geos.MultiPoint)) self.assertTrue(isinstance(gigis.routes, geos.MultiLineString)) self.assertTrue(isinstance(gigis.delivery_areas, geos.MultiPolygon)) self.assertTrue( isinstance(gigis.all_the_things, geos.GeometryCollection)) self.assertTrue(isinstance(gigis.rast, gdal.GDALRaster))
def test_password(self): user = factory.make( "auth.User", fields={"username": "******", "password": "******"} ) self.assertTrue(user.check_password("password")) user, created = factory.update_or_make( "auth.User", lookup={"username": "******"}, fields={"password": "******"}, ) self.assertFalse(created) self.assertTrue(user.check_password("new_password"))
def test_postgres_fields(self): gigis_special = factory.make('tests.SpecialtyPizza') self.assertTrue(isinstance(gigis_special.toppings, list)) for topping in gigis_special.toppings: self.assertTrue(isinstance(topping, text_type)) self.assertTrue(isinstance(gigis_special.metadata, dict)) self.assertTrue(isinstance(gigis_special.price_range, NumericRange)) self.assertTrue(isinstance(gigis_special.price_range.lower, int)) self.assertTrue(isinstance(gigis_special.sales, NumericRange)) self.assertTrue(isinstance(gigis_special.sales.lower, int)) self.assertTrue(isinstance(gigis_special.available_on, DateTimeTZRange)) self.assertTrue(isinstance(gigis_special.available_on.lower, datetime)) self.assertNotEqual(gigis_special.available_on.lower.tzinfo, None)
def test_gis_fields(self): from django.contrib.gis import gdal, geos gigis = factory.make('tests.Pizzeria') self.assertTrue(isinstance(gigis.hq, geos.Point)) self.assertTrue(isinstance(gigis.directions, geos.LineString)) self.assertTrue(isinstance(gigis.floor_plan, geos.Polygon)) self.assertTrue(isinstance(gigis.locations, geos.MultiPoint)) self.assertTrue(isinstance(gigis.routes, geos.MultiLineString)) self.assertTrue(isinstance(gigis.delivery_areas, geos.MultiPolygon)) self.assertTrue(isinstance(gigis.all_the_things, geos.GeometryCollection)) if django_version >= (1, 9, 0): self.assertTrue(isinstance(gigis.rast, gdal.GDALRaster))
def test_json(self): gigis_special = factory.make( "tests.SpecialtyPizza", fields={"nutritional_values": { "Calories": 310 }}) self.assertEqual(gigis_special.nutritional_values, {"Calories": 310}) gigis_special, created = factory.update_or_make( "tests.SpecialtyPizza", lookup={"id": gigis_special.id}, fields={ "nutritional_values": { "Calories": { "Total": 310, "From Fat": 120 } } }, ) self.assertFalse(created) self.assertEqual( gigis_special.nutritional_values, {"Calories": { "Total": 310, "From Fat": 120 }}, ) summer_special, created = factory.update_or_make( "tests.SpecialtyPizza", lookup={"name": "Summer Special"}, fields={ "nutritional_values": { "Calories": { "Total": 310, "From Fat": 120 } } }, ) self.assertTrue(created) self.assertEqual( summer_special.nutritional_values, {"Calories": { "Total": 310, "From Fat": 120 }}, )
def test_postgres_fields(self): gigis_special = factory.make("tests.SpecialtyPizza") self.assertTrue(isinstance(gigis_special.toppings, list)) for topping in gigis_special.toppings: self.assertTrue(isinstance(topping, text_type)) self.assertTrue(isinstance(gigis_special.metadata, dict)) self.assertTrue(isinstance(gigis_special.price_range, NumericRange)) self.assertTrue(isinstance(gigis_special.price_range.lower, int)) self.assertTrue(isinstance(gigis_special.sales, NumericRange)) self.assertTrue(isinstance(gigis_special.sales.lower, int)) self.assertTrue(isinstance(gigis_special.available_on, DateTimeTZRange)) self.assertTrue(isinstance(gigis_special.available_on.lower, datetime)) self.assertNotEqual(gigis_special.available_on.lower.tzinfo, None) self.assertTrue(isinstance(gigis_special.nutritional_values, dict))
def test_model(self): margherita = factory.make('tests.Pizza') # field.default self.assertFalse(margherita.gluten_free) # field.null self.assertFalse(margherita.price) # field.blank self.assertEqual(margherita.description, '') # field.choices self.assertIn(margherita.thickness, [0, 1, 2]) # required field self.assertNotEqual(margherita.name, '') self.assertNotEqual(margherita.chef.first_name, '')
def test_foreign_keys_fail(self): chef_gusteau = factory.make( 'tests.Chef', fields={ 'first_name': 'Gusteau' } ) self.assertRaises(ForeignKeyError, factory.build, 'tests.Pizza', ) factory.build( 'tests.Pizza', fields={ 'chef': chef_gusteau, } ) chef_skinner = factory.build( 'tests.Chef', fields={ 'first_name': 'Skinner', } ) self.assertRaises(ForeignKeyError, factory.build, 'tests.Pizza', fields={ 'chef': chef_skinner, } ) factory.build( 'tests.Pizza', fields={ 'chef': chef_gusteau, 'critic': None, } )
def test_array(self): gigis_special = factory.make( "tests.SpecialtyPizza", fields={"toppings": ["black olives", "green olives"]}, ) self.assertEqual(gigis_special.toppings, ["black olives", "green olives"]) gigis_special, created = factory.update_or_make( "tests.SpecialtyPizza", lookup={"id": gigis_special.id}, fields={ "toppings": ["black olives", "green olives", "cremini mushrooms"] }, ) self.assertFalse(created) self.assertEqual( gigis_special.toppings, ["black olives", "green olives", "cremini mushrooms"], ) summer_special, created = factory.update_or_make( "tests.SpecialtyPizza", lookup={"name": "Summer Special"}, fields={ "toppings": ["black olives", "green olives", "cremini mushrooms"] }, ) self.assertTrue(created) self.assertEqual( summer_special.toppings, ["black olives", "green olives", "cremini mushrooms"], )
def test_fields(self): margherita = factory.make('tests.Pizza', fields={'name': 'four cheeses'}) self.assertEqual(margherita.name, 'four cheeses') self.assertEqual(margherita.description, '')