Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
test: attachments by project
Browse files Browse the repository at this point in the history
  • Loading branch information
obdulia-losantos committed Dec 11, 2019
1 parent b4507ee commit 7da750a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 59 deletions.
146 changes: 88 additions & 58 deletions aether-kernel/aether/kernel/api/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,64 +366,6 @@ def test_entity_filter__by_date(self):
self.assertEqual(single_count, 1)
self.assertEqual(count, (single_count + gt_count + lt_count))

def test_submission_filter__by_project(self):
url = reverse(viewname='submission-list')
# Generate projects.
for _ in range(random.randint(5, 10)):
generate_project()
submissions_count = models.Submission.objects.count()
# Get a list of all projects.
for project in models.Project.objects.all():
# Request a list of all submissions, filtered by `project`.
# This checks that SubmissionFilter.project exists and that
# SubmissionFilter has been correctly configured.
expected = set([str(s.id) for s in project.submissions.all()])

# by id
kwargs = {'project': str(project.id), 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_id = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_id, 'by id')

# by name
kwargs = {'project': project.name, 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_name = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_name, 'by name')

def test_submission_filter__by_mappingset(self):
url = reverse(viewname='submission-list')
# Generate projects.
for _ in range(random.randint(5, 10)):
generate_project()
submissions_count = models.Submission.objects.count()
# Get a list of all mapping sets.
for mappingset in models.MappingSet.objects.all():
# Request a list of all submissions, filtered by `mappingset`.
# This checks that SubmissionFilter.mappingset exists and that
# SubmissionFilter has been correctly configured.
expected = set([str(e.id) for e in mappingset.submissions.all()])

# by id
kwargs = {'mappingset': str(mappingset.id), 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_id = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_id, 'by id')

# by name
kwargs = {'mappingset': mappingset.name, 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_name = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_name, 'by name')

def test_mapping_filter__by_mappingset(self):
url = reverse(viewname='mapping-list')
# Generate projects.
Expand Down Expand Up @@ -524,6 +466,64 @@ def test_mappingset_filter__by_project(self):
result = set([r['id'] for r in response['results']])
self.assertEqual(expected, result)

def test_submission_filter__by_project(self):
url = reverse(viewname='submission-list')
# Generate projects.
for _ in range(random.randint(5, 10)):
generate_project()
submissions_count = models.Submission.objects.count()
# Get a list of all projects.
for project in models.Project.objects.all():
# Request a list of all submissions, filtered by `project`.
# This checks that SubmissionFilter.project exists and that
# SubmissionFilter has been correctly configured.
expected = set([str(s.id) for s in project.submissions.all()])

# by id
kwargs = {'project': str(project.id), 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_id = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_id, 'by id')

# by name
kwargs = {'project': project.name, 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_name = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_name, 'by name')

def test_submission_filter__by_mappingset(self):
url = reverse(viewname='submission-list')
# Generate projects.
for _ in range(random.randint(5, 10)):
generate_project()
submissions_count = models.Submission.objects.count()
# Get a list of all mapping sets.
for mappingset in models.MappingSet.objects.all():
# Request a list of all submissions, filtered by `mappingset`.
# This checks that SubmissionFilter.mappingset exists and that
# SubmissionFilter has been correctly configured.
expected = set([str(e.id) for e in mappingset.submissions.all()])

# by id
kwargs = {'mappingset': str(mappingset.id), 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_id = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_id, 'by id')

# by name
kwargs = {'mappingset': mappingset.name, 'fields': 'id', 'page_size': submissions_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result_by_name = set([r['id'] for r in response['results']])
self.assertEqual(expected, result_by_name, 'by name')

def test_submission_filter__by_date(self):
for _ in range(random.randint(5, 10)):
generate_project()
Expand Down Expand Up @@ -627,3 +627,33 @@ def test_submission_filter__by_payload__error(self):
'payload__a': '[1', # raise json.decoder.JSONDecodeError
})
self.assertEqual(json.loads(response.content)['count'], submissions_count)

def test_attachment_filter__by_project(self):
url = reverse(viewname='attachement-list')
# Generate projects.
for _ in range(random.randint(5, 10)):
generate_project(include_attachments=True)
attachments_count = models.Attachment.objects.count()
self.assertNotEqual(attachments_count, 0, ' There is at least one attachment')

# Get a list of all projects.
for project in models.Project.objects.all():
expected = set([
str(a.id)
for a in models.Attachment.objects.filter(submission__project__pk=project.pk)
])
# by id
kwargs = {'project': str(project.id), 'fields': 'id', 'page_size': attachments_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result = set([r['id'] for r in response['results']])
self.assertEqual(expected, result)

# by name
kwargs = {'project': project.name, 'fields': 'id', 'page_size': attachments_count}
response = json.loads(self.client.get(url, kwargs).content)
# Check both sets of ids for equality.
self.assertEqual(response['count'], len(expected))
result = set([r['id'] for r in response['results']])
self.assertEqual(expected, result)
13 changes: 12 additions & 1 deletion aether-kernel/aether/kernel/api/tests/utils/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@

from autofixture import AutoFixture

from django.core.files.uploadedfile import SimpleUploadedFile

from aether.python.avro.tools import random_avro
from aether.kernel.api import models
from aether.kernel.api.entity_extractor import run_entity_extraction

MAPPINGS_COUNT_RANGE = (1, 3)
SUBMISSIONS_COUNT_RANGE = (2, 5)
SUBMISSIONS_COUNT_RANGE = (2, 3)
ATTACHMENTS_COUNT_RANGE = (1, 2)


def schema_definition():
Expand Down Expand Up @@ -84,6 +87,7 @@ def generate_project(
mappingset_field_values=None,
mapping_field_values=None,
submission_field_values=None,
include_attachments=False,
):
'''
Generate an Aether Project.
Expand Down Expand Up @@ -177,5 +181,12 @@ def generate_project(
),
).create_one()

if include_attachments:
for index in range(random.randint(*ATTACHMENTS_COUNT_RANGE)):
models.Attachment.objects.create(
submission=submission,
attachment_file=SimpleUploadedFile(f'sample_{index}.txt', b'abc'),
)

# extract entities
run_entity_extraction(submission)

0 comments on commit 7da750a

Please sign in to comment.