def test_insert_new_day_returns_false_on_failure(connection): connection.execute.side_effect = OperationalError() punch = Punch(connection) actual = punch.insert_new_day() connection.commit.assert_not_called() assert actual is False
def test_get_punch_by_id_returns_empty_tuple_on_error(connection): connection.execute.side_effect = OperationalError() punch = Punch(connection) actual = punch.get_punch_by_id(1) connection.execute.assert_called_once() connection.fetchone.assert_not_called() assert actual is ()
def test_get_most_recent_day_returns_empty_tuple_on_error(connection): connection.execute.side_effect = OperationalError() punch = Punch(connection) actual = punch.get_most_recent_day() connection.fetchone.assert_not_called() assert actual is ()
def test_is_work_day_returns_boolean_value(connection, expected): punch = Punch(connection) # Mocking this public function because it is being tested separately, and # we don't want this test dependent on that function. punch.get_most_recent_day = Mock(return_value=(1, datetime.now(), expected)) actual = punch.is_work_day() assert actual is expected
def test_update_is_work_day_returns_false_on_failure(connection): connection.execute.side_effect = OperationalError() punch = Punch(connection) # Do not want this test to depend on get_most_recent_day(), # so it is mocked out, as it is tested separately. punch.get_most_recent_day = Mock(return_value=(1, )) actual = punch.update_is_work_day(True) connection.commit.assert_not_called() assert actual is False
def test_get_most_recent_day_returns_day(connection): # Example of row without punches expected = (1, datetime.now(), True) connection.fetchone.return_value = expected punch = Punch(connection) actual = punch.get_most_recent_day() sql = 'SELECT * FROM punches ORDER BY id DESC LIMIT 1' connection.execute.assert_called_once_with(sql) assert actual is expected
def test_get_punch_by_id_returns_punch(connection): expected = ( 'some', 'tuple', ) connection.fetchone.return_value = expected punch = Punch(connection) actual = punch.get_punch_by_id(1) connection.execute.assert_called_once() connection.fetchone.assert_called_once() assert actual is expected
def test_update_is_work_day_returns_true_on_update(connection, expected_bool): punch = Punch(connection) expected_id = 1 # Do not want this test to depend on get_most_recent_day(), # so it is mocked out, as it is tested separately. punch.get_most_recent_day = Mock(return_value=(expected_id, )) actual = punch.update_is_work_day(expected_bool) connection.execute.assert_called_once_with( 'UPDATE punches SET is_work_day=? WHERE id=?', (expected_bool, expected_id)) assert actual is True
def test_punch_actions(connection, error, action, db_column, expected): id_ = 1 connection.fetchone.return_value = (id_, ) connection.execute.side_effect = [None, error] punch = Punch(connection) punch_time = datetime.now() actions = { 'in': punch.in_, 'start': punch.start, 'end': punch.end, 'out': punch.out, } actual = actions[action](punch_time) if error: connection.commit.assert_not_called() else: connection.commit.assert_called_once() expected_sql = 'UPDATE punches SET %s=? WHERE id=?' % (db_column, ) connection.execute.assert_has_calls([ call('SELECT * FROM punches ORDER BY id DESC LIMIT 1'), call(expected_sql, (punch_time, id_)) ]) connection.fetchone.assert_called_once() assert actual is expected
def main() -> None: # Load up environment configuration load_dotenv(join(dirname(__file__), 'data', '.env')) config = Config() # Connect to the database db = Database(config) # Add the connection to the two models. holiday = Holiday(db) punch = Punch(db) # Set the options to have chrome be headless op = Options() op.add_argument('--headless') # Instantiate the driver driver = Chrome(options=op) driver.implicitly_wait(config.get_implicit_wait()) # Instantiate the pager smtp = SMTP_SSL(GMAIL_DOMAIN) pager = PagerDuty(config, smtp) # Set the args in a dictionary for future use args = { 'config': config, 'driver': driver, 'holiday': holiday, 'pager': pager, 'punch': punch } punch_card_manager = PunchCardManager(args) try: # Start the process manager punch_card_manager.start() except: exception_type, value = exc_info()[:2] pager.alert( 'PROGRAM CRASH, needs restart.\nException - %s\nValue - %s' % (exception_type, value))
def test_insert_new_day_returns_true_on_insert(connection): punch = Punch(connection) actual = punch.insert_new_day() assert actual is True