Source

audrid / tests / test_models.py

Full commit
# coding: utf-8

from audrid.models import (
    Audit,
    Document, 
    Exam, 
    Log,
    User,
)
from audrid import (app, db, utils)
from audrid import validators as V
from sqlalchemy.exc import IntegrityError
from voluptuous import InvalidList
from werkzeug.exceptions import NotFound
import unittest
import json

class ModelTests(unittest.TestCase):

    def setUp(self):
        app.config['TESTING'] = True
        app.config['CSRF_ENABLED'] = False
        # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
        app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://audrid:audrid@localhost/audrid'
        self.app = app.test_client()
        db.drop_all(app=app)
        db.create_all()

        admin = User(username='admin', email='martin.czygan@gmail.com', password='admin', group='admin')
        guest = User(username='guest', email='guest@example.com', password='guest')
        db.session.add_all([admin, guest])
        db.session.commit()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

    def test_basic(self):
        rid = utils.random_id()
        pool = V.pool({"id" : rid, "tasks" : []})
        doc = Document(id=rid, body=json.dumps(pool), type='pool')
        exam = Exam(name="Test Exam %s" % rid, pool_id=rid)

        db.session.add(doc)
        db.session.commit()
        db.session.add(exam)
        db.session.commit()

        rid = utils.random_id()
        trial = V.trial({"user" : 2, "pool" : pool, "id" : rid})
        doc = Document(id=rid, body=json.dumps(trial), type='trial')
        audit = Audit(user_id=2, exam_id=exam.id, trial_id=rid)

        db.session.add(doc)
        db.session.commit()
        db.session.add(audit)
        db.session.commit()        

    def test_can_create_a_document(self):
        rid = utils.random_id()
        doc = Document(id=rid, body=json.dumps({}), type='empty')
        self.assertIsNotNone(doc)
        db.session.add(doc)
        db.session.commit()        

    def test_cannot_create_docs_with_two_ids(self):
        rid = utils.random_id()
        doc1 = Document(id=rid, body=json.dumps({}), type='empty')
        doc2 = Document(id=rid, body=json.dumps({}), type='empty')
        db.session.add(doc1)
        db.session.commit()        
        db.session.add(doc2)
        with self.assertRaises(IntegrityError):
            db.session.commit()
        
    # def test_pool_helper(self):
    #     self.assertEqual(0, Log.query.count())
    #     t = { 
    #             "kind" : u"cloze", 
    #             "id" : utils.random_id(),
    #             "task" : u"Please fill out...",
    #             "text" : u"Hello World this is a test.",
    #             "answer" : {},
    #             "gaps" : {}
    #     }
        
    #     pid = utils.random_id()

    #     create_pool(pool={ "id" : pid, "tasks" : [t]}, user_id=1)
    #     self.assertEqual(1, Log.query.count())

    #     u = { 
    #             "kind" : u"text", 
    #             "id" : utils.random_id(),
    #             "task" : u"Please fill out...",
    #             "answer" : u""
    #     }
        
    #     update_pool(pool={ "id" : pid, "tasks" : [t, u] }, user_id=1)
    #     self.assertEqual(2, Log.query.count())

    #     update_pool(pool={ "id" : pid, "tasks" : [u] }, user_id=1)
    #     self.assertEqual(3, Log.query.count())
    #     self.assertFalse(Document.query.filter_by(id=pid).first().deleted)

    #     with self.assertRaises(InvalidList):
    #         update_pool(pool={ "tasks" : [u] }, user_id=1)

    #     delete_pool(pid=pid, user_id=1)
    #     self.assertEqual(4, Log.query.count())
    #     self.assertEqual(4, Log.query.filter_by(document_id=pid).count())
    #     self.assertTrue(Document.query.filter_by(id=pid).first().deleted)

    #     with self.assertRaises(NotFound):
    #         delete_pool(pid=pid, user_id=1)

    #     self.assertEqual(4, Log.query.count())

    # def test_audit_helper(self):
    #     pass