factories.py 1.28 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
class TagFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Tag
15
        django_get_or_create = ('name',)
Okhin's avatar
Okhin committed
16
17
18
19

    name = factory.Faker("word")


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

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

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

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

38
    status = FuzzyChoice([s[0] for s in STATUS_CHOICES])
39
40
41
    archive = choice([True, False])
    quote = choice([True, False])
    speak = choice([True, False])
42

43
44
45
46
47
48
49
50
    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if not create:
            return

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