summaryrefslogblamecommitdiffstatshomepage
path: root/tests/api/test_tag.py
blob: c863a00eac882b347b6429316c528c270370545a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
























































































































































































































































































































































































































































                                                                                       
from datetime import datetime as dt
from datetime import timezone

import pytest
from conftest import DB, Response
from hircine.db.models import Namespace, Tag


@pytest.fixture
def query_tag(execute_id):
    query = """
    query tag($id: Int!) {
        tag(id: $id) {
            __typename
            ... on FullTag {
                id
                name
                description
                namespaces {
                    __typename
                    id
                }
            }
            ... on Error {
                message
            }
            ... on IDNotFoundError {
                id
            }
        }
    }
    """

    return execute_id(query)


@pytest.fixture
def query_tags(execute):
    query = """
    query tags {
        tags {
            __typename
            count
            edges {
                id
                name
                description
            }
        }
    }
    """

    return execute(query)


@pytest.fixture
def add_tag(execute_add):
    mutation = """
    mutation addTag($input: AddTagInput!) {
        addTag(input: $input) {
            __typename
            ... on AddSuccess {
                id
            }
            ... on Error {
                message
            }
            ... on InvalidParameterError {
                parameter
            }
            ... on IDNotFoundError {
                id
            }
        }
    }
    """

    return execute_add(mutation)


@pytest.fixture
def update_tags(execute_update):
    mutation = """
    mutation updateTags($ids: [Int!]!, $input: UpdateTagInput!) {
        updateTags(ids: $ids, input: $input) {
            __typename
            ... on Success {
                message
            }
            ... on Error {
                message
            }
            ... on IDNotFoundError {
                id
            }
            ... on InvalidParameterError {
                parameter
            }
        }
    }
    """  # noqa: E501

    return execute_update(mutation)


@pytest.fixture
def delete_tags(execute_delete):
    mutation = """
    mutation deleteTags($ids: [Int!]!) {
        deleteTags(ids: $ids) {
            __typename
            ... on Success {
                message
            }
            ... on Error {
                message
            }
            ... on IDNotFoundError {
                id
            }
        }
    }
    """

    return execute_delete(mutation)


@pytest.mark.anyio
async def test_query_tag(query_tag, gen_tag):
    tag = await DB.add(next(gen_tag))

    response = Response(await query_tag(tag.id))
    response.assert_is("FullTag")

    assert response.id == tag.id
    assert response.name == tag.name
    assert response.description == tag.description
    assert set([n["id"] for n in response.namespaces]) == set(
        [n.id for n in tag.namespaces]
    )


@pytest.mark.anyio
async def test_query_tag_fails_not_found(query_tag):
    response = Response(await query_tag(1))
    response.assert_is("IDNotFoundError")
    assert response.id == 1
    assert response.message == "Tag ID not found: '1'"


@pytest.mark.anyio
async def test_query_tags(query_tags, gen_tag):
    tags = await DB.add_all(*gen_tag)
    response = Response(await query_tags())
    response.assert_is("TagFilterResult")

    assert response.count == len(tags)
    assert isinstance((response.edges), list)
    assert len(response.edges) == len(tags)

    edges = iter(response.edges)
    for tag in sorted(tags, key=lambda a: a.name):
        edge = next(edges)
        assert edge["id"] == tag.id
        assert edge["name"] == tag.name
        assert edge["description"] == tag.description


@pytest.mark.anyio
async def test_add_tag(add_tag):
    response = Response(
        await add_tag({"name": "added", "description": "it's been added!"})
    )
    response.assert_is("AddSuccess")

    tag = await DB.get(Tag, response.id)
    assert tag is not None
    assert tag.name == "added"
    assert tag.description == "it's been added!"


@pytest.mark.anyio
async def test_add_tag_with_namespace(add_tag):
    namespace = await DB.add(Namespace(id=1, name="new"))

    response = Response(await add_tag({"name": "added", "namespaces": {"ids": [1]}}))
    response.assert_is("AddSuccess")

    tag = await DB.get(Tag, response.id, full=True)
    assert tag is not None
    assert tag.name == "added"
    assert tag.namespaces[0].id == namespace.id
    assert tag.namespaces[0].name == namespace.name


@pytest.mark.anyio
async def test_add_tag_fails_empty_parameter(add_tag):
    response = Response(await add_tag({"name": ""}))

    response.assert_is("InvalidParameterError")
    assert response.parameter == "name"
    assert response.message == "Invalid parameter 'name': cannot be empty"


@pytest.mark.anyio
async def test_add_tag_fails_namespace_not_found(add_tag):
    response = Response(await add_tag({"name": "added", "namespaces": {"ids": [1]}}))
    response.assert_is("IDNotFoundError")

    assert response.id == 1
    assert response.message == "Namespace ID not found: '1'"


@pytest.mark.anyio
async def test_add_tag_fails_exists(add_tag, gen_tag):
    tag = await DB.add(next(gen_tag))

    response = Response(await add_tag({"name": tag.name}))
    response.assert_is("NameExistsError")
    assert response.message == "Another Tag with this name exists"


@pytest.mark.anyio
async def test_delete_tag(delete_tags, gen_tag):
    tag = await DB.add(next(gen_tag))
    id = tag.id

    response = Response(await delete_tags(id))
    response.assert_is("DeleteSuccess")

    tag = await DB.get(Tag, id)
    assert tag is None


@pytest.mark.anyio
async def test_delete_tag_not_found(delete_tags):
    response = Response(await delete_tags(1))

    response.assert_is("IDNotFoundError")
    assert response.id == 1
    assert response.message == "Tag ID not found: '1'"


