Skip to content

Commit 6c67918

Browse files
26.6
1 parent 33684d7 commit 6c67918

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ django = "*"
88
django-debug-toolbar = "*"
99
mysqlclient = "*"
1010
faker = "*"
11+
factory-boy = "*"
1112

1213
[dev-packages]
1314

Pipfile.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

store/factories.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import random
2+
import factory
3+
from datetime import datetime
4+
from faker import Faker
5+
from factory.django import DjangoModelFactory
6+
7+
from . import models
8+
9+
faker = Faker()
10+
11+
class CategoryFactory(DjangoModelFactory):
12+
class Meta:
13+
model = models.Category
14+
15+
title = factory.Faker(
16+
"sentence",
17+
nb_words=5,
18+
variable_nb_words=True
19+
)
20+
description = factory.Faker('paragraph', nb_sentences=1, variable_nb_sentences=False)
21+
22+
23+
class DiscountFactory(DjangoModelFactory):
24+
class Meta:
25+
model = models.Discount
26+
27+
discount = factory.LazyFunction(lambda: random.randint(1, 80)/100)
28+
description = factory.Faker('paragraph', nb_sentences=1, variable_nb_sentences=False)
29+
30+
31+
class ProductFactory(DjangoModelFactory):
32+
class Meta:
33+
model = models.Product
34+
35+
name = factory.LazyAttribute(lambda x: ' '.join([x.capitalize() for x in faker.words(3)]))
36+
slug = factory.LazyAttribute(lambda x: '-'.join(x.name.split(' ')).lower())
37+
description = factory.Faker('paragraph', nb_sentences=5, variable_nb_sentences=True)
38+
unit_price = factory.LazyFunction(lambda: random.randint(1, 1000) + random.randint(0, 100)/100)
39+
inventory = factory.LazyFunction(lambda: random.randint(1, 100))
40+
41+
42+
class CustomerFactory(DjangoModelFactory):
43+
class Meta:
44+
model = models.Customer
45+
46+
first_name = factory.Faker("first_name")
47+
last_name = factory.Faker("last_name")
48+
email = factory.Faker("email")
49+
phone_number = factory.Faker("phone_number")
50+
birth_date = factory.LazyFunction(lambda: faker.date_time_ad(start_datetime=datetime(1990,1,1), end_datetime=datetime(2015,1,1)))
51+
52+
53+
class AddressFactory(DjangoModelFactory):
54+
class Meta:
55+
model = models.Address
56+
57+
province = factory.Faker("word")
58+
city = factory.Faker("word")
59+
street = factory.LazyFunction(lambda: f'street {random.randint(1, 50)}')
60+
61+
62+
class OrderFactory(DjangoModelFactory):
63+
class Meta:
64+
model = models.Order
65+
66+
status = factory.LazyFunction(lambda: random.choice([models.Order.ORDER_STATUS_UNPAID, models.Order.ORDER_STATUS_CANCELED]))
67+
68+
69+
class OrderItemFactory(DjangoModelFactory):
70+
class Meta:
71+
model = models.OrderItem
72+
73+
quantity = factory.LazyFunction(lambda: random.randint(1, 20))
74+
75+
76+
class CommentFactory(DjangoModelFactory):
77+
class Meta:
78+
model = models.Comment
79+
80+
name = factory.Faker("first_name")
81+
body = factory.Faker('paragraph', nb_sentences=3, variable_nb_sentences=True)
82+
status = factory.LazyFunction(lambda: random.choice([models.Comment.COMMENT_STATUS_WAITING, models.Comment.COMMENT_STATUS_APPROVED, models.Comment.COMMENT_STATUS_NOT_APPROVED]))
83+
84+
85+
class CartFactory(DjangoModelFactory):
86+
class Meta:
87+
model = models.Cart
88+
89+
90+
class CartItemFactory(DjangoModelFactory):
91+
class Meta:
92+
model = models.CartItem
93+
94+
quantity = factory.LazyFunction(lambda: random.randint(1, 20))
95+
96+
97+

0 commit comments

Comments
 (0)