Example #1
0
    def test_feeding_and_watering(self):
        config = dummy_config()
        config.feeding_module_cronstring = "3h 1,2,3"
        config.feeding_module_activated = False
        config.watering_module_cronstring = "4h 1,2,3"
        config.watering_module_activated = False
        db = database.Database(in_memory=True)
        db.recreate_database()
        db.save_config(config)


        c = cron.FeedingCron(db=db)
        self.assertEqual(c.cron_string, "3h 1,2,3")
        self.assertEqual(c.enabled, False)

        c = cron.WateringCron(db=db)
        self.assertEqual(c.cron_string, "4h 1,2,3")
        self.assertEqual(c.enabled, False)

        db.cursor.execute('DELETE FROM current_config')

        c = cron.WateringCron(db=db)
        self.assertEqual(c.cron_string, constants.WATERING_DEFAULT_CRONSTRING)
        self.assertEqual(c.enabled, True)

        c = cron.FeedingCron(db=db)
        self.assertEqual(c.cron_string, constants.FEEDING_DEFAULT_CRONSTRING)
        self.assertEqual(c.enabled, True)
Example #2
0
    def __init__(self):
        print("[GaiaClient] starting...")

        self.db = database.Database(in_memory=False)
        self.db.recreate_database()
        self.system = system.System()
        self.gpio = gpio.GPIOControl()

        action_handler = grpc_client.ActionHandler(
            shutdown_fn=self.system.shutdown,
            reboot_fn=self.system.reboot,
            feed_fn=self.__feed,
            water_fn=self.__water,
            reset_db_fn=self.db.reset_db,
        )

        self.grpc_client = grpc_client.GRPC_Client(
            remote=constants.GAIA_GRPC_URL,
            use_ssl=constants.GAIA_GRPC_USE_SSL,
            db=self.db,
            action_handler=action_handler)
        self.feeding_cron = cron.FeedingCron(db=self.db)
        self.watering_cron = cron.WateringCron(db=self.db)

        print("[GaiaClient] instantiated, db/system/grpc/cron OK")
        self.gpio.lcd_write("Gaia booting...")
Example #3
0
    def test_cron_empty(self):
        db = database.Database(in_memory=True)
        db.recreate_database()

        # actually, the most important thing is that cron can always call "last_run()" (which internally calls
        # db.read_all_crons()), even if the record is not there, it should pretend and return TIME0 as last run date!

        self.assertEqual(count_records('cron', db.cursor), 0)
        self.assertDictEqual(db.get_all_cron(), dict())

        cron = db.get_cron_perhaps_create('test')

        self.assertEqual(count_records('cron', db.cursor), 1)

        self.assertEqual(cron['cron_string'], '?')
        self.assertEqual(cron['last_run'], constants.CRON_TIME0)

        now = datetime.now().replace(microsecond=0)
        db.save_cron('test', cron_string='12h *', last_run=now)

        cron = db.get_cron_perhaps_create('test')

        self.assertEqual(count_records('cron', db.cursor), 1)

        self.assertEqual(cron['cron_string'], '12h *')
        self.assertEqual(cron['last_run'], now)
        self.assertTrue(isinstance(cron['last_run'], datetime))
Example #4
0
    def test_table_deleted(self):
        config = dummy_config()
        db = database.Database(in_memory=True)
        db.recreate_database()
        db.save_config(config)

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="12h *", db=db)
        self.assertEqual(c.cron_string, "23h 1,3,5")

        db.cursor.execute('DROP TABLE current_config')

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="14h 1,2,3,4,5,6", db=db)
        self.assertEqual(c.cron_string, "14h 1,2,3,4,5,6")
Example #5
0
    def test_watering(self):
        config = dummy_config()
        config.watering_module_cronstring = "23h 1,2,3"
        db = database.Database(in_memory=True)
        db.recreate_database()
        db.save_config(config)

        c = cron.WateringCron(db=db)
        self.assertEqual(c.cron_string, "23h 1,2,3")

        db.cursor.execute('DELETE FROM current_config')

        c = cron.WateringCron(db=db)
        self.assertEqual(c.cron_string, constants.WATERING_DEFAULT_CRONSTRING)
Example #6
0
    def test_table_contains_wrong_value(self):
        config = dummy_config()
        config.watering_module_cronstring = "abc"
        db = database.Database(in_memory=True)
        db.recreate_database()
        db.save_config(config)

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="14h 1,2,3,4,5,6", db=db)
        self.assertEqual(c.cron_string, "14h 1,2,3,4,5,6")

        db.cursor.execute('UPDATE current_config SET watering_module_cronstring="12h *"')

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="14h 1,2,3,4,5,6", db=db)
        self.assertEqual(c.cron_string, "12h *")
Example #7
0
    def test_db_creation(self):
        db = database.Database(in_memory=True)
        self.assertNotEqual(db.db, None)
        self.assertNotEqual(db.cursor, None)

        db.recreate_database()

        self.assertNotEqual(db.db, None)
        self.assertNotEqual(db.cursor, None)

        tables = db.cursor.execute(
            'SELECT name FROM sqlite_master WHERE type=\'table\'').fetchall()
        tables = [x[0] for x in tables]

        for expectedTable in ["current_config", "cron"]:
            if expectedTable not in tables:
                self.fail("Table " + expectedTable + " wasn't created")
