Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move tests to a separate directory

Patryk Zawadzki6d185010d8e9
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 7adff8a2d9..7d4b78f1b3 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -2,17 +2,17 @@ from __future__ import unicode_literals
from itertools import chain
+from babeldjango.templatetags.babel import currencyfmt
from django.contrib import messages
from django.http import JsonResponse
-from django.shortcuts import redirect, get_object_or_404
+from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
-from babeldjango.templatetags.babel import currencyfmt
-from .forms import ReplaceCartLineForm
-from .decorators import get_or_create_db_cart, get_or_empty_db_cart
-from .utils import check_product_availability_and_warn
from ..product.forms import get_form_class_for_product
from ..product.models import Product, ProductVariant
+from .decorators import get_or_create_db_cart, get_or_empty_db_cart
+from .forms import ReplaceCartLineForm
+from .utils import check_product_availability_and_warn
@get_or_empty_db_cart
diff --git a/saleor/product/test_product.py b/saleor/product/test_product.py
deleted file mode 100644
index f1217e10b4..0000000000
--- a/saleor/product/test_product.py
+++ /dev/null
@@ -1,35 +0,0 @@
-import pytest
-
-from . import models
-
-
-def product_in_stock(db):
- product = models.Product.objects.create(
- name='Test product', price=10, weight=1)
- variant = models.ProductVariant.objects.create(product=product, sku='123')
- models.Stock.objects.create(
- variant=variant, cost_price=1, quantity=5, quantity_allocated=5,
- location='Warehouse 1')
- models.Stock.objects.create(
- variant=variant, cost_price=100, quantity=5, quantity_allocated=5,
- location='Warehouse 2')
- models.Stock.objects.create(
- variant=variant, cost_price=10, quantity=5, quantity_allocated=0,
- location='Warehouse 3')
- return product
-
-
-def test_stock_selector(product_in_stock):
- variant = product_in_stock.variants.get()
- preferred_stock = variant.select_stockrecord(5)
- assert preferred_stock.quantity_available >= 5
-
-
-def test_stock_allocator(product_in_stock):
- variant = product_in_stock.variants.get()
- stock = variant.select_stockrecord(5)
- assert stock.quantity_allocated == 0
- models.Stock.objects.allocate_stock(stock, 1)
- stock = models.Stock.objects.get(pk=stock.pk)
- assert stock.quantity_allocated == 1
diff --git a/setup.cfg b/setup.cfg
index 242723011c..fa7b04371d 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -13,8 +13,8 @@ exclude_lines =
return NotImplemented
[tool:pytest]
-testpaths = saleor
-DJANGO_SETTINGS_MODULE = saleor.test_settings
+testpaths = tests
+DJANGO_SETTINGS_MODULE = tests.settings
[flake8]
exclude =
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000000..d9fc54d76f
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,32 @@
+import pytest
+
+from saleor.product.models import Product, ProductVariant, Stock
+from saleor.userprofile.models import Address
+
+
+def billing_address(db): # pylint: disable=I0011, W0613
+ return Address.objects.create(
+ first_name='John', last_name='Doe',
+ company_name='Mirumee Software',
+ street_address_1='Tęczowa 7',
+ city='Wrocław',
+ postal_code='53-601',
+ country='PL')
+
+
+def product_in_stock(db): # pylint: disable=I0011, W0613
+ product = Product.objects.create(
+ name='Test product', price=10, weight=1)
+ variant = ProductVariant.objects.create(product=product, sku='123')
+ Stock.objects.create(
+ variant=variant, cost_price=1, quantity=5, quantity_allocated=5,
+ location='Warehouse 1')
+ Stock.objects.create(
+ variant=variant, cost_price=100, quantity=5, quantity_allocated=5,
+ location='Warehouse 2')
+ Stock.objects.create(
+ variant=variant, cost_price=10, quantity=5, quantity_allocated=0,
+ location='Warehouse 3')
+ return product
diff --git a/tests/dashboard/__init__.py b/tests/dashboard/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/dashboard/conftest.py b/tests/dashboard/conftest.py
similarity index 100%
rename from saleor/dashboard/conftest.py
rename to tests/dashboard/conftest.py
diff --git a/saleor/dashboard/product/test_product.py b/tests/dashboard/test_product.py
similarity index 93%
rename from saleor/dashboard/product/test_product.py
rename to tests/dashboard/test_product.py
index 9fb29a7134..15e9520cce 100644
--- a/saleor/dashboard/product/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -3,8 +3,6 @@ from __future__ import unicode_literals
from django.core.urlresolvers import reverse
import pytest
-from ...product.test_product import product_in_stock # NOQA
-
@pytest.mark.integration
@pytest.mark.django_db
diff --git a/saleor/test_settings.py b/tests/settings.py
similarity index 50%
rename from saleor/test_settings.py
rename to tests/settings.py
index fed3d7fb3b..b39884fb2c 100644
--- a/saleor/test_settings.py
+++ b/tests/settings.py
@@ -1,10 +1,11 @@
-from .settings import *
+# pylint: disable=I0011, W0401, W0614
+from saleor.settings import * # noqa
SECRET_KEY = 'NOTREALLY'
DEFAULT_CURRENCY = 'USD'
-DATABASES['default']['TEST'] = {
+DATABASES['default']['TEST'] = { # noqa
'SERIALIZE': False,
'NAME': ':memory:',
'MIRROR': None}
diff --git a/saleor/cart/test_cart.py b/tests/test_cart.py
similarity index 78%
rename from saleor/cart/test_cart.py
rename to tests/test_cart.py
index 7765cf0553..7500b1cc86 100644
--- a/saleor/cart/test_cart.py
+++ b/tests/test_cart.py
@@ -1,55 +1,36 @@
from __future__ import unicode_literals
-from decimal import Decimal
+
import json
-from prices import Price
import pytest
-from mock import Mock, MagicMock, patch
+from django.core.exceptions import ObjectDoesNotExist
+from mock import MagicMock, Mock
from satchless.item import InsufficientStock
-from django.core.exceptions import ObjectDoesNotExist
-from .models import Cart
-from .context_processors import cart_counter
-from . import forms
-from . import utils
-from . import views
-from . import decorators
-from ..product.models import Product, ProductVariant
+from saleor.cart import decorators, forms, utils, views
+from saleor.cart.context_processors import cart_counter
+from saleor.cart.models import Cart
@pytest.fixture
-def cart(db):
+def cart(db): # pylint: disable=I0011, W0613
return Cart.objects.create()
-def product(db):
- product = Product(name='Big Ship', price=Price(10, currency='USD'),
- weight=Decimal(123))
- product.save()
- return product
-
-
-def variant(db, monkeypatch, product):
- variant = ProductVariant(name='Big Ship', product=product)
- variant.save()
- monkeypatch.setattr('saleor.product.models.ProductVariant.check_quantity',
- Mock())
- return variant
-
-
-def test_adding_without_checking(cart, variant):
+def test_adding_without_checking(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1000, check_quantity=False)
assert len(cart) == 1
-def test_adding_zero_quantity(cart, variant):
+def test_adding_zero_quantity(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 0)
assert len(cart) == 0
-def test_adding_same_variant(cart, variant):
+def test_adding_same_variant(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1)
cart.add(variant, 2)
price_total = 10 * 3
@@ -58,21 +39,23 @@ def test_adding_same_variant(cart, variant):
assert cart.get_total().gross == price_total
-def test_replacing_same_variant(cart, variant):
+def test_replacing_same_variant(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1, replace=True)
cart.add(variant, 2, replace=True)
assert len(cart) == 1
assert cart.count() == {'total_quantity': 2}
-def test_adding_invalid_quantity(cart, variant):
+def test_adding_invalid_quantity(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
with pytest.raises(ValueError):
cart.add(variant, -1)
-def test_getting_line(cart, variant):
+def test_getting_line(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
assert cart.get_line(variant) is None
-
line = cart.create_line(variant, 1, None)
assert line == cart.get_line(variant)
@@ -87,13 +70,14 @@ def test_change_status(cart):
assert cart.status == Cart.CANCELED
-def test_shipping_detection(cart, variant):
+def test_shipping_detection(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
assert not cart.is_shipping_required()
cart.add(variant, 1, replace=True)
assert cart.is_shipping_required()
-def test_cart_counter(db, monkeypatch):
+def test_cart_counter(monkeypatch):
monkeypatch.setattr('saleor.cart.context_processors.get_cart_from_request',
Mock(return_value=Mock(quantity=4)))
ret = cart_counter(Mock())
@@ -121,9 +105,9 @@ def test_get_user_open_cart_token(monkeypatch):
assert decorators.get_user_open_cart_token(Mock()) == token
-def test_find_and_assign_cart(monkeypatch, cart, django_user_model):
+def test_find_and_assign_cart(cart, django_user_model):
credentials = {'email': '[email protected]', 'password': 'admin'}
- user, created = django_user_model.objects.get_or_create(
+ user, _created = django_user_model.objects.get_or_create(
email=credentials['email'], defaults={
'is_active': True, 'is_staff': True, 'is_superuser': True})
request = Mock(user=user, get_signed_cookie=lambda x, default: cart.token)
@@ -146,7 +130,9 @@ def test_contains_unavailable_variants():
assert not utils.contains_unavailable_variants(cart)
-def test_check_product_availability_and_warn(monkeypatch, cart, variant):
+def test_check_product_availability_and_warn(
+ monkeypatch, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1)
monkeypatch.setattr('django.contrib.messages.warning',
Mock(warning=Mock()))
@@ -221,7 +207,8 @@ def test_add_to_cart_form_when_insufficient_stock():
assert not form.is_valid()
-def test_replace_cart_line_form(cart, variant):
+def test_replace_cart_line_form(cart, product_in_stock):
+ variant = product_in_stock.variants.get()
initial_quantity = 1
replaced_quantity = 4
@@ -233,8 +220,9 @@ def test_replace_cart_line_form(cart, variant):
assert cart.quantity == replaced_quantity
-def test_replace_cartline_form_when_insufficient_stock(monkeypatch, cart,
- variant):
+def test_replace_cartline_form_when_insufficient_stock(
+ monkeypatch, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
initial_quantity = 1
replaced_quantity = 4
@@ -258,11 +246,12 @@ def test_view_empty_cart(monkeypatch, client, cart):
)
request = client.get('/cart/')
request.discounts = None
- response = views.index(request)
+ response = views.index(request) # pylint: disable=I0011, E1120
assert response.status_code == 200
-def test_view_cart(monkeypatch, client, cart, variant):
+def test_view_cart(monkeypatch, client, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1)
monkeypatch.setattr(
decorators, 'get_cart_from_request',
@@ -270,26 +259,30 @@ def test_view_cart(monkeypatch, client, cart, variant):
)
request = client.get('/cart/')
request.discounts = None
- response = views.index(request)
+ response = views.index(request) # pylint: disable=I0011, E1120
assert response.status_code == 200
-def test_view_update_cart_quantity(monkeypatch, client, cart, variant):
+def test_view_update_cart_quantity(
+ monkeypatch, client, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1)
monkeypatch.setattr(
decorators, 'get_cart_from_request',
- lambda request: cart
- )
- request = client.post('/cart/update/{}'.format(variant.pk), {'quantity': 3})
+ lambda request: cart)
+ request = client.post(
+ '/cart/update/{}'.format(variant.pk), {'quantity': 3})
request.discounts = None
request.POST = {'quantity': 3}
request.is_ajax = lambda: True
+ # pylint: disable=I0011, E1120
response = views.update(request, variant.pk)
assert response.status_code == 200
assert cart.quantity == 3
-def test_view_invalid_update_cart(monkeypatch, client, cart, variant):
+def test_view_invalid_update_cart(monkeypatch, client, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
cart.add(variant, 1)
monkeypatch.setattr(
decorators, 'get_cart_from_request',
@@ -299,6 +292,7 @@ def test_view_invalid_update_cart(monkeypatch, client, cart, variant):
request.discounts = None
request.POST = {}
request.is_ajax = lambda: True
+ # pylint: disable=I0011, E1120
response = views.update(request, variant.pk)
resp_decoded = json.loads(response.content.decode('utf-8'))
assert response.status_code == 400
@@ -306,7 +300,8 @@ def test_view_invalid_update_cart(monkeypatch, client, cart, variant):
assert cart.quantity == 1
-def test_view_invalid_add_to_cart(monkeypatch, client, cart, variant):
+def test_view_invalid_add_to_cart(monkeypatch, client, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
initial_quantity = 1
cart.add(variant, initial_quantity)
monkeypatch.setattr(
@@ -317,23 +312,25 @@ def test_view_invalid_add_to_cart(monkeypatch, client, cart, variant):
request.discounts = None
request.POST = {}
request.user = Mock(is_authenticated=lambda: False)
+ # pylint: disable=I0011, E1120
response = views.add_to_cart(request, variant.pk)
assert response.status_code == 302
assert cart.quantity == initial_quantity
-def test_view_add_to_cart(db, monkeypatch, client, cart, variant):
+def test_view_add_to_cart(monkeypatch, client, cart, product_in_stock):
+ variant = product_in_stock.variants.get()
initial_quantity = 1
cart.add(variant, initial_quantity)
monkeypatch.setattr(
decorators, 'get_cart_from_request',
- lambda request, create: cart
- )
+ lambda request, create: cart)
request = client.post('/cart/add/{}'.format(variant.pk), {})
request.discounts = None
request.POST = {'quantity': 1, 'variant': variant.pk}
request.user = Mock(is_authenticated=lambda: False)
+ # pylint: disable=I0011, E1120
response = views.add_to_cart(request, variant.pk)
assert response.status_code == 302
assert cart.quantity == initial_quantity + 1
diff --git a/saleor/checkout/test_checkout.py b/tests/test_checkout.py
similarity index 96%
rename from saleor/checkout/test_checkout.py
rename to tests/test_checkout.py
index 6d816dff0f..cb22f93e5d 100644
--- a/saleor/checkout/test_checkout.py
+++ b/tests/test_checkout.py
@@ -1,14 +1,13 @@
-from mock import Mock, MagicMock
-
+import pytest
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
+from mock import MagicMock, Mock
from prices import Price
-import pytest
-from .core import Checkout, STORAGE_SESSION_KEY
-from ..shipping.models import ShippingMethodCountry
-from ..userprofile.models import Address
-from . import views
+from saleor.checkout import views
+from saleor.checkout.core import STORAGE_SESSION_KEY, Checkout
+from saleor.shipping.models import ShippingMethodCountry
+from saleor.userprofile.models import Address
def test_checkout_version():
diff --git a/saleor/checkout/test_checkout_integration.py b/tests/test_checkout_integration.py
similarity index 93%
rename from saleor/checkout/test_checkout_integration.py
rename to tests/test_checkout_integration.py
index 97eba89494..cccc10d768 100644
--- a/saleor/checkout/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -2,11 +2,10 @@ from django.contrib.sites.models import Site
from django.core import signing
from django.core.urlresolvers import reverse
-from ..cart.test_cart import variant, product # noqa
-from ..cart.models import Cart
-from ..shipping.models import ShippingMethod
-from ..order import Status as OrderStatus
-from ..order.models import Order
+from saleor.cart.models import Cart
+from saleor.order import Status as OrderStatus
+from saleor.order.models import Order
+from saleor.shipping.models import ShippingMethod
def assert_redirect(response, expected_url):
@@ -18,12 +17,12 @@ def assert_redirect(response, expected_url):
assert location == expected_url
-def test_checkout_flow(product, variant, client):
+def test_checkout_flow(product_in_stock, client):
"""
Basic test case that confirms if core checkout flow works
"""
+ variant = product_in_stock.variants.get()
# Prepare some data
- variant.stock.create(quantity=100, location='Warehouse')
cart = Cart.objects.create()
cart.add(variant)
shipping_method = ShippingMethod.objects.create(name='DHL')
diff --git a/saleor/core/test_core.py b/tests/test_core.py
similarity index 94%
rename from saleor/core/test_core.py
rename to tests/test_core.py
index c57c331536..2ced53cbb3 100644
--- a/saleor/core/test_core.py
+++ b/tests/test_core.py
@@ -1,7 +1,7 @@
-from mock import Mock
import pytest
+from mock import Mock
-from . import get_country_by_ip, get_currency_for_country, Country
+from saleor.core import Country, get_country_by_ip, get_currency_for_country
@pytest.mark.parametrize('reader, expected_country', [
diff --git a/saleor/checkout/test_decorators.py b/tests/test_decorators.py
similarity index 96%
rename from saleor/checkout/test_decorators.py
rename to tests/test_decorators.py
index 2c316f9bd2..241c9165d0 100644
--- a/saleor/checkout/test_decorators.py
+++ b/tests/test_decorators.py
@@ -2,9 +2,9 @@ import pytest
from django.contrib.auth.models import AnonymousUser
from mock import Mock
-from .core import get_or_empty_db_cart
-from ..cart import decorators
-from ..cart.models import Cart
+from saleor.cart import decorators
+from saleor.cart.decorators import get_or_empty_db_cart
+from saleor.cart.models import Cart
def get_request(django_user_model, cookie_token, authenticated=True):
diff --git a/saleor/discount/test_discounts.py b/tests/test_discounts.py
similarity index 98%
rename from saleor/discount/test_discounts.py
rename to tests/test_discounts.py
index 78e7fc6619..2fba35fe6e 100644
--- a/saleor/discount/test_discounts.py
+++ b/tests/test_discounts.py
@@ -1,10 +1,11 @@
from decimal import Decimal
+
+import pytest
from mock import Mock
from prices import FixedDiscount, FractionalDiscount, Price
-import pytest
-from ..product.models import ProductVariant, Product
-from .models import Sale, Voucher, NotApplicable
+from saleor.discount.models import NotApplicable, Sale, Voucher
+from saleor.product.models import Product, ProductVariant
@pytest.fixture
diff --git a/saleor/order/test_order.py b/tests/test_order.py
similarity index 85%
rename from saleor/order/test_order.py
rename to tests/test_order.py
index 950938961f..393429b49c 100644
--- a/saleor/order/test_order.py
+++ b/tests/test_order.py
@@ -1,9 +1,7 @@
from prices import Price
-from ..cart.models import Cart
-from ..product.test_product import product_in_stock # NOQA
-from ..userprofile.test_userprofile import billing_address # NOQA
-from . import models
+from saleor.cart.models import Cart
+from saleor.order import models
def test_total_property():
diff --git a/tests/test_product.py b/tests/test_product.py
new file mode 100644
index 0000000000..e5dd38e7fa
--- /dev/null
+++ b/tests/test_product.py
@@ -0,0 +1,18 @@
+import pytest
+
+from saleor.product import models
+
+
+def test_stock_selector(product_in_stock):
+ variant = product_in_stock.variants.get()
+ preferred_stock = variant.select_stockrecord(5)
+ assert preferred_stock.quantity_available >= 5
+
+
+def test_stock_allocator(product_in_stock):
+ variant = product_in_stock.variants.get()
+ stock = variant.select_stockrecord(5)
+ assert stock.quantity_allocated == 0
+ models.Stock.objects.allocate_stock(stock, 1)
+ stock = models.Stock.objects.get(pk=stock.pk)
+ assert stock.quantity_allocated == 1
diff --git a/saleor/registration/test_registration.py b/tests/test_registration.py
similarity index 96%
rename from saleor/registration/test_registration.py
rename to tests/test_registration.py
index fcf108beaf..f022666243 100644
--- a/saleor/registration/test_registration.py
+++ b/tests/test_registration.py
@@ -1,24 +1,18 @@
from unittest import TestCase
-from django.core.urlresolvers import resolve
+import pytest
from django.conf import settings
+from django.core.urlresolvers import resolve
from django.http import HttpRequest
from django.test import override_settings
-from mock import call, Mock, MagicMock, patch, sentinel
+from mock import MagicMock, Mock, call, patch, sentinel
from purl import URL
-import pytest
-
-from .forms import OAuth2CallbackForm
-from .utils import (
- FACEBOOK,
- FacebookClient,
- GOOGLE,
- GoogleClient,
- OAuth2RequestAuthorizer,
- OAuth2Client,
- parse_response)
-from .views import oauth_callback, change_email
+from saleor.registration.forms import OAuth2CallbackForm
+from saleor.registration.utils import (FACEBOOK, GOOGLE, FacebookClient,
+ GoogleClient, OAuth2Client,
+ OAuth2RequestAuthorizer, parse_response)
+from saleor.registration.views import change_email, oauth_callback
JSON_MIME_TYPE = 'application/json; charset=UTF-8'
URLENCODED_MIME_TYPE = 'application/x-www-form-urlencoded; charset=UTF-8'
diff --git a/saleor/userprofile/test_userprofile.py b/tests/test_userprofile.py
similarity index 97%
rename from saleor/userprofile/test_userprofile.py
rename to tests/test_userprofile.py
index 4a6261e915..cf1483ead5 100644
--- a/saleor/userprofile/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -4,8 +4,7 @@ from __future__ import unicode_literals
import i18naddress
import pytest
-from . import forms
-from . import models
+from saleor.userprofile import forms, models
@pytest.fixture