5
5
import psycopg2 .extras
6
6
from dateutil .relativedelta import relativedelta
7
7
8
+ from microsetta_private_api .exceptions import RepoException
8
9
import microsetta_private_api .model .project as p
9
10
10
11
from werkzeug .exceptions import Unauthorized , NotFound
@@ -323,15 +324,19 @@ def make_tz_datetime(y, m, d):
323
324
"barcode" : test_barcode ,
324
325
"scan_timestamp" : make_tz_datetime (2017 , 7 , 16 ),
325
326
"sample_status" : 'no-registered-account' ,
326
- "technician_notes" : "huh?"
327
+ "technician_notes" : "huh?" ,
328
+ "observations" : [{'observation_id' : None , 'observation' : None ,
329
+ 'category' : None }]
327
330
}
328
331
329
332
second_scan = {
330
333
"barcode_scan_id" : second_scan_id ,
331
334
"barcode" : test_barcode ,
332
335
"scan_timestamp" : make_tz_datetime (2020 , 12 , 4 ),
333
336
"sample_status" : 'sample-is-valid' ,
334
- "technician_notes" : None
337
+ "technician_notes" : None ,
338
+ "observations" : [{'observation_id' : None , 'observation' : None ,
339
+ 'category' : None }]
335
340
}
336
341
try :
337
342
add_dummy_scan (first_scan )
@@ -347,6 +352,7 @@ def make_tz_datetime(y, m, d):
347
352
self .assertGreater (len (diag ['projects_info' ]), 0 )
348
353
self .assertEqual (len (diag ['scans_info' ]), 2 )
349
354
# order matters in the returned vals, so test that
355
+ print (diag ['scans_info' ][0 ], first_scan )
350
356
self .assertEqual (diag ['scans_info' ][0 ], first_scan )
351
357
self .assertEqual (diag ['scans_info' ][1 ], second_scan )
352
358
self .assertEqual (diag ['latest_scan' ], second_scan )
@@ -776,30 +782,127 @@ def test_scan_barcode_success(self):
776
782
# TODO FIXME HACK: Need to build mock barcodes rather than using
777
783
# these fixed ones
778
784
779
- TEST_BARCODE = '000000001 '
785
+ TEST_BARCODE = '000010860 '
780
786
TEST_STATUS = "sample-has-inconsistencies"
781
787
TEST_NOTES = "THIS IS A UNIT TEST"
782
788
admin_repo = AdminRepo (t )
789
+ with t .dict_cursor () as cur :
790
+ cur .execute ("SELECT observation_id "
791
+ "FROM "
792
+ "barcodes.sample_observation_project_associations "
793
+ "WHERE project_id = 1" )
794
+ observation_id = cur .fetchone ()
795
+
796
+ # check that before doing a scan,
797
+ # no scans are recorded for this
798
+ diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
799
+ self .assertEqual (len (diag ['scans_info' ]), 0 )
800
+
801
+ # do a scan
802
+ admin_repo .scan_barcode (
803
+ TEST_BARCODE ,
804
+ {
805
+ "sample_status" : TEST_STATUS ,
806
+ "technician_notes" : TEST_NOTES ,
807
+ "observations" : observation_id
808
+ }
809
+ )
810
+
811
+ # show that now a scan is recorded for this barcode
812
+ diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
813
+ self .assertEqual (len (diag ['scans_info' ]), 1 )
814
+ first_scan = diag ['scans_info' ][0 ]
815
+ first_observation = first_scan ['observations' ][0 ]
816
+ scan_observation_id = first_observation ['observation_id' ]
817
+
818
+ self .assertEqual (first_scan ['technician_notes' ], TEST_NOTES )
819
+ self .assertEqual (first_scan ['sample_status' ], TEST_STATUS )
820
+ self .assertEqual (scan_observation_id , observation_id [0 ])
821
+
822
+ def test_scan_with_no_observations (self ):
823
+ with Transaction () as t :
824
+
825
+ TEST_BARCODE = '000010860'
826
+ TEST_NOTES = "THIS IS A UNIT TEST"
827
+ TEST_STATUS = "sample-has-inconsistencies"
828
+ admin_repo = AdminRepo (t )
783
829
784
830
# check that before doing a scan, no scans are recorded for this
785
831
diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
786
832
self .assertEqual (len (diag ['scans_info' ]), 0 )
787
833
788
- # do a scan
789
834
admin_repo .scan_barcode (
790
835
TEST_BARCODE ,
791
836
{
792
837
"sample_status" : TEST_STATUS ,
793
- "technician_notes" : TEST_NOTES
838
+ "technician_notes" : TEST_NOTES ,
839
+ "observations" : None
794
840
}
795
841
)
796
-
797
- # show that now a scan is recorded for this barcode
798
842
diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
799
- self .assertEqual (len (diag ['scans_info' ]), 1 )
800
843
first_scan = diag ['scans_info' ][0 ]
801
- self .assertEqual (first_scan ['technician_notes' ], TEST_NOTES )
802
- self .assertEqual (first_scan ['sample_status' ], TEST_STATUS )
844
+ first_observation = first_scan ['observations' ][0 ]
845
+ scan_observation = first_observation ['observation' ]
846
+ self .assertEqual (scan_observation , None )
847
+
848
+ def test_scan_with_multiple_observations (self ):
849
+ with Transaction () as t :
850
+
851
+ TEST_BARCODE = '000010860'
852
+ TEST_NOTES = "THIS IS A UNIT TEST"
853
+ TEST_STATUS = "sample-has-inconsistencies"
854
+ admin_repo = AdminRepo (t )
855
+
856
+ with t .dict_cursor () as cur :
857
+ cur .execute ("SELECT observation_id "
858
+ "FROM "
859
+ "barcodes.sample_observation_project_associations "
860
+ "WHERE project_id = 1" )
861
+ rows = cur .fetchmany (2 )
862
+ observation_ids = [row ['observation_id' ] for row in rows ]
863
+
864
+ # check that before doing a scan,
865
+ # no scans are recorded for this
866
+ diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
867
+ self .assertEqual (len (diag ['scans_info' ]), 0 )
868
+
869
+ admin_repo .scan_barcode (
870
+ TEST_BARCODE ,
871
+ {
872
+ "sample_status" : TEST_STATUS ,
873
+ "technician_notes" : TEST_NOTES ,
874
+ "observations" : observation_ids
875
+ }
876
+ )
877
+ diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
878
+ scans = [scan ['observations' ] for scan in diag ['scans_info' ]]
879
+ scans_observation_ids = [obs ['observation_id' ] for scan in
880
+ scans for obs in scan ]
881
+
882
+ self .assertEqual (scans_observation_ids , observation_ids )
883
+
884
+ def test_scan_with_wrong_observation (self ):
885
+ with Transaction () as t :
886
+
887
+ TEST_BARCODE = '000000001'
888
+ TEST_NOTES = "THIS IS A UNIT TEST"
889
+ TEST_STATUS = "sample-has-inconsistencies"
890
+ TEST_OBSERVATIONS = ["ad374d60-466d-4db0-9a91-5e3e8aec7698" ]
891
+ admin_repo = AdminRepo (t )
892
+
893
+ # check that before doing a scan, no scans are recorded for this
894
+ diag = admin_repo .retrieve_diagnostics_by_barcode (TEST_BARCODE )
895
+ self .assertEqual (len (diag ['scans_info' ]), 0 )
896
+
897
+ with self .assertRaises (RepoException ):
898
+ admin_repo .scan_barcode (
899
+ TEST_BARCODE ,
900
+ {
901
+ "sample_status" : TEST_STATUS ,
902
+ "technician_notes" : TEST_NOTES ,
903
+ "observations" : TEST_OBSERVATIONS
904
+ }
905
+ )
803
906
804
907
def test_scan_barcode_error_nonexistent (self ):
805
908
with Transaction () as t :
0 commit comments