Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

#675 Increase options count at the fixtures #677

Merged
merged 14 commits into from
Jun 7, 2019
2 changes: 1 addition & 1 deletion stroyprombeton/fixtures/dump.json

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions stroyprombeton/management/commands/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,21 @@ def create_products(count, categories, tags_):
page=ModelPage.objects.create(name=name),
# remove this in favor Option
)
option = stb_models.Option.objects.create(
option_left = stb_models.Option.objects.create(
price=i * 100,
mark=f'mark #{i}',
mark=f'mark #{2*i - 1}',
product=product,
)
option_right = stb_models.Option.objects.create(
price=i * 200,
mark=f'mark #{2*i}',
product=product,
)
for tag in tags_:
option.tags.add(tag)
option.save()
option_left.tags.add(tag)
option_left.save()
option_right.tags.add(tag)
option_right.save()

if product.id in self.PRODUCTS_WITH_IMAGE:
create_images(product.page)
Expand Down
10 changes: 3 additions & 7 deletions stroyprombeton/tests/tests_commands.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import os

from xml.etree import ElementTree

from django.conf import settings
from django.core.management import call_command
from django.test import TestCase, tag

from pages.models import Page

from django.core.management import call_command

from stroyprombeton.models import Option, Product
from stroyprombeton.management.commands.seo_texts import populate_entities
from stroyprombeton.models import Option, Product


@tag('fast')
Expand Down Expand Up @@ -107,10 +104,9 @@ def test_keep_unsimilar_options(self):
options = list(product.options.all())
call_command(
'remove_option_duplicates',
'code', 'product',
'mark', 'product',
noinput='true',
)
self.assertEqual(len(options), product.options.count())
self.assertEqual(options, list(product.options.all()))

def test_remove_similar_options(self):
Expand Down
32 changes: 32 additions & 0 deletions stroyprombeton/tests/tests_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.conf import settings
from django.test import TestCase, tag

from catalog import context
from stroyprombeton import models as stb_models


@tag('fast')
class Pagination(TestCase):

fixtures = ['dump.json']

def test_options(self):
category = (
stb_models.Category.objects
.filter(products__page__is_active=True)
.first()
)
options = stb_models.Option.objects.filter(
product__in=category.products.all()
)
sliced_options = context.products.PaginatedProducts(
products=stb_models.Option.objects.filter(
product__in=category.products.all()
),
url=category.url,
page_number=1,
per_page=settings.CATEGORY_STEP_MULTIPLIERS[0],
)
sliced, whole = set(sliced_options.products), set(options)
# every `sliced` is contained in `whole`
self.assertEqual(sliced, sliced.intersection(whole))
62 changes: 44 additions & 18 deletions stroyprombeton/tests/tests_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
from urllib.parse import urljoin

from django.core import mail
from django.db.models import Count
from django.template.defaultfilters import floatformat
from django.test import tag
from django.urls import reverse
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC, ui
Expand Down Expand Up @@ -220,25 +218,29 @@ def test_order_page_remove_all_products(self):

self.assertIn('Нет выбранных позиций', order_wrapper_text)

# will be fixed with #665
@unittest.expectedFailure
def test_change_count_in_cart(self):
option = stb_models.Option.objects.first()
self.buy_on_product_page(option=option)
self.proceed_order_page()

change_count_to = 42
count = 42
total_before = self.get_total()

def wait_total_changes(driver):
return self.get_total(driver) != total_before

self.send_keys_and_wait(change_count_to, (By.CLASS_NAME, 'js-count-input'))
self.send_keys_and_wait(count, (By.CLASS_NAME, 'js-count-input'))
self.wait.until(wait_total_changes)

self.assertEqual(
f'{floatformat(str(option.price * change_count_to), 0)} руб',
f'{floatformat(str(option.price * count), 0)} руб',
self.get_total(),
)

# will be fixed with #665
@unittest.expectedFailure
@test_helpers.disable_celery
def test_order_email(self):
option = stb_models.Option.objects.first()
Expand Down Expand Up @@ -281,7 +283,7 @@ def test_order_email(self):
)
self.assertIn(
'<a href="http://www.stroyprombeton.ru{0}"'
.format(reverse('product', args=(1,))),
.format(option.product.url),
sent_mail_body
)
self.assertInHTML(
Expand Down Expand Up @@ -411,6 +413,22 @@ def test_load_more_products(self):

self.assertTrue(before_load_products < after_load_products)

@staticmethod
def get_category_with_options_limit(low: int, high: int):
"""Return the first category having only one load_more click."""
return next(iter(
stb_models.Category.objects.raw(
'''
SELECT cat.id FROM stroyprombeton_category as cat
INNER JOIN stroyprombeton_product as prod ON prod.category_id = cat.id
INNER JOIN stroyprombeton_option as opt ON opt.product_id = prod.id
GROUP BY cat.id
HAVING COUNT(*) > %s AND COUNT(*) < %s;
''',
[low, high]
)
))

def test_load_more_button_disabled_state(self):
"""
Test the load more lint state.
Expand All @@ -419,27 +437,34 @@ def test_load_more_button_disabled_state(self):
see that `Load more` link becomes disabled. That means that there are
no more filtered products to load from server.
"""
self.load_category_page(self.middle_category)
per_page = request_data.Category.PRODUCTS_ON_PAGE_PC
self.load_category_page(
self.get_category_with_options_limit(
low=per_page, high=2 * per_page
)
)
self.send_keys_and_wait('#1', (By.ID, self.FILTER_ID))
self.click_load_more_button()
self.assertTrue(self.is_load_more_disabled())

@unittest.expectedFailure
def test_load_more_button_disabled_state_with_few_products(self):
"""
Test the load more link state.

`Load more` link should be disabled by default if there are less
than PRODUCTS_TO_LOAD products on page.
"""
# contains low products count
small_category = (
stb_models.Category.objects
.annotate(prod_count=Count('products'))
.exclude(prod_count=0)
.filter(prod_count__lt=request_data.Category.PRODUCTS_ON_PAGE_PC)
.first()
# @todo #675:30m At the fixtures create category with a few options.
# "A few options" means with less then options per_page limit count.
# See the code below.

per_page = request_data.Category.PRODUCTS_ON_PAGE_PC
self.load_category_page(
self.get_category_with_options_limit(
low=0, high=per_page
)
)
self.load_category_page(category=small_category)
self.assertTrue(self.is_load_more_disabled())

# @todo #419:30m Create fast search tests.
Expand Down Expand Up @@ -621,11 +646,12 @@ def test_autocomplete_see_all_item(self):

def test_search_have_results(self):
"""Search results page should contain links on relevant pages."""
self.search('#10')

query = 'Product #10'
self.search(query)
product = stb_models.Product.objects.filter(name__startswith=query).first()
self.assertTrue(
self.browser.find_element_by_link_text(
'Product #10 of Category #1 of #42'
product.name
)
)

Expand Down
18 changes: 9 additions & 9 deletions stroyprombeton/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

CANONICAL_HTML_TAG = '<link rel="canonical" href="{base_url}{path}">'
CATEGORY_ROOT_NAME = 'Category root #0'
PRODUCT_WITH_IMAGE = 110
OPTION_WITH_IMAGE = 220


def json_to_dict(response: HttpResponse) -> dict():
Expand Down Expand Up @@ -262,16 +264,18 @@ def test_active_options(self):

def test_product_images(self):
# product with image
product = models.Product.objects.get(id=110)
# @todo #665:30m In tests get product-option with image from DB.
# Reuse the getting all over the tests.
product = models.Product.objects.get(id=PRODUCT_WITH_IMAGE)
response = self.client.get(product.category.url) # Ignore CPDBear
image = response.context['product_images'][110]
image = response.context['product_images'][OPTION_WITH_IMAGE]
self.assertTrue(image)
self.assertTrue(image.image.url)

def test_product_image_button(self):
"""Category page should contain button to open existing product image."""
# product with image
product = models.Product.objects.get(id=110)
product = models.Product.objects.get(id=PRODUCT_WITH_IMAGE) # Ignore CPDBear
response = self.client.get(product.category.url)
self.assertContains(response, 'table-photo-ico')

Expand Down Expand Up @@ -1072,7 +1076,7 @@ def test_emtpy_404(self):
def test_product_images(self):
"""Series page should contain only options with active related products."""
# product with image
product = models.Product.objects.get(id=110)
product = models.Product.objects.get(id=PRODUCT_WITH_IMAGE)
series = models.Series.objects.first()
product.options.update(series=series)
response = self.client.get(series.url)
Expand All @@ -1083,11 +1087,7 @@ def test_product_images(self):
def test_product_image_button(self):
"""Series page should contain button to open existing product image."""
# product with image
product = models.Product.objects.get(id=110)
# @todo #30m Make product/option ids out of sync.
# Now current product.id is equal to it's option id.
# See the assertion below.
assert product.id == product.options.first().id
product = models.Product.objects.get(id=PRODUCT_WITH_IMAGE)
response = self.client.get(product.category.url)
self.assertContains(response, 'table-photo-ico')

Expand Down
2 changes: 1 addition & 1 deletion templates/catalog/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% for product in products %}
<tr class="table-tr" data-id="{{ product.id }}" data-name="{{ product.name }}">
<td class="table-td table-photo text-center">
{% with image=product_images|get_item:product.product.id %}
{% with image=product_images|get_item:product.id %}
{% include 'catalog/option_image.html' with image=image page=product.page only %}
{% endwith %}
</td>
Expand Down