File: test_dev_experience.py

package info (click to toggle)
factory-boy 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 996 kB
  • sloc: python: 7,316; makefile: 107; sh: 24
file content (58 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download
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))