From 9255c5db32d20047eacf9899aedb6892bee2b988 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Mon, 6 Jan 2025 09:16:55 -0800 Subject: [PATCH] Added unit tests to verify that the stats are generated in both cases: (i) when there is new data and (ii) when there is no new data. Changed the test to track ts on both cases and simplified. --- .../analysisTests/intakeTests/TestUserStat.py | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/emission/tests/analysisTests/intakeTests/TestUserStat.py b/emission/tests/analysisTests/intakeTests/TestUserStat.py index 207aa0a98..8caafa61b 100644 --- a/emission/tests/analysisTests/intakeTests/TestUserStat.py +++ b/emission/tests/analysisTests/intakeTests/TestUserStat.py @@ -57,11 +57,20 @@ def tearDown(self): edb.get_analysis_timeseries_db().delete_many({"user_id": self.testUUID}) edb.get_profile_db().delete_one({"user_id": self.testUUID}) - def testGetAndStoreUserStats(self): + def testGetAndStoreUserStatsDefault(self): """ - Test get_and_store_user_stats for the user to ensure that user statistics + Case (i): Test get_and_store_user_stats for the user to ensure that user statistics are correctly aggregated and stored in the user profile. """ + # Load in new data + + with open("emission/tests/data/real_examples/shankari_2015-aug-27") as fp: + self.entries = json.load(fp, object_hook=esj.wrapped_object_hook) + + etc.setupRealExampleWithEntries(self) + + # Rerun Pipeline + etc.runIntakePipeline(self.testUUID) # Retrieve the updated user profile from the database profile = edb.get_profile_db().find_one({"user_id": self.testUUID}) @@ -75,7 +84,7 @@ def testGetAndStoreUserStats(self): self.assertIn("pipeline_range", profile, "User profile should contain 'pipeline_range'.") self.assertIn("last_call_ts", profile, "User profile should contain 'last_call_ts'.") - expected_total_trips = 5 + expected_total_trips = 13 expected_labeled_trips = 0 self.assertEqual(profile["total_trips"], expected_total_trips, @@ -89,15 +98,58 @@ def testGetAndStoreUserStats(self): self.assertIn("end_ts", pipeline_range, "Pipeline range should contain 'end_ts'.") expected_start_ts = 1440168891.095 - expected_end_ts = 1440209488.817 + expected_end_ts = 1440729142.709 self.assertEqual(pipeline_range["start_ts"], expected_start_ts, f"Expected start_ts to be {expected_start_ts}, got {pipeline_range['start_ts']}") self.assertEqual(pipeline_range["end_ts"], expected_end_ts, f"Expected end_ts to be {expected_end_ts}, got {pipeline_range['end_ts']}") + + test_call_ts = time.time() + enac.store_server_api_time(self.testUUID, "test_call_ts", test_call_ts, 69420) + etc.runIntakePipeline(self.testUUID) + + # Retrieve the profile from the database + profile = edb.get_profile_db().find_one({"user_id": self.testUUID}) + + # Verify that last_call_ts is updated correctly + expected_last_call_ts = test_call_ts + actual_last_call_ts = profile.get("last_call_ts") + + self.assertEqual( + actual_last_call_ts, + expected_last_call_ts, + f"Expected last_call_ts to be {expected_last_call_ts}, got {actual_last_call_ts}" + ) + + def testGetAndStoreUserStatsSecondRunNoNewData(self): + """ + Case (ii): Verify stats remain unchanged if we run the pipeline again + without adding new data. + """ + # Check stats after the initial run (from setUp()). + initial_profile = edb.get_profile_db().find_one({"user_id": self.testUUID}) + self.assertIsNotNone(initial_profile, "User profile should exist after first run.") + initial_total_trips = initial_profile["total_trips"] + initial_labeled_trips = initial_profile["labeled_trips"] + + # Run the pipeline again, but don't add any new data + etc.runIntakePipeline(self.testUUID) + + # Stats should remain the same + updated_profile = edb.get_profile_db().find_one({"user_id": self.testUUID}) + self.assertIsNotNone(updated_profile, "Profile should still exist.") + self.assertEqual( + updated_profile["total_trips"], + initial_total_trips, + f"Expected total_trips to remain {initial_total_trips}, got {updated_profile['total_trips']}" + ) + self.assertEqual( + updated_profile["labeled_trips"], + initial_labeled_trips, + f"Expected labeled_trips to remain {initial_labeled_trips}, got {updated_profile['labeled_trips']}" + ) - def testLastCall(self): - # Call the function with all required arguments test_call_ts = time.time() enac.store_server_api_time(self.testUUID, "test_call_ts", test_call_ts, 69420) etc.runIntakePipeline(self.testUUID)