|
| 1 | +from labthings.core import utilities |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +@pytest.fixture |
| 6 | +def example_class(): |
| 7 | + class ExampleClass: |
| 8 | + """ |
| 9 | + First line of class docstring. |
| 10 | + Second line of class docstring. |
| 11 | + """ |
| 12 | + |
| 13 | + def class_method(self): |
| 14 | + """ |
| 15 | + First line of class method docstring. |
| 16 | + Second line of class method docstring. |
| 17 | + """ |
| 18 | + return self |
| 19 | + |
| 20 | + def class_method_no_docstring(self): |
| 21 | + return self |
| 22 | + |
| 23 | + return ExampleClass |
| 24 | + |
| 25 | + |
| 26 | +def test_get_docstring(example_class): |
| 27 | + assert ( |
| 28 | + utilities.get_docstring(example_class) |
| 29 | + == "First line of class docstring. \nSecond line of class docstring. \n" |
| 30 | + ) |
| 31 | + |
| 32 | + assert ( |
| 33 | + utilities.get_docstring(example_class.class_method) |
| 34 | + == "First line of class method docstring. \nSecond line of class method docstring. \n" |
| 35 | + ) |
| 36 | + |
| 37 | + assert utilities.get_docstring(example_class.class_method_no_docstring) == "" |
| 38 | + |
| 39 | + |
| 40 | +def test_get_summary(example_class): |
| 41 | + assert utilities.get_summary(example_class) == "First line of class docstring." |
| 42 | + |
| 43 | + assert ( |
| 44 | + utilities.get_summary(example_class.class_method) |
| 45 | + == "First line of class method docstring." |
| 46 | + ) |
| 47 | + |
| 48 | + assert utilities.get_summary(example_class.class_method_no_docstring) == "" |
| 49 | + |
| 50 | + |
| 51 | +def test_rupdate(): |
| 52 | + d1 = {"a": "String", "b": 5, "c": [], "d": {"a": "String", "b": 5, "c": []}} |
| 53 | + |
| 54 | + d2 = { |
| 55 | + "a": "String 2", |
| 56 | + "b": 50, |
| 57 | + "c": [10, 20, 30, 40, 50], |
| 58 | + "d": {"a": "String 2d", "b": 50, "c": [10, 20, 30, 40, 50]}, |
| 59 | + } |
| 60 | + |
| 61 | + assert utilities.rupdate(d1, d2) == d2 |
| 62 | + |
| 63 | + |
| 64 | +def test_rapply(): |
| 65 | + d1 = { |
| 66 | + "a": "String", |
| 67 | + "b": 5, |
| 68 | + "c": [10, 20, 30, 40, 50], |
| 69 | + "d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]}, |
| 70 | + } |
| 71 | + |
| 72 | + def as_str(v): |
| 73 | + return str(v) |
| 74 | + |
| 75 | + d2 = { |
| 76 | + "a": "String", |
| 77 | + "b": "5", |
| 78 | + "c": ["10", "20", "30", "40", "50"], |
| 79 | + "d": {"a": "String", "b": "5", "c": ["10", "20", "30", "40", "50"]}, |
| 80 | + } |
| 81 | + |
| 82 | + assert utilities.rapply(d1, as_str) == d2 |
| 83 | + |
| 84 | + d2_no_iter = { |
| 85 | + "a": "String", |
| 86 | + "b": "5", |
| 87 | + "c": "[10, 20, 30, 40, 50]", |
| 88 | + "d": {"a": "String", "b": "5", "c": "[10, 20, 30, 40, 50]"}, |
| 89 | + } |
| 90 | + |
| 91 | + assert utilities.rapply(d1, as_str, apply_to_iterables=False) == d2_no_iter |
| 92 | + |
| 93 | + |
| 94 | +def test_get_by_path(): |
| 95 | + d1 = { |
| 96 | + "a": {"b": "String"}, |
| 97 | + } |
| 98 | + |
| 99 | + assert utilities.get_by_path(d1, ("a", "b")) == "String" |
| 100 | + |
| 101 | + |
| 102 | +def test_set_by_path(): |
| 103 | + d1 = { |
| 104 | + "a": {"b": "String"}, |
| 105 | + } |
| 106 | + |
| 107 | + utilities.set_by_path(d1, ("a", "b"), "Set") |
| 108 | + |
| 109 | + assert d1["a"]["b"] == "Set" |
| 110 | + |
| 111 | + |
| 112 | +def test_create_from_path(): |
| 113 | + assert utilities.create_from_path(["a", "b", "c"]) == {"a": {"b": {"c": {}}}} |
| 114 | + |
| 115 | + |
| 116 | +def test_camel_to_snake(): |
| 117 | + assert utilities.camel_to_snake("SomeCamelString") == "some_camel_string" |
| 118 | + |
| 119 | + |
| 120 | +def camel_to_spine(): |
| 121 | + assert utilities.camel_to_snake("SomeCamelString") == "some-camel-string" |
| 122 | + |
| 123 | + |
| 124 | +def test_snake_to_spinee(): |
| 125 | + assert utilities.snake_to_spine("some_snake_string") == "some-snake-string" |
0 commit comments