@pytest.mark.anyio
async def test_update_tag(update_tags, gen_tag, gen_namespace):
    tag = await DB.add(next(gen_tag))
    namespace = await DB.add(next(gen_namespace))

    input = {
        "name": "updated",
        "description": "how different, how unique",
        "namespaces": {"ids": [1]},
    }
    response = Response(await update_tags(tag.id, input))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, tag.id, full=True)
    assert tag is not None
    assert tag.name == "updated"
    assert tag.description == "how different, how unique"
    assert tag.namespaces[0].id == namespace.id
    assert tag.namespaces[0].name == namespace.name


@pytest.mark.parametrize(
    "empty",
    [
        None,
        "",
    ],
    ids=[
        "with None",
        "with empty string",
    ],
)
@pytest.mark.anyio
async def test_update_tag_clears_description(update_tags, gen_tag, empty):
    tag = await DB.add(next(gen_tag))

    input = {
        "description": empty,
    }
    response = Response(await update_tags(tag.id, input))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, tag.id)
    assert tag is not None
    assert tag.description is None


@pytest.mark.anyio
async def test_update_tag_fails_exists(update_tags, gen_tag):
    first = await DB.add(next(gen_tag))
    second = await DB.add(next(gen_tag))

    response = Response(await update_tags(second.id, {"name": first.name}))
    response.assert_is("NameExistsError")
    assert response.message == "Another Tag with this name exists"


@pytest.mark.anyio
async def test_update_tag_fails_not_found(update_tags):
    response = Response(await update_tags(1, {"name": "updated"}))

    response.assert_is("IDNotFoundError")
    assert response.id == 1
    assert response.message == "Tag ID not found: '1'"


@pytest.mark.anyio
async def test_update_tags_cannot_bulk_edit_name(update_tags, gen_tag):
    first = await DB.add(next(gen_tag))
    second = await DB.add(next(gen_tag))

    response = Response(await update_tags([first.id, second.id], {"name": "unique"}))
    response.assert_is("InvalidParameterError")


@pytest.mark.parametrize(
    "empty",
    [
        None,
        "",
    ],
    ids=[
        "none",
        "empty string",
    ],
)
@pytest.mark.anyio
async def test_update_tag_fails_empty_parameter(update_tags, gen_tag, empty):
    tag = await DB.add(next(gen_tag))
    response = Response(await update_tags(tag.id, {"name": empty}))

    response.assert_is("InvalidParameterError")
    assert response.parameter == "name"
    assert response.message == "Invalid parameter 'name': cannot be empty"


@pytest.mark.anyio
async def test_update_tag_fails_namespace_not_found(update_tags, gen_tag):
    tag = await DB.add(next(gen_tag))
    response = Response(await update_tags(tag.id, {"namespaces": {"ids": [1]}}))
    response.assert_is("IDNotFoundError")

    assert response.id == 1
    assert response.message == "Namespace ID not found: '1'"


@pytest.mark.parametrize(
    "options",
    [
        None,
        {},
        {"mode": "REPLACE"},
    ],
    ids=[
        "by default (none)",
        "by default (empty record)",
        "when defined explicitly",
    ],
)
@pytest.mark.anyio
async def test_update_tag_replaces_assocs(update_tags, gen_tag, options):
    original_tag = await DB.add(next(gen_tag))
    new_namespace = await DB.add(Namespace(name="new"))

    input = {
        "namespaces": {"ids": [new_namespace.id]},
    }
    response = Response(await update_tags(original_tag.id, input))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, original_tag.id, full=True)

    assert set([o.id for o in tag.namespaces]) == set([o.id for o in [new_namespace]])


@pytest.mark.anyio
async def test_update_tag_adds_assocs(update_tags, gen_tag):
    original_tag = await DB.add(next(gen_tag))
    new_namespace = await DB.add(Namespace(name="new"))
    added_namespaces = original_tag.namespaces + [new_namespace]

    input = {
        "namespaces": {"ids": [new_namespace.id], "options": {"mode": "ADD"}},
    }
    response = Response(await update_tags(original_tag.id, input))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, original_tag.id, full=True)

    assert set([o.id for o in tag.namespaces]) == set([o.id for o in added_namespaces])


@pytest.mark.anyio
async def test_update_tag_removes_assocs(update_tags):
    removed_namespace = Namespace(id=1, name="new")
    remaining_namespace = Namespace(id=2, name="newtwo")
    original_tag = await DB.add(
        Tag(id=1, name="tag", namespaces=[removed_namespace, remaining_namespace])
    )

    input = {
        "namespaces": {"ids": [removed_namespace.id], "options": {"mode": "REMOVE"}},
    }
    response = Response(await update_tags(original_tag.id, input))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, original_tag.id, full=True)

    assert set([o.id for o in tag.namespaces]) == set([remaining_namespace.id])


@pytest.mark.anyio
async def test_update_tag_changes_updated_at(update_tags):
    original_tag = Tag(name="tag")
    original_tag.updated_at = dt(2023, 1, 1, tzinfo=timezone.utc)
    original_tag = await DB.add(original_tag)

    response = Response(await update_tags(original_tag.id, {"name": "updated"}))
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, original_tag.id)
    assert tag.updated_at > original_tag.updated_at


@pytest.mark.anyio
async def test_update_tag_assoc_changes_updated_at(update_tags):
    original_tag = Tag(name="tag")
    original_tag.updated_at = dt(2023, 1, 1, tzinfo=timezone.utc)
    original_tag = await DB.add(original_tag)
    await DB.add(Namespace(id=1, name="namespace"))

    response = Response(
        await update_tags(original_tag.id, {"namespaces": {"ids": [1]}})
    )
    response.assert_is("UpdateSuccess")

    tag = await DB.get(Tag, original_tag.id)
    assert tag.updated_at > original_tag.updated_at