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

Commit

Permalink
#703 Section tests (#711)
Browse files Browse the repository at this point in the history
* #703  Fixtures for sections

* #703  Test for sections

* #703  Review#1 naming fixes

* #703  Regen fixtures and media

* #703  Apply linter riles
  • Loading branch information
duker33 committed Jun 28, 2019
1 parent 211650e commit e9b2908
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion stroyprombeton/fixtures/dump.json

Large diffs are not rendered by default.

42 changes: 38 additions & 4 deletions stroyprombeton/management/commands/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
EMPTY_ROOT_PATTERN = 'Category root empty #{}'

# use empty series to feel how UI looks like.
REAL_SERIES_COUNT = 2
FILLED_SERIES_COUNT = 2
EMPTY_SERIES_COUNT = 38
SERIES_PATTERN = 'Series #{}'
EMPTY_SERIES_PATTERN = 'Series empty #{}'

FILLED_SECTIONS_COUNT = 2
EMPTY_SECTIONS_COUNT = 18
SECTIONS_PATTERN = 'Sections #{}'
EMPTY_SECTIONS_PATTERN = 'Sections empty #{}'

CATEGORY_PATTERN = 'Category #{} of #{}'
FEEDBACKS_COUNT = 9
REVIEW_IMAGE = os.path.join(
Expand Down Expand Up @@ -134,7 +139,9 @@ def handle(self, *args, **options):
create_pages()
self.create_products(parents=list(third_level), tags=tags)
series = self.create_series()
self.bind_products(series)
section = self.create_sections()
self.bind_options(series)
self.bind_products(section)
self.create_templates()
self.save_dump()

Expand Down Expand Up @@ -189,19 +196,46 @@ def create(id: int, pattern: str, parent):
parent = CustomPage.objects.get(slug='series')
real_series = [
create(id=i, pattern=SERIES_PATTERN, parent=parent)
for i in range(REAL_SERIES_COUNT)
for i in range(FILLED_SERIES_COUNT)
]
for i in range(EMPTY_SERIES_COUNT):
create(id=i, pattern=EMPTY_SERIES_PATTERN, parent=parent)
# return only real series, because only they should have children
return real_series

@staticmethod
def bind_products(series: typing.List[stb_models.Series]):
def create_sections():
def create(id: int, pattern: str, parent):
return stb_models.Section.objects.create( # Ignore CPDBear
name=pattern.format(id),
page=ModelPage.objects.create(
name=pattern.format(id),
parent=parent,
),
)

parent = CustomPage.objects.get(slug='sections')
real_sections = [
create(id=i, pattern=SERIES_PATTERN, parent=parent)
for i in range(FILLED_SECTIONS_COUNT)
]
for i in range(EMPTY_SECTIONS_COUNT):
create(id=i, pattern=EMPTY_SERIES_PATTERN, parent=parent)
# return only real series, because only they should have children
return real_sections

@staticmethod
def bind_options(series: typing.List[stb_models.Series]):
options = stb_models.Option.objects.all()
options.filter(id__range=(1, 50)).update(series=series[0])
options.filter(id__range=(51, 55)).update(series=series[1])

@staticmethod
def bind_products(sections: typing.List[stb_models.Section]):
products = stb_models.Product.objects.all()
products.filter(id__range=(1, 25)).update(section=sections[0])
products.filter(id__range=(26, 30)).update(section=sections[1])

def create_tag_groups(self):
for i, name in enumerate(self.group_names, start=1):
yield stb_models.TagGroup.objects.create(
Expand Down
4 changes: 4 additions & 0 deletions stroyprombeton/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def get_robots_content():
'slug': 'series',
'name': 'Серии',
},
'sections': {
'slug': 'sections',
'name': 'Типы изделий',
},
}

# region-coordinates mapping
Expand Down
55 changes: 17 additions & 38 deletions stroyprombeton/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,28 +1068,6 @@ def test_product_image_button(self):
self.assertContains(response, 'table-photo-ico')


# will be removed during this Section class implementing.
@tag('fast', 'catalog')
class SectionFirst(BaseCatalogTestCase):
fixtures = ['dump.json']

def test_page_success(self):
section = models.Section.objects.create(
name='First section',
page=ModelPage.objects.create(
name='First section',
slug='first',
),
)
models.Product.objects.active().update(section=section)
response = self.client.get(section.url)
self.assertEqual(200, response.status_code)


# @todo #669:120m Create tests for Section.
# Use Section tests draft below. Rm current SectionFirst draft.
# Create fixtures with sections.
@unittest.skip
@tag('fast', 'catalog')
class Section(BaseCatalogTestCase):
fixtures = ['dump.json']
Expand All @@ -1114,25 +1092,26 @@ def get_section_soup(self, *args, **kwargs) -> BeautifulSoup:
'html.parser'
)

def test_options_are_from_section(self):
def test_page_success(self):
section = models.Section.objects.first()
response = self.client.get(section.url)
self.assertEqual(200, response.status_code)

def test_products_are_from_section(self):
response = self.client.get(self.get_section_url(self.section))
self.assertTrue(
all(option.section == self.section for option in response.context['products'])
all(p.section == self.section for p in response.context['products']),
response.context['products']
)

def test_active_options(self):
"""Section page should contain only options with active related products."""
options_qs = (
self.section.options
.bind_fields()
# @todo #699:60m Implement `OptionsQS.default_order` method. se2
.order_by(*settings.OPTIONS_ORDERING)
)
products = self.section.products.bind_fields()
# make inactive the first option in a section page list
inactive = options_qs.first()
inactive.product.page.is_active = False
inactive.product.page.save()
active = options_qs.active().first()
inactive = products.first()
inactive.page.is_active = False
inactive.page.save()
active = products.active().first()

response = self.client.get(self.get_section_url(self.section))
self.assertIn(active, response.context['products'])
Expand All @@ -1142,7 +1121,7 @@ def test_emtpy_404(self):
"""Section with not active options should return response 404."""
section = (
models.Section.objects
.annotate(count=Count('options'))
.annotate(count=Count('products'))
.filter(count=0)
).first()
response = self.get_section_page(section)
Expand All @@ -1152,9 +1131,9 @@ def test_product_images(self):
"""Section page should contain only options with active related products."""
# product with image
product = models.Product.objects.get(id=PRODUCT_WITH_IMAGE)
section = models.Section.objects.first()
product.options.update(section=section)
response = self.client.get(section.url)
product.section = models.Section.objects.first()
product.save()
response = self.client.get(product.section.url)
image = response.context['product_images'][110]
self.assertTrue(image)
self.assertTrue(image.image.url)
Expand Down

2 comments on commit e9b2908

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e9b2908 Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 669-92706ca5 disappeared from stroyprombeton/tests/tests_views.py, that's why I closed #703. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e9b2908 Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 699-0168785b disappeared from stroyprombeton/tests/tests_views.py, that's why I closed #704. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.