def test_load_store_and_flush(): storage = subject_storage_factory("empty") storage.store(inserts=(SubjectProperty("p1", 1), SubjectProperty("p2", "2'3"), SubjectExtProperty("px1", 3), SubjectExtProperty("p1", "s1"))) props, ext_props = storage.load() assert len(props) == 0 assert len(ext_props) == 0 storage.store(updates=( SubjectProperty("p2", 3), SubjectExtProperty("p1", "s2"), ), deletes=( SubjectProperty("p1"), SubjectExtProperty("px1"), )) props, ext_props = storage.load() assert len(props) == 0 assert len(ext_props) == 0 storage.flush() assert len(props) == 0 assert len(ext_props) == 0
def test_multiple_subjects(storage_subject1, storage_subject2): storage_subject1.store(inserts=( SubjectProperty("p1", 1), SubjectProperty("p2", 2), SubjectExtProperty("p3", 3), )) storage_subject2.store(inserts=( SubjectProperty("p1", 1), SubjectExtProperty("p2", 2), )) props1, ext_props1 = storage_subject1.load() props2, ext_props2 = storage_subject2.load() assert len(props1) == 2 assert len(ext_props1) == 1 assert len(props2) == 1 assert len(ext_props2) == 1 storage_subject1.store(updates=(SubjectProperty("p1", 10), SubjectExtProperty("p3", 30)), deletes=(SubjectProperty("p2"), )) props1, ext_props1 = storage_subject1.load() props2, ext_props2 = storage_subject2.load() assert len(props1) == 1 assert len(props2) == 1 assert len(ext_props2) == 1 assert props1["p1"] == 10 assert props2["p1"] == 1
def test_set_and_get(): global NAMESPACE, POD_NAME spod = subject_storage_factory(f"/api/v1/namespaces/{NAMESPACE}/pods/{POD_NAME}") with pytest.raises(AttributeError): spod.get(SubjectProperty("pset")) # simple value new_value, old_value = spod.set(SubjectProperty("pset", 1)) assert old_value is None assert new_value == 1 assert spod.get(SubjectProperty("pset")) == 1 spod.delete(SubjectProperty("pset")) new_value, old_value = spod.set(SubjectProperty("pset", 1), 0) assert old_value == 0 assert new_value == 1 new_value, old_value = spod.set(SubjectProperty("pset", "1'2")) assert new_value == "1'2" assert old_value == 1 assert spod.get(SubjectProperty("pset")) == "1'2" # computed value spod.set(SubjectProperty("pset", lambda: "1'2")) # no args spod.set(SubjectProperty("pset", lambda x: x.replace("'", "$"))) assert spod.get(SubjectProperty("pset")) == "1$2"
def test_load_store_and_flush(): global NAMESPACE, POD_NAME spod = subject_storage_factory(f"/api/v1/namespaces/{NAMESPACE}/pods/{POD_NAME}") props, ext_props = spod.load() initial_len_props = len(props) initial_len_ext_props = len(ext_props) spod.store( inserts=( SubjectProperty("p1", 1), SubjectProperty("p2", "2'3"), SubjectExtProperty("px1", 3), SubjectExtProperty("p1", "s1") ) ) spod._resource_body = None props, ext_props = spod.load() assert len(props) == 2+initial_len_props assert len(ext_props) == 2+initial_len_ext_props assert props["p1"] == 1 assert props["p2"] == "2'3" assert ext_props["px1"] == 3 assert ext_props["p1"] == "s1" spod.store( updates=( SubjectProperty("p2", 3), SubjectExtProperty("p1", "s2"), ), deletes=( SubjectProperty("p1"), SubjectExtProperty("px1"), ) ) spod._resource_body = None props, ext_props = spod.load() assert len(props) == 1+initial_len_props assert len(ext_props) == 1+initial_len_ext_props assert props["p2"] == 3 assert ext_props["p1"] == "s2" spod.flush() props, ext_props = spod.load() assert len(props) == initial_len_props and len(ext_props) == initial_len_ext_props
def test_ext_props(): storage = subject_storage_factory("test") storage.flush() storage.set(SubjectProperty("p1", 1)) storage.set(SubjectProperty("p2", 2)) storage.set(SubjectExtProperty("p3", 3)) storage.set(SubjectExtProperty("p4", 4)) props = storage.get_ext_props() assert len(props) == 0
def test_ext_props(storage_subject1, storage_subject2): storage_subject1.flush() storage_subject2.flush() storage_subject1.set(SubjectProperty("p1", 1)) storage_subject1.set(SubjectProperty("p2", 2)) storage_subject1.set(SubjectExtProperty("p3", 3)) storage_subject1.set(SubjectExtProperty("p4", 4)) storage_subject2.set(SubjectExtProperty("p5", 5)) props = storage_subject1.get_ext_props() assert len(props) == 2 assert "p3" in props and props["p3"] == 3 assert "p4" in props and props["p4"] == 4
def test_ret_values(storage_subject1): storage_subject1.flush() vp1 = 1 new_val, old_val = storage_subject1.set(SubjectProperty("p1", vp1)) assert new_val == vp1 assert old_val is None new_val, old_val = storage_subject1.set(SubjectProperty("p1", lambda v: v+1)) assert old_val == vp1 assert new_val == vp1+1
def test_load_store_and_flush(storage_subject1): props, ext_props = storage_subject1.load() assert props == {} and ext_props == {} storage_subject1.store( inserts=( SubjectProperty("p1", 1), SubjectProperty("p2", "2'3"), # sql needs escape SubjectExtProperty("px1", 3), SubjectExtProperty("p1", "s1") ) ) props, ext_props = storage_subject1.load() assert len(props) == 2 assert len(ext_props) == 2 assert props["p1"] == 1 assert props["p2"] == "2'3" assert ext_props["px1"] == 3 assert ext_props["p1"] == "s1" storage_subject1.store( updates=( SubjectProperty("p2", 3), SubjectExtProperty("p1", "s2"), ), deletes=( SubjectProperty("p1"), SubjectExtProperty("px1"), ) ) props, ext_props = storage_subject1.load() assert len(props) == 1 assert len(ext_props) == 1 assert props["p2"] == 3 assert ext_props["p1"] == "s2" storage_subject1.flush() props, ext_props = storage_subject1.load() assert props == {} and ext_props == {}
def test_ext_props(): global NAMESPACE, POD_NAME spod = subject_storage_factory(f"/api/v1/namespaces/{NAMESPACE}/pods/{POD_NAME}") spod.flush() initial_len = len(spod.get_ext_props()) spod.set(SubjectProperty("p1", 1)) spod.set(SubjectProperty("p2", 2)) spod.set(SubjectExtProperty("p3", 3)) spod.set(SubjectExtProperty("p4", 4)) props = spod.get_ext_props() assert len(props) == 2+initial_len assert "p3" in props and props["p3"] == 3 assert "p4" in props and props["p4"] == 4
def test_set_and_get(): storage = subject_storage_factory("empty") storage.flush() assert storage.get("my_prop") is None new_value, old_value = storage.set(SubjectProperty("my_prop", 1)) assert old_value is None assert new_value is None assert storage.get(SubjectProperty("my_prop")) is None storage.delete(SubjectProperty("my_prop")) new_value, old_value = storage.set(SubjectProperty("pset", 1), 0) assert old_value is None assert new_value is None new_value, old_value = storage.set(SubjectProperty("pset", "1'2")) assert new_value is None assert old_value is None assert storage.get(SubjectProperty("pset")) is None
def test_property_kinds(): prop_p = SubjectProperty("p") assert prop_p.value is None assert prop_p.type == PropertyType.DEFAULT prop_e = SubjectExtProperty("p") assert prop_e.type == PropertyType.EXTENDED # test simple value prop = SubjectProperty("p", 1) assert prop.json_value() == json.dumps(1) assert prop.get_value() == 1 prop = SubjectExtProperty("p", 2) assert prop.get_value() == 2 assert prop.json_value() == json.dumps(2) # callable no args prop = SubjectProperty("p", lambda: 10) assert prop.json_value() == json.dumps(10) assert prop.get_value() == 10 # callable with args prop = SubjectProperty("p", lambda x: x * 2) assert prop.json_value(2) == json.dumps(4) assert prop.get_value() == 4 prop = SubjectExtProperty("p", lambda x: x.format(2)) assert prop.get_value("value is {}") == "value is 2" assert prop.json_value("value is {}") == json.dumps("value is 2")
def _call_wait_and_incr(): storage_subject1.set(SubjectProperty("psetX", _wait_and_incr))
def test_set_and_get(storage_subject1): with pytest.raises(AttributeError): storage_subject1.get(SubjectProperty("pset")) # simple value new_value, old_value = storage_subject1.set(SubjectProperty("pset", 1)) assert old_value is None assert new_value == 1 storage_subject1.delete(SubjectProperty("pset")) new_value, old_value = storage_subject1.set(SubjectProperty("pset", 1), 0) assert old_value == 0 assert new_value == 1 new_value, old_value = storage_subject1.set(SubjectProperty("pset", "1'2")) assert new_value == "1'2" assert old_value == 1 assert storage_subject1.get(SubjectProperty("pset")) == "1'2" # computed value storage_subject1.set(SubjectProperty("pset", lambda: "1'2")) # no args storage_subject1.set(SubjectProperty("pset", lambda x: x.replace("'", "$"))) assert storage_subject1.get(SubjectProperty("pset")) == "1$2" # having exceptions in writing operations does not cause bad status (eg; write-locked database) with pytest.raises(Exception): storage_subject1.set(SubjectProperty("pset", lambda x: x / 0)) storage_subject1.set(SubjectProperty("psetX", 0)) # test concurrency if storage_subject1.is_concurrency_safe(): def _wait_and_incr(val): time.sleep(1) return val + 1 def _call_wait_and_incr(): storage_subject1.set(SubjectProperty("psetX", _wait_and_incr)) p = Process(target=_call_wait_and_incr) p.start() storage_subject1.set(SubjectProperty("psetX", lambda x: x + 1)) p.join() assert storage_subject1.get(SubjectProperty("psetX")) == 2