@@ -75,7 +75,8 @@ def setUp(self):
75
75
'receive_processing_job_emails' : True ,
76
76
'social_orcid' : None ,
77
77
'social_researchgate' : None ,
78
- 'social_googlescholar' : None
78
+ 'social_googlescholar' : None ,
79
+ 'creation_timestamp' : datetime (2015 , 12 , 3 , 13 , 52 , 42 , 751331 )
79
80
}
80
81
81
82
def tearDown (self ):
@@ -88,7 +89,22 @@ def test_instantiate_unknown_user(self):
88
89
with self .assertRaises (qdb .exceptions .QiitaDBUnknownIDError ):
89
90
qdb .
user .
User (
'[email protected] ' )
90
91
91
- def _check_correct_info (self , obs , exp ):
92
+ def _check_correct_info (self , obs , exp , ts_before = None ):
93
+ """Compares info dict of user with special handling of specific keys.
94
+
95
+ Parameters
96
+ ----------
97
+ obs : dict
98
+ Observed user info dictionary.
99
+ exp : dict
100
+ Expected user info dictionary.
101
+ ts_before : datetime.datetime or None
102
+ User.create records the creation timestamp through SQL's NOW().
103
+ Since it is set by the database to the microsecond, we can't
104
+ predict it a priori and therefore simply record timestamp before
105
+ execution of user.create() and compare the relation.
106
+ The DB creation_timestamp column is optional, i.e. can be None.
107
+ """
92
108
self .assertEqual (set (exp .keys ()), set (obs .keys ()))
93
109
for key in exp :
94
110
# user_verify_code and password seed randomly generated so just
@@ -97,10 +113,14 @@ def _check_correct_info(self, obs, exp):
97
113
self .assertEqual (len (obs [key ]), 20 )
98
114
elif key == "password" :
99
115
self .assertEqual (len (obs [key ]), 60 )
116
+ elif key == "creation_timestamp" :
117
+ self .assertTrue (((exp [key ] is None ) and (obs [key ] is None ))
118
+ or (ts_before <= exp [key ]))
100
119
else :
101
120
self .assertEqual (obs [key ], exp [key ])
102
121
103
122
def test_create_user (self ):
123
+ before = datetime .now ()
104
124
user = qdb .
user .
User .
create (
'[email protected] ' ,
'password' )
105
125
106
126
# adding a couple of messages
@@ -131,8 +151,9 @@ def test_create_user(self):
131
151
132
152
'social_orcid' : None ,
133
153
'social_researchgate' : None ,
134
- 'social_googlescholar' : None }
135
- self ._check_correct_info (obs , exp )
154
+ 'social_googlescholar' : None ,
155
+ 'creation_timestamp' : datetime .now ()}
156
+ self ._check_correct_info (obs , exp , before )
136
157
137
158
# Make sure new system messages are linked to user
138
159
sql = """SELECT message_id FROM qiita.message_user
@@ -146,6 +167,7 @@ def test_create_user(self):
146
167
qdb .util .clear_system_messages ()
147
168
148
169
def test_create_user_info (self ):
170
+ before = datetime .now ()
149
171
user = qdb .
user .
User .
create (
'[email protected] ' ,
'password' ,
150
172
self .userinfo )
151
173
self .
assertEqual (
user .
id ,
'[email protected] ' )
@@ -171,8 +193,9 @@ def test_create_user_info(self):
171
193
172
194
'social_orcid' : None ,
173
195
'social_researchgate' : None ,
174
- 'social_googlescholar' : None }
175
- self ._check_correct_info (obs , exp )
196
+ 'social_googlescholar' : None ,
197
+ 'creation_timestamp' : datetime .now ()}
198
+ self ._check_correct_info (obs , exp , before )
176
199
177
200
def test_create_user_column_not_allowed (self ):
178
201
self .userinfo ["email" ] = "FAIL"
@@ -241,8 +264,20 @@ def test_get_info(self):
241
264
'phone' : '222-444-6789' ,
242
265
'social_orcid' : None ,
243
266
'social_researchgate' : None ,
244
- 'social_googlescholar' : None
267
+ 'social_googlescholar' : None ,
268
+ 'creation_timestamp' : datetime (2015 , 12 , 3 , 13 , 52 , 42 , 751331 )
245
269
}
270
+
271
+ # test database is re-populated during testing several times.
272
+ # Creation_timestamp depends on the percise timing of the repopulation,
273
+ # i.e. we cannot predict its value. We just test that this date should
274
+ # be within an hour and now. For the remainder of tests, we update
275
+ # our expectation.
276
+ self .assertTrue (datetime .now () - timedelta (hours = 1 ) <
277
+ self .user .info ['creation_timestamp' ] <
278
+ datetime .now ())
279
+ expinfo ['creation_timestamp' ] = self .user .info ['creation_timestamp' ]
280
+
246
281
self .assertEqual (self .user .info , expinfo )
247
282
248
283
def test_set_info (self ):
0 commit comments