|
25 | 25 | import unittest.mock as mock |
26 | 26 |
|
27 | 27 | from labkey import utils |
28 | | -from labkey.domain import create, Domain, drop, get, infer_fields, save |
| 28 | +from labkey.domain import create, conditional_format, Domain, drop, encode_conditional_format_filter, \ |
| 29 | + get, infer_fields, save |
29 | 30 | from labkey.exceptions import RequestAuthorizationError |
| 31 | +from labkey.query import QueryFilter |
30 | 32 |
|
31 | | -from .utilities import MockLabKey, mock_server_context, success_test, success_test_get, throws_error_test, throws_error_test_get |
| 33 | +from .utilities import MockLabKey, mock_server_context, success_test, success_test_get, throws_error_test, \ |
| 34 | + throws_error_test_get |
32 | 35 |
|
33 | 36 |
|
34 | 37 | domain_controller = 'property' |
@@ -244,14 +247,143 @@ def test_unauthorized(self): |
244 | 247 | save, *self.args, **self.expected_kwargs) |
245 | 248 |
|
246 | 249 |
|
| 250 | +class TestConditionalFormatCreate(unittest.TestCase): |
| 251 | + |
| 252 | + def setUp(self): |
| 253 | + |
| 254 | + self.domain_definition = { |
| 255 | + 'kind': 'IntList', |
| 256 | + 'domainDesign': { |
| 257 | + 'name': 'TheTestList_cf', |
| 258 | + 'fields': [{ |
| 259 | + 'name': 'theKey', |
| 260 | + 'rangeURI': 'int', |
| 261 | + 'conditionalFormats': [{ |
| 262 | + 'filter': encode_conditional_format_filter(QueryFilter('theKey', 500)), |
| 263 | + 'textColor': 'f44e3b', |
| 264 | + 'backgroundColor': 'fcba03', |
| 265 | + 'bold': True, |
| 266 | + 'italic': True, |
| 267 | + 'strikethrough': False |
| 268 | + }] |
| 269 | + }] |
| 270 | + }, |
| 271 | + } |
| 272 | + |
| 273 | + class MockCreate(MockLabKey): |
| 274 | + api = 'createDomain.api' |
| 275 | + default_action = domain_controller |
| 276 | + default_success_body = self.domain_definition |
| 277 | + |
| 278 | + self.service = MockCreate() |
| 279 | + |
| 280 | + self.expected_kwargs = { |
| 281 | + 'expected_args': [self.service.get_server_url()], |
| 282 | + 'data': json.dumps(self.domain_definition), |
| 283 | + 'headers': {'Content-Type': 'application/json'}, |
| 284 | + 'timeout': 300 |
| 285 | + } |
| 286 | + |
| 287 | + self.args = [ |
| 288 | + mock_server_context(self.service), self.domain_definition |
| 289 | + ] |
| 290 | + |
| 291 | + def test_success(self): |
| 292 | + test = self |
| 293 | + success_test(test, self.service.get_successful_response(), |
| 294 | + create, False, *self.args, **self.expected_kwargs) |
| 295 | + |
| 296 | + def test_unauthorized(self): |
| 297 | + test = self |
| 298 | + throws_error_test(test, RequestAuthorizationError, self.service.get_unauthorized_response(), |
| 299 | + create, *self.args, **self.expected_kwargs) |
| 300 | + |
| 301 | + |
| 302 | +class TestConditionalFormatSave(unittest.TestCase): |
| 303 | + |
| 304 | + schema_name = 'lists' |
| 305 | + query_name = 'TheTestList_cf' |
| 306 | + |
| 307 | + def setUp(self): |
| 308 | + self.test_domain = Domain(**{ |
| 309 | + 'container': 'TestContainer', |
| 310 | + 'description': 'A Test Domain', |
| 311 | + 'domain_id': 5314, |
| 312 | + 'fields': [{ |
| 313 | + 'name': 'theKey', |
| 314 | + 'rangeURI': 'int' |
| 315 | + }] |
| 316 | + }) |
| 317 | + |
| 318 | + self.test_domain.fields[0].conditional_formats = [ |
| 319 | + # create conditional format using our utility for a QueryFilter |
| 320 | + conditional_format( |
| 321 | + background_color='fcba03', |
| 322 | + bold=True, |
| 323 | + italic=True, |
| 324 | + query_filter=QueryFilter('theKey', 200), |
| 325 | + strike_through=True, |
| 326 | + text_color='f44e3b', |
| 327 | + ), |
| 328 | + # create conditional format using our utility for a QueryFilter list |
| 329 | + conditional_format( |
| 330 | + background_color='fcba03', |
| 331 | + bold=True, |
| 332 | + italic=True, |
| 333 | + query_filter=[ |
| 334 | + QueryFilter('theKey', 500, QueryFilter.Types.GREATER_THAN), |
| 335 | + QueryFilter('theKey', 1000, QueryFilter.Types.LESS_THAN) |
| 336 | + ], |
| 337 | + strike_through=True, |
| 338 | + text_color='f44e3b', |
| 339 | + ) |
| 340 | + ] |
| 341 | + |
| 342 | + class MockSave(MockLabKey): |
| 343 | + api = 'saveDomain.api' |
| 344 | + default_action = domain_controller |
| 345 | + default_success_body = {} |
| 346 | + |
| 347 | + self.service = MockSave() |
| 348 | + |
| 349 | + payload = { |
| 350 | + 'domainDesign': self.test_domain.to_json(), |
| 351 | + 'queryName': self.query_name, |
| 352 | + 'schemaName': self.schema_name |
| 353 | + } |
| 354 | + |
| 355 | + self.expected_kwargs = { |
| 356 | + 'expected_args': [self.service.get_server_url()], |
| 357 | + 'data': json.dumps(payload), |
| 358 | + 'headers': {'Content-Type': 'application/json'}, |
| 359 | + 'timeout': 300 |
| 360 | + } |
| 361 | + |
| 362 | + self.args = [ |
| 363 | + mock_server_context(self.service), self.schema_name, self.query_name, self.test_domain |
| 364 | + ] |
| 365 | + |
| 366 | + def test_success(self): |
| 367 | + test = self |
| 368 | + success_test(test, self.service.get_successful_response(), |
| 369 | + save, True, *self.args, **self.expected_kwargs) |
| 370 | + |
| 371 | + def test_unauthorized(self): |
| 372 | + test = self |
| 373 | + throws_error_test(test, RequestAuthorizationError, self.service.get_unauthorized_response(), |
| 374 | + save, *self.args, **self.expected_kwargs) |
| 375 | + |
| 376 | + |
247 | 377 | def suite(): |
248 | 378 | load_tests = unittest.TestLoader().loadTestsFromTestCase |
249 | 379 | return unittest.TestSuite([ |
250 | 380 | load_tests(TestCreate), |
251 | 381 | load_tests(TestDrop), |
252 | 382 | load_tests(TestGet), |
253 | 383 | load_tests(TestInferFields), |
254 | | - load_tests(TestSave) |
| 384 | + load_tests(TestSave), |
| 385 | + load_tests(TestConditionalFormatCreate), |
| 386 | + load_tests(TestConditionalFormatSave) |
255 | 387 | ]) |
256 | 388 |
|
257 | 389 |
|
|
0 commit comments