import sys
import os
PROJECT_HOME = os.path.abspath(
    os.path.join(os.path.dirname(__file__), '../../../')
)
sys.path.append(PROJECT_HOME)
import unittest
from flask.ext.testing import TestCase
from mc.app import create_app
from mc.models import db, Commit, Build

class TestDatabase(TestCase):
    """
    Test flask-sqlalchemy database operations
    """
    def create_app(self):
        """
        Called once at the beginning of the tests; must return the application
        instance
        :return: flask.Flask application instance
        """
        _app = create_app()
        # Override whatever database uri is in the config for tests;
        # Use an in-memory sqlite database to ensure that no production data
        # are touched
        _app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"
        return _app

    def setUp(self):
        """
        setUp and tearDown are run at the start of each test; ensure
        that a fresh database is used for each test.
        """
        db.create_all()


    def tearDown(self):
        """
        setUp and tearDown are run at the start of each test; ensure
        that a fresh database is used for each test.
        """
        db.session.remove()
        db.drop_all()

    def test_commits(self):
        """
        CRUD on Commits
        """

        # Create
        commit = Commit(commit_hash='test-hash')
        db.session.add(commit)
        db.session.commit()

        # Read
        c = Commit.query.first()
        self.assertEqual(c, commit)

        # Update
        c.commit_hash = 'mutated'
        db.session.commit()
        self.assertEqual(Commit.query.first().commit_hash, 'mutated')

        # Delete
        c = Commit.query.first()
        db.session.delete(c)
        db.session.commit()
        self.assertIsNone(Commit.query.first())

if __name__ == '__main__':
    unittest.main(verbosity=2)


@@ -275,16 +275,14 @@
 Test
-Database
+Models
 (Tes
@@ -1258,17 +1258,22 @@
 t_commit
-s
+_model
 (self):%0A
@@ -1304,15 +1304,21 @@
  on 
+models.
 Commit
-s
 %0A   
@@ -1844,16 +1844,803 @@
 rst())%0A%0A
+    def test_build_model(self):%0A        %22%22%22%0A        CRUD on models.Build%0A        %22%22%22%0A%0A        # Create%0A        commit = Commit(commit_hash='test-hash')%0A        db.session.add(commit)%0A        build = Build(commit=commit)%0A        db.session.commit()%0A%0A        # Read%0A        b = build.query.first()%0A        self.assertEqual(b, build)%0A        self.assertEqual(b.commit, commit)%0A%0A        # Update%0A        b.no_cache = True%0A        b.commit = Commit(commit_hash='mutated')%0A        db.session.commit()%0A        self.assertEqual(Build.query.first().no_cache, True)%0A        self.assertEqual(Build.query.first().commit.commit_hash, 'mutated')%0A%0A        # Delete%0A        b = Build.query.first()%0A        db.session.delete(b)%0A        db.session.commit()%0A        self.assertIsNone(Build.query.first())%0A%0A
 if __nam

implement models.Build