Browse Source

Move test utilities to projects app

pull/1671/head
Hironsan 2 years ago
parent
commit
c236a5c56b
21 changed files with 147 additions and 134 deletions
  1. 103
      backend/api/tests/api/utils.py
  2. 10
      backend/auto_labeling/tests/test_views.py
  3. 2
      backend/data_export/tests/test_repositories.py
  4. 3
      backend/data_export/tests/test_views.py
  5. 7
      backend/data_import/tests/test_tasks.py
  6. 3
      backend/data_import/tests/test_views.py
  7. 3
      backend/examples/tests/test_comment.py
  8. 5
      backend/examples/tests/test_document.py
  9. 3
      backend/examples/tests/test_example_state.py
  10. 2
      backend/examples/tests/test_filters.py
  11. 2
      backend/examples/tests/test_models.py
  12. 3
      backend/label_types/tests/test_views.py
  13. 4
      backend/labels/tests/test_category.py
  14. 4
      backend/labels/tests/test_span.py
  15. 4
      backend/labels/tests/test_text_label.py
  16. 7
      backend/labels/tests/test_views.py
  17. 3
      backend/metrics/tests.py
  18. 5
      backend/projects/tests/test_member.py
  19. 3
      backend/projects/tests/test_project.py
  20. 3
      backend/projects/tests/test_tag.py
  21. 102
      backend/projects/tests/utils.py

103
backend/api/tests/api/utils.py

@ -1,109 +1,6 @@
from typing import List
from django.conf import settings
from model_mommy import mommy
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from projects.models import (DOCUMENT_CLASSIFICATION, IMAGE_CLASSIFICATION,
INTENT_DETECTION_AND_SLOT_FILLING, SEQ2SEQ,
SEQUENCE_LABELING, SPEECH2TEXT, Member)
from roles.models import Role
from roles.tests.utils import create_default_roles
from users.tests.utils import make_user
class ProjectData:
def __init__(self, item, members):
self.item = item
self.members = members
@property
def admin(self):
return self.members[0]
@property
def approver(self):
return self.members[1]
@property
def annotator(self):
return self.members[2]
@property
def staffs(self):
return [self.approver, self.annotator]
def assign_user_to_role(project_member, project, role_name):
role, _ = Role.objects.get_or_create(name=role_name)
if Member.objects.filter(user=project_member, project=project).exists():
mapping = Member.objects.get(user=project_member, project=project)
mapping.role = role
mapping.save()
else:
mapping = Member.objects.get_or_create(role_id=role.id, user_id=project_member.id, project_id=project.id)
return mapping
def make_project(
task: str,
users: List[str],
roles: List[str] = None,
collaborative_annotation=False,
**kwargs):
create_default_roles()
# create users.
users = [
make_user(name) for name in users
]
# create a project.
project_model = {
DOCUMENT_CLASSIFICATION: 'TextClassificationProject',
SEQUENCE_LABELING: 'SequenceLabelingProject',
SEQ2SEQ: 'Seq2seqProject',
SPEECH2TEXT: 'Speech2TextProject',
IMAGE_CLASSIFICATION: 'ImageClassificationProject',
INTENT_DETECTION_AND_SLOT_FILLING: 'IntentDetectionAndSlotFillingProject'
}.get(task, 'Project')
project = mommy.make(
_model=project_model,
project_type=task,
collaborative_annotation=collaborative_annotation,
created_by=users[0],
**kwargs
)
# assign roles to the users.
for user, role in zip(users, roles):
assign_user_to_role(user, project, role)
return ProjectData(
item=project,
members=users
)
def make_tag(project):
return mommy.make('Tag', project=project)
def prepare_project(task: str = 'Any', collaborative_annotation=False, **kwargs):
return make_project(
task=task,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
],
collaborative_annotation=collaborative_annotation,
**kwargs
)
class CRUDMixin(APITestCase): class CRUDMixin(APITestCase):
url = '' url = ''

10
backend/auto_labeling/tests/test_views.py

