Skip to content

Commit

Permalink
refactor and complete test_priorities.py
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Oct 23, 2023
1 parent 0aa0bf7 commit 246abcb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
80 changes: 38 additions & 42 deletions tests/test_priorities.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from contextlib import contextmanager

import pytest


@pytest.fixture(scope="function")
def assert_existing_priorities(zammad_api):
def _cleanup(names):
priority_map = {}
priority_map.update(
(info["name"], info["id"])
for info in zammad_api("GET", "ticket_priorities").json()
)
for name in names:
priority_id = priority_map.get(name)
if not priority_id:
continue
zammad_api("DELETE", f"ticket_priorities/{priority_id}")

@contextmanager
def _create_temporary(*names, delete=()):
temporary_priorities = set(delete)
_cleanup(temporary_priorities)

for priority_name in names:
if priority_name not in temporary_priorities:
zammad_api(
"POST",
"ticket_priorities",
{"name": priority_name, "note": "pytest"},
)
temporary_priorities.add(priority_name)

yield

_cleanup(temporary_priorities)

return _create_temporary


def test_create_and_update_priority(rclient, assert_existing_priorities):
with assert_existing_priorities(delete={"pytest_prio"}):
new_priority = rclient.ticket_priorities.create("pytest_prio")
def test_create_priority(rclient, temporary_resources):
with temporary_resources("ticket_priorities") as priorities:
new_priority = rclient.ticket_priorities.create("pytest_priority")
priorities.append(new_priority.view())
assert new_priority.active is True
updated_priority = new_priority.update(active=False)
assert new_priority.note is None
assert new_priority.default_create is False
assert new_priority.ui_icon is None
assert new_priority.ui_color is None


def test_update_priority(rclient, temporary_resources):
with temporary_resources("ticket_priorities", {"name": "pytest_priority"}) as infos:
info = infos[0]
priority = rclient.ticket_priorities(info["id"], info=info)
assert priority.active is True
updated_priority = priority.update(active=False)
assert updated_priority.active is False


def test_update_priority_with_reload(rclient, temporary_resources):
with temporary_resources("ticket_priorities", {"name": "pytest_priority"}) as infos:
info = infos[0]
priority = rclient.ticket_priorities(info["id"], info=info)
assert priority.active is True
priority.update(active=False)
priority.reload()
assert priority.active is False


def test_delete_priority(rclient, temporary_resources):
from zammadoo import APIException

with temporary_resources("ticket_priorities", {"name": "pytest_priority"}) as infos:
info = infos[0]
priority = rclient.ticket_priorities(info["id"], info=info)
priority.delete()
with pytest.raises(APIException, match="Couldn't find Ticket::Priority"):
priority.reload()
infos.clear()
10 changes: 0 additions & 10 deletions tests/test_priorities/test_create_and_update_priority.log

This file was deleted.

4 changes: 4 additions & 0 deletions tests/test_priorities/test_create_priority.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"method": "POST", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "230"}, "url": "https://localhost/api/v1/ticket_priorities", "status_code": 201, "encoding": "utf-8", "reason": "Created", "content_size": 230}
{"id":14,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"active":true,"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T19:08:57.516Z","updated_at":"2023-10-23T19:08:57.516Z"}
{"method": "DELETE", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "2"}, "url": "https://localhost/api/v1/ticket_priorities/14#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
6 changes: 6 additions & 0 deletions tests/test_priorities/test_delete_priority.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"method": "POST", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "230"}, "url": "https://localhost/api/v1/ticket_priorities#fixture", "status_code": 201, "encoding": "utf-8", "reason": "Created", "content_size": 230}
{"id":17,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"active":true,"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T19:08:58.814Z","updated_at":"2023-10-23T19:08:58.814Z"}
{"method": "DELETE", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "2"}, "url": "https://localhost/api/v1/ticket_priorities/17", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "headers": {"Content-Type": "application/json; charset=utf-8"}, "url": "https://localhost/api/v1/ticket_priorities/17", "status_code": 404, "encoding": "utf-8", "reason": "Not Found", "content_size": 115}
{"error":"Couldn't find Ticket::Priority with 'id'=17","error_human":"Couldn't find Ticket::Priority with 'id'=17"}
6 changes: 6 additions & 0 deletions tests/test_priorities/test_update_priority.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"method": "POST", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "230"}, "url": "https://localhost/api/v1/ticket_priorities#fixture", "status_code": 201, "encoding": "utf-8", "reason": "Created", "content_size": 230}
{"id":15,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"active":true,"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T19:08:57.821Z","updated_at":"2023-10-23T19:08:57.821Z"}
{"method": "PUT", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "231"}, "url": "https://localhost/api/v1/ticket_priorities/15", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 231}
{"active":false,"updated_by_id":3,"id":15,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"created_by_id":3,"created_at":"2023-10-23T19:08:57.821Z","updated_at":"2023-10-23T19:08:57.952Z"}
{"method": "DELETE", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "2"}, "url": "https://localhost/api/v1/ticket_priorities/15#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
8 changes: 8 additions & 0 deletions tests/test_priorities/test_update_priority_with_reload.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"method": "POST", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "230"}, "url": "https://localhost/api/v1/ticket_priorities#fixture", "status_code": 201, "encoding": "utf-8", "reason": "Created", "content_size": 230}
{"id":16,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"active":true,"updated_by_id":3,"created_by_id":3,"created_at":"2023-10-23T19:08:58.220Z","updated_at":"2023-10-23T19:08:58.220Z"}
{"method": "PUT", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "231"}, "url": "https://localhost/api/v1/ticket_priorities/16", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 231}
{"active":false,"updated_by_id":3,"id":16,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"created_by_id":3,"created_at":"2023-10-23T19:08:58.220Z","updated_at":"2023-10-23T19:08:58.439Z"}
{"method": "GET", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "231"}, "url": "https://localhost/api/v1/ticket_priorities/16", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 231}
{"active":false,"updated_by_id":3,"id":16,"name":"pytest_priority","default_create":false,"ui_icon":null,"ui_color":null,"note":null,"created_by_id":3,"created_at":"2023-10-23T19:08:58.220Z","updated_at":"2023-10-23T19:08:58.439Z"}
{"method": "DELETE", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "2"}, "url": "https://localhost/api/v1/ticket_priorities/16#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}

0 comments on commit 246abcb

Please sign in to comment.