-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce factory.declaration.Transformer
Transforms a value using provided `transform` function. Values coming from the declaration and values overridden through keywords arguments are transformed before the generated object attribute is set. Removes the need to save objects with a post generation hook twice to the database. Facilitates overriding Django passwords when instantiating the factory. Fixes #316 Fixes #366
- Loading branch information
1 parent
f3dce54
commit be63501
Showing
11 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright: See the LICENSE file. | ||
|
||
from unittest import TestCase | ||
|
||
from factory import Factory, Transformer | ||
|
||
|
||
class TransformCounter: | ||
calls_count = 0 | ||
|
||
@classmethod | ||
def __call__(cls, x): | ||
cls.calls_count += 1 | ||
return x.upper() | ||
|
||
@classmethod | ||
def reset(cls): | ||
cls.calls_count = 0 | ||
|
||
|
||
transform = TransformCounter() | ||
|
||
|
||
class Upper: | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
|
||
class UpperFactory(Factory): | ||
name = Transformer(transform, "value") | ||
|
||
class Meta: | ||
model = Upper | ||
|
||
|
||
class TransformerTest(TestCase): | ||
def setUp(self): | ||
transform.reset() | ||
|
||
def test_transform_count(self): | ||
self.assertEqual("VALUE", UpperFactory().name) | ||
self.assertEqual(transform.calls_count, 1) | ||
|
||
def test_transform_kwarg(self): | ||
self.assertEqual("TEST", UpperFactory(name="test").name) | ||
self.assertEqual(transform.calls_count, 1) |