lingo/tests/pricing/manager/test_pricing.py

345 lines
14 KiB
Python

import datetime
import pytest
from lingo.agendas.models import Agenda
from lingo.pricing.models import AgendaPricing, Criteria, CriteriaCategory, Pricing, PricingCriteriaCategory
from tests.utils import login
pytestmark = pytest.mark.django_db
def test_add_pricing(app, admin_user):
app = login(app)
resp = app.get('/manage/')
resp = resp.click('Pricing')
resp = resp.click(href='/manage/pricing/models/')
resp = resp.click('New pricing model')
resp.form['label'] = 'Pricing model for lunch'
resp = resp.form.submit()
pricing = Pricing.objects.latest('pk')
assert resp.location.endswith('/manage/pricing/model/%s/' % pricing.pk)
assert pricing.label == 'Pricing model for lunch'
assert pricing.slug == 'pricing-model-for-lunch'
def test_detail_pricing(app, admin_user):
pricing = Pricing.objects.create(label='Model')
app = login(app)
resp = app.get('/manage/pricing/models/')
resp = resp.click(href='/manage/pricing/model/%s/' % pricing.pk)
assert '/manage/pricing/model/%s/edit/' % pricing.pk in resp
assert '/manage/pricing/model/%s/delete/' % pricing.pk in resp
agenda = Agenda.objects.create(label='Foo Bar')
AgendaPricing.objects.create(
agenda=agenda,
pricing=pricing,
date_start=datetime.date(year=2021, month=9, day=1),
date_end=datetime.date(year=2022, month=9, day=1),
)
AgendaPricing.objects.create(
agenda=agenda,
pricing=pricing,
date_start=datetime.date(year=2022, month=9, day=1),
date_end=datetime.date(year=2023, month=9, day=1),
)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
assert resp.text.count('"/manage/pricing/agenda/%s/"' % agenda.pk) == 1
def test_edit_pricing(app, admin_user):
pricing = Pricing.objects.create(label='Model 1')
pricing2 = Pricing.objects.create(label='Model 2')
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/edit/' % pricing.pk)
resp.form['label'] = 'Model Foo'
resp.form['slug'] = pricing2.slug
resp = resp.form.submit()
assert resp.context['form'].errors['slug'] == ['Pricing with this Identifier already exists.']
resp.form['slug'] = 'foo-bar'
resp = resp.form.submit()
assert resp.location.endswith('/manage/pricing/model/%s/' % pricing.pk)
pricing.refresh_from_db()
assert pricing.label == 'Model Foo'
assert pricing.slug == 'foo-bar'
def test_delete_pricing(app, admin_user):
pricing = Pricing.objects.create(label='Model')
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/delete/' % pricing.pk)
resp = resp.form.submit()
assert resp.location.endswith('/manage/pricing/models/')
assert Pricing.objects.exists() is False
def test_duplicate_pricing(app, admin_user):
pricing = Pricing.objects.create(label='Model')
assert Pricing.objects.count() == 1
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/duplicate/' % pricing.pk)
resp = resp.form.submit()
assert Pricing.objects.count() == 2
new_pricing = Pricing.objects.latest('pk')
assert resp.location == '/manage/pricing/model/%s/' % new_pricing.pk
assert new_pricing.pk != pricing.pk
resp = resp.follow()
assert 'copy-of-model' in resp.text
resp = resp.click(href='/manage/pricing/model/%s/duplicate/' % new_pricing.pk)
resp.form['label'] = 'hop'
resp = resp.form.submit().follow()
assert 'hop' in resp.text
def test_pricing_edit_extra_variables(app, admin_user):
pricing = Pricing.objects.create(label='Model')
assert pricing.extra_variables == {}
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
assert '<label>Extra variables:</label>' not in resp.text
resp = resp.click(href='/manage/pricing/model/%s/variable/' % pricing.pk)
resp.form['form-0-key'] = 'foo'
resp.form['form-0-value'] = 'bar'
resp = resp.form.submit().follow()
pricing.refresh_from_db()
assert pricing.extra_variables == {'foo': 'bar'}
assert '<label>Extra variables:</label>' in resp.text
assert '<i>foo</i>' in resp
resp = resp.click(href='/manage/pricing/model/%s/variable/' % pricing.pk)
assert resp.form['form-TOTAL_FORMS'].value == '2'
assert resp.form['form-0-key'].value == 'foo'
assert resp.form['form-0-value'].value == 'bar'
assert resp.form['form-1-key'].value == ''
assert resp.form['form-1-value'].value == ''
resp.form['form-0-value'] = 'bar-bis'
resp.form['form-1-key'] = 'blah'
resp.form['form-1-value'] = 'baz'
resp = resp.form.submit().follow()
pricing.refresh_from_db()
assert pricing.extra_variables == {
'foo': 'bar-bis',
'blah': 'baz',
}
assert '<i>blah</i>, <i>foo</i>' in resp
resp = resp.click(href='/manage/pricing/model/%s/variable/' % pricing.pk)
assert resp.form['form-TOTAL_FORMS'].value == '3'
assert resp.form['form-0-key'].value == 'blah'
assert resp.form['form-0-value'].value == 'baz'
assert resp.form['form-1-key'].value == 'foo'
assert resp.form['form-1-value'].value == 'bar-bis'
assert resp.form['form-2-key'].value == ''
assert resp.form['form-2-value'].value == ''
resp.form['form-1-key'] = 'foo'
resp.form['form-1-value'] = 'bar'
resp.form['form-0-key'] = ''
resp = resp.form.submit().follow()
pricing.refresh_from_db()
assert pricing.extra_variables == {
'foo': 'bar',
}
assert '<i>foo</i>' in resp
def test_pricing_add_category(app, admin_user):
pricing = Pricing.objects.create(label='Model')
category1 = CriteriaCategory.objects.create(label='Cat 1')
category2 = CriteriaCategory.objects.create(label='Cat 2')
category3 = CriteriaCategory.objects.create(label='Cat 3')
category4 = CriteriaCategory.objects.create(label='Cat 4')
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/category/add/' % pricing.pk)
assert list(resp.context['form'].fields['category'].queryset) == [
category1,
category2,
category3,
category4,
]
resp.form['category'] = category1.pk
resp = resp.form.submit()
assert resp.location.endswith('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.follow()
assert '/manage/pricing/model/%s/category/add/' % pricing.pk in resp
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category1.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1
]
resp = app.get('/manage/pricing/model/%s/category/add/' % pricing.pk)
assert list(resp.context['form'].fields['category'].queryset) == [category2, category3, category4]
resp.form['category'] = category4.pk
resp = resp.form.submit().follow()
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category1.pk, category4.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1,
2,
]
resp = app.get('/manage/pricing/model/%s/category/add/' % pricing.pk)
assert list(resp.context['form'].fields['category'].queryset) == [category2, category3]
resp.form['category'] = category2.pk
resp = resp.form.submit().follow()
assert '/manage/pricing/model/%s/category/add/' % pricing.pk not in resp
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category1.pk, category4.pk, category2.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1,
2,
3,
]
app.get('/manage/pricing/model/%s/category/add/' % pricing.pk, status=404)
def test_pricing_edit_category(app, admin_user):
category1 = CriteriaCategory.objects.create(label='Cat 1')
criteria1 = Criteria.objects.create(label='Crit 1', category=category1)
criteria2 = Criteria.objects.create(label='Crit 2', category=category1)
criteria3 = Criteria.objects.create(label='Crit 3', category=category1)
criteria4 = Criteria.objects.create(label='Crit 4', category=category1)
category2 = CriteriaCategory.objects.create(label='cat 2')
criteria5 = Criteria.objects.create(label='Crit 5', category=category2)
pricing = Pricing.objects.create(label='Model')
pricing.categories.add(category1, through_defaults={'order': 1})
pricing.categories.add(category2, through_defaults={'order': 2})
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/category/%s/edit/' % (pricing.pk, category1.pk))
assert list(resp.context['form'].fields['criterias'].queryset) == [
criteria1,
criteria2,
criteria3,
criteria4,
]
assert list(resp.context['form'].initial['criterias']) == []
resp.form['criterias'] = [criteria1.pk, criteria3.pk]
resp = resp.form.submit()
assert resp.location.endswith('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.follow()
assert list(pricing.criterias.order_by('pk')) == [criteria1, criteria3]
resp = app.get('/manage/pricing/model/%s/category/%s/edit/' % (pricing.pk, category1.pk))
assert list(resp.context['form'].initial['criterias']) == [criteria1, criteria3]
resp.form['criterias'] = [criteria1.pk, criteria4.pk]
resp = resp.form.submit().follow()
assert list(pricing.criterias.order_by('pk')) == [criteria1, criteria4]
resp = app.get('/manage/pricing/model/%s/category/%s/edit/' % (pricing.pk, category2.pk))
assert list(resp.context['form'].fields['criterias'].queryset) == [criteria5]
assert list(resp.context['form'].initial['criterias']) == []
resp.form['criterias'] = [criteria5.pk]
resp = resp.form.submit().follow()
assert list(pricing.criterias.order_by('pk')) == [criteria1, criteria4, criteria5]
def test_pricing_delete_category(app, admin_user):
category1 = CriteriaCategory.objects.create(label='Cat 1')
criteria1 = Criteria.objects.create(label='Crit 1', category=category1)
category2 = CriteriaCategory.objects.create(label='Cat 2')
criteria2 = Criteria.objects.create(label='Crit 2', category=category2)
pricing = Pricing.objects.create(label='Model')
pricing.categories.add(category1, through_defaults={'order': 1})
pricing.categories.add(category2, through_defaults={'order': 2})
pricing.criterias.add(criteria1, criteria2)
app = login(app)
resp = app.get('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.click(href='/manage/pricing/model/%s/category/%s/delete/' % (pricing.pk, category1.pk))
resp = resp.form.submit()
assert resp.location.endswith('/manage/pricing/model/%s/' % pricing.pk)
resp = resp.follow()
assert list(pricing.categories.all()) == [category2]
assert list(pricing.criterias.all()) == [criteria2]
# not linked
app.get('/manage/pricing/model/%s/category/%s/delete/' % (pricing.pk, category1.pk), status=404)
# unknown
app.get('/manage/pricing/model/%s/category/%s/delete/' % (pricing.pk, 0), status=404)
def test_pricing_reorder_categories(app, admin_user):
category1 = CriteriaCategory.objects.create(label='Cat 1')
category2 = CriteriaCategory.objects.create(label='Cat 2')
category3 = CriteriaCategory.objects.create(label='Cat 3')
category4 = CriteriaCategory.objects.create(label='Cat 4')
pricing = Pricing.objects.create(label='Model')
pricing.categories.add(category1, through_defaults={'order': 1})
pricing.categories.add(category2, through_defaults={'order': 2})
pricing.categories.add(category3, through_defaults={'order': 3})
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category1.pk, category2.pk, category3.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1,
2,
3,
]
app = login(app)
# missing get params
app.get('/manage/pricing/model/%s/order/' % (pricing.pk), status=400)
# bad new-order param
bad_params = [
# missing category3 in order
','.join(str(x) for x in [category1.pk, category2.pk]),
# category1 mentionned twice
','.join(str(x) for x in [category1.pk, category2.pk, category3.pk, category1.pk]),
# category4 not in pricing categories
','.join(str(x) for x in [category1.pk, category2.pk, category3.pk, category4.pk]),
# not an id
'foo,1,2,3',
' 1 ,2,3',
]
for bad_param in bad_params:
app.get(
'/manage/pricing/model/%s/order/' % (pricing.pk),
params={'new-order': bad_param},
status=400,
)
# not changed
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category1.pk, category2.pk, category3.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1,
2,
3,
]
# change order
app.get(
'/manage/pricing/model/%s/order/' % (pricing.pk),
params={'new-order': ','.join(str(x) for x in [category3.pk, category1.pk, category2.pk])},
)
assert list(
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
) == [category3.pk, category1.pk, category2.pk]
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
1,
2,
3,
]