From 8455c7d1d004cf51820a6b6c31a4ca6520fc3617 Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Mon, 13 Aug 2018 16:02:55 +0300 Subject: [PATCH] Add example code for `CourseFactory` --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index cb7b726..dc73fcc 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,36 @@ There's a lot going on in this test: * We assert that a validation error is going to be raised if we call `full_clean`. * We are not hitting the database at all, since there's no need for that. +Here's how `CourseFactory` looks like: + +```python +class CourseFactory(factory.DjangoModelFactory): + name = factory.Sequence(lambda n: f'{n}{faker.word()}') + start_date = factory.LazyAttribute( + lambda _: get_now() + ) + end_date = factory.LazyAttribute( + lambda _: get_now() + timedelta(days=30) + ) + + slug_url = factory.Sequence(lambda n: f'{n}{faker.slug()}') + + repository = factory.LazyAttribute(lambda _: faker.url()) + video_channel = factory.LazyAttribute(lambda _: faker.url()) + facebook_group = factory.LazyAttribute(lambda _: faker.url()) + + class Meta: + model = Course + + @classmethod + def _build(cls, model_class, *args, **kwargs): + return kwargs + + @classmethod + def _create(cls, model_class, *args, **kwargs): + return create_course(**kwargs) +``` + ## Services A service is a simple function that: