def test_DBInterface(): dbintf = DBInterface() dbintf.set_redis_kwargs("", "127.0.0.1", 6379) dbintf.connect(15, "TEST_DB") db = SonicV2Connector(use_unix_socket_path=True, namespace='') assert db.namespace == '' db.connect("TEST_DB") db.set("TEST_DB", "key0", "field1", "value2") fvs = db.get_all("TEST_DB", "key0") assert "field1" in fvs assert fvs["field1"] == "value2"
def test_DBInterface(): dbintf = DBInterface() dbintf.set_redis_kwargs("", "127.0.0.1", 6379) dbintf.connect(15, "TEST_DB") db = SonicV2Connector(use_unix_socket_path=True, namespace='') assert db.TEST_DB == 'TEST_DB' assert db.namespace == '' db.connect("TEST_DB") db.set("TEST_DB", "key0", "field1", "value2") fvs = db.get_all("TEST_DB", "key0") assert "field1" in fvs assert fvs["field1"] == "value2" # Test keys ks = db.keys("TEST_DB", "key*") assert len(ks) == 1 ks = db.keys("TEST_DB", u"key*") assert len(ks) == 1 # Test del db.set("TEST_DB", "key3", "field4", "value5") deleted = db.delete("TEST_DB", "key3") assert deleted == 1 deleted = db.delete("TEST_DB", "key3") assert deleted == 0 # Test pubsub redisclient = db.get_redis_client("TEST_DB") pubsub = redisclient.pubsub() dbid = db.get_dbid("TEST_DB") pubsub.psubscribe("__keyspace@{}__:pub_key*".format(dbid)) msg = pubsub.get_message() assert len(msg) == 0 db.set("TEST_DB", "pub_key", "field1", "value1") msg = pubsub.get_message() assert len(msg) == 4 assert msg["data"] == "hset" assert msg["channel"] == "__keyspace@{}__:pub_key".format(dbid) msg = pubsub.get_message() assert len(msg) == 0 db.set("TEST_DB", "pub_key", "field1", "value1") db.set("TEST_DB", "pub_key", "field2", "value2") db.set("TEST_DB", "pub_key", "field3", "value3") db.set("TEST_DB", "pub_key", "field4", "value4") msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 0 # Test dict.get() assert fvs.get("field1") == "value2" assert fvs.get("field1_noexisting") == None assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test dict.update() other = {"field1": "value3", "field4": "value4"} fvs.update(other) assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" # Test dict.update() accepts no arguments, and then no update happens fvs.update() assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" fvs.update(field5='value5', field6='value6') assert fvs["field5"] == "value5" with pytest.raises(TypeError): fvs.update(fvs, fvs) # Test blocking fvs = db.get_all("TEST_DB", "key0", blocking=True) assert "field1" in fvs assert fvs["field1"] == "value2" assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test empty/none namespace db = SonicV2Connector(use_unix_socket_path=True, namespace=None) assert db.namespace == '' # Test default namespace parameter db = SonicV2Connector(use_unix_socket_path=True) assert db.namespace == '' # Test no exception try: db = SonicV2Connector(host='127.0.0.1') db = SonicV2Connector(use_unix_socket_path=True, namespace='', decode_responses=True) db = SonicV2Connector(use_unix_socket_path=False, decode_responses=True) db = SonicV2Connector(host="127.0.0.1", decode_responses=True) except: assert False, 'Unexpected exception raised' # Test exception with pytest.raises(ValueError): db = SonicV2Connector(decode_responses=False)
def test_DBInterface(): dbintf = DBInterface() dbintf.set_redis_kwargs("", "127.0.0.1", 6379) dbintf.connect(15, "TEST_DB") db = SonicV2Connector(use_unix_socket_path=True, namespace='') assert db.TEST_DB == 'TEST_DB' assert db.namespace == '' db.connect("TEST_DB") redisclient = db.get_redis_client("TEST_DB") redisclient.flushdb() db.set("TEST_DB", "key0", "field1", "value2") fvs = db.get_all("TEST_DB", "key0") assert "field1" in fvs assert fvs["field1"] == "value2" try: json.dumps(fvs) except: assert False, 'Unexpected exception raised in json dumps' # Test keys ks = db.keys("TEST_DB", "key*") assert len(ks) == 1 ks = db.keys("TEST_DB", u"key*") assert len(ks) == 1 # Test keys could be sorted in place db.set("TEST_DB", "key11", "field1", "value2") db.set("TEST_DB", "key12", "field1", "value2") db.set("TEST_DB", "key13", "field1", "value2") ks = db.keys("TEST_DB", "key*") ks0 = ks ks.sort(reverse=True) assert ks == sorted(ks0, reverse=True) assert isinstance(ks, list) # Test del db.set("TEST_DB", "key3", "field4", "value5") deleted = db.delete("TEST_DB", "key3") assert deleted == 1 deleted = db.delete("TEST_DB", "key3") assert deleted == 0 # Test pubsub pubsub = redisclient.pubsub() dbid = db.get_dbid("TEST_DB") pubsub.psubscribe("__keyspace@{}__:pub_key*".format(dbid)) msg = pubsub.get_message() assert len(msg) == 0 db.set("TEST_DB", "pub_key", "field1", "value1") msg = pubsub.get_message() assert len(msg) == 4 assert msg["data"] == "hset" assert msg["channel"] == "__keyspace@{}__:pub_key".format(dbid) msg = pubsub.get_message() assert len(msg) == 0 db.set("TEST_DB", "pub_key", "field1", "value1") db.set("TEST_DB", "pub_key", "field2", "value2") db.set("TEST_DB", "pub_key", "field3", "value3") db.set("TEST_DB", "pub_key", "field4", "value4") msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 4 msg = pubsub.get_message() assert len(msg) == 0 # Test dict.get() assert fvs.get("field1") == "value2" assert fvs.get("field1_noexisting") == None assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test dict.update() other = {"field1": "value3", "field4": "value4"} fvs.update(other) assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" # Test dict.update() accepts no arguments, and then no update happens fvs.update() assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" fvs.update(field5='value5', field6='value6') assert fvs["field5"] == "value5" with pytest.raises(TypeError): fvs.update(fvs, fvs) # Test blocking reading existing data in Redis fvs = db.get_all("TEST_DB", "key0", blocking=True) assert "field1" in fvs assert fvs["field1"] == "value2" assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test blocking reading coming data in Redis thread = Thread(target=thread_coming_data) thread.start() fvs = db.get_all("TEST_DB", "key0_coming", blocking=True) assert "field1" in fvs assert fvs["field1"] == "value2" assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test hmset fvs = {"field1": "value3", "field2": "value4"} db.hmset("TEST_DB", "key5", fvs) attrs = db.get_all("TEST_DB", "key5") assert len(attrs) == 2 assert attrs["field1"] == "value3" assert attrs["field2"] == "value4" fvs = {"field5": "value5"} db.hmset("TEST_DB", "key5", fvs) attrs = db.get_all("TEST_DB", "key5") assert len(attrs) == 3 assert attrs["field5"] == "value5" # Test empty/none namespace db = SonicV2Connector(use_unix_socket_path=True, namespace=None) assert db.namespace == '' # Test default namespace parameter db = SonicV2Connector(use_unix_socket_path=True) assert db.namespace == '' # Test no exception try: db = SonicV2Connector(host='127.0.0.1') db = SonicV2Connector(use_unix_socket_path=True, namespace='', decode_responses=True) db = SonicV2Connector(use_unix_socket_path=False, decode_responses=True) db = SonicV2Connector(host="127.0.0.1", decode_responses=True) except: assert False, 'Unexpected exception raised' # Test exception with pytest.raises(ValueError): db = SonicV2Connector(decode_responses=False)
def test_DBInterface(): dbintf = DBInterface() dbintf.set_redis_kwargs("", "127.0.0.1", 6379) dbintf.connect(15, "TEST_DB") db = SonicV2Connector(use_unix_socket_path=True, namespace='') assert db.TEST_DB == 'TEST_DB' assert db.namespace == '' db.connect("TEST_DB") db.set("TEST_DB", "key0", "field1", "value2") fvs = db.get_all("TEST_DB", "key0") assert "field1" in fvs assert fvs["field1"] == "value2" # Test dict.get() assert fvs.get("field1") == "value2" assert fvs.get("field1_noexisting") == None assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test dict.update() other = { "field1": "value3", "field4": "value4" } fvs.update(other) assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" # Test dict.update() accepts no arguments, and then no update happens fvs.update() assert len(fvs) == 2 assert fvs["field1"] == "value3" assert fvs["field4"] == "value4" fvs.update(field5='value5', field6='value6') assert fvs["field5"] == "value5" with pytest.raises(TypeError): fvs.update(fvs, fvs) # Test blocking fvs = db.get_all("TEST_DB", "key0", blocking=True) assert "field1" in fvs assert fvs["field1"] == "value2" assert fvs.get("field1", "default") == "value2" assert fvs.get("nonfield", "default") == "default" # Test empty/none namespace db = SonicV2Connector(use_unix_socket_path=True, namespace=None) assert db.namespace == '' # Test default namespace parameter db = SonicV2Connector(use_unix_socket_path=True) assert db.namespace == '' # Test no exception try: db = SonicV2Connector(host='127.0.0.1') db = SonicV2Connector(use_unix_socket_path=True, namespace='', decode_responses=True) db = SonicV2Connector(use_unix_socket_path=False, decode_responses=True) db = SonicV2Connector(host="127.0.0.1", decode_responses=True) except: assert False, 'Unexpected exception raised' # Test exception with pytest.raises(ValueError): db = SonicV2Connector(decode_responses=False)