ccdb-api/ccdb/locations/tests/test_models.py

58 lines
1.8 KiB
Python

from django.test import TestCase
from django.db import IntegrityError, transaction
from ..models import Region, Site, MunicipalLocation, StudyLocation, StorageLocation
from ..factories import RegionFactory, SiteFactory, MunicipalLocationFactory, \
StudyLocationFactory, StorageLocationFactory
class RegionTestCase(TestCase):
def test_creation(self):
r = RegionFactory()
self.assertTrue(isinstance(r, Region))
self.assertEqual(r.__str__(), r.name)
def test_uniqueness(self):
r1 = RegionFactory()
with transaction.atomic(), self.assertRaises(IntegrityError):
RegionFactory(name=r1.name, code=r1.code)
r3 = RegionFactory()
self.assertTrue(isinstance(r3, Region))
class SiteTestCase(TestCase):
def test_creation(self):
s = SiteFactory()
self.assertTrue(isinstance(s, Site))
self.assertEqual(s.__str__(), s.name)
class MunicipalLocationTestCase(TestCase):
def test_creation(self):
m = MunicipalLocationFactory()
self.assertTrue(isinstance(m, MunicipalLocation))
self.assertEqual(m.__str__(), m.name)
class StudyLocationTestCase(TestCase):
def test_creation(self):
s = StudyLocationFactory()
self.assertTrue(isinstance(s, StudyLocation))
self.assertEqual(s.__str__(), s.code)
def test_uniqueness(self):
s1 = StudyLocationFactory()
with transaction.atomic(), self.assertRaises(IntegrityError):
StudyLocationFactory(site=s1.site, name=s1.name)
s3 = StudyLocationFactory()
self.assertTrue(isinstance(s3, StudyLocation))
class StorageLocationTestCase(TestCase):
def test_creation(self):
s = StorageLocationFactory()
self.assertTrue(isinstance(s, StorageLocation))
self.assertEqual(s.__str__(), s.code)