def test_get_habits_with_period(self): # Arrange connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) # Assert self.assertEqual(habit_collection.get_habits_with_period(3), "Workout") self.assertEqual(habit_collection.get_habits_with_period(1), "Learn")
def test_get_current_streak(self): connection = connect_to_database("testing.db") test_habit_1 = Habit("Workout", 4, datetime.now()) test_habit_2 = Habit("Wash dishes", 4, datetime.now()) test_habit_3 = Habit("test_habit_3", 4, datetime.now()) self.assertEqual(test_habit_1.get_overall_longest_streak(connection), 9) self.assertEqual(test_habit_2.get_overall_longest_streak(connection), 13) self.assertEqual(test_habit_3.get_overall_longest_streak(connection), 0) close_connection_to_database(connection)
def test_delete_habit_in_database(self): connection = connect_to_database("testing.db") test_habit_1 = Habit("test_habit_delete", 4, datetime.now()) test_habit_1.create_habit_in_database(connection) test_habit_2 = Habit("Call sister", 4, datetime.now()) # Assert self.assertEqual(test_habit_1.delete_habit_in_database(connection), True) self.assertEqual(test_habit_2.delete_habit_in_database(connection), False) close_connection_to_database(connection)
def test_create_habit_in_database(self): connection = connect_to_database("testing.db") test_habit_1 = Habit("test_habit_create", 4, datetime.now()) test_habit_2 = Habit("Call mum", 4, datetime.now()) self.assertEqual(test_habit_1.create_habit_in_database(connection), True) self.assertEqual(test_habit_2.create_habit_in_database(connection), False) # Cleanup test_habit_1.delete_habit_in_database(connection) close_connection_to_database(connection)
def test_delete_habit(self): connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) self.assertEqual( delete_habit(connection, habit_collection, "Call son"), False) self.assertEqual( delete_habit(connection, habit_collection, "Call dad"), True) close_connection_to_database(connection)
def test_get_current_longest_streak(self): # Arrange connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) # Assert habit_name, habit_streak = habit_collection.get_current_longest_streak( connection) self.assertEqual(habit_name, "Learn") self.assertEqual(habit_streak, 29)
def prepare_connection(db_name): """ :param db_name: name of the database :return: connection object Removes existing database and initialize a new one """ if utils.check_file_existing(db_name): os.remove(db_name) utils.init_sqlite_table(db_name) connection = utils.connect_to_database(db_name) return connection
def test_get_tracked_habits(self): # Arrange connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) active_habits = """Workout Learn Wash dishes Change bedding Call mum""" # Assert self.assertEqual(habit_collection.get_tracked_habits(), active_habits)
def test_get_longest_streak_for_habit(self): # Arrange connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) # Assert self.assertEqual( habit_collection.get_longest_streak_for_habit( "Workout", connection), 9) self.assertEqual( habit_collection.get_longest_streak_for_habit( "Wash dishes", connection), 13)
def main(): database_name = "habits.db" # Initialize database when database doesn't exist if not check_file_existing(database_name): init_sqlite_table(database_name) # Get DB connection connection = connect_to_database(database_name) # Start program display_title_bar() while True: user_choice = get_main_user_choice() if not user_choice == "exit": evaluate_main_user_choice(user_choice, connection) else: break
def test_confirm_task(self): connection = connect_to_database("testing.db") # First test case = new habit # Create values test_habit_1 = Habit("test_habit_ct", 4, datetime.now()) test_habit_1.create_habit_in_database(connection) # Test first_value = test_habit_1.get_current_streak(connection) test_habit_1.confirm_task(connection) self.assertEqual(test_habit_1.get_current_streak(connection), first_value + 1) test_habit_1.confirm_task(connection) self.assertEqual(test_habit_1.get_current_streak(connection), first_value + 2) # Second test case = existing habit test_habit_2 = Habit("Learn", 1, datetime.now()) first_value = test_habit_2.get_current_streak(connection) test_habit_2.confirm_task(connection) self.assertEqual(test_habit_2.get_current_streak(connection), first_value + 1) test_habit_2.confirm_task(connection) self.assertEqual(test_habit_2.get_current_streak(connection), first_value + 2) # Cleanup test_habit_1.delete_habit_in_database(connection) close_connection_to_database(connection)
def test_get_open_tasks(self): # Arrange connection = connect_to_database("testing.db") habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) open_tasks_1 = """Workout Wash dishes Change bedding Call mum""" open_tasks_2 = """Workout Wash dishes Change bedding Call mum test_habit_open""" open_tasks_3 = """Workout Wash dishes Change bedding test_habit_open""" # Assert 1 self.assertEqual(habit_collection.get_open_tasks(), open_tasks_1) # Arrange for 2 create_new_habit(connection, "test_habit_open", 1, datetime.now() - timedelta(days=2)) habit_list = get_all_habits(connection) habit_collection = HabitCollection(habit_list) # Assert for 2 self.assertEqual(habit_collection.get_open_tasks(), open_tasks_2) # Arrange for 3 call_mum_habit = habit_collection._get_habit_by_name("Call mum") call_mum_habit.confirm_task(connection) # Assert for 3 self.assertEqual(habit_collection.get_open_tasks(), open_tasks_3) # Close open_habit = habit_collection._get_habit_by_name("test_habit_open") open_habit.delete_habit_in_database(connection) close_connection_to_database(connection)
def test_create_new_habit(self): connection = connect_to_database("testing.db") self.assertEqual(create_new_habit(connection, "Call mum", 5), False) self.assertEqual(create_new_habit(connection, "Call dad", 5), True) close_connection_to_database(connection)