Skip to content

Commit

Permalink
more testing e.g. meassure task
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlotan committed Dec 6, 2015
1 parent f6e2acd commit 9970998
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 26 deletions.
3 changes: 0 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 8 additions & 13 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 2 additions & 1 deletion app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
8 changes: 4 additions & 4 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
30 changes: 30 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion tests/test_main.py → tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@


class TestMainHandler(BaseTestCase):
def setUp(self):
super(TestMainHandler, self).setUp()

def test_basic_routes_functioning(self):
with self.c as c:
rv = c.get('/')
self.assertEqual(rv.status_code, 200, 'Unable to access index')
rv = c.get('/login/')
self.assertEqual(rv.status_code, 200, 'Unable to access login')


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

0 comments on commit 9970998

Please sign in to comment.