def test_should_read_history_from_db(self):
        store = LightsSqliteStore(self.test_db_file)

        history = store.read_history()
        assert_that(history, has_length(1))

        entry = history[0]
        self.assert_entry_correct(entry)
    def test_should_remove_trigger_from_db(self):
        shutil.copyfile(self.test_db_file, self.temp_file.name)

        store = LightsSqliteStore(self.temp_file.name)
        store.remove_trigger(self.trigger.job_id)

        conn = sqlite3.connect(self.temp_file.name)
        cursor = conn.cursor()
        rows = cursor.execute("SELECT * FROM triggers").fetchall()
        assert_that(rows, is_(empty()))

        triggers = store.read_triggers()
        assert_that(triggers, is_(empty()))
    def test_should_read_triggers_from_db(self):
        self.file_has_data(self.test_db_file)
        store = LightsSqliteStore(self.test_db_file)

        triggers = store.read_triggers()
        assert_that(triggers, has_length(1))

        trigger = triggers[0]
        assert_that(trigger.job_id, is_(self.trigger.job_id))
        assert_that(trigger.state, is_(self.trigger.state))
        assert_that(trigger.hour, is_(self.trigger.hour))
        assert_that(trigger.minute, is_(self.trigger.minute))
        assert_that(trigger.repeat_weekday, is_(self.trigger.repeat_weekday))
        assert_that(trigger.repeat_weekend, is_(self.trigger.repeat_weekend))
        assert_that(trigger.next_run_time, is_(None))
    def test_should_add_trigger_to_db(self):
        store = LightsSqliteStore(self.temp_file.name)
        store.add_trigger(self.trigger)

        conn = sqlite3.connect(self.temp_file.name)
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        rows = cursor.execute("SELECT * FROM triggers").fetchall()
        assert_that(rows, has_length(1))

        actual = rows[0]
        assert_that(actual["job_id"], is_(self.trigger.job_id))
        assert_that(actual["state"], is_(self.trigger.state))
        assert_that(actual["hour"], is_(self.trigger.hour))
        assert_that(actual["minute"], is_(self.trigger.minute))
        assert_that(actual["repeat_weekday"], is_(self.trigger.repeat_weekday))
        assert_that(actual["repeat_weekend"], is_(self.trigger.repeat_weekend))
        conn.close()
    def test_should_add_entry_to_history_db(self):
        store = LightsSqliteStore(self.temp_file.name)
        store.add_entry(True, SOURCE)

        conn = sqlite3.connect(self.temp_file.name)
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        rows = cursor.execute("SELECT * FROM history").fetchall()
        assert_that(rows, has_length(1))

        entry = rows[0]
        assert_that(entry["timestamp"], is_(TIMESTAMP))
        assert_that(entry["source"], is_(SOURCE))
        assert_that(entry["state"], is_(True))

        history = store.read_history()
        assert_that(history, has_length(1))

        entry = history[0]
        self.assert_entry_correct(entry)
    def test_should_remove_only_single_trigger(self):
        shutil.copyfile("tests/two_triggers.db", self.temp_file.name)

        store = LightsSqliteStore(self.temp_file.name)

        conn = sqlite3.connect(self.temp_file.name)
        cursor = conn.cursor()
        rows = cursor.execute("SELECT * FROM triggers").fetchall()
        assert_that(rows, has_length(2))

        triggers = store.read_triggers()
        assert_that(triggers, has_length(2))

        store.remove_trigger(self.trigger.job_id)

        rows = cursor.execute("SELECT * FROM triggers").fetchall()
        assert_that(rows, has_length(1))

        triggers = store.read_triggers()
        assert_that(triggers, has_length(1))
    def test_should_return_empty_if_blank_db_file(self):
        store = LightsSqliteStore(self.temp_file.name)

        triggers = store.read_triggers()
        assert_that(triggers, is_(empty()))