summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2015-05-22 20:24:13 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2015-05-22 20:26:22 +0200
commit6f37f9be2d2e1bc75340068911db18b2bbcbe722 (patch)
treeb8b6e7c90c8a4e278daad118573ae1dfef1ae35c /tests
parentda8d2e6323014be6065a9a490754173859fb95b4 (diff)
downloadfactory-boy-6f37f9be2d2e1bc75340068911db18b2bbcbe722.tar
factory-boy-6f37f9be2d2e1bc75340068911db18b2bbcbe722.tar.gz
Add factory.Faker()
This relies on the ``fake-factory`` library, and provides realistic random values for most field types.
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/test_faker.py105
2 files changed, 106 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index dc1a119..b2c772d 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -7,6 +7,7 @@ from .test_django import *
from .test_base import *
from .test_containers import *
from .test_declarations import *
+from .test_faker import *
from .test_fuzzy import *
from .test_helpers import *
from .test_using import *
diff --git a/tests/test_faker.py b/tests/test_faker.py
new file mode 100644
index 0000000..41f8e19
--- /dev/null
+++ b/tests/test_faker.py
@@ -0,0 +1,105 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2010 Mark Sandstrom
+# Copyright (c) 2011-2015 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+
+import factory
+import unittest
+
+
+class MockFaker(object):
+ def __init__(self, expected):
+ self.expected = expected
+
+ def format(self, provider, **kwargs):
+ return self.expected[provider]
+
+
+class FakerTests(unittest.TestCase):
+ def setUp(self):
+ self._real_fakers = factory.Faker._FAKER_REGISTRY
+ factory.Faker._FAKER_REGISTRY = {}
+
+ def tearDown(self):
+ factory.Faker._FAKER_REGISTRY = self._real_fakers
+
+ def _setup_mock_faker(self, locale=None, **definitions):
+ if locale is None:
+ locale = factory.Faker._DEFAULT_LOCALE
+ factory.Faker._FAKER_REGISTRY[locale] = MockFaker(definitions)
+
+ def test_simple_biased(self):
+ self._setup_mock_faker(name="John Doe")
+ faker_field = factory.Faker('name')
+ self.assertEqual("John Doe", faker_field.generate({}))
+
+ def test_full_factory(self):
+ class Profile(object):
+ def __init__(self, first_name, last_name, email):
+ self.first_name = first_name
+ self.last_name = last_name
+ self.email = email
+
+ class ProfileFactory(factory.Factory):
+ class Meta:
+ model = Profile
+ first_name = factory.Faker('first_name')
+ last_name = factory.Faker('last_name', locale='fr_FR')
+ email = factory.Faker('email')
+
+ self._setup_mock_faker(first_name="John", last_name="Doe", email="john.doe@example.org")
+ self._setup_mock_faker(first_name="Jean", last_name="Valjean", email="jvaljean@exemple.fr", locale='fr_FR')
+
+ profile = ProfileFactory()
+ self.assertEqual("John", profile.first_name)
+ self.assertEqual("Valjean", profile.last_name)
+ self.assertEqual('john.doe@example.org', profile.email)
+
+ def test_override_locale(self):
+ class Profile(object):
+ def __init__(self, first_name, last_name):
+ self.first_name = first_name
+ self.last_name = last_name
+
+ class ProfileFactory(factory.Factory):
+ class Meta:
+ model = Profile
+
+ first_name = factory.Faker('first_name')
+ last_name = factory.Faker('last_name', locale='fr_FR')
+
+ self._setup_mock_faker(first_name="John", last_name="Doe")
+ self._setup_mock_faker(first_name="Jean", last_name="Valjean", locale='fr_FR')
+ self._setup_mock_faker(first_name="Johannes", last_name="Brahms", locale='de_DE')
+
+ profile = ProfileFactory()
+ self.assertEqual("John", profile.first_name)
+ self.assertEqual("Valjean", profile.last_name)
+
+ with factory.Faker.override_default_locale('de_DE'):
+ profile = ProfileFactory()
+ self.assertEqual("Johannes", profile.first_name)
+ self.assertEqual("Valjean", profile.last_name)
+
+ profile = ProfileFactory()
+ self.assertEqual("John", profile.first_name)
+ self.assertEqual("Valjean", profile.last_name)
+