Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New delete case endpoint #14

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions tests/erp_service_testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@

existing_template = ns(
# An existing template with semantic id
erp_id = 183, # Extremely fragile
xml_id = 'giscedata_switching.notification_atr_M1_01',
name = 'ATR M101: Solicitud de modificación cambio de titular',
erp_id = 189, # Extremely fragile
xml_id = 'giscedata_switching.notification_atr_M1_06',
name = 'ATR M106: Solicitud de anulación al distribuidor',
model = 'giscedata.switching',
subject_ca_ES =
'Som Energia: Canvi de titular. Verificació de dades. '
'Contracte ${object.polissa_ref_id.name}',
subject_es_ES =
'Som Energia: Cambio de titular. Verificación de datos. '
'Contrato ${object.polissa_ref_id.name}',
'${object.company_id.name}: Sol·licitada anulació de la modificació de '
'contracte d\'accés la seva distribuidora ${object.partner_id.name}',
subject_es_ES =
'${object.company_id.name}: Solicitada anulación de la modificación de '
'contrato de acceso a su distribuidora ${object.partner_id.name}',
)
def edited_values(**kwds):
result = ns(
Expand Down Expand Up @@ -311,7 +311,7 @@ async def test__save_template__missingSubjectTranslations_recreated(self, erp_se
'ca_ES': edited.def_subject_ca_ES,
}

async def test__save_template__bodyTranslation_cloneSuppoerted(self, erp_service, erp_translations):
async def test__save_template__bodyTranslation_cloneSupported(self, erp_service, erp_translations):
edited = edited_values()

await erp_service.save_template(
Expand All @@ -322,6 +322,7 @@ async def test__save_template__bodyTranslation_cloneSuppoerted(self, erp_service
assert erp_translations.list('def_body_text') == {
'es_ES': edited.def_body_text,
'ca_ES': edited.def_body_text,
'en_US': edited.def_body_text,
}

async def test__save_template__bodyTranslation_cloneMissingSupported(self, erp_service, erp_translations):
Expand All @@ -337,23 +338,28 @@ async def test__save_template__bodyTranslation_cloneMissingSupported(self, erp_s
assert erp_translations.list('def_body_text') == {
'es_ES': edited.def_body_text,
'ca_ES': edited.def_body_text,
'en_US': edited.def_body_text,
}

async def test__save_template__bodyTranslation_cloneExistingUnsupported(self, erp_service, erp_translations):
erp_translations.edit('def_body_text', 'en_US', dict(value='Former value'), create=True)
# Given a template with the body translated to an unsupported language
erp_translations.edit('def_body_text', 'de_DE', dict(value='Former value'), create=True)

edited = edited_values()

# When we save the templated
await erp_service.save_template(
id = existing_template.erp_id,
**edited,
)

# Then the body is copied also into the unsupported language
assert erp_translations.list('def_body_text') == {
'es_ES': edited.def_body_text,
'ca_ES': edited.def_body_text,
'en_US': edited.def_body_text,
'de_DE': edited.def_body_text,
}
# Because our template bodies have mako code handling the language
# and we want that code to run whichever the language.

@pytest.mark.skip("Not yet deployed in testing")
async def test__render_template__by_erp_id(self, erp_service):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_erp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def list(self, field):
],['lang', 'value'])
return {
x['lang']: x['value']
for x in translations or []
for x in translations or [] if x['value']
}

def remove(self, field, lang):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_erp_service_double.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def erp_service():
def_subject = "Untranslated subject",
def_subject_es_ES = existing_template.subject_es_ES,
def_subject_ca_ES = existing_template.subject_ca_ES,
def_subject_en_US = "",
def_body_text_es_ES = "Previous Body",
def_body_text_ca_ES = "Previous Body",
def_body_text_en_US = "",
name = existing_template.name,
model_int_name = existing_template.model,
)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,20 @@ async def test_create_template_case_repeated_name(self, test_app):
post = await get_template_cases(template_id)
assert len(pre) + 1 == len(post)

async def test_delete_template_case_not_exists(self, test_app):
template_id = 1
pre = await get_template_cases(template_id)
# await delete_template_case(DB_MANAGER, template_id, 'not_exist_case', 3)
with pytest.raises(UIQMakoBaseException):
result = await delete_template_case(DB_MANAGER, 3)
post = await get_template_cases(template_id)
assert len(pre) == len(post)

async def test_delete_template_case_ok(self, test_app):
template_id = 1
await create_template_case(DB_MANAGER, template_id, 'case_exists', 3)
pre = await get_template_cases(template_id)
case_id = pre[0].id
result = await delete_template_case(DB_MANAGER, case_id)
post = await get_template_cases(template_id)
assert len(pre) - 1 == len(post)
18 changes: 14 additions & 4 deletions uiqmako_api/api/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@ async def create_case(
case_name: str = Form(...),
case_id: str = Form(..., regex=".+\..+|[1-9][0-9]?")):
from .api import app
created = await create_template_case(app.db_manager, template_id, case_name, case_id)
created = await create_template_case(
app.db_manager,
template_id,
case_name,
erp_case_id=case_id
)
return {'result': created}


@router.delete("/{template_id}/cases/{case_id}", dependencies=[Depends(get_current_active_user)])
async def delete_case(case_id: int):
from .api import app
deleted = await delete_template_case(app.db_manager, case_id)
return {'result': deleted}


@router.get("/importable/{source}", dependencies=[Depends(get_current_active_user)])
async def importable_template_list(source: str):
from . import app
templates = await app.ERP.service().template_list()
return {'templates': templates}



12 changes: 10 additions & 2 deletions uiqmako_api/models/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ async def get_case_orm(case_id=None, case_erp_id=None, name=None, template_id=No
return False


async def get_or_create_template_case_orm(template_id, case_name, case_id):
async def get_or_create_template_case_orm(template_id, case_name, erp_case_id):
case, created = await db.create_or_get(
CaseModel,
name=case_name, case_erp_id=int(case_id), template=template_id
name=case_name, case_erp_id=int(erp_case_id), template=template_id
)
return case, created


async def delete_template_case_orm(case_id):
case = await db.get(
CaseModel,
id=case_id
)
return case.delete_instance()
13 changes: 11 additions & 2 deletions uiqmako_api/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
get_template_cases_orm,
get_case_orm,
get_or_create_template_case_orm,
delete_template_case_orm,
)
from uiqmako_api.schemas.templates import Template, TemplateInfoBase
from uiqmako_api.utils.git import create_or_update_template
Expand Down Expand Up @@ -73,9 +74,17 @@ async def get_template_cases(template_id):
return cases


async def create_template_case(db, template_id, case_name, case_id):
async def create_template_case(db, template_id, case_name, erp_case_id):
case_same_name = await get_case_orm(name=case_name, template_id=template_id)
if case_same_name:
raise UIQMakoBaseException("Existing name case for template")
case, created = await get_or_create_template_case_orm(template_id, case_name, case_id)
case, created = await get_or_create_template_case_orm(template_id, case_name, erp_case_id)
return created


async def delete_template_case(db, case_id):
case_same_name = await get_case_orm(case_id=case_id)
if not case_same_name:
raise UIQMakoBaseException("Not existing name case for template")
deleted = await delete_template_case_orm(case_id)
return deleted