1
+ from __future__ import unicode_literals , print_function , division , absolute_import
2
+ import unittest
3
+ import uuid
4
+ import logging
5
+ import json
6
+ import os
7
+ import time
8
+ import pandas as pd
9
+
10
+ from builtins import *
11
+ from future import standard_library
12
+ standard_library .install_aliases ()
13
+
14
+ # Standard imports
15
+ import emission .storage .json_wrappers as esj
16
+
17
+ # Our imports
18
+ import emission .core .get_database as edb
19
+ import emission .storage .timeseries .timequery as estt
20
+ import emission .storage .timeseries .abstract_timeseries as esta
21
+ import emission .storage .decorations .analysis_timeseries_queries as esda
22
+ import emission .core .wrapper .user as ecwu
23
+ import emission .net .api .stats as enac
24
+ import emission .pipeline .intake_stage as epi
25
+
26
+ # Test imports
27
+ import emission .tests .common as etc
28
+
29
+
30
+ class TestUserStats (unittest .TestCase ):
31
+ def setUp (self ):
32
+ """
33
+ Set up the test environment by loading real example data for both Android and users.
34
+ """
35
+ # Set up the real example data with entries
36
+ self .testUUID = uuid .uuid4 ()
37
+ with open ("emission/tests/data/real_examples/shankari_2015-aug-21" ) as fp :
38
+ self .entries = json .load (fp , object_hook = esj .wrapped_object_hook )
39
+ # Retrieve the user profile
40
+ etc .setupRealExampleWithEntries (self )
41
+ profile = edb .get_profile_db ().find_one ({"user_id" : self .testUUID })
42
+ if profile is None :
43
+ # Initialize the profile if it does not exist
44
+ edb .get_profile_db ().insert_one ({"user_id" : self .testUUID })
45
+
46
+ #etc.runIntakePipeline(self.testUUID)
47
+ epi .run_intake_pipeline_for_user (self .testUUID , skip_if_no_new_data = False )
48
+ logging .debug ("UUID = %s" % (self .testUUID ))
49
+
50
+ def tearDown (self ):
51
+ """
52
+ Clean up the test environment by removing analysis configuration and deleting test data from databases.
53
+ """
54
+
55
+ edb .get_timeseries_db ().delete_many ({"user_id" : self .testUUID })
56
+ edb .get_pipeline_state_db ().delete_many ({"user_id" : self .testUUID })
57
+ edb .get_analysis_timeseries_db ().delete_many ({"user_id" : self .testUUID })
58
+ edb .get_profile_db ().delete_one ({"user_id" : self .testUUID })
59
+
60
+ def testGetAndStoreUserStats (self ):
61
+ """
62
+ Test get_and_store_user_stats for the user to ensure that user statistics
63
+ are correctly aggregated and stored in the user profile.
64
+ """
65
+
66
+ # Retrieve the updated user profile from the database
67
+ profile = edb .get_profile_db ().find_one ({"user_id" : self .testUUID })
68
+
69
+ # Ensure that the profile exists
70
+ self .assertIsNotNone (profile , "User profile should exist after storing stats." )
71
+
72
+ # Verify that the expected fields are present
73
+ self .assertIn ("total_trips" , profile , "User profile should contain 'total_trips'." )
74
+ self .assertIn ("labeled_trips" , profile , "User profile should contain 'labeled_trips'." )
75
+ self .assertIn ("pipeline_range" , profile , "User profile should contain 'pipeline_range'." )
76
+ self .assertIn ("last_call_ts" , profile , "User profile should contain 'last_call_ts'." )
77
+
78
+ expected_total_trips = 5
79
+ expected_labeled_trips = 0
80
+
81
+ self .assertEqual (profile ["total_trips" ], expected_total_trips ,
82
+ f"Expected total_trips to be { expected_total_trips } , got { profile ['total_trips' ]} " )
83
+ self .assertEqual (profile ["labeled_trips" ], expected_labeled_trips ,
84
+ f"Expected labeled_trips to be { expected_labeled_trips } , got { profile ['labeled_trips' ]} " )
85
+
86
+ # Verify pipeline range
87
+ pipeline_range = profile .get ("pipeline_range" , {})
88
+ self .assertIn ("start_ts" , pipeline_range , "Pipeline range should contain 'start_ts'." )
89
+ self .assertIn ("end_ts" , pipeline_range , "Pipeline range should contain 'end_ts'." )
90
+
91
+ expected_start_ts = 1440168891.095
92
+ expected_end_ts = 1440209488.817
93
+
94
+ self .assertEqual (pipeline_range ["start_ts" ], expected_start_ts ,
95
+ f"Expected start_ts to be { expected_start_ts } , got { pipeline_range ['start_ts' ]} " )
96
+ self .assertEqual (pipeline_range ["end_ts" ], expected_end_ts ,
97
+ f"Expected end_ts to be { expected_end_ts } , got { pipeline_range ['end_ts' ]} " )
98
+
99
+ def testLastCall (self ):
100
+ # Call the function with all required arguments
101
+ test_call_ts = time .time ()
102
+ enac .store_server_api_time (self .testUUID , "test_call_ts" , test_call_ts , 69420 )
103
+ etc .runIntakePipeline (self .testUUID )
104
+
105
+ # Retrieve the profile from the database
106
+ profile = edb .get_profile_db ().find_one ({"user_id" : self .testUUID })
107
+
108
+ # Verify that last_call_ts is updated correctly
109
+ expected_last_call_ts = test_call_ts
110
+ actual_last_call_ts = profile .get ("last_call_ts" )
111
+
112
+ self .assertEqual (
113
+ actual_last_call_ts ,
114
+ expected_last_call_ts ,
115
+ f"Expected last_call_ts to be { expected_last_call_ts } , got { actual_last_call_ts } "
116
+ )
117
+
118
+ if __name__ == '__main__' :
119
+ # Configure logging for the test
120
+ etc .configLogging ()
121
+ unittest .main ()
0 commit comments