@ -7,11 +7,13 @@ from model_mommy import mommy
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ
from labels.models import Category, Span, TextLabel
from api.tests.api.utils import CRUDMixin, prepare_project
from examples.tests.utils import make_doc
from api.tests.api.utils import CRUDMixin
from auto_labeling.pipeline.labels import Categories, Spans, Texts from auto_labeling.pipeline.labels import Categories, Spans, Texts
from examples.tests.utils import make_doc
from labels.models import Category, Span, TextLabel
from projects.tests.utils import prepare_project
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ
data_dir = pathlib.Path(__file__).parent / 'data' data_dir = pathlib.Path(__file__).parent / 'data'

2
backend/data_export/tests/test_repositories.py

@ -3,7 +3,7 @@ import unittest
from model_mommy import mommy from model_mommy import mommy
from projects.models import INTENT_DETECTION_AND_SLOT_FILLING from projects.models import INTENT_DETECTION_AND_SLOT_FILLING
from api.tests.api.utils import prepare_project
from projects.tests.utils import prepare_project
from ..pipeline.repositories import IntentDetectionSlotFillingRepository from ..pipeline.repositories import IntentDetectionSlotFillingRepository

3
backend/data_export/tests/test_views.py

@ -1,8 +1,9 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import CRUDMixin
from projects.models import DOCUMENT_CLASSIFICATION from projects.models import DOCUMENT_CLASSIFICATION
from api.tests.api.utils import CRUDMixin, prepare_project
from projects.tests.utils import prepare_project
class TestDownloadCatalog(CRUDMixin): class TestDownloadCatalog(CRUDMixin):

7
backend/data_import/tests/test_tasks.py

@ -3,12 +3,13 @@ import pathlib
from django.test import TestCase from django.test import TestCase
from data_import.celery_tasks import import_dataset from data_import.celery_tasks import import_dataset
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ, IMAGE_CLASSIFICATION, \
INTENT_DETECTION_AND_SLOT_FILLING
from examples.models import Example from examples.models import Example
from label_types.models import CategoryType, SpanType from label_types.models import CategoryType, SpanType
from labels.models import Category, Span from labels.models import Category, Span
from api.tests.api.utils import prepare_project
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ, IMAGE_CLASSIFICATION, \
INTENT_DETECTION_AND_SLOT_FILLING
from projects.tests.utils import prepare_project
class TestImportData(TestCase): class TestImportData(TestCase):

3
backend/data_import/tests/test_views.py

@ -1,8 +1,9 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import CRUDMixin
from projects.models import DOCUMENT_CLASSIFICATION from projects.models import DOCUMENT_CLASSIFICATION
from api.tests.api.utils import CRUDMixin, prepare_project
from projects.tests.utils import prepare_project
class TestImportCatalog(CRUDMixin): class TestImportCatalog(CRUDMixin):

3
backend/examples/tests/test_comment.py

@ -1,7 +1,8 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import (CRUDMixin, prepare_project)
from api.tests.api.utils import CRUDMixin
from projects.tests.utils import prepare_project
from users.tests.utils import make_user from users.tests.utils import make_user
from .utils import make_comment, make_doc from .utils import make_comment, make_doc

5
backend/examples/tests/test_document.py

@ -3,9 +3,10 @@ from django.utils.http import urlencode
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from projects.models import DOCUMENT_CLASSIFICATION
from api.tests.api.utils import (CRUDMixin, assign_user_to_role, prepare_project)
from api.tests.api.utils import CRUDMixin
from users.tests.utils import make_user from users.tests.utils import make_user
from projects.models import DOCUMENT_CLASSIFICATION
from projects.tests.utils import assign_user_to_role, prepare_project
from .utils import make_doc, make_example_state from .utils import make_doc, make_example_state

3
backend/examples/tests/test_example_state.py

@ -1,7 +1,8 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import (CRUDMixin, prepare_project)
from api.tests.api.utils import (CRUDMixin)
from projects.tests.utils import prepare_project
from users.tests.utils import make_user from users.tests.utils import make_user
from .utils import make_doc, make_example_state from .utils import make_doc, make_example_state

