21
21
except ImportError :
22
22
import unittest .mock as mock
23
23
24
- from labkey .utils import create_server_context
25
24
from labkey .experiment import load_batch , save_batch , Batch , Run
26
25
from labkey .exceptions import RequestError , QueryNotFoundError , ServerNotFoundError , RequestAuthorizationError
27
- from mock_server_responses import MockLoadBatch , MockSaveBatch
28
26
27
+ from test_utils import MockLabKey , mock_server_context , success_test , throws_error_test
29
28
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 )
34
29
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 }}}
38
34
39
35
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 }
45
40
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
- }
57
41
58
42
assay_id = 12345
59
43
batch_id = 54321
60
- server_context = create_server_context (configs ['server' ], configs ['project_path' ], configs ['context_path' ])
61
44
62
45
63
46
class TestLoadBatch (unittest .TestCase ):
64
47
65
48
def setUp (self ):
66
- self .configs = configs .copy ()
67
- self .service = MockLoadBatch (** self .configs )
49
+ self .service = MockLoadBatch ()
68
50
self .expected_kwargs = {
69
51
'expected_args' : [self .service .get_server_url ()]
70
52
, 'data' : '{"assayId": 12345, "batchId": 54321}'
71
53
, 'headers' : {'Content-type' : 'application/json' , 'Accept' : 'text/plain' }
72
54
}
73
55
74
56
self .args = [
75
- server_context , assay_id , batch_id
57
+ mock_server_context ( self . service ) , assay_id , batch_id
76
58
]
77
59
78
60
def test_success (self ):
79
61
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 )
81
63
82
64
def test_unauthorized (self ):
83
65
test = self
@@ -104,35 +86,34 @@ class TestSaveBatch(unittest.TestCase):
104
86
105
87
def setUp (self ):
106
88
107
- dataRows = []
89
+ data_rows = []
108
90
109
91
# 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'
114
96
115
97
# Generate the Batch object(s)
116
98
batch = Batch ()
117
- batch .runs = [runTest ]
99
+ batch .runs = [run ]
118
100
# batch.name = 'python batch'
119
101
batch .properties ['PropertyName' ] = 'Property Value'
120
102
121
- self .configs = configs .copy ()
122
- self .service = MockSaveBatch (** self .configs )
103
+ self .service = MockSaveBatch ()
123
104
self .expected_kwargs = {
124
105
'expected_args' : [self .service .get_server_url ()]
125
106
, '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"}}]}]}'
126
107
, 'headers' : {'Content-type' : 'application/json' , 'Accept' : 'text/plain' }
127
108
}
128
109
129
110
self .args = [
130
- server_context , assay_id , batch
111
+ mock_server_context ( self . service ) , assay_id , batch
131
112
]
132
113
133
114
def test_success (self ):
134
115
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 )
136
117
137
118
def test_unauthorized (self ):
138
119
test = self
@@ -163,4 +144,4 @@ def suite():
163
144
])
164
145
165
146
if __name__ == '__main__' :
166
- unittest .main ()
147
+ unittest .main ()
0 commit comments