diff --git a/app/__init__.py b/app/__init__.py index 174674c..a157a64 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -6,9 +6,6 @@ from app.config import config -""" - Logging configuration -""" logging.basicConfig(format='%(asctime)s:%(levelname)s:%(name)s:%(message)s') logging.getLogger().setLevel(logging.ERROR) diff --git a/app/models.py b/app/models.py index d01b7be..7f0c2a7 100644 --- a/app/models.py +++ b/app/models.py @@ -1,24 +1,19 @@ from flask.ext.appbuilder import Model from flask.ext.appbuilder.models.mixins import AuditMixin, FileColumn, ImageColumn -from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, REAL, Date +from sqlalchemy import Column, Integer, \ + String, ForeignKey, DateTime, \ + Boolean, REAL, Date from sqlalchemy.orm import relationship from sqlalchemy.sql import func from app import appbuilder, db - -""" - -You can use the extra Flask-AppBuilder fields and Mixin's - -AuditMixin will add automatic timestamp of created and modified by who - - -""" +from flask import current_app def new_event(text): - print(text) - db.session.add(EventLog(text=text)) - db.session.commit() + if not current_app.config['TESTING']: + print(text) + db.session.add(EventLog(text=text)) + db.session.commit() class EventLog(Model): diff --git a/app/tasks.py b/app/tasks.py index 39885be..147aef9 100644 --- a/app/tasks.py +++ b/app/tasks.py @@ -3,6 +3,7 @@ from app.models import LightDevice, new_event, FlowerData, WaterDevice, GrowSession from app import appbuilder, db from flask import current_app +import logging import urllib import urllib2 @@ -31,7 +32,7 @@ def meassure(): for water_device in flower_device.grow_session.water_devices: if data['Water'] < water_device.water_threshhold: # Have to water. start_water(water_device.id) - print("Meassure completed") + logging.getLogger().warning("Meassure completed") def switch_light(light_id, on): diff --git a/app/views.py b/app/views.py index 400973a..1ded9f2 100644 --- a/app/views.py +++ b/app/views.py @@ -34,7 +34,7 @@ def view(self): from app.hardware import remote_socket remote_socket.switch([1, 0, 0, 1, 1], 1, False) - return "bla" + return "bla" class EventLogModelView(ModelView): @@ -185,4 +185,3 @@ class FlowerDataChartView(DirectByChartView): category_icon="fa-envelope") appbuilder.add_view(JobsView, "Jobs", category="View") appbuilder.add_view(SocketManualView, "SocketManual", category="View") -db.create_all() diff --git a/manage.py b/manage.py index 85f55eb..4c1df96 100644 --- a/manage.py +++ b/manage.py @@ -1,8 +1,8 @@ import os -if os.path.exists('/.env'): +if os.path.exists('.env'): print('Importing environment from .env...') - for line in open('/.env'): + for line in open('.env'): var = line.strip().split('=') if len(var) == 2: os.environ[var[0]] = var[1] diff --git a/tests/base.py b/tests/base.py index 276b874..7b9c908 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,7 +1,7 @@ import unittest from app.config import config -from app import app, db +from app import app, db, appbuilder class BaseTestCase(unittest.TestCase): @@ -10,8 +10,8 @@ def setUp(self): app.config.from_object(config['testing']) self.app = app self.c = self.app.test_client() - db.create_all() + self.db = db + self.db.create_all() def tearDown(self): - db.session.remove() - db.drop_all() + pass diff --git a/tests/test_tasks.py b/tests/test_tasks.py new file mode 100644 index 0000000..174f506 --- /dev/null +++ b/tests/test_tasks.py @@ -0,0 +1,30 @@ +import unittest +from base import BaseTestCase +from app.models import GrowSession, FlowerDevice, FlowerData +from app.tasks import meassure +from datetime import datetime + + +class TestTaskHandler(BaseTestCase): + def setUp(self): + super(TestTaskHandler, self).setUp() + self.db.session.add(GrowSession(name="grow_session", start_date=datetime.now())) + grow_session = self.db.session.query(GrowSession).first() + self.db.session.add(FlowerDevice(name="flower", + mac="A0:14:3D:08:B4:90", + grow_session_id=grow_session.id)) + self.db.session.commit() + + def tearDown(self): + super(TestTaskHandler, self).tearDown() + self.db.session.query(GrowSession).delete() + self.db.session.query(FlowerDevice).delete() + self.db.session.commit() + + def test_meassure_functioning(self): + data_points_before = len(self.db.session.query(FlowerData).all()) + meassure() + data_points_after = len(self.db.session.query(FlowerData).all()) + self.assertEqual(data_points_before, data_points_after - 1, "Wrong number of insertions in meassure") +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_main.py b/tests/test_views.py similarity index 86% rename from tests/test_main.py rename to tests/test_views.py index 15f236e..c92f20e 100644 --- a/tests/test_main.py +++ b/tests/test_views.py @@ -3,6 +3,9 @@ class TestMainHandler(BaseTestCase): + def setUp(self): + super(TestMainHandler, self).setUp() + def test_basic_routes_functioning(self): with self.c as c: rv = c.get('/') @@ -10,6 +13,5 @@ def test_basic_routes_functioning(self): rv = c.get('/login/') self.assertEqual(rv.status_code, 200, 'Unable to access login') - if __name__ == '__main__': unittest.main()