-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor test_tags.py with lib independent fixture
- Loading branch information
1 parent
c956160
commit a12ba2d
Showing
8 changed files
with
140 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
[] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
{} |