2022-08-27 06:34:59 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.test import APITestCase
|
2022-08-19 10:35:05 +00:00
|
|
|
|
2022-08-27 12:28:16 +00:00
|
|
|
from frog_api.models import Category, Entry, Measure, Project, Version
|
2022-08-27 06:34:59 +00:00
|
|
|
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
class CreateCategoryTests(APITestCase):
|
2022-08-27 06:34:59 +00:00
|
|
|
def test_create_categories(self) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that the category creation endpoint works
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create a test Project and Version
|
|
|
|
project = Project(slug="oot", name="Ocarina of Time", auth_key="test_key_123")
|
|
|
|
project.save()
|
|
|
|
|
|
|
|
version = Version(slug="us", name="US", project=project)
|
|
|
|
version.save()
|
|
|
|
|
|
|
|
response = self.client.post(
|
2022-09-22 20:40:44 +00:00
|
|
|
reverse("category-structure", args=[project.slug, version.slug, "total"]),
|
|
|
|
{
|
|
|
|
"api_key": "test_key_123",
|
|
|
|
"name": "Total",
|
|
|
|
},
|
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("category-structure", args=[project.slug, version.slug, "actors"]),
|
|
|
|
{
|
|
|
|
"api_key": "test_key_123",
|
|
|
|
"name": "Actors",
|
|
|
|
},
|
2022-08-27 06:34:59 +00:00
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm we created the categories and that they are in the DB
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
2022-09-22 20:40:44 +00:00
|
|
|
self.assertEqual(Category.objects.count(), 2)
|
2022-08-27 12:28:16 +00:00
|
|
|
|
|
|
|
|
2023-05-25 09:50:09 +00:00
|
|
|
SAMPLE_PROJECT_SLUG = "oot"
|
|
|
|
SAMPLE_VERSION_SLUG = "us"
|
|
|
|
SAMPLE_DATA = {
|
|
|
|
"api_key": "test_key_123",
|
|
|
|
"entries": [
|
|
|
|
{
|
|
|
|
"categories": {
|
|
|
|
"default": {
|
|
|
|
"code_matching": 103860,
|
|
|
|
"code_total": 4747584,
|
|
|
|
"asm": 4597948,
|
|
|
|
"nonmatching_functions_count": 49,
|
|
|
|
"assets_identified": 0,
|
|
|
|
"assets_total": 40816656,
|
|
|
|
"code_decompiled": 120152,
|
|
|
|
"assets_debinarised": 0,
|
|
|
|
},
|
|
|
|
"actors": {
|
|
|
|
"code_matching": 103860,
|
|
|
|
"code_total": 4747584,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"timestamp": 1615435438,
|
|
|
|
"git_hash": "e788bfecbfb10afd4182332db99bb562ea75b1de",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-27 12:28:16 +00:00
|
|
|
class CreateEntriesTests(APITestCase):
|
2023-05-25 09:50:09 +00:00
|
|
|
def create_project_metadata(self, project_slug: str, version_slug: str) -> None:
|
|
|
|
# Create a test Project, Version, and Categories
|
|
|
|
project = Project(
|
|
|
|
slug=project_slug, name="Ocarina of Time", auth_key="test_key_123"
|
|
|
|
)
|
|
|
|
project.save()
|
|
|
|
|
|
|
|
version = Version(slug=version_slug, name="US", project=project)
|
|
|
|
version.save()
|
|
|
|
|
|
|
|
category1 = Category(slug="default", name="Default", version=version)
|
|
|
|
category1.save()
|
|
|
|
|
|
|
|
category2 = Category(slug="actors", name="Actors", version=version)
|
|
|
|
category2.save()
|
|
|
|
|
2022-08-27 12:28:16 +00:00
|
|
|
def test_create_entries(self) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that the entry creation endpoint works
|
|
|
|
"""
|
2023-05-25 09:50:09 +00:00
|
|
|
|
|
|
|
self.create_project_metadata(SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG)
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("version-data", args=[SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG]),
|
|
|
|
SAMPLE_DATA,
|
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm we created the entries and that they are in the DB
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
self.assertEqual(Entry.objects.count(), 2)
|
|
|
|
self.assertEqual(Measure.objects.count(), 10)
|
|
|
|
|
|
|
|
def test_create_duplicate_entries(self) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that it's impossible to make duplicate entries
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.create_project_metadata(SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG)
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("version-data", args=[SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG]),
|
|
|
|
SAMPLE_DATA,
|
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm we created the entries and that they are in the DB
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
self.assertEqual(Entry.objects.count(), 2)
|
|
|
|
self.assertEqual(Measure.objects.count(), 10)
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("version-data", args=[SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG]),
|
|
|
|
SAMPLE_DATA,
|
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ensure we got a bad request code since we shouldn't be able to create duplicate entries
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(Entry.objects.count(), 2)
|
|
|
|
self.assertEqual(Measure.objects.count(), 10)
|
|
|
|
|
|
|
|
def test_atomicity(self) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that if some entries fail to be created, none are created
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.create_project_metadata(SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG)
|
|
|
|
|
|
|
|
yucky_data = {
|
2022-08-27 12:28:16 +00:00
|
|
|
"api_key": "test_key_123",
|
|
|
|
"entries": [
|
|
|
|
{
|
|
|
|
"categories": {
|
2023-05-25 09:50:09 +00:00
|
|
|
"actors": {
|
2022-08-27 12:28:16 +00:00
|
|
|
"code_matching": 103860,
|
|
|
|
"code_total": 4747584,
|
|
|
|
},
|
2023-05-25 09:50:09 +00:00
|
|
|
},
|
|
|
|
"timestamp": 1615435438,
|
|
|
|
"git_hash": "e788bfecbfb10afd4182332db99bb562ea75b1de",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"categories": {
|
2022-08-27 12:28:16 +00:00
|
|
|
"actors": {
|
|
|
|
"code_matching": 103860,
|
|
|
|
"code_total": 4747584,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"timestamp": 1615435438,
|
|
|
|
"git_hash": "e788bfecbfb10afd4182332db99bb562ea75b1de",
|
2023-05-25 09:50:09 +00:00
|
|
|
},
|
2022-08-27 12:28:16 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.client.post(
|
2023-05-25 09:50:09 +00:00
|
|
|
reverse("version-data", args=[SAMPLE_PROJECT_SLUG, SAMPLE_VERSION_SLUG]),
|
|
|
|
yucky_data,
|
2022-08-27 12:28:16 +00:00
|
|
|
format="json",
|
|
|
|
)
|
|
|
|
|
2023-05-25 09:50:09 +00:00
|
|
|
# Confirm no entries or measures are created
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
2022-08-27 12:28:16 +00:00
|
|
|
|
2023-05-25 09:50:09 +00:00
|
|
|
self.assertEqual(Entry.objects.count(), 0)
|
|
|
|
self.assertEqual(Measure.objects.count(), 0)
|