2
backend/examples/tests/test_filters.py

@ -2,9 +2,9 @@ from unittest.mock import MagicMock
from django.test import TestCase from django.test import TestCase
from api.tests.api.utils import prepare_project
from examples.models import Example from examples.models import Example
from examples.filters import ExampleFilter from examples.filters import ExampleFilter
from projects.tests.utils import prepare_project
from .utils import make_doc, make_example_state from .utils import make_doc, make_example_state

2
backend/examples/tests/test_models.py

@ -2,7 +2,7 @@ from django.test import TestCase
from model_mommy import mommy from model_mommy import mommy
from projects.models import SEQUENCE_LABELING, IMAGE_CLASSIFICATION from projects.models import SEQUENCE_LABELING, IMAGE_CLASSIFICATION
from api.tests.api.utils import prepare_project
from projects.tests.utils import prepare_project
from examples.models import ExampleState from examples.models import ExampleState

3
backend/label_types/tests/test_views.py

@ -5,8 +5,9 @@ from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from api.tests.api.utils import CRUDMixin
from projects.models import DOCUMENT_CLASSIFICATION from projects.models import DOCUMENT_CLASSIFICATION
from api.tests.api.utils import (CRUDMixin, make_project, prepare_project)
from projects.tests.utils import make_project, prepare_project
from users.tests.utils import make_user from users.tests.utils import make_user
from .utils import make_label from .utils import make_label

4
backend/labels/tests/test_category.py

@ -4,9 +4,9 @@ from django.db import IntegrityError
from django.test import TestCase from django.test import TestCase
from model_mommy import mommy from model_mommy import mommy
from projects.models import DOCUMENT_CLASSIFICATION
from labels.models import Category from labels.models import Category
from api.tests.api.utils import prepare_project
from projects.models import DOCUMENT_CLASSIFICATION
from projects.tests.utils import prepare_project
class TestCategoryLabeling(abc.ABC, TestCase): class TestCategoryLabeling(abc.ABC, TestCase):

4
backend/labels/tests/test_span.py

@ -5,10 +5,10 @@ from django.db import IntegrityError
from django.test import TestCase from django.test import TestCase
from model_mommy import mommy from model_mommy import mommy
from projects.models import SEQUENCE_LABELING
from label_types.models import SpanType from label_types.models import SpanType
from labels.models import Span from labels.models import Span
from api.tests.api.utils import prepare_project
from projects.models import SEQUENCE_LABELING
from projects.tests.utils import prepare_project
class TestSpanLabeling(abc.ABC, TestCase): class TestSpanLabeling(abc.ABC, TestCase):

4
backend/labels/tests/test_text_label.py

@ -4,9 +4,9 @@ from django.db import IntegrityError
from django.test import TestCase from django.test import TestCase
from model_mommy import mommy from model_mommy import mommy
from projects.models import SEQ2SEQ
from labels.models import TextLabel from labels.models import TextLabel
from api.tests.api.utils import prepare_project
from projects.models import SEQ2SEQ
from projects.tests.utils import prepare_project
class TestTextLabeling(abc.ABC, TestCase): class TestTextLabeling(abc.ABC, TestCase):

7
backend/labels/tests/test_views.py

@ -1,11 +1,12 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ
from labels.models import Category, Span, TextLabel
from api.tests.api.utils import (CRUDMixin, prepare_project)
from api.tests.api.utils import CRUDMixin
from examples.tests.utils import make_doc from examples.tests.utils import make_doc
from labels.models import Category, Span, TextLabel
from label_types.tests.utils import make_label from label_types.tests.utils import make_label
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ
from projects.tests.utils import prepare_project
from users.tests.utils import make_user from users.tests.utils import make_user
from .utils import make_annotation from .utils import make_annotation

3
backend/metrics/tests.py

