diff --git a/.env b/.env new file mode 100644 index 0000000..63e4fde --- /dev/null +++ b/.env @@ -0,0 +1 @@ +MANIFEST_URL='https://storage.googleapis.com/ccdb-db-files/manifest.json' diff --git a/ccdb/projects/factories.py b/ccdb/projects/factories.py new file mode 100644 index 0000000..fc687a5 --- /dev/null +++ b/ccdb/projects/factories.py @@ -0,0 +1,36 @@ +import factory + +from .models import Project, Grant, GrantReport + + +class ProjectFactory(factory.DjangoModelFactory): + class Meta: + model = Project + + name = factory.Sequence(lambda n: 'project{}'.format(n)) + code = factory.Sequence(lambda n: 'p{}'.format(n)) + iacuc_number = 'abc' + description = 'lorem ipsum' + sort_order = factory.Sequence(lambda n: n) + + +class GrantFactory(factory.DjangoModelFactory): + class Meta: + model = Grant + + title = factory.Sequence(lambda n: 'grant{}'.format(n)) + code = factory.Sequence(lambda n: 'g{}'.format(n)) + description = 'lorem ipsum' + sort_order = factory.Sequence(lambda n: n) + + +class GrantReportFactory(factory.DjangoModelFactory): + class Meta: + model = GrantReport + + grant = 0 + title = factory.Sequence(lambda n: 'grant{}'.format(n)) + code = factory.Sequence(lambda n: 'g{}'.format(n)) + description = 'lorem ipsum' + sort_order = factory.Sequence(lambda n: n) + diff --git a/ccdb/projects/migrations/0005_DATA_initial.py b/ccdb/projects/migrations/0005_DATA_initial.py new file mode 100644 index 0000000..d2b48c6 --- /dev/null +++ b/ccdb/projects/migrations/0005_DATA_initial.py @@ -0,0 +1,83 @@ +from django.db import migrations +from django.forms import modelform_factory + +from ccdb.utils.data import get_data_sources + + +class Migration(migrations.Migration): + def migrate(apps, schema_editor): + sources = get_data_sources() + if not sources: + print('no sources') + return + + c = sources['db0'] + + Project = apps.get_model('projects', 'Project') + Grant = apps.get_model('projects', 'Grant') + GrantReport = apps.get_model('projects', 'GrantReport') + + Project.objects.all().delete() + Grant.objects.all().delete() + GrantReport.objects.all().delete() + + ProjectForm = modelform_factory(Project, fields=('name', 'code', + 'iacuc_number', + 'description', + 'sort_order')) + GrantForm = modelform_factory(Grant, fields=('title', 'code', + 'description', 'sort_order')) + GrantReportForm = modelform_factory(GrantReport, fields=('grant', 'title', + 'report_type', + 'description', + 'due_date', + 'submitted_date', + 'sort_order')) + + for r in c.execute('SELECT * FROM tbl_lu_projects;'): + form = ProjectForm(dict(name=r[1], code=r[2], + iacuc_number=r[3], description=r[4], + sort_order=int(r[5]))) + if form.is_valid(): + Project.objects.create(id=r[0], **form.cleaned_data) + else: + print('project', r[0:], form.errors.as_data()) + + for r in c.execute('SELECT * FROM tbl_lu_grants;'): + form = GrantForm(dict(title=r[1], code=r[2], description=r[3], sort_order=r[4])) + if form.is_valid(): + Grant.objects.create(id=r[0], **form.cleaned_data) + else: + print('grant', r[0:], form.errors.as_data()) + + for r in c.execute('SELECT * FROM tbl_hash_project_grants;'): + p = Project.objects.get(id=r[0]) + g = Grant.objects.get(id=r[1]) + p.grants.add(g) + p.save() + + for r in c.execute('SELECT *, report_due_date AS "due_date [dtdt]" FROM tbl_lu_grant_reports;'): + form = GrantReportForm(dict(grant=r[0], title=r[1], report_type=r[2], + description=r[3], due_date=r[8], + submitted_date=r[5], sort_order=r[7])) + if form.is_valid(): + form.save() # No PK field in Andre's file + else: + print('grant report', r[0:], form.errors.as_data()) + + + def rollback(apps, schema_editor): + Project = apps.get_model('projects', 'Project') + Grant = apps.get_model('projects', 'Grant') + GrantReport = apps.get_model('projects', 'GrantReport') + + for model in [Project, Grant, GrantReport]: + model.objects.all().delete() + + dependencies = [ + ('projects', '0004_initial_grantreport'), + ] + + operations = [ + migrations.RunPython(migrate, rollback), + ] diff --git a/ccdb/projects/tests/__init__.py b/ccdb/projects/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ccdb/projects/tests/test_models.py b/ccdb/projects/tests/test_models.py new file mode 100644 index 0000000..ed1f0bb --- /dev/null +++ b/ccdb/projects/tests/test_models.py @@ -0,0 +1,67 @@ +from datetime import datetime + +from django.test import TestCase +from django.db import IntegrityError, transaction + +from ..models import Project, Grant, GrantReport + + +def _project(name='project', code='p', iacuc_number='xyz', description='lorem ipsum', + sort_order=1): + return Project.objects.create(name=name, code=code, iacuc_number=iacuc_number, + description=description, sort_order=sort_order) + +def _grant(title='grant', code='g', description='lorem ipsum', sort_order=1): + return Grant.objects.create(title=title, code=code, description=description, + sort_order=sort_order) + + +def _grant_report(title='grant report', grant=None, report_type='g', + description='lorem ipsum', due_date=datetime.now(), sort_order=1): + if not grant: + grant = _grant() + return GrantReport.objects.create(grant=grant, title=title, report_type=report_type, + description=description, due_date=due_date, + sort_order=sort_order) + + +class ProjectTestCase(TestCase): + def test_creation(self): + p = _project() + self.assertTrue(isinstance(p, Project)) + self.assertEqual(p.__str__(), p.name) + + def test_uniqueness(self): + p1 = _project() + with transaction.atomic(), self.assertRaises(IntegrityError): + p2 = _project() + p3 = _project(name='abc') + self.assertTrue(isinstance(p3, Project)) + + +class GrantTestCase(TestCase): + def test_creation(self): + g = _grant() + self.assertTrue(isinstance(g, Grant)) + self.assertEqual(g.__str__(), g.title) + + def test_uniqueness(self): + g1 = _grant() + with transaction.atomic(), self.assertRaises(IntegrityError): + g2 = _grant() + g3 = _grant(title='abc') + self.assertTrue(isinstance(g3, Grant)) + + +class GrantReportTestCase(TestCase): + def test_creation(self): + g = _grant_report() + self.assertTrue(isinstance(g, GrantReport)) + self.assertEqual(g.__str__(), g.title) + + def test_uniqueness(self): + g1 = _grant_report() + with transaction.atomic(), self.assertRaises(IntegrityError): + g2 = _grant_report() + g3 = _grant_report(title='abc') + self.assertTrue(isinstance(g3, GrantReport)) diff --git a/ccdb/utils/data.py b/ccdb/utils/data.py index 998f619..7074cb9 100644 --- a/ccdb/utils/data.py +++ b/ccdb/utils/data.py @@ -45,7 +45,7 @@ def _write_url(url, filename): def _get_db0(): - dbfile = 'data/Replica_Hibernators_Back_UAF_Laptop_29_June_2015.sqlite' + dbfile = 'data/CC_Database_020216.sqlite' return setup_sqlite(dbfile) diff --git a/config/settings/local.py b/config/settings/local.py index da25471..7280f1c 100644 --- a/config/settings/local.py +++ b/config/settings/local.py @@ -41,6 +41,10 @@ EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND', MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) INSTALLED_APPS += ('debug_toolbar', ) +# Testing +# ------------------------------------------------------------------------------ +INSTALLED_APPS += ('django_extensions', 'test_without_migrations') + INTERNAL_IPS = ('127.0.0.1', ) DEBUG_TOOLBAR_CONFIG = { @@ -54,6 +58,8 @@ DEBUG_TOOLBAR_CONFIG = { # ------------------------------------------------------------------------------ TEST_RUNNER = 'django.test.runner.DiscoverRunner' +MANIFEST_URL = env('MANIFEST_URL', default=None) +CORS_ORIGIN_ALLOW_ALL = True DJOSER = { 'SITE_NAME': 'CCDB (test)',