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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
|
Reference
=========
.. module:: factory
This section offers an in-depth description of factory_boy features.
For internals and customization points, please refer to the :doc:`internals` section.
The :class:`Factory` class
--------------------------
Meta options
""""""""""""
.. class:: FactoryOptions
.. versionadded:: 2.4.0
A :class:`Factory`'s behavior can be tuned through a few settings.
For convenience, they are declared in a single ``class Meta`` attribute:
.. code-block:: python
class MyFactory(factory.Factory):
class Meta:
model = MyObject
abstract = False
.. attribute:: model
This optional attribute describes the class of objects to generate.
If unset, it will be inherited from parent :class:`Factory` subclasses.
.. versionadded:: 2.4.0
.. method:: get_model_class()
Returns the actual model class (:attr:`FactoryOptions.model` might be the
path to the class; this function will always return a proper class).
.. attribute:: abstract
This attribute indicates that the :class:`Factory` subclass should not
be used to generate objects, but instead provides some extra defaults.
It will be automatically set to ``True`` if neither the :class:`Factory`
subclass nor its parents define the :attr:`~FactoryOptions.model` attribute.
.. warning:: This flag is reset to ``False`` when a :class:`Factory` subclasses
another one if a :attr:`~FactoryOptions.model` is set.
.. versionadded:: 2.4.0
.. attribute:: inline_args
Some factories require non-keyword arguments to their :meth:`~object.__init__`.
They should be listed, in order, in the :attr:`inline_args`
attribute:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
inline_args = ('login', 'email')
login = 'john'
email = factory.LazyAttribute(lambda o: '%s@example.com' % o.login)
firstname = "John"
.. code-block:: pycon
>>> UserFactory()
<User: john>
>>> User('john', 'john@example.com', firstname="John") # actual call
.. versionadded:: 2.4.0
.. attribute:: exclude
While writing a :class:`Factory` for some object, it may be useful to
have general fields helping defining others, but that should not be
passed to the model class; for instance, a field named 'now' that would
hold a reference time used by other objects.
Factory fields whose name are listed in :attr:`exclude` will
be removed from the set of args/kwargs passed to the underlying class;
they can be any valid factory_boy declaration:
.. code-block:: python
class OrderFactory(factory.Factory):
class Meta:
model = Order
exclude = ('now',)
now = factory.LazyFunction(datetime.datetime.utcnow)
started_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(hours=1))
paid_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(minutes=50))
.. code-block:: pycon
>>> OrderFactory() # The value of 'now' isn't passed to Order()
<Order: started 2013-04-01 12:00:00, paid 2013-04-01 12:10:00>
>>> # An alternate value may be passed for 'now'
>>> OrderFactory(now=datetime.datetime(2013, 4, 1, 10))
<Order: started 2013-04-01 09:00:00, paid 2013-04-01 09:10:00>
.. versionadded:: 2.4.0
.. attribute:: rename
Sometimes, a model expects a field with a name already used by one
of :class:`Factory`'s methods.
In this case, the :attr:`rename` attributes allows to define renaming
rules: the keys of the :attr:`rename` dict are those used in the
:class:`Factory` declarations, and their values the new name:
.. code-block:: python
class ImageFactory(factory.Factory):
# The model expects "attributes"
form_attributes = ['thumbnail', 'black-and-white']
class Meta:
model = Image
rename = {'form_attributes': 'attributes'}
.. versionadded: 2.6.0
.. attribute:: strategy
Use this attribute to change the strategy used by a :class:`Factory`.
The default is :data:`CREATE_STRATEGY`.
Attributes and methods
""""""""""""""""""""""
.. class:: Factory
**Class-level attributes:**
.. attribute:: Meta
.. attribute:: _meta
.. versionadded:: 2.4.0
The :class:`FactoryOptions` instance attached to a :class:`Factory` class is available
as a :attr:`_meta` attribute.
.. attribute:: Params
.. versionadded:: 2.7.0
The extra parameters attached to a :class:`Factory` are declared through a :attr:`Params`
class.
See :ref:`the "Parameters" section <parameters>` for more information.
.. attribute:: _options_class
.. versionadded:: 2.4.0
If a :class:`Factory` subclass needs to define additional, extra options, it has to
provide a custom :class:`FactoryOptions` subclass.
A pointer to that custom class should be provided as :attr:`_options_class` so that
the :class:`Factory`-building metaclass can use it instead.
**Base functions:**
.. classmethod:: __call__(**kwargs)
The :class:`Factory` class provides a few methods for getting objects;
the usual way being to simply call the class:
.. code-block:: pycon
>>> UserFactory() # Calls UserFactory.create()
>>> UserFactory(login='john') # Calls UserFactory.create(login='john')
Under the hood, factory_boy will define the :class:`Factory`
:meth:`~object.__new__` method to call the default :ref:`strategy <strategies>`
of the :class:`Factory`.
A specific strategy for getting instance can be selected by calling the
adequate method:
.. classmethod:: build(cls, **kwargs)
Provides a new object, using the 'build' strategy.
.. classmethod:: build_batch(cls, size, **kwargs)
Provides a list of ``size`` instances from the :class:`Factory`,
through the 'build' strategy.
.. classmethod:: create(cls, **kwargs)
Provides a new object, using the 'create' strategy.
.. classmethod:: create_batch(cls, size, **kwargs)
Provides a list of ``size`` instances from the :class:`Factory`,
through the 'create' strategy.
.. classmethod:: stub(cls, **kwargs)
Provides a new stub
.. classmethod:: stub_batch(cls, size, **kwargs)
Provides a list of ``size`` stubs from the :class:`Factory`.
.. classmethod:: generate(cls, strategy, **kwargs)
Provide a new instance, with the provided ``strategy``.
.. classmethod:: generate_batch(cls, strategy, size, **kwargs)
Provides a list of ``size`` instances using the specified strategy.
.. classmethod:: simple_generate(cls, create, **kwargs)
Provide a new instance, either built (``create=False``) or created (``create=True``).
.. classmethod:: simple_generate_batch(cls, create, size, **kwargs)
Provides a list of ``size`` instances, either built or created
according to ``create``.
**Extension points:**
A :class:`Factory` subclass may override a couple of class methods to adapt
its behavior:
.. classmethod:: _adjust_kwargs(cls, **kwargs)
.. OHAI_VIM**
The :meth:`_adjust_kwargs` extension point allows for late fields tuning.
It is called once keyword arguments have been resolved and post-generation
items removed, but before the :attr:`~FactoryOptions.inline_args` extraction
phase.
.. code-block:: python
class UserFactory(factory.Factory):
@classmethod
def _adjust_kwargs(cls, **kwargs):
# Ensure ``lastname`` is upper-case.
kwargs['lastname'] = kwargs['lastname'].upper()
return kwargs
.. OHAI_VIM**
.. classmethod:: _setup_next_sequence(cls)
This method will compute the first value to use for the sequence counter
of this factory.
It is called when the first instance of the factory (or one of its subclasses)
is created.
Subclasses may fetch the next free ID from the database, for instance.
.. classmethod:: _build(cls, model_class, *args, **kwargs)
.. OHAI_VIM*
This class method is called whenever a new instance needs to be built.
It receives the model class (provided to :attr:`~FactoryOptions.model`), and
the positional and keyword arguments to use for the class once all has
been computed.
Subclasses may override this for custom APIs.
.. classmethod:: _create(cls, model_class, *args, **kwargs)
.. OHAI_VIM*
The :meth:`_create` method is called whenever an instance needs to be
created.
It receives the same arguments as :meth:`_build`.
Subclasses may override this for specific persistence backends:
.. code-block:: python
class BaseBackendFactory(factory.Factory):
class Meta:
abstract = True # Optional
@classmethod
def _create(cls, model_class, *args, **kwargs):
obj = model_class(*args, **kwargs)
obj.save()
return obj
.. OHAI_VIM*
.. classmethod:: _after_postgeneration(cls, obj, create, results=None)
:arg object obj: The object just generated
:arg bool create: Whether the object was 'built' or 'created'
:arg dict results: Map of post-generation declaration name to call
result
The :meth:`_after_postgeneration` is called once post-generation
declarations have been handled.
Its arguments allow to handle specifically some post-generation return
values, for instance.
**Advanced functions:**
.. classmethod:: reset_sequence(cls, value=None, force=False)
:arg int value: The value to reset the sequence to
:arg bool force: Whether to force-reset the sequence
Allows to reset the sequence counter for a :class:`~factory.Factory`.
The new value can be passed in as the ``value`` argument:
.. code-block:: pycon
>>> SomeFactory.build().sequenced_attribute
0
>>> SomeFactory.reset_sequence(4)
>>> SomeFactory.build().sequenced_attribute
4
Since subclasses of a non-:attr:`abstract <factory.FactoryOptions.abstract>`
:class:`~factory.Factory` share the same sequence counter, special care needs
to be taken when resetting the counter of such a subclass.
By default, :meth:`reset_sequence` will raise a :exc:`ValueError` when
called on a subclassed :class:`~factory.Factory` subclass. This can be
avoided by passing in the ``force=True`` flag:
.. code-block:: pycon
>>> InheritedFactory.reset_sequence()
Traceback (most recent call last):
File "factory_boy/tests/test_base.py", line 179, in test_reset_sequence_subclass_parent
SubTestObjectFactory.reset_sequence()
File "factory_boy/factory/base.py", line 250, in reset_sequence
"Cannot reset the sequence of a factory subclass. "
ValueError: Cannot reset the sequence of a factory subclass. Please call reset_sequence() on the root factory, or call reset_sequence(forward=True).
>>> InheritedFactory.reset_sequence(force=True)
>>>
This is equivalent to calling :meth:`reset_sequence` on the base
factory in the chain.
.. _parameters:
Parameters
""""""""""
.. versionadded:: 2.7.0
Some models have many fields that can be summarized by a few parameters; for instance,
a train with many cars — each complete with serial number, manufacturer, ...;
or an order that can be pending/shipped/received, with a few fields to describe each step.
When building instances of such models, a couple of parameters can be enough to determine
all other fields; this is handled by the :class:`~Factory.Params` section of a :class:`Factory` declaration.
Simple parameters
~~~~~~~~~~~~~~~~~
Some factories only need little data:
.. code-block:: python
class ConferenceFactory(factory.Factory):
class Meta:
model = Conference
class Params:
duration = 'short' # Or 'long'
start_date = factory.fuzzy.FuzzyDate()
end_date = factory.LazyAttribute(
lambda o: o.start_date + datetime.timedelta(days=2 if o.duration == 'short' else 7)
)
sprints_start = factory.LazyAttribute(
lambda o: o.end_date - datetime.timedelta(days=0 if o.duration == 'short' else 1)
)
.. code-block:: pycon
>>> ConferenceFactory(duration='short')
<Conference: DUTH 2015 (2015-11-05 - 2015-11-08, sprints 2015-11-08)>
>>> ConferenceFactory(duration='long')
<Conference: DjangoConEU 2016 (2016-03-30 - 2016-04-03, sprints 2016-04-02)>
Any simple parameter provided to the :class:`Factory.Params` section is available to the whole factory,
but not passed to the final class (similar to the :attr:`~FactoryOptions.exclude` behavior).
Traits
~~~~~~
.. class:: Trait(**kwargs)
.. OHAI VIM**
.. versionadded:: 2.7.0
A trait's parameters are the fields it should alter when enabled.
For more complex situations, it is helpful to override a few fields at once:
.. code-block:: python
class OrderFactory(factory.Factory):
class Meta:
model = Order
state = 'pending'
shipped_on = None
shipped_by = None
class Params:
shipped = factory.Trait(
state='shipped',
shipped_on=datetime.date.today(),
shipped_by=factory.SubFactory(EmployeeFactory),
)
Such a :class:`Trait` is activated or disabled by a single boolean field:
.. code-block:: pycon
>>> OrderFactory()
<Order: pending>
Order(state='pending')
>>> OrderFactory(shipped=True)
<Order: shipped by John Doe on 2016-04-02>
A :class:`Trait` can be enabled/disabled by a :class:`Factory` subclass:
.. code-block:: python
class ShippedOrderFactory(OrderFactory):
shipped = True
Values set in a :class:`Trait` can be overridden by call-time values:
.. code-block:: pycon
>>> OrderFactory(shipped=True, shipped_on=last_year)
<Order: shipped by John Doe on 2015-04-20>
:class:`Traits <Trait>` can be chained:
.. code-block:: python
class OrderFactory(factory.Factory):
class Meta:
model = Order
# Can be pending/shipping/received
state = 'pending'
shipped_on = None
shipped_by = None
received_on = None
received_by = None
class Params:
shipped = factory.Trait(
state='shipped',
shipped_on=datetime.date.today,
shipped_by=factory.SubFactory(EmployeeFactory),
)
received = factory.Trait(
shipped=True,
state='received',
shipped_on=datetime.date.today - datetime.timedelta(days=4),
received_on=datetime.date.today,
received_by=factory.SubFactory(CustomerFactory),
)
.. code-block:: pycon
>>> OrderFactory(received=True)
<Order: shipped by John Doe on 2016-03-20, received by Joan Smith on 2016-04-02>
A :class:`Trait` might be overridden in :class:`Factory` subclasses:
.. code-block:: python
class LocalOrderFactory(OrderFactory):
class Params:
received = factory.Trait(
shipped=True,
state='received',
shipped_on=datetime.date.today - datetime.timedelta(days=1),
received_on=datetime.date.today,
received_by=factory.SubFactory(CustomerFactory),
)
.. code-block:: pycon
>>> LocalOrderFactory(received=True)
<Order: shipped by John Doe on 2016-04-01, received by Joan Smith on 2016-04-02>
.. note:: When overriding a :class:`Trait`, the whole declaration **MUST** be replaced.
.. _strategies:
Strategies
""""""""""
factory_boy supports two main strategies for generating instances, plus stubs.
.. data:: BUILD_STRATEGY
The 'build' strategy is used when an instance should be created,
but not persisted to any datastore.
It is usually a simple call to the :meth:`~object.__init__` method of the
:attr:`~FactoryOptions.model` class.
.. data:: CREATE_STRATEGY
The 'create' strategy builds and saves an instance into its appropriate datastore.
This is the default strategy of factory_boy; it would typically instantiate an
object, then save it:
.. code-block:: pycon
>>> obj = self._associated_class(*args, **kwargs)
>>> obj.save()
>>> return obj
.. function:: use_strategy(strategy)
.. deprecated:: 3.2
Use :py:attr:`factory.FactoryOptions.strategy` instead.
*Decorator*
Change the default strategy of the decorated :class:`Factory` to the chosen ``strategy``:
.. code-block:: python
@use_strategy(factory.BUILD_STRATEGY)
class UserBuildingFactory(UserFactory):
pass
.. data:: STUB_STRATEGY
The 'stub' strategy is an exception in the factory_boy world: it doesn't return
an instance of the :attr:`~FactoryOptions.model` class, and actually doesn't
require one to be present.
Instead, it returns an instance of :class:`StubObject` whose attributes have been
set according to the declarations.
.. class:: StubObject
A plain, stupid object. No method, no helpers, simply a bunch of attributes.
It is typically instantiated, then has its attributes set:
.. code-block:: pycon
>>> obj = StubObject()
>>> obj.x = 1
>>> obj.y = 2
.. class:: StubFactory(Factory)
An :attr:`abstract <FactoryOptions.abstract>` :class:`Factory`,
with a default strategy set to :data:`STUB_STRATEGY`.
.. function:: debug(logger='factory', stream=None)
:param str logger: The name of the logger to enable debug for
:param io.StringIO stream: The stream to send debug output to, defaults to :obj:`sys.stderr`
Context manager to help debugging factory_boy behavior.
It will temporarily put the target logger (e.g ``'factory'``) in debug mode,
sending all output to ``stream``;
upon leaving the context, the logging levels are reset.
A typical use case is to understand what happens during a single factory call:
.. code-block:: python
with factory.debug():
obj = TestModel2Factory()
This will yield messages similar to those (artificial indentation):
.. code-block:: ini
BaseFactory: Preparing tests.test_using.TestModel2Factory(extra={})
LazyStub: Computing values for tests.test_using.TestModel2Factory(two=<OrderedDeclarationWrapper for <factory.declarations.SubFactory object at 0x1e15610>>)
SubFactory: Instantiating tests.test_using.TestModelFactory(__containers=(<LazyStub for tests.test_using.TestModel2Factory>,), one=4), create=True
BaseFactory: Preparing tests.test_using.TestModelFactory(extra={'__containers': (<LazyStub for tests.test_using.TestModel2Factory>,), 'one': 4})
LazyStub: Computing values for tests.test_using.TestModelFactory(one=4)
LazyStub: Computed values, got tests.test_using.TestModelFactory(one=4)
BaseFactory: Generating tests.test_using.TestModelFactory(one=4)
LazyStub: Computed values, got tests.test_using.TestModel2Factory(two=<tests.test_using.TestModel object at 0x1e15410>)
BaseFactory: Generating tests.test_using.TestModel2Factory(two=<tests.test_using.TestModel object at 0x1e15410>)
.. _declarations:
Declarations
------------
Faker
"""""
.. class:: Faker(provider, locale=None, **kwargs)
.. OHAIVIM**
In order to easily define realistic-looking factories,
use the :class:`Faker` attribute declaration.
This is a wrapper around `faker <https://faker.readthedocs.io/en/latest/>`_;
its argument is the name of a ``faker`` provider:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
name = factory.Faker('name')
.. code-block:: pycon
>>> user = UserFactory()
>>> user.name
'Lucy Cechtelar'
Some providers accept parameters; they should be passed after the provider name:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
arrival = factory.Faker(
'date_between_dates',
date_start=datetime.date(2020, 1, 1),
date_end=datetime.date(2020, 5, 31),
)
As with :class:`~factory.SubFactory`, the parameters can be any valid declaration.
This does not apply to the provider name or the locale.
.. code-block:: python
class TripFactory(factory.Factory):
class Meta:
model = Trip
departure = factory.Faker(
'date',
end_datetime=datetime.date.today(),
)
arrival = factory.Faker(
'date_between_dates',
date_start=factory.SelfAttribute('..departure'),
)
.. note:: When using :class:`~factory.SelfAttribute` or :class:`~factory.LazyAttribute`
in a :class:`factory.Faker` parameter, the current object is the declarations
provided to the :class:`~factory.Faker` declaration; go :ref:`up a level <factory-parent>`
to reach fields of the surrounding :class:`~factory.Factory`, as shown
in the ``SelfAttribute('..xxx')`` example above.
.. attribute:: locale
If a custom locale is required for one specific field,
use the ``locale`` parameter:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
name = factory.Faker('name', locale='fr_FR')
.. code-block:: pycon
>>> user = UserFactory()
>>> user.name
'Jean Valjean'
.. classmethod:: override_default_locale(cls, locale)
If the locale needs to be overridden for a whole test,
use :meth:`~factory.Faker.override_default_locale`:
.. code-block:: pycon
>>> with factory.Faker.override_default_locale('de_DE'):
... UserFactory()
<User: Johannes Brahms>
.. classmethod:: add_provider(cls, locale=None)
Some projects may need to fake fields beyond those provided by ``faker``;
in such cases, use :meth:`factory.Faker.add_provider` to declare additional providers
for those fields:
.. code-block:: python
factory.Faker.add_provider(SmileyProvider)
class FaceFactory(factory.Factory):
class Meta:
model = Face
smiley = factory.Faker('smiley')
LazyFunction
""""""""""""
.. class:: LazyFunction(method_to_call)
The :class:`LazyFunction` is the simplest case where the value of an attribute
does not depend on the object being built.
It takes as an argument a function to call; that should not take any arguments and
return a value.
.. code-block:: python
class LogFactory(factory.Factory):
class Meta:
model = models.Log
timestamp = factory.LazyFunction(datetime.now)
.. code-block:: pycon
>>> LogFactory()
<Log: log at 2016-02-12 17:02:34>
>>> # The LazyFunction can be overridden
>>> LogFactory(timestamp=now - timedelta(days=1))
<Log: log at 2016-02-11 17:02:34>
:class:`LazyFunction` is also useful for assigning copies of mutable objects
(like lists) to an object's property. Example:
.. code-block:: python
DEFAULT_TEAM = ['Player1', 'Player2']
class TeamFactory(factory.Factory):
class Meta:
model = models.Team
teammates = factory.LazyFunction(lambda: list(DEFAULT_TEAM))
Decorator
~~~~~~~~~
The class :class:`LazyFunction` does not provide a decorator.
For complex cases, use :meth:`~factory.lazy_attribute` directly.
LazyAttribute
"""""""""""""
.. class:: LazyAttribute(method_to_call)
The :class:`LazyAttribute` is a simple yet extremely powerful building brick
for extending a :class:`Factory`.
It takes as argument a method to call (usually a lambda); that method should
accept the object being built as sole argument, and return a value.
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
username = 'john'
email = factory.LazyAttribute(lambda o: '%s@example.com' % o.username)
.. code-block:: pycon
>>> u = UserFactory()
>>> u.email
'john@example.com'
>>> u = UserFactory(username='leo')
>>> u.email
'leo@example.com'
The object passed to :class:`LazyAttribute` is not an instance of the target class,
but instead a ``builder.Resolver``: a temporary container that computes
the value of all declared fields.
Decorator
~~~~~~~~~
.. function:: lazy_attribute
If a simple lambda isn't enough, you may use the :meth:`lazy_attribute` decorator instead.
This decorates an instance method that should take a single argument, ``self``;
the name of the method will be used as the name of the attribute to fill with the
return value of the method:
.. code-block:: python
class UserFactory(factory.Factory)
class Meta:
model = User
name = "Jean"
@factory.lazy_attribute
def email(self):
# Convert to plain ascii text
clean_name = (unicodedata.normalize('NFKD', self.name)
.encode('ascii', 'ignore')
.decode('utf8'))
return '%s@example.com' % clean_name
.. code-block:: pycon
>>> joel = UserFactory(name="Joël")
>>> joel.email
'joel@example.com'
Transformer
"""""""""""
.. class:: Transformer(default_value, *, transform)
.. versionadded:: 3.3.0
A :class:`Transformer` applies a ``transform`` function to the provided value
before to set the transformed value on the generated object.
It expects one positional argument and one keyword argument:
- ``default_value``: the default value, which passes through the ``transform``
function.
- ``transform``: a function taking the value as parameter and returning the
transformed value,
.. code-block:: python
class UpperFactory(factory.Factory):
name = factory.Transformer("Joe", transform=str.upper)
class Meta:
model = Upper
.. code-block:: pycon
>>> UpperFactory().name
'JOE'
>>> UpperFactory(name="John").name
'JOHN'
Disabling
~~~~~~~~~
To disable a :class:`Transformer`, wrap the value in ``Transformer.Force``:
.. code-block:: pycon
>>> UpperFactory(name=factory.Transformer.Force("John")).name
'John'
Sequence
""""""""
.. class:: Sequence(lambda)
If a field should be unique, and thus different for all built instances,
use a :class:`Sequence`.
This declaration takes a single argument, a function accepting a single parameter
- the current sequence counter - and returning the related value.
.. code-block:: python
class UserFactory(factory.Factory)
class Meta:
model = User
phone = factory.Sequence(lambda n: '123-555-%04d' % n)
.. code-block:: pycon
>>> UserFactory().phone
'123-555-0000'
>>> UserFactory().phone
'123-555-0001'
.. note:: The sequence counter starts at 0 and can be set or reset,
see :ref:`Forcing a sequence counter <forcing-a-sequence-counter>`.
Decorator
~~~~~~~~~
.. function:: sequence
As with :meth:`lazy_attribute`, a decorator is available for complex situations.
:meth:`sequence` decorates an instance method, whose ``self`` method will actually
be the sequence counter - this might be confusing:
.. code-block:: python
class UserFactory(factory.Factory)
class Meta:
model = User
@factory.sequence
def phone(n):
a = n // 10000
b = n % 10000
return '%03d-555-%04d' % (a, b)
.. code-block:: pycon
>>> UserFactory().phone # current sequence counter at 9999
'000-555-9999'
>>> UserFactory().phone # current sequence counter at 10000
'001-555-0000'
Sharing
~~~~~~~
The sequence counter is shared across all :class:`Sequence` attributes of the
:class:`Factory`:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
phone = factory.Sequence(lambda n: '%04d' % n)
office = factory.Sequence(lambda n: 'A23-B%03d' % n)
.. code-block:: pycon
>>> u = UserFactory()
>>> u.phone, u.office
'0040', 'A23-B040'
>>> u2 = UserFactory()
>>> u2.phone, u2.office
'0041', 'A23-B041'
Inheritance
~~~~~~~~~~~
When a :class:`Factory` inherits from another :class:`Factory` and the `model`
of the subclass inherits from the `model` of the parent, the sequence counter
is shared across the :class:`Factory` classes:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
phone = factory.Sequence(lambda n: '123-555-%04d' % n)
class EmployeeFactory(UserFactory):
office_phone = factory.Sequence(lambda n: '%04d' % n)
.. code-block:: pycon
>>> u = UserFactory()
>>> u.phone
'123-555-0000'
>>> e = EmployeeFactory()
>>> e.phone, e.office_phone
'123-555-0001', '0001'
>>> u2 = UserFactory()
>>> u2.phone
'123-555-0002'
.. _forcing-a-sequence-counter:
Forcing a sequence counter
~~~~~~~~~~~~~~~~~~~~~~~~~~
If a specific value of the sequence counter is required for one instance, the
``__sequence`` keyword argument should be passed to the factory method.
This will force the sequence counter during the call, without altering the
class-level value.
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
uid = factory.Sequence(int)
.. code-block:: pycon
>>> UserFactory()
<User: 0>
>>> UserFactory()
<User: 1>
>>> UserFactory(__sequence=42)
<User: 42>
.. warning:: The impact of setting ``__sequence=n`` on a ``_batch`` call is
undefined. Each generated instance may share a same counter, or
use incremental values starting from the forced value.
LazyAttributeSequence
"""""""""""""""""""""
.. class:: LazyAttributeSequence(method_to_call)
The :class:`LazyAttributeSequence` declaration merges features of :class:`Sequence`
and :class:`LazyAttribute`.
It takes a single argument, a function whose two parameters are, in order:
* The object being built
* The sequence counter
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
login = 'john'
email = factory.LazyAttributeSequence(lambda o, n: '%s@s%d.example.com' % (o.login, n))
.. code-block:: pycon
>>> UserFactory().email
'john@s0.example.com'
>>> UserFactory(login='jack').email
'jack@s1.example.com'
Decorator
~~~~~~~~~
.. function:: lazy_attribute_sequence(method_to_call)
As for :meth:`lazy_attribute` and :meth:`sequence`, the :meth:`lazy_attribute_sequence`
handles more complex cases:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
login = 'john'
@lazy_attribute_sequence
def email(self, n):
bucket = n % 10
return '%s@s%d.example.com' % (self.login, bucket)
SubFactory
""""""""""
.. class:: SubFactory(factory, **kwargs)
.. OHAI_VIM**
This attribute declaration calls another :class:`Factory` subclass,
selecting the same build strategy and collecting extra kwargs in the process.
The :class:`SubFactory` attribute should be called with:
* A :class:`Factory` subclass as first argument, or the fully qualified import
path to that :class:`Factory` (see :ref:`Circular imports <subfactory-circular>`)
* An optional set of keyword arguments that should be passed when calling that
factory
.. note::
When passing an actual :class:`~factory.Factory` for the
:class:`~factory.SubFactory`'s ``factory`` argument, make sure to pass
the class and not instance (i.e no ``()`` after the class):
.. code-block:: python
class FooFactory(factory.Factory):
class Meta:
model = Foo
bar = factory.SubFactory(BarFactory) # Not BarFactory()
Definition
~~~~~~~~~~
.. code-block:: python
# A standard factory
class UserFactory(factory.Factory):
class Meta:
model = User
# Various fields
first_name = 'John'
last_name = factory.Sequence(lambda n: 'D%se' % ('o' * n)) # De, Doe, Dooe, Doooe, ...
email = factory.LazyAttribute(lambda o: '%s.%s@example.org' % (o.first_name.lower(), o.last_name.lower()))
# A factory for an object with a 'User' field
class CompanyFactory(factory.Factory):
class Meta:
model = Company
name = factory.Sequence(lambda n: 'FactoryBoyz' + 'z' * n)
# Let's use our UserFactory to create that user, and override its first name.
owner = factory.SubFactory(UserFactory, first_name='Jack')
Calling
~~~~~~~
The wrapping factory will call of the inner factory:
.. code-block:: pycon
>>> c = CompanyFactory()
>>> c
<Company: FactoryBoyz>
# Notice that the first_name was overridden
>>> c.owner
<User: Jack De>
>>> c.owner.email
jack.de@example.org
Fields of the :class:`~factory.SubFactory` may be overridden from the external factory:
.. code-block:: pycon
>>> c = CompanyFactory(owner__first_name='Henry')
>>> c.owner
<User: Henry Doe>
# Notice that the updated first_name was propagated to the email LazyAttribute.
>>> c.owner.email
henry.doe@example.org
# It is also possible to override other fields of the SubFactory
>>> c = CompanyFactory(owner__last_name='Jones')
>>> c.owner
<User: Henry Jones>
>>> c.owner.email
henry.jones@example.org
Strategies
~~~~~~~~~~
The strategy chosen for the external factory will be propagated to all subfactories:
.. code-block:: pycon
>>> c = CompanyFactory()
>>> c.pk # Saved to the database
3
>>> c.owner.pk # Saved to the database
8
>>> c = CompanyFactory.build()
>>> c.pk # Not saved
None
>>> c.owner.pk # Not saved either
None
.. _subfactory-circular:
Circular imports
~~~~~~~~~~~~~~~~
Some factories may rely on each other in a circular manner.
This issue can be handled by passing the absolute import path to the target
:class:`Factory` to the :class:`SubFactory`.
.. versionadded:: 1.3.0
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
username = 'john'
main_group = factory.SubFactory('users.factories.GroupFactory')
class GroupFactory(factory.Factory):
class Meta:
model = Group
name = "MyGroup"
owner = factory.SubFactory(UserFactory)
Obviously, such circular relationships require careful handling of loops:
.. code-block:: pycon
>>> owner = UserFactory(main_group=None)
>>> UserFactory(main_group__owner=owner)
<john (group: MyGroup)>
SelfAttribute
"""""""""""""
.. class:: SelfAttribute(dotted_path_to_attribute)
Some fields should reference another field of the object being constructed, or an attribute thereof.
This is performed by the :class:`~factory.SelfAttribute` declaration.
That declaration takes a single argument, a dot-delimited path to the attribute to fetch:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
birthdate = factory.fuzzy.FuzzyDate()
birthmonth = factory.SelfAttribute('birthdate.month')
.. code-block:: pycon
>>> u = UserFactory()
>>> u.birthdate
date(2000, 3, 15)
>>> u.birthmonth
3
.. _factory-parent:
Parents
~~~~~~~
When used in conjunction with :class:`~factory.SubFactory`, the :class:`~factory.SelfAttribute`
gains an "upward" semantic through the double-dot notation, as used in Python imports.
``factory.SelfAttribute('..country.language')`` means
"Select the ``language`` of the ``country`` of the :class:`~factory.Factory` calling me".
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
language = 'en'
class CompanyFactory(factory.Factory):
class Meta:
model = Company
country = factory.SubFactory(CountryFactory)
owner = factory.SubFactory(UserFactory, language=factory.SelfAttribute('..country.language'))
.. code-block:: pycon
>>> company = CompanyFactory()
>>> company.country.language
'fr'
>>> company.owner.language
'fr'
Obviously, this "follow parents" ability also handles overriding some attributes on call:
.. code-block:: pycon
>>> company = CompanyFactory(country=china)
>>> company.owner.language
'cn'
This feature is also available to :class:`LazyAttribute` and :class:`LazyAttributeSequence`,
through the ``factory_parent`` attribute of the passed-in object:
.. code-block:: python
class CompanyFactory(factory.Factory):
class Meta:
model = Company
country = factory.SubFactory(CountryFactory)
owner = factory.SubFactory(UserFactory,
language=factory.LazyAttribute(lambda user: user.factory_parent.country.language),
)
Iterator
""""""""
.. class:: Iterator(iterable, cycle=True, getter=None)
The :class:`Iterator` declaration takes successive values from the given
iterable. When it is exhausted, it starts again from zero (unless ``cycle=False``).
.. attribute:: cycle
The ``cycle`` argument is only useful for advanced cases, where the provided
iterable has no end (as wishing to cycle it means storing values in memory...).
.. versionadded:: 1.3.0
The ``cycle`` argument is available as of v1.3.0; previous versions
had a behavior equivalent to ``cycle=False``.
.. attribute:: getter
A custom function called on each value returned by the iterable.
See the :ref:`iterator-getter` section for details.
.. versionadded:: 1.3.0
.. method:: reset()
Reset the internal iterator used by the attribute, so that the next value
will be the first value generated by the iterator.
May be called several times.
Each call to the factory will receive the next value from the iterable:
.. code-block:: python
class UserFactory(factory.Factory)
lang = factory.Iterator(['en', 'fr', 'es', 'it', 'de'])
.. code-block:: pycon
>>> UserFactory().lang
'en'
>>> UserFactory().lang
'fr'
When a value is passed in for the argument, the iterator will *not* be advanced:
.. code-block:: pycon
>>> UserFactory().lang
'en'
>>> UserFactory(lang='cn').lang
'cn'
>>> UserFactory().lang
'fr'
.. _iterator-getter:
Getter
~~~~~~
Some situations may reuse an existing iterable, using only some component.
This is handled by the :attr:`~Iterator.getter` attribute: this is a function
that accepts as sole parameter a value from the iterable, and returns an
adequate value.
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
# CATEGORY_CHOICES is a list of (key, title) tuples
category = factory.Iterator(User.CATEGORY_CHOICES, getter=lambda c: c[0])
Decorator
~~~~~~~~~
.. function:: iterator(func)
When generating items of the iterator gets too complex for a simple list comprehension,
use the :func:`iterator` decorator:
.. warning:: The decorated function takes **no** argument,
notably no ``self`` parameter.
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
@factory.iterator
def name():
with open('test/data/names.dat', 'r') as f:
for line in f:
yield line
.. warning:: Values from the underlying iterator are *kept* in memory; once the
initial iterator has been emptied, saved values are used instead of
executing the function instead.
Use ``factory.Iterator(my_func, cycle=False)`` to disable value
recycling.
Resetting
~~~~~~~~~
In order to start back at the first value in an :class:`Iterator`,
simply call the :meth:`~Iterator.reset` method of that attribute
(accessing it from the bare :class:`~Factory` subclass):
.. code-block:: pycon
>>> UserFactory().lang
'en'
>>> UserFactory().lang
'fr'
>>> UserFactory.lang.reset()
>>> UserFactory().lang
'en'
Dict and List
"""""""""""""
When a factory expects lists or dicts as arguments, such values can be generated
through the whole range of factory_boy declarations,
with the :class:`Dict` and :class:`List` attributes:
.. class:: Dict(params[, dict_factory=factory.DictFactory])
The :class:`Dict` class is used for dict-like attributes.
It receives as non-keyword argument a dictionary of fields to define, whose
value may be any factory-enabled declarations:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
is_superuser = False
roles = factory.Dict({
'role1': True,
'role2': False,
'role3': factory.Iterator([True, False]),
'admin': factory.SelfAttribute('..is_superuser'),
})
.. note:: Declarations used as a :class:`Dict` values are evaluated within
that :class:`Dict`'s context; this means that you must use
the ``..foo`` syntax to access fields defined at the factory level.
On the other hand, the :class:`Sequence` counter is aligned on the
containing factory's one.
The :class:`Dict` behavior can be tuned through the following parameters:
.. attribute:: dict_factory
The actual factory to use for generating the dict can be set as a keyword
argument, if an exotic dictionary-like object (SortedDict, ...) is required.
.. class:: List(items[, list_factory=factory.ListFactory])
The :class:`List` can be used for list-like attributes.
Internally, the fields are converted into a ``index=value`` dict, which
makes it possible to override some values at use time:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
flags = factory.List([
'user',
'active',
'admin',
])
.. code-block:: pycon
>>> u = UserFactory(flags__2='superadmin')
>>> u.flags
['user', 'active', 'superadmin']
The :class:`List` behavior can be tuned through the following parameters:
.. attribute:: list_factory
The actual factory to use for generating the list can be set as a keyword
argument, if another type (tuple, set, ...) is required.
Maybe
"""""
.. class:: Maybe(decider, yes_declaration, no_declaration)
Sometimes, the way to build a given field depends on the value of another,
for instance of a parameter.
In those cases, use the :class:`~factory.Maybe` declaration:
it takes the name of a "decider" boolean field, and two declarations; depending on
the value of the field whose name is held in the 'decider' parameter, it will
apply the effects of one or the other declaration:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
is_active = True
deactivation_date = factory.Maybe(
'is_active',
yes_declaration=None,
no_declaration=factory.fuzzy.FuzzyDateTime(timezone.now() - datetime.timedelta(days=10)),
)
.. code-block:: pycon
>>> u = UserFactory(is_active=True)
>>> u.deactivation_date
None
>>> u = UserFactory(is_active=False)
>>> u.deactivation_date
datetime.datetime(2017, 4, 1, 23, 21, 23, tzinfo=UTC)
.. note:: If the condition for the decider is complex, use a :class:`LazyAttribute`
defined in the :attr:`~Factory.Params` section of your factory to
handle the computation.
.. _post-generation-hooks:
Post-generation hooks
"""""""""""""""""""""
Some objects expect additional method calls or complex processing for proper definition.
For instance, a ``User`` may need to have a related ``Profile``, where the ``Profile`` is built from the ``User`` object.
To support this pattern, factory_boy provides the following tools:
- :class:`PostGenerationMethodCall`: allows you to hook a particular attribute to a function call
- :class:`PostGeneration`: this class allows calling a given function with the generated object as argument
- :func:`post_generation`: decorator performing the same functions as :class:`PostGeneration`
- :class:`RelatedFactory`: this builds or creates a given factory *after* building/creating the first Factory.
- :class:`RelatedFactoryList`: this builds or creates a *list* of the given factory *after* building/creating the first Factory.
Post-generation hooks are called in the same order they are declared in the factory class, so that
functions can rely on the side effects applied by the previous post-generation hook.
Extracting parameters
"""""""""""""""""""""
All post-building hooks share a common base for picking parameters from the
set of attributes passed to the :class:`Factory`.
For instance, a :class:`PostGeneration` hook is declared as ``post``:
.. code-block:: python
class SomeFactory(factory.Factory):
class Meta:
model = SomeObject
@post_generation
def post(obj, create, extracted, **kwargs):
obj.set_origin(create)
.. OHAI_VIM**
When calling the factory, some arguments will be extracted for this method:
- If a ``post`` argument is passed, it will be passed as the ``extracted`` field
- Any argument starting with ``post__XYZ`` will be extracted, its ``post__`` prefix
removed, and added to the kwargs passed to the post-generation hook.
Extracted arguments won't be passed to the :attr:`~FactoryOptions.model` class.
Thus, in the following call:
.. code-block:: pycon
>>> SomeFactory(
post=1,
post_x=2,
post__y=3,
post__z__t=42,
)
The ``post`` hook will receive ``1`` as ``extracted`` and ``{'y': 3, 'z__t': 42}``
as keyword arguments; ``{'post_x': 2}`` will be passed to ``SomeFactory._meta.model``.
RelatedFactory
""""""""""""""
.. class:: RelatedFactory(factory, factory_related_name='', **kwargs)
.. OHAI_VIM**
A :class:`RelatedFactory` behaves mostly like a :class:`SubFactory`,
with the main difference that the related :class:`Factory` will be generated
*after* the base :class:`Factory`.
.. attribute:: factory
As for :class:`SubFactory`, the :attr:`factory` argument can be:
- A :class:`Factory` subclass
- Or the fully qualified path to a :class:`Factory` subclass
(see :ref:`subfactory-circular` for details)
.. attribute:: factory_related_name
If set, the object generated by the factory declaring the
``RelatedFactory`` is passed as keyword argument to the related factory.
.. code-block:: python
class CityFactory(factory.Factory):
class Meta:
model = City
capital_of = None
name = "Toronto"
class CountryFactory(factory.Factory):
class Meta:
model = Country
lang = 'fr'
capital_city = factory.RelatedFactory(
CityFactory, # Not CityFactory()
factory_related_name='capital_of',
name="Paris",
)
.. code-block:: pycon
>>> france = CountryFactory()
>>> City.objects.get(capital_of=france)
<City: Paris>
Extra kwargs may be passed to the related factory, through the usual ``ATTR__SUBATTR`` syntax:
.. code-block:: pycon
>>> england = CountryFactory(lang='en', capital_city__name="London")
>>> City.objects.get(capital_of=england)
<City: London>
If a value is passed for the :class:`RelatedFactory` attribute, this disables
:class:`RelatedFactory` generation:
.. code-block:: pycon
>>> france = CountryFactory()
>>> paris = City.objects.get()
>>> paris
<City: Paris>
>>> reunion = CountryFactory(capital_city=paris)
>>> City.objects.count() # No new capital_city generated
1
>>> guyane = CountryFactory(capital_city=paris, capital_city__name='Kourou')
>>> City.objects.count() # No new capital_city generated, ``name`` ignored.
1
.. note:: The target of the :class:`RelatedFactory` is evaluated *after* the initial factory has been instantiated.
However, the build context is passed down to that factory; this means that calls to
:class:`factory.SelfAttribute` *can* go back to the calling factory's context:
.. code-block:: python
class CountryFactory(factory.Factory):
class Meta:
model = Country
lang = 'fr'
capital_city = factory.RelatedFactory(
CityFactory,
factory_related_name='capital_of',
# Would also work with SelfAttribute('capital_of.lang')
main_lang=factory.SelfAttribute('..lang'),
)
RelatedFactoryList
""""""""""""""""""
.. class:: RelatedFactoryList(factory, factory_related_name='', size=2, **kwargs)
.. OHAI_VIM**
A :class:`RelatedFactoryList` behaves like a :class:`RelatedFactory`, only it returns a
list of factories. This is useful for simulating one-to-many relations, rather than the
one-to-one relation generated by :class:`RelatedFactory`.
.. attribute:: factory
As for :class:`SubFactory`, the :attr:`factory` argument can be:
- A :class:`Factory` subclass
- Or the fully qualified path to a :class:`Factory` subclass
(see :ref:`subfactory-circular` for details)
.. attribute:: factory_related_name
If set, the object generated by the factory declaring the
``RelatedFactory`` is passed as keyword argument to the related factory.
.. attribute:: size
Either an ``int``, or a ``lambda`` that returns an ``int``, which will define the number
of related Factories to be generated for each parent object.
.. versionadded:: 2.12
Note that the API for :class:`RelatedFactoryList` is considered experimental, and might change
in a future version for increased consistency with other declarations.
.. note::
Note that using a ``lambda`` for ``size`` allows the number of related objects per
parents object to vary. This is useful for testing, when you likely don't want your mock
data to have parent objects with the exact same, static number of related objects.
.. code-block:: python
class FooFactory(factory.Factory):
class Meta:
model = Foo
# Generate a list of `factory` objects of random size, ranging from 1 -> 5
bar = factory.RelatedFactoryList(BarFactory, size=lambda: random.randint(1, 5))
# Each Foo object will have exactly 3 Bar objects generated for its foobar attribute.
foobar = factory.RelatedFactoryList(BarFactory, size=3)
PostGeneration
""""""""""""""
.. class:: PostGeneration(callable)
The :class:`PostGeneration` declaration performs actions once the model object
has been generated.
Its sole argument is a callable, that will be called once the base object has
been generated.
Once the base object has been generated, the provided callable will be called
as ``callable(obj, create, extracted, **kwargs)``, where:
- ``obj`` is the base object previously generated
- ``create`` is a boolean indicating which strategy was used
- ``extracted`` is ``None`` unless a value was passed in for the
:class:`PostGeneration` declaration at :class:`Factory` declaration time
- ``kwargs`` are any extra parameters passed as ``attr__key=value`` when calling
the :class:`Factory`:
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
login = 'john'
make_mbox = factory.PostGeneration(
lambda obj, create, extracted, **kwargs: os.makedirs(obj.login))
.. OHAI_VIM**
Decorator
~~~~~~~~~
.. function:: post_generation
A decorator is also provided, decorating a single method accepting the same
``obj``, ``create``, ``extracted`` and keyword arguments as :class:`PostGeneration`.
.. code-block:: python
class UserFactory(factory.Factory):
class Meta:
model = User
login = 'john'
@factory.post_generation
def mbox(obj, create, extracted, **kwargs):
if not create:
return
path = extracted or os.path.join('/tmp/mbox/', obj.login)
os.path.makedirs(path)
.. OHAI_VIM**
.. code-block:: pycon
>>> UserFactory.build() # Nothing was created
>>> UserFactory.create() # Creates dir /tmp/mbox/john
>>> UserFactory.create(login='jack') # Creates dir /tmp/mbox/jack
>>> UserFactory.create(mbox='/tmp/alt') # Creates dir /tmp/alt
PostGenerationMethodCall
""""""""""""""""""""""""
.. class:: PostGenerationMethodCall(method_name, *arg, **kwargs)
.. OHAI_VIM*
The :class:`PostGenerationMethodCall` declaration will call a method on
the generated object just after instantiation. This declaration class
provides a friendly means of generating attributes of a factory instance
during initialization. The declaration is created using the following arguments:
.. attribute:: method_name
The name of the method to call on the :attr:`~FactoryOptions.model` object
.. attribute:: arg
The default, optional, positional argument to pass to the method given in
:attr:`method_name`
.. attribute:: kwargs
The default set of keyword arguments to pass to the method given in
:attr:`method_name`
Once the factory instance has been generated, the method specified in
:attr:`~PostGenerationMethodCall.method_name` will be called on the generated object
with any arguments specified in the :class:`PostGenerationMethodCall` declaration, by
default.
For example, we could use ``PostGenerationMethodCall`` to register created
users in an external system.
.. code-block:: python
class User(models.Model):
name = models.CharField(max_length=191)
def register(self, system, auth_token="ABC"):
self.registration_id = system.register(auth_token)
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
name = 'user'
register = factory.PostGenerationMethodCall("register", DefaultRegistry())
If the :class:`PostGenerationMethodCall` declaration contained no
arguments or one argument, an overriding value can be passed
directly to the method through a keyword argument matching the attribute name.
.. code-block:: pycon
>>> # DefaultRegistry uses UUID for identifiers.
>>> UserFactory().registration_id
'edf42c11-0065-43ad-ad3d-78ab7497aaae'
>>> # OtherRegistry uses int for identifiers.
>>> UserFactory(register=OtherRegistry()).registration_id
123456
.. warning:: In order to keep a consistent and simple API, a :class:`PostGenerationMethodCall`
allows *at most one* positional argument; all other parameters should be passed as
keyword arguments.
Keywords extracted from the factory arguments are merged into the
defaults present in the :class:`PostGenerationMethodCall` declaration.
.. code-block:: pycon
>>> # Calls user.register(DefaultRegistry(), auth_token="DEF")
>>> UserFactory(register__auth_token="DEF")
Module-level functions
----------------------
Beyond the :class:`Factory` class and the various :ref:`declarations` classes
and methods, factory_boy exposes a few module-level functions, mostly useful
for lightweight factory generation.
Lightweight factory declaration
"""""""""""""""""""""""""""""""
.. function:: make_factory(klass, **kwargs)
.. OHAI_VIM**
The :func:`make_factory` function takes a class, declarations as keyword arguments,
and generates a new :class:`Factory` for that class accordingly:
.. code-block:: python
UserFactory = make_factory(User,
login='john',
email=factory.LazyAttribute(lambda u: '%s@example.com' % u.login),
)
# This is equivalent to:
class UserFactory(factory.Factory):
class Meta:
model = User
login = 'john'
email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)
An alternate base class to :class:`Factory` can be specified in the
``FACTORY_CLASS`` argument:
.. code-block:: python
UserFactory = make_factory(models.User,
login='john',
email=factory.LazyAttribute(lambda u: '%s@example.com' % u.login),
FACTORY_CLASS=factory.django.DjangoModelFactory,
)
# This is equivalent to:
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.User
login = 'john'
email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)
.. versionadded:: 2.0.0
The ``FACTORY_CLASS`` kwarg was added in 2.0.0.
Instance building
"""""""""""""""""
The :mod:`factory` module provides a bunch of shortcuts for creating a factory and
extracting instances from them. Helper methods can be used to create factories
in a dynamic way based on parameters.
Internally, helper methods use :func:`make_factory` to create a new
:class:`Factory` and perform additional calls on the newly created
:class:`Factory` according to the method name.
Please note, that all Factories created with this methods inherit from the
:class:`factory.Factory` class. For full support of your ``ORM``, specify
a base class with the ``FACTORY_CLASS`` parameter as shown in
:func:`make_factory` examples.
.. function:: build(klass, FACTORY_CLASS=None, **kwargs)
.. function:: build_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for ``klass`` using declarations passed in kwargs;
return an instance built from that factory with :data:`BUILD_STRATEGY`,
or a list of ``size`` instances (for :func:`build_batch`).
:param type klass: Class of the instance to build
:param int size: Number of instances to build
:param kwargs: Declarations to use for the generated factory
:param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
.. function:: create(klass, FACTORY_CLASS=None, **kwargs)
.. function:: create_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for ``klass`` using declarations passed in kwargs;
return an instance created from that factory with :data:`CREATE_STRATEGY`,
or a list of ``size`` instances (for :func:`create_batch`).
:param type klass: Class of the instance to create
:param int size: Number of instances to create
:param kwargs: Declarations to use for the generated factory
:param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
.. function:: stub(klass, FACTORY_CLASS=None, **kwargs)
.. function:: stub_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for ``klass`` using declarations passed in kwargs;
return an instance stubbed from that factory with :data:`STUB_STRATEGY`,
or a list of ``size`` instances (for :func:`stub_batch`).
:param type klass: Class of the instance to stub
:param int size: Number of instances to stub
:param kwargs: Declarations to use for the generated factory
:param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
.. function:: generate(klass, strategy, FACTORY_CLASS=None, **kwargs)
.. function:: generate_batch(klass, strategy, size, FACTORY_CLASS=None, **kwargs)
Create a factory for ``klass`` using declarations passed in kwargs;
return an instance generated from that factory with the ``strategy`` strategy,
or a list of ``size`` instances (for :func:`generate_batch`).
:param type klass: Class of the instance to generate
:param str strategy: The strategy to use
:param int size: Number of instances to generate
:param kwargs: Declarations to use for the generated factory
:param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
.. function:: simple_generate(klass, create, FACTORY_CLASS=None, **kwargs)
.. function:: simple_generate_batch(klass, create, size, FACTORY_CLASS=None, **kwargs)
Create a factory for ``klass`` using declarations passed in kwargs;
return an instance generated from that factory according to the ``create`` flag,
or a list of ``size`` instances (for :func:`simple_generate_batch`).
:param type klass: Class of the instance to generate
:param bool create: Whether to build (``False``) or create (``True``) instances
:param int size: Number of instances to generate
:param kwargs: Declarations to use for the generated factory
:param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
Randomness management
---------------------
.. module:: factory.random
Using :mod:`random` in factories allows to "fuzz" a program efficiently.
However, it's sometimes required to *reproduce* a failing test.
:mod:`factory.fuzzy` and :class:`factory.Faker` share a dedicated instance
of :class:`random.Random`, which can be managed through the :mod:`factory.random` module:
.. method:: get_random_state()
Call :meth:`get_random_state` to retrieve the random generator's current
state. This method synchronizes both Faker’s and factory_boy’s random state.
The returned object is implementation-specific.
.. method:: set_random_state(state)
Use :meth:`set_random_state` to set a custom state into the random generator
(fetched from :meth:`get_random_state` in a previous run, for instance)
.. method:: reseed_random(seed)
The :meth:`reseed_random` function allows to load a chosen seed into the random generator.
That seed can be anything accepted by :func:`random.seed`.
.. data:: randgen
The :class:`random.Random` global instance used by :mod:`factory.fuzzy`
and :class:`factory.Faker`.
See :ref:`recipe-random-management` for help in using those methods in a test setup.
|