Skip to content

Commit 52bf04f

Browse files
Add tests for public methods create and update
1 parent c38b1fd commit 52bf04f

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

backend/problem/serializers_test.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,144 @@ def test_create_update_and_remove_kb_annotations(
378378
assert new_annotation.entity1 == "new_e1"
379379
assert new_annotation.entity2 == "new_e2"
380380
assert new_annotation.relationship == "superset"
381+
382+
383+
@pytest.mark.django_db
384+
def test_create_problem_with_kb_annotations(annotator):
385+
"""Test creating a problem with KB annotations through create()."""
386+
data = {
387+
"premises": ["A cat is running."],
388+
"hypothesis": "A cat is moving.",
389+
"kbItems": [
390+
{
391+
"entity1": "cat",
392+
"entity2": "feline",
393+
"relationship": "equal",
394+
"notes": "Test note",
395+
},
396+
{
397+
"entity1": "running",
398+
"entity2": "moving",
399+
"relationship": "subset",
400+
},
401+
],
402+
}
403+
404+
serializer = input_serializer_with_user(annotator, data=data)
405+
assert serializer.is_valid(raise_exception=True)
406+
problem = serializer.save()
407+
408+
# Verify problem was created
409+
assert problem.pk is not None
410+
assert problem.dataset == Problem.Dataset.USER
411+
assert problem.hypothesis.text == "A cat is moving."
412+
assert problem.premises.count() == 1
413+
assert problem.premises.first().text == "A cat is running."
414+
415+
# Verify KB annotations were created
416+
kb_annotations = KnowledgeBaseAnnotation.objects.filter(problem=problem)
417+
assert kb_annotations.count() == 2
418+
419+
kb1 = kb_annotations.get(entity1="cat")
420+
assert kb1.entity2 == "feline"
421+
assert kb1.relationship == "equal"
422+
assert kb1.notes == "Test note"
423+
assert kb1.created_by == annotator
424+
425+
kb2 = kb_annotations.get(entity1="running")
426+
assert kb2.entity2 == "moving"
427+
assert kb2.relationship == "subset"
428+
assert kb2.created_by == annotator
429+
430+
431+
@pytest.mark.django_db
432+
def test_create_problem_with_multiple_premises_and_kb(annotator):
433+
"""Test creating a problem with multiple premises."""
434+
data = {
435+
"premises": ["Birds can fly.", "Penguins are birds."],
436+
"hypothesis": "Penguins can fly.",
437+
}
438+
439+
serializer = input_serializer_with_user(annotator, data=data)
440+
assert serializer.is_valid(raise_exception=True)
441+
problem = serializer.save()
442+
443+
assert problem.premises.count() == 2
444+
premise_texts = [p.text for p in problem.premises.all()]
445+
assert "Birds can fly." in premise_texts
446+
assert "Penguins are birds." in premise_texts
447+
assert problem.hypothesis.text == "Penguins can fly."
448+
449+
450+
@pytest.mark.django_db
451+
def test_update_user_problem_with_new_kb_annotations(user_problem, annotator):
452+
"""Test updating a user problem adds new KB annotations through update()."""
453+
data = {
454+
"id": user_problem.pk,
455+
"premises": ["Updated premise."],
456+
"hypothesis": "Updated hypothesis.",
457+
"kbItems": [
458+
{
459+
"entity1": "new_entity1",
460+
"entity2": "new_entity2",
461+
"relationship": "subset",
462+
}
463+
],
464+
}
465+
466+
assert (
467+
KnowledgeBaseAnnotation.objects.filter(problem=user_problem).count() == 0
468+
), "Precondition: user_problem should have no KB annotations."
469+
470+
serializer = input_serializer_with_user(annotator, data=data, instance=user_problem)
471+
assert serializer.is_valid(raise_exception=True)
472+
updated_problem = serializer.save()
473+
474+
# Verify problem was updated
475+
assert updated_problem.pk == user_problem.pk
476+
assert updated_problem.hypothesis.text == "Updated hypothesis."
477+
assert updated_problem.premises.first().text == "Updated premise."
478+
479+
# Verify KB annotation was added
480+
kb_annotations = KnowledgeBaseAnnotation.objects.filter(problem=updated_problem)
481+
assert kb_annotations.count() == 1
482+
kb_annotation = kb_annotations.first()
483+
assert kb_annotation is not None
484+
assert kb_annotation.entity1 == "new_entity1"
485+
486+
487+
@pytest.mark.django_db
488+
def test_update_non_user_problem_adds_kb_only(non_user_problem, annotator):
489+
"""Test updating a non-user problem only adds KB annotations, not other fields."""
490+
original_hypothesis = non_user_problem.hypothesis.text
491+
original_premise_count = non_user_problem.premises.count()
492+
493+
data = {
494+
"id": non_user_problem.pk,
495+
"premises": ["This should be ignored."],
496+
"hypothesis": "This should also be ignored.",
497+
"kbItems": [
498+
{
499+
"entity1": "entity1",
500+
"entity2": "entity2",
501+
"relationship": "equal",
502+
}
503+
],
504+
}
505+
506+
# Preconditions
507+
assert KnowledgeBaseAnnotation.objects.filter(problem=non_user_problem).count() == 0, "Non_user_problem should have no KB annotations to begin with."
508+
assert original_hypothesis != data["hypothesis"]
509+
510+
serializer = input_serializer_with_user(annotator, data=data, instance=non_user_problem)
511+
assert serializer.is_valid(raise_exception=True)
512+
updated_problem = serializer.save()
513+
514+
# Verify problem fields were not updated
515+
assert updated_problem.hypothesis.text == original_hypothesis
516+
assert updated_problem.premises.count() == original_premise_count
517+
518+
# Verify KB annotation was added
519+
kb_annotations = KnowledgeBaseAnnotation.objects.filter(problem=updated_problem)
520+
assert kb_annotations.count() == 1
521+

0 commit comments

Comments
 (0)