Some Sonar code smell suggestions

This commit is contained in:
shamoon
2025-09-14 15:58:43 -07:00
parent bc3098419c
commit a2f4c5aea0
4 changed files with 9 additions and 18 deletions

View File

@@ -34,17 +34,17 @@ describe('flattenTags', () => {
expect(flat.map((t) => t.orderIndex)).toEqual([0, 1, 2, 3, 4, 5, 6]) expect(flat.map((t) => t.orderIndex)).toEqual([0, 1, 2, 3, 4, 5, 6])
// Children are rebuilt // Children are rebuilt
const aRoot = flat.find((t) => t.name === 'A-root')! const aRoot = flat.find((t) => t.name === 'A-root')
expect(new Set(aRoot.children?.map((c) => c.name))).toEqual( expect(new Set(aRoot.children?.map((c) => c.name))).toEqual(
new Set(['child 2', 'Child 10']) new Set(['child 2', 'Child 10'])
) )
const bRoot = flat.find((t) => t.name === 'B-root')! const bRoot = flat.find((t) => t.name === 'B-root')
expect(new Set(bRoot.children?.map((c) => c.name))).toEqual( expect(new Set(bRoot.children?.map((c) => c.name))).toEqual(
new Set(['Alpha', 'beta']) new Set(['Alpha', 'beta'])
) )
const child2 = flat.find((t) => t.name === 'child 2')! const child2 = flat.find((t) => t.name === 'child 2')
expect(new Set(child2.children?.map((c) => c.name))).toEqual( expect(new Set(child2.children?.map((c) => c.name))).toEqual(
new Set(['Sub 1']) new Set(['Sub 1'])
) )

View File

@@ -8,7 +8,7 @@ export function flattenTags(all: Tag[]): Tag[] {
for (const t of map.values()) { for (const t of map.values()) {
if (t.parent) { if (t.parent) {
const p = map.get(t.parent) const p = map.get(t.parent)
p && p.children.push(t) p?.children.push(t)
} }
} }
const roots = Array.from(map.values()).filter((t) => !t.parent) const roots = Array.from(map.values()).filter((t) => !t.parent)
@@ -29,6 +29,7 @@ export function flattenTags(all: Tag[]): Tag[] {
} }
} }
} }
roots.sort(sortByName).forEach((r) => walk(r, 0)) roots.sort(sortByName)
roots.forEach((r) => walk(r, 0))
return ordered return ordered
} }

View File

@@ -522,7 +522,7 @@ def update_document_parent_tags(tag: Tag, new_parent: Tag) -> None:
When a tag's parent changes, ensure all documents containing the tag also have When a tag's parent changes, ensure all documents containing the tag also have
the parent tag (and its ancestors) applied. the parent tag (and its ancestors) applied.
""" """
DocumentTagRelationship = Document.tags.through doc_tag_relationship = Document.tags.through
doc_ids: list[int] = list( doc_ids: list[int] = list(
Document.objects.filter(tags=tag).values_list("pk", flat=True), Document.objects.filter(tags=tag).values_list("pk", flat=True),
@@ -540,13 +540,13 @@ def update_document_parent_tags(tag: Tag, new_parent: Tag) -> None:
missing_qs = Document.objects.filter(id__in=doc_ids).exclude(tags=parent) missing_qs = Document.objects.filter(id__in=doc_ids).exclude(tags=parent)
missing_ids = list(missing_qs.values_list("pk", flat=True)) missing_ids = list(missing_qs.values_list("pk", flat=True))
to_create.extend( to_create.extend(
DocumentTagRelationship(document_id=doc_id, tag_id=parent.id) doc_tag_relationship(document_id=doc_id, tag_id=parent.id)
for doc_id in missing_ids for doc_id in missing_ids
) )
affected.update(missing_ids) affected.update(missing_ids)
if to_create: if to_create:
DocumentTagRelationship.objects.bulk_create( doc_tag_relationship.objects.bulk_create(
to_create, to_create,
ignore_conflicts=True, ignore_conflicts=True,
) )

View File

@@ -31,16 +31,6 @@ class TestTagHierarchy(APITestCase):
mime_type="application/pdf", mime_type="application/pdf",
) )
def test_api_add_child_adds_parent(self):
self.client.patch(
f"/api/documents/{self.document.pk}/",
{"tags": [self.child.pk]},
format="json",
)
self.document.refresh_from_db()
tags = set(self.document.tags.values_list("pk", flat=True))
assert tags == {self.parent.pk, self.child.pk}
def test_document_api_add_child_adds_parent(self): def test_document_api_add_child_adds_parent(self):
self.client.patch( self.client.patch(
f"/api/documents/{self.document.pk}/", f"/api/documents/{self.document.pk}/",