@ -2,10 +2,11 @@ from model_mommy import mommy
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import CRUDMixin, prepare_project
from api.tests.api.utils import CRUDMixin
from examples.tests.utils import make_doc from examples.tests.utils import make_doc
from label_types.tests.utils import make_label from label_types.tests.utils import make_label
from projects.models import DOCUMENT_CLASSIFICATION from projects.models import DOCUMENT_CLASSIFICATION
from projects.tests.utils import prepare_project
class TestMemberProgress(CRUDMixin): class TestMemberProgress(CRUDMixin):

5
backend/projects/tests/test_member.py

@ -5,9 +5,10 @@ from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from model_mommy import mommy from model_mommy import mommy
from roles.models import Role
from api.tests.api.utils import CRUDMixin
from projects.models import Member from projects.models import Member
from api.tests.api.utils import (CRUDMixin, prepare_project)
from projects.tests.utils import prepare_project
from roles.models import Role
from users.tests.utils import make_user from users.tests.utils import make_user

3
backend/projects/tests/test_project.py

@ -1,7 +1,8 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import CRUDMixin, prepare_project
from api.tests.api.utils import CRUDMixin
from projects.tests.utils import prepare_project
from roles.tests.utils import create_default_roles from roles.tests.utils import create_default_roles
from users.tests.utils import make_user from users.tests.utils import make_user

3
backend/projects/tests/test_tag.py

@ -1,7 +1,8 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from api.tests.api.utils import CRUDMixin, make_tag, prepare_project
from api.tests.api.utils import CRUDMixin
from projects.tests.utils import make_tag, prepare_project
from users.tests.utils import make_user from users.tests.utils import make_user

102
backend/projects/tests/utils.py

@ -0,0 +1,102 @@
from typing import List
from django.conf import settings
from model_mommy import mommy
from projects.models import Role, Member
from projects.models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, SEQ2SEQ, SPEECH2TEXT,\
IMAGE_CLASSIFICATION, INTENT_DETECTION_AND_SLOT_FILLING
from roles.tests.utils import create_default_roles
from users.tests.utils import make_user
class ProjectData:
def __init__(self, item, members):
self.item = item
self.members = members
@property
def admin(self):
return self.members[0]
@property
def approver(self):
return self.members[1]
@property
def annotator(self):
return self.members[2]
@property
def staffs(self):
return [self.approver, self.annotator]
def assign_user_to_role(project_member, project, role_name):
role, _ = Role.objects.get_or_create(name=role_name)
if Member.objects.filter(user=project_member, project=project).exists():
mapping = Member.objects.get(user=project_member, project=project)
mapping.role = role
mapping.save()
else:
mapping = Member.objects.get_or_create(role_id=role.id, user_id=project_member.id, project_id=project.id)
return mapping
def make_project(
task: str,
users: List[str],
roles: List[str] = None,
collaborative_annotation=False,
**kwargs):
create_default_roles()
# create users.
users = [
make_user(name) for name in users
]
# create a project.
project_model = {
DOCUMENT_CLASSIFICATION: 'TextClassificationProject',
SEQUENCE_LABELING: 'SequenceLabelingProject',
SEQ2SEQ: 'Seq2seqProject',
SPEECH2TEXT: 'Speech2TextProject',
IMAGE_CLASSIFICATION: 'ImageClassificationProject',
INTENT_DETECTION_AND_SLOT_FILLING: 'IntentDetectionAndSlotFillingProject'
}.get(task, 'Project')
project = mommy.make(
_model=project_model,
project_type=task,
collaborative_annotation=collaborative_annotation,
created_by=users[0],
**kwargs
)
# assign roles to the users.
for user, role in zip(users, roles):
assign_user_to_role(user, project, role)
return ProjectData(
item=project,
members=users
)
def make_tag(project):
return mommy.make('Tag', project=project)
def prepare_project(task: str = 'Any', collaborative_annotation=False, **kwargs):
return make_project(
task=task,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
],
collaborative_annotation=collaborative_annotation,
**kwargs
)
Loading…
Cancel
Save