Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

104 unit testing for api #116

Merged
merged 3 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions bco_api/api/tests/test_model_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.utils import timezone
from django.contrib.auth.models import Group, Permission, User
# from django.urls import reverse
from api.model.prefix import Prefix
from api.model.prefix import Prefix, prefix_table
from datetime import timedelta


Expand All @@ -26,7 +26,7 @@ def setUp(self):
self.expiration = timezone.now() + timedelta(seconds=600) # make valid for 10 minutes

def create_prefix(self):
"""Create Test BCO
"""Create Test Prefix

"""

Expand Down Expand Up @@ -59,3 +59,37 @@ def test_prefix_creation(self):
self.assertEqual(prefix.owner_group, Group.objects.get(name=self.name))
# Check that the expiration is set.
self.assertEqual(prefix.expires, self.expiration)


class PrefixTableTestCase(TestCase):
"""Test for Prefix Table

"""

def setUp(self):
self.n_objects = 4
self.prefix = 'TEST'

def create_prefix_table(self):
"""Create Test Prefix Table

"""

return prefix_table.objects.create(
prefix=self.prefix,
n_objects=self.n_objects
)

def test_prefix_creation(self):
"""Test prefix creation

Creates prefix,
"""

ptable = self.create_prefix_table()
# Test if the prefix object is actually a Prefix Table object
self.assertTrue(isinstance(ptable, prefix_table))
# Test that the prefix was set correctly
self.assertEqual(ptable.__str__(), self.prefix)
# Check that the number of records in the table are correct
self.assertEqual(ptable.n_objects, self.n_objects)
87 changes: 87 additions & 0 deletions bco_api/api/tests/test_prefix_post_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3

"""Prefix Model Testing

"""

import json
from django.test import TestCase, Client
from django.utils import timezone
from django.contrib.auth.models import Group, Permission, User
# from django.urls import reverse
from api.models import BCO
from datetime import timedelta


class ApiTestCase(TestCase):
"""Tests for API calls

"""

def setUp(self):
# Client allows us to call the endpoints as if the server was running.
# Since the server isn't running, this lets us use this for testing.
self.client = Client()

# Sample BCO information for various API calls
# This will let us insert direclty in the DB or
# insert via API call.
json_file = open('api/tests/test_bcos.json')
data = json.load(json_file)
self.sample_bco = {
"object_id_root": 'BCO_000001',
"object_id_version": '/1.5',
"owner_user": 'anon',
"owner_group": 'bco_drafter',
"prefix": 'BCO',
"schema": 'IEEE',
"state": 'PUBLISHED',
"contents": json.dumps(data[0])
}
self.sample_bco["object_id"] = 'http://localhost:8000/{}{}'.format(self.sample_bco["object_id_root"], self.sample_bco["object_id_version"])

self.expiration = timezone.now() + timedelta(seconds=600) # make valid for 10 minutes

def create_bco_direct(self):
"""Create BCO directly in DB if needed

"""
# We can use this to create a BCO in the DB if the API call to create fails.
return BCO.objects.create(
contents=self.sample_bco["contents"],
object_class=None,
object_id=self.sample_bco["object_id"],
owner_user=User.objects.get(username=self.sample_bco["owner_user"]),
owner_group=Group.objects.get(name=self.sample_bco["owner_group"]),
prefix=self.sample_bco["prefix"],
schema=self.sample_bco["schema"],
state=self.sample_bco["state"],
last_update=timezone.now()
)

def test_post_api_prefixes_create(self):
"""Test post_api_prefixes_create API endpoint

Creates a prefix.
"""
# Try to force login as the owner
if self.client.force_login(user=User.objects.get(username=self.sample_bco["owner_user"])):
response = self.client.post('/api/objects/drafts/create/', {
"POST_api_objects_draft_create":
[
{
"prefix": self.sample_bco["prefix"],
"owner_group": self.sample_bco["owner_group"],
"object_id": self.sample_bco["object_id"],
"schema": self.sample_bco["schema"],
"contents": self.sample_bco["contents"],
}
]
})
print("Response: ".format(response))
else:
# Appears login failed
print("Failed login")
return False