Skip to content

Commit

Permalink
refactor test_tags.py with lib independent fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
flashdagger committed Oct 21, 2023
1 parent c956160 commit a12ba2d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 51 deletions.
4 changes: 3 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def zammad_api(request, client_url, api_token):
session.headers["Authorization"] = f"Token token={api_token}"

def _request(method: str, endpoint: str, *args, **kwargs):
response = session.request(method, f"{client_url}/{endpoint}", *args, **kwargs)
response = session.request(
method, f"{client_url}/{endpoint}#fixture", *args, **kwargs
)
response.raise_for_status()
return response

Expand Down
105 changes: 73 additions & 32 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,94 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from contextlib import suppress

from contextlib import contextmanager

import pytest

from zammadoo.utils import TypedTag

@pytest.fixture(scope="function")
def assert_existing_tags(request, zammad_api):
missing_only = request.config.getoption("--record-missing")
recording = missing_only or request.config.getoption("--record")

def _cleanup(_tags):
for tag_name in _tags:
tag_infos = zammad_api("GET", f"tag_search?term={tag_name}").json()
for info in tag_infos:
if info["value"] == tag_name:
zammad_api("DELETE", f"tag_list/{info['id']}")

@contextmanager
def _create_temporary(*names, delete=()):
temporary_tags = set(delete)
if not recording:
yield
return

_cleanup(temporary_tags)

for tag_name in names:
if tag_name not in temporary_tags:
zammad_api("POST", "tag_list", {"name": tag_name})
temporary_tags.add(tag_name)

yield

def test_representation_of_tags(request, client):
client_url = request.config.getoption("--client-url")
_cleanup(temporary_tags)

return _create_temporary


def test_representation_of_tags(client_url, client):
assert repr(client.tags) == f"<Tags '{client_url}/tag_list'>"


def test_tag_iteration(rclient):
for tag in rclient.tags:
assert tag.keys() == {"name", "id", "count"}
break
def test_tags_getitem(rclient, assert_existing_tags):
with assert_existing_tags("footag", "baztag"):
assert "footag" in rclient.tags
assert "baztag" in rclient.tags


def test_tag_iteration(rclient, assert_existing_tags):
tag_names = set()
with assert_existing_tags("footag", "baztag"):
for tag in rclient.tags:
assert tag.keys() == {"name", "id", "count"}
tag_names.add(tag["name"])

assert {"footag", "baztag"}.issubset(tag_names)

def test_mutable_tag_operations(rclient):

def test_create_tag(rclient, assert_existing_tags):
tag_name = "__pytest__"
new_tag_name = "__pytest_new__"
tags = rclient.tags

# make sure tag does not exist
with suppress(KeyError):
tags.delete(tag_name)
with suppress(KeyError):
tags.delete(new_tag_name)

assert tag_name not in tags
with assert_existing_tags(delete={tag_name}):
rclient.tags.create(tag_name)
assert tag_name in tags
tag_info = tags[tag_name]
assert tag_info["name"] == tag_name
assert tag_info["count"] == 0
assert tag_name in tags.search(tag_name)

with pytest.raises(KeyError, match=tag_name):
tags.delete(tag_name)

tags.create(tag_name)
assert tag_name in tags
tag_info = tags[tag_name]
assert tag_info["name"] == tag_name
assert tag_info["count"] == 0
def test_rename_tag(rclient, assert_existing_tags):
tag_name = "__pytest__"
new_tag_name = "__pytest_new__"
tags = rclient.tags

assert tag_name in tags.search(tag_name)
with assert_existing_tags(tag_name, delete={new_tag_name}):
tag_info = tags[tag_name]
tags.cache.clear()
tags.rename(tag_name, new_tag_name)
new_tag_info = tags[new_tag_name]
assert new_tag_info["id"] == tag_info["id"]

tags.cache.clear()
tags.rename(tag_name, new_tag_name)
assert new_tag_name in tags
assert tag_name not in tags

tags.cache.clear()
assert tag_info["id"] == tags[new_tag_name]["id"]
def test_delete_tag(rclient, assert_existing_tags):
tag_name = "__pytest__"
tags = rclient.tags

tags.delete(new_tag_name)
with assert_existing_tags(tag_name):
tags.delete(tag_name)
assert tag_name not in tags
12 changes: 12 additions & 0 deletions tests/test_tags/test_create_tag.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
[]
{"method": "POST", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 116}
[{"id":126,"name":"__pytest__","count":0},{"id":2,"name":"barracuda","count":0},{"id":7,"name":"special","count":0}]
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest__", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 33}
[{"id":126,"value":"__pytest__"}]
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 33}
[{"id":126,"value":"__pytest__"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/126#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
8 changes: 8 additions & 0 deletions tests/test_tags/test_delete_tag.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 116}
[{"id":128,"name":"__pytest__","count":0},{"id":2,"name":"barracuda","count":0},{"id":7,"name":"special","count":0}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/128", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
[]
16 changes: 0 additions & 16 deletions tests/test_tags/test_mutable_tag_operations.log

This file was deleted.

16 changes: 16 additions & 0 deletions tests/test_tags/test_rename_tag.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest_new__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
[]
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 116}
[{"id":127,"name":"__pytest__","count":0},{"id":2,"name":"barracuda","count":0},{"id":7,"name":"special","count":0}]
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 116}
[{"id":127,"name":"__pytest__","count":0},{"id":2,"name":"barracuda","count":0},{"id":7,"name":"special","count":0}]
{"method": "PUT", "url": "https://localhost/api/v1/tag_list/127", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest_new__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 37}
[{"id":127,"value":"__pytest_new__"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/127#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=__pytest__#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
[]
16 changes: 14 additions & 2 deletions tests/test_tags/test_tag_iteration.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 109}
[{"id":32,"name":"atag","count":0},{"id":2,"name":"barracuda","count":0},{"id":7,"name":"special","count":0}]
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=footag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=baztag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 149}
[{"id":2,"name":"barracuda","count":0},{"id":125,"name":"baztag","count":0},{"id":124,"name":"footag","count":0},{"id":7,"name":"special","count":0}]
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=footag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 29}
[{"id":124,"value":"footag"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/124#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=baztag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 29}
[{"id":125,"value":"baztag"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/125#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
14 changes: 14 additions & 0 deletions tests/test_tags/test_tags_getitem.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=footag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "POST", "url": "https://localhost/api/v1/tag_list?name=baztag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_list", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 149}
[{"id":2,"name":"barracuda","count":0},{"id":123,"name":"baztag","count":0},{"id":122,"name":"footag","count":0},{"id":7,"name":"special","count":0}]
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=footag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 29}
[{"id":122,"value":"footag"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/122#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}
{"method": "GET", "url": "https://localhost/api/v1/tag_search?term=baztag#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 29}
[{"id":123,"value":"baztag"}]
{"method": "DELETE", "url": "https://localhost/api/v1/tag_list/123#fixture", "status_code": 200, "encoding": "utf-8", "reason": "OK", "content_size": 2}
{}

0 comments on commit a12ba2d

Please sign in to comment.