Skip to content

Commit cca8ca5

Browse files
committed
Fix assignTagToBoxes mutation for box with already multiple tags
1 parent f553242 commit cca8ca5

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

back/boxtribute_server/business_logic/warehouse/box/mutations.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ def resolve_assign_tag_to_boxes(*_, update_input):
173173
on=(
174174
(TagsRelation.object_id == Box.id)
175175
& (TagsRelation.object_type == TaggableObjectType.Box)
176+
& (TagsRelation.tag == tag.id)
176177
),
177178
)
178179
.where(
179180
Box.label_identifier << label_identifiers,
180181
authorized_bases_filter(Location, permission="stock:write"),
181-
# Any boxes that already have the new tag assigned are silently ignored
182-
(TagsRelation.tag != tag.id) | (TagsRelation.tag.is_null()),
182+
# Any boxes that already have the new tag assigned are ignored
183+
TagsRelation.tag.is_null(),
183184
(~Box.deleted_on | Box.deleted_on.is_null()),
184185
)
185186
.order_by(Box.id)

back/test/data/tag.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def data():
5252
"type": TagType.Beneficiary,
5353
"deleted_on": "2022-01-01",
5454
},
55+
{
56+
"id": 6,
57+
"base": base_data()[0]["id"],
58+
"color": "#123456",
59+
"description": "for all",
60+
"name": "tag-for-all",
61+
"type": TagType.All,
62+
"deleted_on": None,
63+
},
5564
]
5665

5766

@@ -62,7 +71,7 @@ def tags():
6271

6372
@pytest.fixture
6473
def base1_active_tags():
65-
return data()[:3]
74+
return data()[:3] + [data()[5]]
6675

6776

6877
def create():

back/test/endpoint_tests/test_box.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,29 @@ def test_box_mutations(
379379
"invalidBoxLabelIdentifiers": [],
380380
}
381381

382+
another_generic_tag_id = str(tags[5]["id"])
383+
mutation = f"""mutation {{ assignTagToBoxes( updateInput: {{
384+
labelIdentifiers: [{label_identifiers}],
385+
tagId: {another_generic_tag_id} }} ) {{
386+
...on BoxResult {{
387+
updatedBoxes {{ tags {{ id }} }}
388+
invalidBoxLabelIdentifiers
389+
}} }} }}"""
390+
response = assert_successful_request(client, mutation)
391+
assert response == {
392+
"updatedBoxes": [
393+
{
394+
"tags": [
395+
{"id": tag_id},
396+
{"id": generic_tag_id},
397+
{"id": another_generic_tag_id},
398+
]
399+
}
400+
for _ in range(2)
401+
],
402+
"invalidBoxLabelIdentifiers": [],
403+
}
404+
382405
# Test case 8.2.24a
383406
label_identifier = f'"{created_box["labelIdentifier"]}"'
384407
mutation = f"""mutation {{ unassignTagFromBoxes( updateInput: {{
@@ -389,15 +412,17 @@ def test_box_mutations(
389412
}} }} }}"""
390413
response = assert_successful_request(client, mutation)
391414
assert response == {
392-
"updatedBoxes": [{"tags": [{"id": tag_id}]}],
415+
"updatedBoxes": [{"tags": [{"id": tag_id}, {"id": another_generic_tag_id}]}],
393416
"invalidBoxLabelIdentifiers": [],
394417
}
395418

396419
# Verify that tag is not removed from the other box
397420
query = f"""query {{ box(labelIdentifier: "{another_created_box_label_identifier}")
398421
{{ tags {{ id }} }} }}"""
399422
response = assert_successful_request(client, query)
400-
assert response == {"tags": [{"id": tag_id}, {"id": generic_tag_id}]}
423+
assert response == {
424+
"tags": [{"id": tag_id}, {"id": generic_tag_id}, {"id": another_generic_tag_id}]
425+
}
401426

402427
# Test case 8.2.24c
403428
mutation = f"""mutation {{ unassignTagFromBoxes( updateInput: {{
@@ -408,7 +433,7 @@ def test_box_mutations(
408433
}} }} }}"""
409434
response = assert_successful_request(client, mutation)
410435
assert response == {
411-
"updatedBoxes": [{"tags": [{"id": tag_id}]}],
436+
"updatedBoxes": [{"tags": [{"id": tag_id}, {"id": another_generic_tag_id}]}],
412437
"invalidBoxLabelIdentifiers": [created_box["labelIdentifier"]],
413438
}
414439

back/test/endpoint_tests/test_tag.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ def test_tags_query(
7979
},
8080
],
8181
},
82+
{
83+
"id": str(tags[5]["id"]),
84+
"name": tags[5]["name"],
85+
"type": tags[5]["type"].name,
86+
"taggedResources": [],
87+
},
8288
]
8389

8490

85-
def test_tags_mutations(client, tags, another_beneficiary, lost_box):
91+
def test_tags_mutations(client, tags, base1_active_tags, another_beneficiary, lost_box):
8692
# Test case 4.2.9
8793
deleted_tag_id = tags[0]["id"]
8894
mutation = f"""mutation {{ deleteTag(id: {deleted_tag_id}) {{
@@ -99,7 +105,7 @@ def test_tags_mutations(client, tags, another_beneficiary, lost_box):
99105
query = """query { tags { id } }"""
100106
all_tags = assert_successful_request(client, query)
101107
# Expect the deleted tag to not appear in the list of active tags of base
102-
assert all_tags == [{"id": str(t["id"])} for t in tags[1:3]]
108+
assert all_tags == [{"id": str(t["id"])} for t in base1_active_tags[1:]]
103109

104110
# Test case 4.2.1
105111
name = "Box Group 1"
@@ -379,9 +385,9 @@ def test_assign_tag_with_invalid_resource_type(
379385
@pytest.mark.parametrize(
380386
"filter_input,tag_ids",
381387
[
382-
["", ["1", "2", "3"]],
383-
["(resourceType: Box)", ["2", "3"]],
384-
["(resourceType: Beneficiary)", ["1", "3"]],
388+
["", ["1", "2", "3", "6"]],
389+
["(resourceType: Box)", ["2", "3", "6"]],
390+
["(resourceType: Beneficiary)", ["1", "3", "6"]],
385391
],
386392
)
387393
def test_base_tags_query(read_only_client, filter_input, tag_ids):

0 commit comments

Comments
 (0)