|
| 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 |
0 commit comments