forked from saleor/saleor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_core.py
145 lines (116 loc) · 4.66 KB
/
test_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pytest
from mock import Mock
from saleor.core.utils import (
Country, create_superuser, get_country_by_ip, get_currency_for_country,
random_data)
from saleor.discount.models import Sale, Voucher
from saleor.order.models import Order
from saleor.product.models import Product
from saleor.shipping.models import ShippingMethod
from saleor.userprofile.models import Address, User
class_schema = {'Vegetable': {
'category': 'Food',
'product_attributes': {
'Sweetness': ['Sweet', 'Sour'],
'Healthiness': ['Healthy', 'Not really']
},
'variant_attributes': {
'GMO': ['Yes', 'No']
},
'images_dir': 'candy/',
'is_shipping_required': True}}
@pytest.mark.parametrize('ip_data, expected_country', [
({'country': {'iso_code': 'PL'}}, Country('PL')),
({'country': {'iso_code': 'UNKNOWN'}}, None),
(None, None),
({}, None),
({'country': {}}, None)])
def test_get_country_by_ip(ip_data, expected_country, monkeypatch):
monkeypatch.setattr(
'saleor.core.utils.georeader.get',
Mock(return_value=ip_data))
country = get_country_by_ip('127.0.0.1')
assert country == expected_country
@pytest.mark.parametrize('country, expected_currency', [
(Country('PL'), 'PLN'),
(Country('US'), 'USD'),
(Country('GB'), 'GBP')])
def test_get_currency_for_country(country, expected_currency, monkeypatch):
currency = get_currency_for_country(country)
assert currency == expected_currency
def test_create_superuser(db, client):
credentials = {'email': '[email protected]', 'password': 'admin'}
# Test admin creation
assert User.objects.all().count() == 0
create_superuser(credentials)
assert User.objects.all().count() == 1
admin = User.objects.all().first()
assert admin.is_superuser
# Test duplicating
create_superuser(credentials)
assert User.objects.all().count() == 1
# Test logging in
response = client.post('/account/login/',
{'username': credentials['email'],
'password': credentials['password']},
follow=True)
assert response.context['request'].user == admin
def test_create_shipping_methods(db):
assert ShippingMethod.objects.all().count() == 0
for _ in random_data.create_shipping_methods():
pass
assert ShippingMethod.objects.all().count() == 2
def test_create_fake_user(db):
assert User.objects.all().count() == 0
random_data.create_fake_user()
assert User.objects.all().count() == 1
user = User.objects.all().first()
assert not user.is_superuser
def test_create_fake_users(db):
how_many = 5
for _ in random_data.create_users(how_many):
pass
assert User.objects.all().count() == 5
def test_create_address(db):
assert Address.objects.all().count() == 0
random_data.create_address()
assert Address.objects.all().count() == 1
def test_create_attribute(db):
data = {'slug': 'best_attribute', 'name': 'Best attribute'}
attribute = random_data.create_attribute(**data)
assert attribute.name == data['name']
assert attribute.slug == data['slug']
def test_create_product_classes_by_schema(db):
p_class = random_data.create_product_classes_by_schema(class_schema)[0][0]
assert p_class.name == 'Vegetable'
assert p_class.product_attributes.count() == 2
assert p_class.variant_attributes.count() == 1
assert p_class.is_shipping_required
def test_create_products_by_class(db):
assert Product.objects.all().count() == 0
how_many = 5
p_class = random_data.create_product_classes_by_schema(class_schema)[0][0]
random_data.create_products_by_class(p_class, class_schema['Vegetable'],
'/', how_many=how_many,
create_images=False)
assert Product.objects.all().count() == how_many
def test_create_fake_order(db):
for _ in random_data.create_shipping_methods():
pass
for _ in random_data.create_users(3):
pass
random_data.create_products_by_schema('/', 10, False)
how_many = 5
for _ in random_data.create_orders(how_many):
pass
Order.objects.all().count() == 5
def test_create_product_sales(db):
how_many = 5
for _ in random_data.create_product_sales(how_many):
pass
assert Sale.objects.all().count() == 5
def test_create_vouchers(db):
assert Voucher.objects.all().count() == 0
for _ in random_data.create_vouchers():
pass
assert Voucher.objects.all().count() == 2