1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# Copyright: See the LICENSE file.
"""Tests about developer experience: help messages, errors, etc."""
import collections
import unittest
import factory
import factory.errors
Country = collections.namedtuple('Country', ['name', 'continent', 'capital_city'])
City = collections.namedtuple('City', ['name', 'population'])
class DeclarationTests(unittest.TestCase):
def test_subfactory_to_model(self):
"""A helpful error message occurs when pointing a subfactory to a model."""
class CountryFactory(factory.Factory):
class Meta:
model = Country
name = factory.Faker('country')
continent = "Antarctica"
# Error here: pointing the SubFactory to a model, not a factory.
capital_city = factory.SubFactory(City)
with self.assertRaises(factory.errors.AssociatedClassError) as raised:
CountryFactory()
self.assertIn('City', str(raised.exception))
self.assertIn('Country', str(raised.exception))
def test_subfactory_to_factorylike_model(self):
"""A helpful error message occurs when pointing a subfactory to a model.
This time with a model that looks more like a factory (ie has a `._meta`)."""
class CityModel:
_meta = None
name = "Coruscant"
population = 0
class CountryFactory(factory.Factory):
class Meta:
model = Country
name = factory.Faker('country')
continent = "Antarctica"
# Error here: pointing the SubFactory to a model, not a factory.
capital_city = factory.SubFactory(CityModel)
with self.assertRaises(factory.errors.AssociatedClassError) as raised:
CountryFactory()
self.assertIn('CityModel', str(raised.exception))
self.assertIn('Country', str(raised.exception))
|