factories.py 980 Bytes
Newer Older
luxcem's avatar
luxcem committed
1 2 3
import datetime

import pytz
4
import factory
5
from factory.fuzzy import FuzzyDateTime, FuzzyChoice
6

7
from .models import Article, STATUS_CHOICES
8 9 10 11 12


class ArticleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Article
luxcem's avatar
luxcem committed
13

14
    url = factory.Faker("url")
15
    lang = "EN"
luxcem's avatar
luxcem committed
16

17
    title = factory.Faker("sentence", nb_words=4)
luxcem's avatar
luxcem committed
18
    website = factory.Sequence(lambda n: "Website {}".format(n))
19
    extracts = factory.Faker("text")
luxcem's avatar
luxcem committed
20 21 22 23 24 25 26 27

    created_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))
    updated_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))
    published_at = FuzzyDateTime(
        datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))

28
    status = FuzzyChoice([s[0] for s in STATUS_CHOICES])
29 30 31 32 33 34 35 36 37

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for tag in extracted:
                self.tags.add(tag)