404 lines
14 KiB
Python
404 lines
14 KiB
Python
import copy
|
|
import datetime
|
|
|
|
import pytest
|
|
|
|
from lingo.agendas.models import Agenda, CheckType, CheckTypeGroup
|
|
from lingo.pricing.models import (
|
|
AgendaPricing,
|
|
BillingDate,
|
|
Criteria,
|
|
CriteriaCategory,
|
|
Pricing,
|
|
PricingCriteriaCategory,
|
|
)
|
|
from lingo.pricing.utils import export_site, import_site
|
|
from lingo.utils.misc import LingoImportError
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
def test_import_export(app):
|
|
Agenda.objects.create(label='Foo Bar')
|
|
pricing = Pricing.objects.create(label='Foo')
|
|
AgendaPricing.objects.create(
|
|
pricing=pricing,
|
|
date_start=datetime.date(year=2021, month=9, day=1),
|
|
date_end=datetime.date(year=2021, month=10, day=1),
|
|
)
|
|
CriteriaCategory.objects.create(label='Foo bar')
|
|
|
|
data = export_site()
|
|
assert len(data['agendas']) == 1
|
|
assert len(data['pricings']) == 1
|
|
assert len(data['pricings'][0]['agendas']) == 0
|
|
assert len(data['pricing_models']) == 1
|
|
assert len(data['pricing_categories']) == 1
|
|
import_site(data={})
|
|
assert AgendaPricing.objects.count() == 1
|
|
assert Pricing.objects.count() == 1
|
|
assert CriteriaCategory.objects.count() == 1
|
|
|
|
|
|
def test_import_export_agenda_pricing(app):
|
|
pricing = Pricing.objects.create(label='Foo')
|
|
agenda = Agenda.objects.create(label='Foo Bar')
|
|
agenda_pricing = AgendaPricing.objects.create(
|
|
label='Bar',
|
|
pricing=pricing,
|
|
date_start=datetime.date(year=2021, month=9, day=1),
|
|
date_end=datetime.date(year=2021, month=10, day=1),
|
|
pricing_data={
|
|
'foo': 'bar',
|
|
},
|
|
)
|
|
agenda_pricing.agendas.set([agenda])
|
|
data = export_site()
|
|
|
|
Agenda.objects.all().delete()
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo-bar" agenda'
|
|
|
|
agenda2 = Agenda.objects.create(label='Baz')
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo-bar" agenda'
|
|
|
|
del data['pricing_models']
|
|
Pricing.objects.all().delete()
|
|
agenda = Agenda.objects.create(label='Foo Bar')
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo" pricing model'
|
|
|
|
pricing = Pricing.objects.create(label='Foo')
|
|
import_site(data)
|
|
agenda_pricing = AgendaPricing.objects.latest('pk')
|
|
assert list(agenda_pricing.agendas.all()) == [agenda]
|
|
assert agenda_pricing.pricing == pricing
|
|
assert agenda_pricing.date_start == datetime.date(year=2021, month=9, day=1)
|
|
assert agenda_pricing.date_end == datetime.date(year=2021, month=10, day=1)
|
|
assert agenda_pricing.pricing_data == {'foo': 'bar'}
|
|
|
|
# again
|
|
import_site(data)
|
|
agenda_pricing = AgendaPricing.objects.get(pk=agenda_pricing.pk)
|
|
assert list(agenda_pricing.agendas.all()) == [agenda]
|
|
assert agenda_pricing.pricing == pricing
|
|
|
|
data['pricings'].append(
|
|
{
|
|
'slug': 'baz',
|
|
'label': 'Baz',
|
|
'pricing': 'foo',
|
|
'agendas': ['foo-bar', 'baz'],
|
|
'date_start': '2022-09-01',
|
|
'date_end': '2022-10-01',
|
|
'pricing_data': {'foo': 'bar'},
|
|
}
|
|
)
|
|
import_site(data)
|
|
agenda_pricing = AgendaPricing.objects.latest('pk')
|
|
assert list(agenda_pricing.agendas.all().order_by('slug')) == [agenda2, agenda]
|
|
assert agenda_pricing.pricing == pricing
|
|
assert agenda_pricing.date_start == datetime.date(year=2022, month=9, day=1)
|
|
assert agenda_pricing.date_end == datetime.date(year=2022, month=10, day=1)
|
|
assert agenda_pricing.pricing_data == {'foo': 'bar'}
|
|
|
|
|
|
def test_import_export_agenda_pricing_with_billing_dates(app):
|
|
pricing = Pricing.objects.create(label='Foo')
|
|
agenda_pricing = AgendaPricing.objects.create(
|
|
label='Bar',
|
|
pricing=pricing,
|
|
date_start=datetime.date(year=2021, month=9, day=1),
|
|
date_end=datetime.date(year=2021, month=10, day=1),
|
|
)
|
|
BillingDate.objects.create(
|
|
agenda_pricing=agenda_pricing,
|
|
date_start=datetime.date(year=2021, month=9, day=1),
|
|
label='Period 1',
|
|
)
|
|
BillingDate.objects.create(
|
|
agenda_pricing=agenda_pricing,
|
|
date_start=datetime.date(year=2021, month=9, day=15),
|
|
label='Period 2',
|
|
)
|
|
|
|
data = export_site()
|
|
|
|
import_site(data)
|
|
agenda_pricing = AgendaPricing.objects.latest('pk')
|
|
assert agenda_pricing.billingdates.count() == 2
|
|
billing_date1 = agenda_pricing.billingdates.all()[0]
|
|
assert billing_date1.date_start == datetime.date(year=2021, month=9, day=1)
|
|
assert billing_date1.label == 'Period 1'
|
|
billing_date2 = agenda_pricing.billingdates.all()[1]
|
|
assert billing_date2.date_start == datetime.date(year=2021, month=9, day=15)
|
|
assert billing_date2.label == 'Period 2'
|
|
|
|
|
|
def test_import_export_agenda_with_check_types(app):
|
|
group = CheckTypeGroup.objects.create(label='foo')
|
|
agenda = Agenda.objects.create(label='Foo Bar', check_type_group=group)
|
|
data = export_site()
|
|
|
|
group.delete()
|
|
del data['check_type_groups']
|
|
agenda.check_type_group = None
|
|
agenda.save()
|
|
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo" check type group'
|
|
|
|
CheckTypeGroup.objects.create(label='foobar')
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo" check type group'
|
|
|
|
group = CheckTypeGroup.objects.create(label='foo')
|
|
import_site(data)
|
|
agenda.refresh_from_db()
|
|
assert agenda.check_type_group == group
|
|
|
|
|
|
def test_import_export_pricing_criteria_category(app):
|
|
payload = export_site()
|
|
assert len(payload['pricing_categories']) == 0
|
|
|
|
category = CriteriaCategory.objects.create(label='Foo bar')
|
|
Criteria.objects.create(label='Foo reason', category=category)
|
|
Criteria.objects.create(label='Baz', category=category)
|
|
|
|
payload = export_site()
|
|
assert len(payload['pricing_categories']) == 1
|
|
|
|
category.delete()
|
|
assert not CriteriaCategory.objects.exists()
|
|
assert not Criteria.objects.exists()
|
|
|
|
import_site(copy.deepcopy(payload))
|
|
assert CriteriaCategory.objects.count() == 1
|
|
category = CriteriaCategory.objects.first()
|
|
assert category.label == 'Foo bar'
|
|
assert category.slug == 'foo-bar'
|
|
assert category.criterias.count() == 2
|
|
assert Criteria.objects.get(category=category, label='Foo reason', slug='foo-reason')
|
|
assert Criteria.objects.get(category=category, label='Baz', slug='baz')
|
|
|
|
# update
|
|
update_payload = copy.deepcopy(payload)
|
|
update_payload['pricing_categories'][0]['label'] = 'Foo bar Updated'
|
|
import_site(update_payload)
|
|
category.refresh_from_db()
|
|
assert category.label == 'Foo bar Updated'
|
|
|
|
# insert another category
|
|
category.slug = 'foo-bar-updated'
|
|
category.save()
|
|
import_site(copy.deepcopy(payload))
|
|
assert CriteriaCategory.objects.count() == 2
|
|
category = CriteriaCategory.objects.latest('pk')
|
|
assert category.label == 'Foo bar'
|
|
assert category.slug == 'foo-bar'
|
|
assert category.criterias.count() == 2
|
|
assert Criteria.objects.get(category=category, label='Foo reason', slug='foo-reason')
|
|
assert Criteria.objects.get(category=category, label='Baz', slug='baz')
|
|
|
|
|
|
def test_import_export_pricing(app):
|
|
payload = export_site()
|
|
assert len(payload['pricing_models']) == 0
|
|
|
|
pricing = Pricing.objects.create(label='Foo bar', extra_variables={'foo': 'bar'})
|
|
|
|
payload = export_site()
|
|
assert len(payload['pricing_models']) == 1
|
|
|
|
pricing.delete()
|
|
assert not Pricing.objects.exists()
|
|
|
|
import_site(copy.deepcopy(payload))
|
|
assert Pricing.objects.count() == 1
|
|
pricing = Pricing.objects.first()
|
|
assert pricing.label == 'Foo bar'
|
|
assert pricing.slug == 'foo-bar'
|
|
assert pricing.extra_variables == {'foo': 'bar'}
|
|
|
|
# update
|
|
update_payload = copy.deepcopy(payload)
|
|
update_payload['pricing_models'][0]['label'] = 'Foo bar Updated'
|
|
import_site(update_payload)
|
|
pricing.refresh_from_db()
|
|
assert pricing.label == 'Foo bar Updated'
|
|
|
|
# insert another pricing
|
|
pricing.slug = 'foo-bar-updated'
|
|
pricing.save()
|
|
import_site(copy.deepcopy(payload))
|
|
assert Pricing.objects.count() == 2
|
|
pricing = Pricing.objects.latest('pk')
|
|
assert pricing.label == 'Foo bar'
|
|
assert pricing.slug == 'foo-bar'
|
|
assert pricing.extra_variables == {'foo': 'bar'}
|
|
|
|
|
|
def test_import_export_pricing_with_categories(app):
|
|
pricing = Pricing.objects.create(label='Foo bar')
|
|
category = CriteriaCategory.objects.create(label='Foo bar')
|
|
pricing.categories.add(category, through_defaults={'order': 42})
|
|
|
|
data = export_site()
|
|
category.delete()
|
|
del data['pricing_categories']
|
|
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo-bar" pricing category'
|
|
|
|
CriteriaCategory.objects.create(label='Foobar')
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "foo-bar" pricing category'
|
|
|
|
category = CriteriaCategory.objects.create(label='Foo bar')
|
|
import_site(data)
|
|
pricing = Pricing.objects.get(slug=pricing.slug)
|
|
assert list(pricing.categories.all()) == [category]
|
|
assert PricingCriteriaCategory.objects.first().order == 42
|
|
|
|
category2 = CriteriaCategory.objects.create(label='Foo bar 2')
|
|
category3 = CriteriaCategory.objects.create(label='Foo bar 3')
|
|
pricing.categories.add(category2, through_defaults={'order': 1})
|
|
data = export_site()
|
|
del data['pricing_categories']
|
|
data['pricing_models'][0]['categories'] = [
|
|
{
|
|
'category': 'foo-bar-3',
|
|
'order': 1,
|
|
'criterias': [],
|
|
},
|
|
{
|
|
'category': 'foo-bar',
|
|
'order': 35,
|
|
'criterias': [],
|
|
},
|
|
]
|
|
import_site(data)
|
|
assert list(pricing.categories.all()) == [category, category3]
|
|
assert list(
|
|
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
|
|
) == [category3.pk, category.pk]
|
|
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
|
|
1,
|
|
35,
|
|
]
|
|
assert list(pricing.criterias.all()) == []
|
|
|
|
criteria1 = Criteria.objects.create(label='Crit 1', category=category)
|
|
Criteria.objects.create(label='Crit 2', category=category)
|
|
criteria3 = Criteria.objects.create(label='Crit 3', category=category)
|
|
|
|
# unknown criteria
|
|
data['pricing_models'][0]['categories'] = [
|
|
{
|
|
'category': 'foo-bar-3',
|
|
'order': 1,
|
|
'criterias': ['unknown'],
|
|
},
|
|
{
|
|
'category': 'foo-bar',
|
|
'order': 35,
|
|
'criterias': [],
|
|
},
|
|
]
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "unknown" pricing criteria for "foo-bar-3" category'
|
|
|
|
# wrong criteria (from another category)
|
|
data['pricing_models'][0]['categories'] = [
|
|
{
|
|
'category': 'foo-bar-3',
|
|
'order': 1,
|
|
'criterias': ['crit-1'],
|
|
},
|
|
{
|
|
'category': 'foo-bar',
|
|
'order': 35,
|
|
'criterias': [],
|
|
},
|
|
]
|
|
with pytest.raises(LingoImportError) as excinfo:
|
|
import_site(data)
|
|
assert str(excinfo.value) == 'Missing "crit-1" pricing criteria for "foo-bar-3" category'
|
|
|
|
data['pricing_models'][0]['categories'] = [
|
|
{
|
|
'category': 'foo-bar-3',
|
|
'order': 1,
|
|
'criterias': [],
|
|
},
|
|
{
|
|
'category': 'foo-bar',
|
|
'order': 35,
|
|
'criterias': ['crit-1', 'crit-3'],
|
|
},
|
|
]
|
|
import_site(data)
|
|
assert list(pricing.categories.all()) == [category, category3]
|
|
assert list(
|
|
PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('category', flat=True)
|
|
) == [category3.pk, category.pk]
|
|
assert list(PricingCriteriaCategory.objects.filter(pricing=pricing).values_list('order', flat=True)) == [
|
|
1,
|
|
35,
|
|
]
|
|
assert set(pricing.criterias.all()) == {criteria1, criteria3}
|
|
|
|
|
|
def test_import_export_check_type_group(app):
|
|
payload = export_site()
|
|
assert len(payload['check_type_groups']) == 0
|
|
|
|
group = CheckTypeGroup.objects.create(label='Foo bar')
|
|
CheckType.objects.create(label='Foo reason', group=group)
|
|
CheckType.objects.create(label='Baz', group=group)
|
|
|
|
payload = export_site()
|
|
assert len(payload['check_type_groups']) == 1
|
|
|
|
group.delete()
|
|
assert not CheckTypeGroup.objects.exists()
|
|
assert not CheckType.objects.exists()
|
|
|
|
import_site(copy.deepcopy(payload))
|
|
assert CheckTypeGroup.objects.count() == 1
|
|
group = CheckTypeGroup.objects.first()
|
|
assert group.label == 'Foo bar'
|
|
assert group.slug == 'foo-bar'
|
|
assert group.check_types.count() == 2
|
|
assert CheckType.objects.get(group=group, label='Foo reason', slug='foo-reason')
|
|
assert CheckType.objects.get(group=group, label='Baz', slug='baz')
|
|
|
|
# update
|
|
update_payload = copy.deepcopy(payload)
|
|
update_payload['check_type_groups'][0]['label'] = 'Foo bar Updated'
|
|
import_site(update_payload)
|
|
group.refresh_from_db()
|
|
assert group.label == 'Foo bar Updated'
|
|
|
|
# insert another group
|
|
group.slug = 'foo-bar-updated'
|
|
group.save()
|
|
import_site(copy.deepcopy(payload))
|
|
assert CheckTypeGroup.objects.count() == 2
|
|
group = CheckTypeGroup.objects.latest('pk')
|
|
assert group.label == 'Foo bar'
|
|
assert group.slug == 'foo-bar'
|
|
assert group.check_types.count() == 2
|
|
assert CheckType.objects.get(group=group, label='Foo reason', slug='foo-reason')
|
|
assert CheckType.objects.get(group=group, label='Baz', slug='baz')
|