Skip to content

Commit dc50912

Browse files
Add backend tests for Problem.status
1 parent 9cb9758 commit dc50912

5 files changed

Lines changed: 123 additions & 35 deletions

File tree

backend/annotation/serializers_test.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from typing import Any
33

4-
from django.utils import timezone
54
from django.contrib.auth.models import Permission
65
from rest_framework.test import APIRequestFactory
76

@@ -11,7 +10,6 @@
1110
LabelAnnotationSerializer,
1211
SaveLabelsInputSerializer,
1312
)
14-
from problem.serializers import ProblemSerializer
1513

1614

1715
@pytest.mark.django_db

backend/problem/conftest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from problem.models import Problem, Sentence
4+
5+
6+
@pytest.fixture
7+
def hypothesis_sentence(db):
8+
return Sentence.objects.create(text="Hypothesis")
9+
10+
11+
@pytest.fixture
12+
def premise_sentence(db):
13+
return Sentence.objects.create(text="Premise")
14+
15+
16+
@pytest.fixture
17+
def user_problem(db, hypothesis_sentence, premise_sentence):
18+
problem = Problem.objects.create(
19+
dataset=Problem.Dataset.USER,
20+
hypothesis=hypothesis_sentence,
21+
extra_data={},
22+
)
23+
problem.premises.add(premise_sentence)
24+
return problem
25+
26+
27+
@pytest.fixture
28+
def non_user_problem(db, hypothesis_sentence, premise_sentence):
29+
problem = Problem.objects.create(
30+
dataset=Problem.Dataset.SICK,
31+
hypothesis=hypothesis_sentence,
32+
extra_data={},
33+
)
34+
problem.premises.add(premise_sentence)
35+
return problem

backend/problem/models_test.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import pytest
2+
3+
from annotation.models import KnowledgeBaseAnnotation, LabelAnnotation
4+
from problem.models import Problem
5+
6+
7+
@pytest.fixture
8+
def kb_annotation(db, annotator_session, non_user_problem):
9+
return KnowledgeBaseAnnotation.objects.create(
10+
problem=non_user_problem,
11+
entity1="dog",
12+
entity2="canine",
13+
relationship=KnowledgeBaseAnnotation.Relationship.EQUAL,
14+
session=annotator_session,
15+
created_by=annotator_session.user,
16+
)
17+
18+
@pytest.mark.django_db
19+
def test_status_bronze_no_annotations(db, non_user_problem):
20+
"""
21+
A problem with no annotations and gold=False is bronze.
22+
23+
This also functions as an initial assumption check for the tests below.
24+
"""
25+
assert non_user_problem.status == Problem.Status.BRONZE
26+
27+
28+
@pytest.mark.django_db
29+
def test_status_gold_without_annotation(db, non_user_problem):
30+
"""A problem with gold=True is gold without annotations."""
31+
non_user_problem.gold = True
32+
non_user_problem.save()
33+
assert non_user_problem.status == Problem.Status.GOLD
34+
35+
@pytest.mark.django_db
36+
def test_status_gold_with_annotation(db, non_user_problem, kb_annotation):
37+
"""A problem with gold=True is gold even with annotations."""
38+
non_user_problem.gold = True
39+
non_user_problem.save()
40+
assert non_user_problem.status == Problem.Status.GOLD
41+
42+
43+
@pytest.mark.django_db
44+
def test_status_silver_with_kb_annotation(db, non_user_problem, kb_annotation):
45+
"""A problem with an active KB annotation and gold=False is silver."""
46+
assert non_user_problem.status == Problem.Status.SILVER
47+
48+
@pytest.mark.django_db
49+
def test_status_silver_with_label_annotation(db, non_user_problem, annotator_session, sample_label):
50+
"""A problem with an active label annotation and gold=False is silver."""
51+
LabelAnnotation.objects.create(
52+
problem=non_user_problem,
53+
label=sample_label,
54+
session=annotator_session,
55+
created_by=annotator_session.user,
56+
)
57+
assert non_user_problem.status == Problem.Status.SILVER
58+
59+
60+
@pytest.mark.django_db
61+
def test_status_bronze_when_all_annotations_removed(db, non_user_problem, annotator_session):
62+
"""A problem whose only annotation is removed reverts to bronze."""
63+
from django.utils import timezone
64+
65+
kb = KnowledgeBaseAnnotation.objects.create(
66+
problem=non_user_problem,
67+
entity1="dog",
68+
entity2="canine",
69+
relationship=KnowledgeBaseAnnotation.Relationship.EQUAL,
70+
session=annotator_session,
71+
created_by=annotator_session.user,
72+
)
73+
kb.removed_at = timezone.now()
74+
kb.removed_by = annotator_session.user
75+
kb.save()
76+
77+
assert non_user_problem.status == Problem.Status.BRONZE
78+
79+
80+
@pytest.mark.django_db
81+
def test_status_serialized(db, non_user_problem):
82+
"""The status field is correctly serialized."""
83+
from problem.serializers import ProblemSerializer
84+
85+
serializer = ProblemSerializer(non_user_problem)
86+
assert serializer.data["status"] == Problem.Status.BRONZE
87+
assert serializer.data["gold"] is False

backend/problem/serializers_test.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,6 @@
33

44
from annotation.models import KnowledgeBaseAnnotation
55
from .serializers import ProblemInputSerializer
6-
from .models import Problem, Sentence
7-
8-
9-
@pytest.fixture
10-
def hypothesis_sentence(db):
11-
return Sentence.objects.create(text="Hypothesis")
12-
13-
14-
@pytest.fixture
15-
def premise_sentence(db):
16-
return Sentence.objects.create(text="Premise")
17-
18-
19-
@pytest.fixture
20-
def user_problem(db, hypothesis_sentence, premise_sentence):
21-
problem = Problem.objects.create(
22-
dataset=Problem.Dataset.USER,
23-
hypothesis=hypothesis_sentence,
24-
extra_data={},
25-
)
26-
problem.premises.add(premise_sentence)
27-
return problem
28-
29-
30-
@pytest.fixture
31-
def non_user_problem(db, hypothesis_sentence, premise_sentence):
32-
problem = Problem.objects.create(
33-
dataset=Problem.Dataset.SICK,
34-
hypothesis=hypothesis_sentence,
35-
extra_data={},
36-
)
37-
problem.premises.add(premise_sentence)
38-
return problem
396

407

418
@pytest.mark.django_db

backend/user/tests/test_user_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_user_details(user_client, user_data):
1818
"canCopyProblem": False,
1919
"canAddLabelAnnotations": False,
2020
"canChangeProblemVisibility": False,
21+
"canChangeProblemStatus": False,
2122
}
2223

2324

0 commit comments

Comments
 (0)