factories.py 1.24 KB
Newer Older
luxcem's avatar
luxcem committed
1
import datetime
Okhin's avatar
Okhin committed
2
from random import choice, randint
luxcem's avatar
luxcem committed
3
4

import pytz
5
import factory
6
from factory.fuzzy import FuzzyDateTime, FuzzyChoice
Okhin's avatar
Okhin committed
7
from taggit.models import Tag
8

9
from .models import Article, STATUS_CHOICES
10
11


Okhin's avatar
Okhin committed
12
13
14
15
16
17
18
class TagFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Tag

    name = factory.Faker("word")


19
20
21
class ArticleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Article
luxcem's avatar
luxcem committed
22

23
    url = factory.Faker("url")
24
    lang = "EN"
luxcem's avatar
luxcem committed
25

26
    title = factory.Faker("sentence", nb_words=4)
luxcem's avatar
luxcem committed
27
    website = factory.Sequence(lambda n: "Website {}".format(n))
28
    extracts = factory.Faker("text")
luxcem's avatar
luxcem committed
29
30
31
32
33
34
35
36

    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))

37
    status = FuzzyChoice([s[0] for s in STATUS_CHOICES])
38
39
40
    archive = choice([True, False])
    quote = choice([True, False])
    speak = choice([True, False])
41
42
43
44
45
46
47
48
49

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

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