2121except ImportError :
2222 import unittest .mock as mock
2323
24- from labkey .utils import create_server_context
2524from labkey .experiment import load_batch , save_batch , Batch , Run
2625from labkey .exceptions import RequestError , QueryNotFoundError , ServerNotFoundError , RequestAuthorizationError
27- from mock_server_responses import MockLoadBatch , MockSaveBatch
2826
27+ from test_utils import MockLabKey , mock_server_context , success_test , throws_error_test
2928
30- def success_test (test , expected_response , api_method , * args , ** expected_kwargs ):
31- with mock .patch ('labkey.utils.requests.Session.post' ) as mock_post :
32- mock_post .return_value = expected_response
33- resp = api_method (* args )
3429
35- # validate call is made as expected
36- expected_args = expected_kwargs .pop ('expected_args' )
37- mock_post .assert_called_once_with (* expected_args , ** expected_kwargs )
30+ class MockLoadBatch (MockLabKey ):
31+ api = 'getAssayBatch.api'
32+ default_action = 'assay'
33+ default_success_body = {"assayId" : 2809 , "batch" : {"lsid" : "urn:lsid:labkey.com:Experiment.Folder-1721:465ad7db-58d8-1033-a587-7eb0c02c2efe" , "createdBy" : "" , "created" : "2015/10/19 18:21:57" , "name" : "python batch" , "modified" : "2015/10/19 18:21:57" , "modifiedBy" : "" , "comment" : None , "id" : 120 , "runs" : [{"dataOutputs" : [], "dataRows" : [{"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }, {"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }, {"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }], "dataInputs" : [], "created" : "2015/10/19 18:21:57" , "materialInputs" : [{"lsid" : "urn:lsid:labkey.com:AssayRunMaterial.Folder-1721:Unknown" , "role" : "Sample" , "created" : "2015/10/19 18:21:57" , "name" : "Unknown" , "modified" : "2015/10/19 18:21:57" , "id" : 7641 }], "lsid" : "urn:lsid:labkey.com:GeneralAssayRun.Folder-1721:465ad7dd-58d8-1033-a587-7eb0c02c2efe" , "materialOutputs" : [], "createdBy" : "" , "name" : "python upload" , "modified" : "2015/10/19 18:21:57" , "modifiedBy" : "" , "comment" : None , "id" : 1526 , "properties" : {}}], "properties" : {"ParticipantVisitResolver" : None , "TargetStudy" : None }}}
3834
3935
40- def throws_error_test (test , expected_error , expected_response , api_method , * args , ** expected_kwargs ):
41- with mock .patch ('labkey.utils.requests.Session.post' ) as mock_post :
42- with test .assertRaises (expected_error ):
43- mock_post .return_value = expected_response
44- api_method (* args )
36+ class MockSaveBatch (MockLabKey ):
37+ api = 'saveAssayBatch.api'
38+ default_action = 'assay'
39+ default_success_body = {"batches" : [{"lsid" : "urn:lsid:labkey.com:Experiment.Folder-1721:50666e45-609f-1033-ba4a-ca4935e31f28" , "createdBy" : "" , "created" : "2015/10/29 12:17:50" , "name" : "python batch 7" , "modified" : "2015/10/29 12:17:51" , "modifiedBy" : "" , "comment" : None , "id" : 139 , "runs" : [{"dataOutputs" : [], "dataRows" : [{"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }, {"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }, {"Treatment Group" : None , "Start Date" : None , "Height _inches_" : None , "Comments" : None , "Status of Infection" : None , "Country" : None , "Gender" : None , "Group Assignment" : None , "Participant ID" : None , "Date" : None }], "dataInputs" : [], "created" : "2015/10/29 12:17:50" , "materialInputs" : [{"lsid" : "urn:lsid:labkey.com:AssayRunMaterial.Folder-1721:Unknown" , "role" : "Sample" , "created" : "2015/10/19 18:21:57" , "name" : "Unknown" , "modified" : "2015/10/19 18:21:57" , "id" : 7641 }], "lsid" : "urn:lsid:labkey.com:GeneralAssayRun.Folder-1721:50666e47-609f-1033-ba4a-ca4935e31f28" , "materialOutputs" : [], "createdBy" : "" , "name" : "python upload" , "modified" : "2015/10/29 12:17:51" , "modifiedBy" : "" , "comment" : None , "id" : 1673 , "properties" : {}}], "properties" : {"ParticipantVisitResolver" : None , "TargetStudy" : None }}], "assayId" : 2809 }
4540
46- # validate call is made as expected
47- expected_args = expected_kwargs .pop ('expected_args' )
48- mock_post .assert_called_once_with (* expected_args , ** expected_kwargs )
49-
50-
51- configs = {
52- 'protocol' : 'https://'
53- , 'server' : 'my_testServer:8080'
54- , 'context_path' : 'testPath'
55- , 'project_path' : 'testProject/subfolder'
56- }
5741
5842assay_id = 12345
5943batch_id = 54321
60- server_context = create_server_context (configs ['server' ], configs ['project_path' ], configs ['context_path' ])
6144
6245
6346class TestLoadBatch (unittest .TestCase ):
6447
6548 def setUp (self ):
66- self .configs = configs .copy ()
67- self .service = MockLoadBatch (** self .configs )
49+ self .service = MockLoadBatch ()
6850 self .expected_kwargs = {
6951 'expected_args' : [self .service .get_server_url ()]
7052 , 'data' : '{"assayId": 12345, "batchId": 54321}'
7153 , 'headers' : {'Content-type' : 'application/json' , 'Accept' : 'text/plain' }
7254 }
7355
7456 self .args = [
75- server_context , assay_id , batch_id
57+ mock_server_context ( self . service ) , assay_id , batch_id
7658 ]
7759
7860 def test_success (self ):
7961 test = self
80- success_test (test , self .service .get_successful_response (), load_batch , * self .args , ** self .expected_kwargs )
62+ success_test (test , self .service .get_successful_response (), load_batch , False , * self .args , ** self .expected_kwargs )
8163
8264 def test_unauthorized (self ):
8365 test = self
@@ -104,35 +86,34 @@ class TestSaveBatch(unittest.TestCase):
10486
10587 def setUp (self ):
10688
107- dataRows = []
89+ data_rows = []
10890
10991 # Generate the Run object(s)
110- runTest = Run ()
111- runTest .name = 'python upload'
112- runTest .data_rows = dataRows
113- runTest .properties ['RunFieldName' ] = 'Run Field Value'
92+ run = Run ()
93+ run .name = 'python upload'
94+ run .data_rows = data_rows
95+ run .properties ['RunFieldName' ] = 'Run Field Value'
11496
11597 # Generate the Batch object(s)
11698 batch = Batch ()
117- batch .runs = [runTest ]
99+ batch .runs = [run ]
118100 # batch.name = 'python batch'
119101 batch .properties ['PropertyName' ] = 'Property Value'
120102
121- self .configs = configs .copy ()
122- self .service = MockSaveBatch (** self .configs )
103+ self .service = MockSaveBatch ()
123104 self .expected_kwargs = {
124105 'expected_args' : [self .service .get_server_url ()]
125106 , 'data' : '{"assayId": 12345, "batches": [{"batchProtocolId": 0, "comment": null, "created": null, "createdBy": null, "lsid": null, "modified": null, "modifiedBy": null, "name": null, "properties": {"PropertyName": "Property Value"}, "runs": [{"comment": null, "created": null, "createdBy": null, "dataInputs": [], "dataRows": [], "experiments": [], "filePathRoot": null, "lsid": null, "materialInputs": [], "materialOutputs": [], "modified": null, "modifiedBy": null, "name": "python upload", "properties": {"RunFieldName": "Run Field Value"}}]}]}'
126107 , 'headers' : {'Content-type' : 'application/json' , 'Accept' : 'text/plain' }
127108 }
128109
129110 self .args = [
130- server_context , assay_id , batch
111+ mock_server_context ( self . service ) , assay_id , batch
131112 ]
132113
133114 def test_success (self ):
134115 test = self
135- success_test (test , self .service .get_successful_response (), save_batch , * self .args , ** self .expected_kwargs )
116+ success_test (test , self .service .get_successful_response (), save_batch , False , * self .args , ** self .expected_kwargs )
136117
137118 def test_unauthorized (self ):
138119 test = self
@@ -163,4 +144,4 @@ def suite():
163144 ])
164145
165146if __name__ == '__main__' :
166- unittest .main ()
147+ unittest .main ()
0 commit comments