Example #8
0
    def test_enabled_disabled(self):
        db = database.Database(in_memory=True)
        db.recreate_database()

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="14h 1,2,3,4,5,6", cron_enabled=True, db=db)
        self.assertEqual(c.cron_string, "14h 1,2,3,4,5,6")
        self.assertEqual(c.enabled, True)
        self.assertTrue("enabled" in str(c), "cron should be enabled")

        config = dummy_config()
        config.watering_module_activated = False
        db.save_config(config)

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="14h 1,2,3,4,5,6", cron_enabled=True, db=db)
        self.assertEqual(c.cron_string, "23h 1,3,5")
        self.assertEqual(c.enabled, False)
        self.assertTrue("disabled" in str(c), "cron should be enabled")
        self.assertFalse(c.should_it_run(None), False)
Example #9
0
    def test_cron_create(self):

        db = database.Database(in_memory=True)

        # cron_name must absolutely be "watering_cron" or "feeding_cron"
        with self.assertRaises(ValueError):
            c = cron.CronWithDefault(cron_name="test", cron_string_default="", db=db)

        with self.assertRaises(ValueError):
            c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="", db=db)

        with self.assertRaises(ValueError):
            c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default=" ", db=db)

        with self.assertRaises(ValueError):
            c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default=None, db=db)

        c = cron.CronWithDefault(cron_name="watering_cron", cron_string_default="12h *", db=db)
        self.assertEqual(c.cron_string, "12h *")
Example #10
0
    def test_config(self):
        db = database.Database(in_memory=True)
        db.recreate_database()

        self.assertEqual(count_records('current_config', db.cursor), 0)
        self.assertEqual(db.get_config(), None)

        config = dummy_config()
        db.save_config(config)

        self.assertEqual(count_records('current_config', db.cursor), 1)
        self.assertNotEqual(db.get_config(), None)

        # only saves the latest
        db.save_config(config)

        self.assertEqual(count_records('current_config', db.cursor), 1)
        self.assertNotEqual(db.get_config(), None)

        config2 = db.get_config()
        self.assertEqual(config2['id'], 2)
        self.assertTrue(isinstance(config2['updated'], datetime))
        self.assertEqual(config2['feeding_module_activated'],
                         config.feeding_module_activated)
        self.assertEqual(config2['watering_module_activated'],
                         config.watering_module_activated)
        self.assertEqual(config2['feeding_module_cronstring'],
                         config.feeding_module_cronstring)
        self.assertEqual(config2['watering_module_cronstring'],
                         config.watering_module_cronstring)
        self.assertEqual(config2['watering_pump_1_duration'],
                         config.watering_pump_1_duration)
        self.assertEqual(config2['watering_pump_2_duration'],
                         config.watering_pump_2_duration)
        self.assertEqual(config2['watering_pump_3_duration'],
                         config.watering_pump_3_duration)
        self.assertEqual(config2['watering_pump_4_duration'],
                         config.watering_pump_4_duration)
Example #11
0
    def test_cron(self):
        db = database.Database(in_memory=True)
        db.recreate_database()

        self.assertEqual(count_records('cron', db.cursor), 0)
        self.assertDictEqual(db.get_all_cron(), dict())

        # insert one
        cron_name = 'test'
        cron_str = '12h *'
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 1)
        all_crons = db.get_all_cron()
        expected = dict()
        expected[cron_name] = dict()
        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        # update one
        cron_str = '15h *'
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 1)
        all_crons = db.get_all_cron()
        expected = dict()
        expected[cron_name] = dict()
        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        # empty the db
        db.delete_all_cron()
        self.assertEqual(count_records('cron', db.cursor), 0)
        self.assertDictEqual(db.get_all_cron(), dict())

        # insert one
        cron_name = 'test'
        cron_str = '12h *'
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 1)
        all_crons = db.get_all_cron()
        expected = dict()
        expected[cron_name] = dict()
        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        # insert another
        cron_name = 'test2'
        cron_str = '02h 1,4,6'
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 2)
        all_crons = db.get_all_cron()

        expected[cron_name] = dict()
        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        # update them individually
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 2)
        all_crons = db.get_all_cron()

        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        # update them individually
        cron_name = 'test'
        now = datetime.now().replace(microsecond=0)
        db.save_cron(cron_name, cron_str, now)

        # assert it's in there
        self.assertEqual(count_records('cron', db.cursor), 2)
        all_crons = db.get_all_cron()

        expected[cron_name]['cron_string'] = cron_str
        expected[cron_name]['last_run'] = now
        self.assertDictEqual(all_crons, expected)

        single_dict = dict()
        single_dict[cron_name] = dict()
        single_dict[cron_name]['cron_string'] = cron_str
        single_dict[cron_name]['last_run'] = now
        self.assertDictEqual(db.get_all_cron(cron_name), single_dict)
Example #12
0
def db_in_memory():
    return database.Database(in_memory=True)
Example #13
0
File: test.py Project: lbarman/gaia
import gaia_client.constants as constants
import gaia_client.cron_with_default as cron
import gaia_client.system as system
import gaia_client.grpc_client as grpc_client
import gaia_client.database as database
from datetime import datetime
import time
import sys

db = database.Database(in_memory=False)
db.recreate_database()


def dummy():
    pass


action_handler = grpc_client.ActionHandler(
    shutdown_fn=dummy,
    reboot_fn=dummy,
    feed_fn=dummy,
    water_fn=dummy,
    reset_db_fn=dummy,
)

grpc_client = grpc_client.GRPC_Client(remote=constants.GAIA_GRPC_URL,
                                      use_ssl=constants.GAIA_GRPC_USE_SSL,
                                      db=db,
                                      action_handler=action_handler)
temperature_sensors = {}
temperature_sensors['t1'] = 9