|
1 | | -from labkey.utils import btoa, encode_uri_component, waf_encode |
| 1 | +from labkey.utils import btoa, encode_uri_component, waf_encode, snake_to_camel, transform_options |
2 | 2 |
|
3 | 3 |
|
4 | 4 | def test_btoa(): |
@@ -30,3 +30,64 @@ def test_waf_encode(): |
30 | 30 | waf_encode("><&/%' \"1äöüÅ") |
31 | 31 | == prefix + "JTNFJTNDJTI2JTJGJTI1JyUyMCUyMjElQzMlQTQlQzMlQjYlQzMlQkMlQzMlODU=" |
32 | 32 | ) |
| 33 | + |
| 34 | + |
| 35 | +def test_snake_to_camel(): |
| 36 | + assert snake_to_camel("snake_case") == "snakeCase" |
| 37 | + assert snake_to_camel("multiple_word_snake_case") == "multipleWordSnakeCase" |
| 38 | + assert snake_to_camel("alreadyCamelCase") == "alreadyCamelCase" |
| 39 | + assert snake_to_camel("single") == "single" |
| 40 | + assert snake_to_camel("UPPER_SNAKE_CASE") == "upperSnakeCase" |
| 41 | + assert snake_to_camel("_leading_underscore") == "leadingUnderscore" |
| 42 | + assert snake_to_camel("trailing_underscore_") == "trailingUnderscore" |
| 43 | + assert snake_to_camel("multiple__underscores") == "multipleUnderscores" |
| 44 | + assert snake_to_camel("") == "" |
| 45 | + |
| 46 | + |
| 47 | +def test_transform_options(): |
| 48 | + options = { |
| 49 | + "include_columns": True, |
| 50 | + "include_system_queries": False, |
| 51 | + "include_title": False, |
| 52 | + "include_user_queries": False, |
| 53 | + "include_view_data_url": True, |
| 54 | + "query_detail_columns": False, |
| 55 | + } |
| 56 | + expected_keys = [ |
| 57 | + "include_columns", |
| 58 | + "include_system_queries", |
| 59 | + "include_user_queries", |
| 60 | + "include_view_data_url", |
| 61 | + ] |
| 62 | + transformed_options = transform_options(options, expected_keys) |
| 63 | + assert transformed_options == { |
| 64 | + "includeColumns": True, |
| 65 | + "includeSystemQueries": False, |
| 66 | + "includeUserQueries": False, |
| 67 | + "includeViewDataUrl": True, |
| 68 | + } |
| 69 | + assert transformed_options["includeColumns"] is True |
| 70 | + |
| 71 | + # Empty options |
| 72 | + assert transform_options({}, ["any_key"]) == {} |
| 73 | + |
| 74 | + # Empty expected_keys |
| 75 | + assert transform_options({"any_key": 1}, []) == {} |
| 76 | + |
| 77 | + # Filtering behavior: only keys in expected_keys are kept and transformed |
| 78 | + options = {"keep_me": 1, "ignore_me": 2} |
| 79 | + expected = ["keep_me"] |
| 80 | + result = transform_options(options, expected) |
| 81 | + assert result == {"keepMe": 1} |
| 82 | + |
| 83 | + # Keys in expected_keys but not in options are ignored |
| 84 | + options = {"present_key": "I am present!"} |
| 85 | + expected = ["present_key", "absent_key"] |
| 86 | + result = transform_options(options, expected) |
| 87 | + assert result == {"presentKey": "I am present!"} |
| 88 | + |
| 89 | + # Non-snake_case keys that are in expected_keys |
| 90 | + options = {"alreadyCamel": 1, "simple": 2, "UPPER_SNAKE": 3} |
| 91 | + expected = ["alreadyCamel", "simple", "UPPER_SNAKE"] |
| 92 | + result = transform_options(options, expected) |
| 93 | + assert result == {"alreadyCamel": 1, "simple": 2, "upperSnake": 3} |
0 commit comments