def test_make_factory(self): fact = factory.make_factory(TestObject, two=2, three=factory.LazyAttribute(lambda o: o.two + 1)) obj = fact.build() self.assertEqual(obj.one, None) self.assertEqual(obj.two, 2) self.assertEqual(obj.three, 3) self.assertEqual(obj.four, None) obj = fact.build(two=4) self.assertEqual(obj.one, None) self.assertEqual(obj.two, 4) self.assertEqual(obj.three, 5) self.assertEqual(obj.four, None)
def test_make_factory(self): fact = factory.make_factory( TestObject, two=2, three=factory.LazyAttribute(lambda o: o.two + 1)) obj = fact.build() self.assertEqual(obj.one, None) self.assertEqual(obj.two, 2) self.assertEqual(obj.three, 3) self.assertEqual(obj.four, None) obj = fact.build(two=4) self.assertEqual(obj.one, None) self.assertEqual(obj.two, 4) self.assertEqual(obj.three, 5) self.assertEqual(obj.four, None)
def setUp(self, mock_now, mock_team_task_assigned): TeamAPITestBase.setUp(self) self.member = UserFactory() self.manager = UserFactory() self.admin = UserFactory() self.team = TeamFactory( workflow_type='O', workflow_enabled=True, task_expiration=1, owner=self.user, admin=self.admin, manager=self.manager, member=self.member, ) workflow = self.team.get_workflow() workflow.review_allowed = 20 # manager must review workflow.approve_allowed = 20 # admin must approve workflow.save() self.list_url = reverse('api:tasks-list', kwargs={ 'team_slug': self.team.slug, }) self.project = ProjectFactory(team=self.team) self.team_video = TeamVideoFactory(team=self.team, project=self.project) self.task_factory = factory.make_factory( TaskFactory, team=self.team, team_video=self.team_video) mock_now.return_value = self.now = datetime(2015, 1, 1) self.mock_team_task_assigned = mock_team_task_assigned
import factory from factory import fuzzy from mymoney.mymoneycore.factories import UserFactory from .models import BankTransactionTag BankTransactionTagFactory = factory.make_factory( BankTransactionTag, FACTORY_CLASS=factory.DjangoModelFactory, name=fuzzy.FuzzyText(), owner=factory.SubFactory(UserFactory), )
from factory import Faker, Sequence, make_factory from lms import models HGroup = make_factory( # pylint:disable=invalid-name models.HGroup, name=Sequence(lambda n: f"Test Group {n}"), authority_provided_id=Faker("hexify", text="^" * 40), )
import factory from factory import fuzzy from mymoney.core.factories import UserFactory from .models import BankTransactionTag BankTransactionTagFactory = factory.make_factory( BankTransactionTag, FACTORY_CLASS=factory.DjangoModelFactory, name=fuzzy.FuzzyText(), owner=factory.SubFactory(UserFactory), )
# Long form way to create a factory # Allows for the most control class StreetFactory(factory.DjangoModelFactory): class Meta: model = models.Street name = fuzzy.FuzzyText(length=30) postal_code = factory.Sequence(lambda x: '{:05d}'.format(x)) # Simple cases can be built with the make_factory call, assuming you don't need # anything special for post-generation. Personally I tend to prefer actual # classes, but there's nothing wrong with make_factory CityFactory = factory.make_factory(models.City, FACTORY_CLASS=factory.DjangoModelFactory, name=fuzzy.FuzzyText(length=25)) class AddressFactory(factory.DjangoModelFactory): STATES = ('CA', 'MA', 'MN', 'TX', 'TN', 'WA', 'OR') class Meta: model = models.Address # If it's not part of your model, make sure you exlucde it! exclude = ('STATES',) street = factory.SubFactory(StreetFactory) city = factory.SubFactory(CityFactory) state = factory.Iterator(STATES)
from tests.factories._attributes import ( H_DISPLAY_NAME, H_USERNAME, OAUTH_CONSUMER_KEY, USER_ID, ) GradingInfo = make_factory( # pylint:disable=invalid-name models.GradingInfo, FACTORY_CLASS=SQLAlchemyModelFactory, lis_result_sourcedid=Faker("numerify", text="test_lis_result_sourcedid_#"), lis_outcome_service_url=Faker( "numerify", text="https://example.com/test-lis-outcome-service-url-#"), oauth_consumer_key=OAUTH_CONSUMER_KEY, user_id=USER_ID, context_id=Faker("hexify", text="^" * 32), resource_link_id=Faker("hexify", text="^" * 32), tool_consumer_info_product_family_code=Faker( "random_element", elements=[ "BlackBoardLearn", "moodle", "canvas", "sakai", "desire2learn", ], ), h_username=H_USERNAME, h_display_name=H_DISPLAY_NAME, )
from factory import Faker, make_factory from lms import models from tests.factories._attributes import OAUTH_CONSUMER_KEY, USER_ID LTIUser = make_factory( # pylint:disable=invalid-name models.LTIUser, user_id=USER_ID, oauth_consumer_key=OAUTH_CONSUMER_KEY, roles=Faker("random_element", elements=["Learner", "Instructor"]), tool_consumer_instance_guid=Faker("hexify", text="^" * 40), display_name=Faker("name"), )
import factory from lms import models from tests.factories._attributes import H_DISPLAY_NAME, H_USERNAME HUser = factory.make_factory( # pylint:disable=invalid-name models.HUser, username=H_USERNAME, display_name=H_DISPLAY_NAME, provider=factory.Faker("hexify", text="^" * 40), provider_unique_id=factory.Faker("hexify", text="^" * 40), )
# Long form way to create a factory # Allows for the most control class StreetFactory(factory.DjangoModelFactory): class Meta: model = models.Street name = fuzzy.FuzzyText(length=30) postal_code = factory.Sequence(lambda x: "{:05d}".format(x)) # Simple cases can be built with the make_factory call, assuming you don't need # anything special for post-generation. Personally I tend to prefer actual # classes, but there's nothing wrong with make_factory CityFactory = factory.make_factory( models.City, FACTORY_CLASS=factory.DjangoModelFactory, name=fuzzy.FuzzyText(length=25) ) class AddressFactory(factory.DjangoModelFactory): STATES = ("CA", "MA", "MN", "TX", "TN", "WA", "OR") class Meta: model = models.Address # If it's not part of your model, make sure you exlucde it! exclude = ("STATES",) street = factory.SubFactory(StreetFactory) city = factory.SubFactory(CityFactory) state = factory.Iterator(STATES)
def dict_factory(cls, create=False, extra=None): # https://github.com/FactoryBoy/factory_boy/blob/master/factory/base.py#L443 declarations = cls._meta.pre_declarations.as_dict() declarations.update(extra or {}) return factory.make_factory(dict, **declarations)