Skip to content

Commit

Permalink
Changed the test to track ts on both cases and simplified.
Browse files Browse the repository at this point in the history
  • Loading branch information
TeachMeTW committed Dec 27, 2024
1 parent 16e79e2 commit a58628b
Showing 1 changed file with 13 additions and 74 deletions.
87 changes: 13 additions & 74 deletions emission/tests/analysisTests/intakeTests/TestUserStat.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def tearDown(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.
"""

Expand Down Expand Up @@ -95,9 +95,7 @@ def testGetAndStoreUserStatsDefault(self):
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']}")

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)
Expand Down Expand Up @@ -143,82 +141,23 @@ def testGetAndStoreUserStatsSecondRunNoNewData(self):
f"Expected labeled_trips to remain {initial_labeled_trips}, got {updated_profile['labeled_trips']}"
)


def testGetAndStoreUserStatsNewData(self):
"""
Case (i): Verify stats are updated properly when new data is inserted
from shankari_2015-aug-27 without modifying the original data and the pipeline is rerun.
We then assert the actual number of total trips (e.g., from 8 to 18).
"""
# 1. Retrieve the initial user profile after setUp()
initial_profile = edb.get_profile_db().find_one({"user_id": self.testUUID})
self.assertIsNotNone(initial_profile, "User profile should exist after the first run.")

# 2. Assert that the initial total trips are as expected (8 trips)
expected_initial_trips = 8
self.assertEqual(
initial_profile["total_trips"],
expected_initial_trips,
f"Expected initial total_trips to be {expected_initial_trips}, got {initial_profile['total_trips']}"
)

# Store initial trips count and labeled trips for later comparison
initial_total_trips = initial_profile["total_trips"]
initial_labeled_trips = initial_profile["labeled_trips"]

# 3. Load and prepare new data from shankari_2015-aug-27
new_entries = []
aug27_file_path = "emission/tests/data/real_examples/shankari_2015-aug-27"

try:
with open(aug27_file_path) as fp:
# Load entries using the existing JSON wrapper
aug27_entries = json.load(fp, object_hook=esj.wrapped_object_hook)
for entry in aug27_entries:
# Replace the user_id UUID with self.testUUID
entry['user_id'] = self.testUUID

# Remove the '_id' field to let MongoDB assign a new one
if '_id' in entry:
del entry['_id']

# Append the modified entry to the new_entries list
new_entries.append(entry)

except FileNotFoundError:
self.fail(f"New data file not found at path: {aug27_file_path}")
except json.JSONDecodeError as e:
self.fail(f"JSON decoding failed for file {aug27_file_path}: {e}")

# 4. Insert the new entries into the timeseries collection
if new_entries:
edb.get_timeseries_db().insert_many(new_entries)
else:
self.fail("No new entries were loaded from the new data file.")

# 5. Run the pipeline again to process the newly inserted entries
test_call_ts = time.time()
enac.store_server_api_time(self.testUUID, "test_call_ts", test_call_ts, 69420)
etc.runIntakePipeline(self.testUUID)

# 6. Retrieve the updated user profile after processing new data
updated_profile = edb.get_profile_db().find_one({"user_id": self.testUUID})
self.assertIsNotNone(updated_profile, "Profile should exist after inserting new data.")
# Retrieve the profile from the database
profile = edb.get_profile_db().find_one({"user_id": self.testUUID})

# 7. Assert that the total trips have increased from 8 to 18
expected_final_trips = 18
self.assertEqual(
updated_profile["total_trips"],
expected_final_trips,
f"Expected total_trips to be {expected_final_trips}, got {updated_profile['total_trips']}"
)
# Verify that last_call_ts is updated correctly
expected_last_call_ts = test_call_ts
actual_last_call_ts = profile.get("last_call_ts")

# 8. Ensure that labeled_trips is not less than it was before
self.assertGreaterEqual(
updated_profile["labeled_trips"],
initial_labeled_trips,
f"Expected labeled_trips >= {initial_labeled_trips}, got {updated_profile['labeled_trips']}"
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}"
)


if __name__ == '__main__':
# Configure logging for the test
etc.configLogging()
Expand Down

0 comments on commit a58628b

Please sign in to comment.