88
99from unittest import main
1010from types import GeneratorType
11+ import datetime
1112
1213from labman .db .testing import LabmanTestCase
1314from labman .db .plate import PlateConfiguration , Plate
@@ -92,9 +93,20 @@ def test_search(self):
9293 Plate .search (plate_notes = 'interesting' , well_notes = 'write' ,
9394 query_type = 'UNION' ), [plate22 , plate23 ])
9495
96+ def strip_out_creation_timestamp (self , plates ):
97+ """Kludge to remove creation_timestamp from plate list results"""
98+ obs_ = []
99+ for o in plates :
100+ o = o .copy ()
101+ self .assertIn ('creation_timestamp' , o )
102+ o .pop ('creation_timestamp' )
103+ obs_ .append (o )
104+ return obs_
105+
95106 def test_list_plates (self ):
96107 # Test returning all plates
97- obs = Plate .list_plates ()
108+ obs = self .strip_out_creation_timestamp (Plate .list_plates ())
109+
98110 # We are creating plates below, but at least we know there are 35
99111 # plates in the test database
100112 self .assertGreaterEqual (len (obs ), 35 )
@@ -107,11 +119,12 @@ def test_list_plates(self):
107119 'external_id' : 'Test plate 1' })
108120
109121 # Test returning sample plates
110- obs = Plate .list_plates (['sample' ])
122+ obs = self . strip_out_creation_timestamp ( Plate .list_plates (['sample' ]) )
111123 self .assertGreaterEqual (len (obs ), 4 )
112124 self .assertEqual (obs [0 ], {'plate_id' : 21 ,
113125 'external_id' : 'Test plate 1' })
114- obs = Plate .list_plates (['sample' ], include_study_titles = True )
126+ obs = self .strip_out_creation_timestamp (Plate .list_plates (['sample' ],
127+ include_study_titles = True ))
115128 self .assertGreaterEqual (len (obs ), 4 )
116129 self .assertEqual (
117130 obs [0 ], {'plate_id' : 21 ,
@@ -120,21 +133,22 @@ def test_list_plates(self):
120133 'for Cannabis Soils' ]})
121134
122135 # Test returning gDNA plates
123- obs = Plate .list_plates (['gDNA' ])
136+ obs = self . strip_out_creation_timestamp ( Plate .list_plates (['gDNA' ]) )
124137 self .assertEqual (
125138 obs , [{'plate_id' : 22 ,
126139 'external_id' : 'Test gDNA plate 1' },
127140 {'external_id' : 'Test gDNA plate 2' , 'plate_id' : 28 },
128141 {'external_id' : 'Test gDNA plate 3' , 'plate_id' : 31 },
129142 {'external_id' : 'Test gDNA plate 4' , 'plate_id' : 34 }])
130143
131- obs = Plate .list_plates (['compressed gDNA' ])
144+ obs = self .strip_out_creation_timestamp (
145+ Plate .list_plates (['compressed gDNA' ]))
132146 self .assertEqual (
133147 obs , [{'plate_id' : 24 ,
134148 'external_id' : 'Test compressed gDNA plates 1-4' }])
135149
136150 # Test returning primer plates
137- obs = Plate .list_plates (['primer' ])
151+ obs = self . strip_out_creation_timestamp ( Plate .list_plates (['primer' ]) )
138152 exp = [
139153 {'plate_id' : 11 ,
140154 'external_id' : 'EMP 16S V4 primer plate 1 10/23/2017' },
@@ -157,7 +171,8 @@ def test_list_plates(self):
157171 self .assertEqual (obs , exp )
158172
159173 # Test returning gDNA and compressed gDNA plates
160- obs = Plate .list_plates (['gDNA' , 'compressed gDNA' ])
174+ obs = self .strip_out_creation_timestamp (
175+ Plate .list_plates (['gDNA' , 'compressed gDNA' ]))
161176 self .assertEqual (
162177 obs , [{'plate_id' : 22 ,
163178 'external_id' : 'Test gDNA plate 1' },
@@ -168,25 +183,38 @@ def test_list_plates(self):
168183 {'external_id' : 'Test gDNA plate 4' , 'plate_id' : 34 }
169184 ])
170185
171- obs = Plate .list_plates (['compressed gDNA' , 'normalized gDNA' ])
186+ obs = self .strip_out_creation_timestamp (
187+ Plate .list_plates (['compressed gDNA' , 'normalized gDNA' ]))
172188 self .assertEqual (
173189 obs , [{'plate_id' : 24 ,
174190 'external_id' : 'Test compressed gDNA plates 1-4' },
175191 {'plate_id' : 25 ,
176192 'external_id' : 'Test normalized gDNA plates 1-4' }])
177193
178- obs = Plate .list_plates (['compressed gDNA' , 'normalized gDNA' ],
179- only_quantified = True ,
180- include_study_titles = True )
194+ obs = self .strip_out_creation_timestamp (
195+ Plate .list_plates (['compressed gDNA' , 'normalized gDNA' ],
196+ only_quantified = True ,
197+ include_study_titles = True ))
181198 self .assertEqual (
182199 obs , [{'plate_id' : 24 ,
183200 'external_id' : 'Test compressed gDNA plates 1-4' ,
184201 'studies' : ['Identification of the Microbiomes '
185202 'for Cannabis Soils' ]}])
186203
204+ def test_plate_list_include_timestamp (self ):
205+ # ...limit pathological failures by testing within an hour of creation
206+ exp = datetime .datetime .now ()
207+ exp = str (datetime .datetime (exp .year ,
208+ exp .month ,
209+ exp .day )).split (None , 1 )[0 ]
210+
211+ for i in Plate .list_plates ():
212+ obs = i ['creation_timestamp' ].split (None , 1 )[0 ]
213+ self .assertEqual (obs , exp )
214+
187215 def test_plate_list_discarded_functionality (self ):
188216 # test case based on the test_list_plates
189- obs = Plate .list_plates ()
217+ obs = self . strip_out_creation_timestamp ( Plate .list_plates () )
190218 Plate (21 ).discard = True
191219 self .assertGreaterEqual (len (obs ), 25 )
192220 self .assertEqual (obs [0 ], {'plate_id' : 1 ,
@@ -202,7 +230,8 @@ def test_plate_list_discarded_functionality(self):
202230 Plate (11 ).discarded = True
203231 Plate (12 ).discarded = True
204232 Plate (13 ).discarded = True
205- obs = Plate .list_plates (['primer' ], include_discarded = False )
233+ obs = self .strip_out_creation_timestamp (Plate .list_plates (['primer' ],
234+ include_discarded = False ))
206235
207236 exp = [
208237 {'plate_id' : 14 ,
@@ -220,7 +249,8 @@ def test_plate_list_discarded_functionality(self):
220249 self .assertEqual (obs , exp )
221250
222251 # Test returning discarded primer plates
223- obs = Plate .list_plates (['primer' ], include_discarded = True )
252+ obs = self .strip_out_creation_timestamp (Plate .list_plates (['primer' ],
253+ include_discarded = True ))
224254 exp = [
225255 {'plate_id' : 11 ,
226256 'external_id' : 'EMP 16S V4 primer plate 1 10/23/2017' },
@@ -268,6 +298,14 @@ def test_create(self):
268298 def test_properties (self ):
269299 # Plate 21 - Defined in the test DB
270300 tester = Plate (21 )
301+
302+ obs = tester .creation_timestamp
303+ obs = str (datetime .datetime (obs .year ,
304+ obs .month ,
305+ obs .day ))
306+ exp = datetime .datetime .now ()
307+ exp = str (datetime .datetime (exp .year , exp .month , exp .day ))
308+ self .assertEqual (obs , exp )
271309 self .assertEqual (tester .external_id , 'Test plate 1' )
272310 self .assertEqual (tester .plate_configuration , PlateConfiguration (1 ))
273311 self .assertFalse (tester .discarded )
0 commit comments