diff --git a/src/P2S_Lib/redisdatastore.cpp b/src/P2S_Lib/redisdatastore.cpp index adbef2a..93d3f95 100644 --- a/src/P2S_Lib/redisdatastore.cpp +++ b/src/P2S_Lib/redisdatastore.cpp @@ -16,14 +16,15 @@ void RedisDataStore::Put(string key, std::vector changes) client.setnx(key, "{}"); json collection = json::parse(this->GetSync(key)); for(auto change : changes){ - if(change["_delete"].get()) + auto changeId = change["_id"].get(); + if(collection.count(changeId) != 0 && change["_delete"].get()) { - collection.erase(change["_id"].get()); + collection.erase(changeId); continue; } - else if(collection.count("_id") == 0 || collection["_id"]["_mtime"].get() < change["_mtime"].get()) + else if(collection.count(changeId) == 0 || collection[changeId]["_mtime"].get() < change["_mtime"].get()) { - collection[change["_id"].get()] = change; + collection[changeId] = change; continue; } } diff --git a/test/P2S_Lib_Test/jsonrpcrequesttest.cpp b/test/P2S_Lib_Test/jsonrpcrequesttest.cpp index 2ded589..f8fbb3d 100644 --- a/test/P2S_Lib_Test/jsonrpcrequesttest.cpp +++ b/test/P2S_Lib_Test/jsonrpcrequesttest.cpp @@ -1,15 +1,15 @@ #include -#include +#include using namespace P2S::Lib; -TEST(RPCResponseTest, ValidJsonRpcResponseTest) { - ValidJsonRpcResponse response("it works", "10"); - EXPECT_EQ("{\"id\":\"10\",\"jsonrpc\":\"2.0\",\"result\":\"it works\"}", response.Serialize()); +TEST(RPCRequestTest, ParseCorrectJsonRPC) { + JsonRpcRequest request; + ASSERT_NO_THROW(request.Deserialize("{\"jsonrpc\": \"2.0\", \"method\":\"example\", \"params\": [42], \"id\": 2}")); } -TEST(RPCResponseTest, ErrorJsonRpcResponseTest) { - ErrorJsonRpcResponse response(-32700,"Parse Error", "10"); - EXPECT_EQ("{\"error\":{\"code\":-32700,\"data\":\"\",\"message\":\"Parse Error\"},\"id\":\"10\",\"jsonrpc\":\"2.0\"}", response.Serialize()); +TEST(RPCRequestTest, ParseIncorrectJsonRPC) { + JsonRpcRequest request; + ASSERT_ANY_THROW(request.Deserialize("{\"jsonrpc\": \"2.0\".... \"method\":\"example\", \"id\": 2}")); } diff --git a/test/P2S_Lib_Test/jsonrpcresponsetest.cpp b/test/P2S_Lib_Test/jsonrpcresponsetest.cpp index ee91830..c2d5e07 100644 --- a/test/P2S_Lib_Test/jsonrpcresponsetest.cpp +++ b/test/P2S_Lib_Test/jsonrpcresponsetest.cpp @@ -1,14 +1,14 @@ #include -#include +#include using namespace P2S::Lib; -TEST(RPCRequestTest, ParseCorrectJsonRPC) { - JsonRpcRequest request; - ASSERT_NO_THROW(request.Deserialize("{\"jsonrpc\": \"2.0\", \"method\":\"example\", \"params\": [42], \"id\": 2}")); +TEST(RPCResponseTest, ValidJsonRpcResponseTest) { + ValidJsonRpcResponse response("it works", "10"); + EXPECT_EQ("{\"id\":\"10\",\"jsonrpc\":\"2.0\",\"result\":\"it works\"}", response.Serialize()); } -TEST(RPCRequestTest, ParseIncorrectJsonRPC) { - JsonRpcRequest request; - ASSERT_ANY_THROW(request.Deserialize("{\"jsonrpc\": \"2.0\".... \"method\":\"example\", \"id\": 2}")); +TEST(RPCResponseTest, ErrorJsonRpcResponseTest) { + ErrorJsonRpcResponse response(-32700,"Parse Error", "10"); + EXPECT_EQ("{\"error\":{\"code\":-32700,\"data\":\"\",\"message\":\"Parse Error\"},\"id\":\"10\",\"jsonrpc\":\"2.0\"}", response.Serialize()); } diff --git a/test/Test_P2S_Postman.json b/test/Test_P2S_Postman.json index e85cffd..09b7d40 100644 --- a/test/Test_P2S_Postman.json +++ b/test/Test_P2S_Postman.json @@ -92,7 +92,65 @@ ], "body": { "mode": "raw", - "raw": "{\"jsonrpc\": \"2.0\", \"method\":\"datastorePut\", \"params\": {\"collection\": \"testCollection\", \"changes\": [{ \"_id\": \"troloro12345\", \"_mtime\": 12345, \"_delete\": false}, { \"_id\": \"troloro123456\", \"_mtime\": 12345, \"_delete\": false}] }, \"id\": 2}" + "raw": "{\"jsonrpc\": \"2.0\", \"method\":\"datastorePut\", \"params\": {\"collection\": \"testCollection\", \"changes\": [{ \"_id\": \"troloro12345\", \"_mtime\": 12345, \"_delete\": false}, { \"_id\": \"troloro123456\", \"_mtime\": 12345, \"_delete\": false}, { \"_id\": \"troloro1234567\", \"_mtime\": 12345, \"_delete\": false}] }, \"id\": 2}" + }, + "description": "" + }, + "response": [] + }, + { + "name": "put remove", + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"\"}'\ntests[\"Body is response\"] = responseBody === expected;" + } + } + ], + "request": { + "url": "http://localhost:8080/rpc", + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "" + } + ], + "body": { + "mode": "raw", + "raw": "{\"jsonrpc\": \"2.0\", \"method\":\"datastorePut\", \"params\": {\"collection\": \"testCollection\", \"changes\": [{ \"_id\": \"troloro1234567\", \"_mtime\": 1234578, \"_delete\": true}] }, \"id\": 2}" + }, + "description": "" + }, + "response": [] + }, + { + "name": "put edit", + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"\"}'\ntests[\"Body is response\"] = responseBody === expected;" + } + } + ], + "request": { + "url": "http://localhost:8080/rpc", + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "description": "" + } + ], + "body": { + "mode": "raw", + "raw": "{\"jsonrpc\": \"2.0\", \"method\":\"datastorePut\", \"params\": {\"collection\": \"testCollection\", \"changes\": [{ \"_id\": \"troloro123456\", \"_mtime\": 12345789, \"_delete\": false, \"data\": \"this is attached information\"}] }, \"id\": 2}" }, "description": "" }, @@ -169,7 +227,7 @@ "listen": "test", "script": { "type": "text/javascript", - "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"[{\\\\\"_delete\\\\\":false,\\\\\"_id\\\\\":\\\\\"troloro12345\\\\\",\\\\\"_mtime\\\\\":12345},{\\\\\"_delete\\\\\":false,\\\\\"_id\\\\\":\\\\\"troloro123456\\\\\",\\\\\"_mtime\\\\\":12345}]\"}'\ntests[\"Body is response\"] = responseBody === expected;" + "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"[{\\\\\"_delete\\\\\":false,\\\\\"_id\\\\\":\\\\\"troloro12345\\\\\",\\\\\"_mtime\\\\\":12345},{\\\\\"_delete\\\\\":false,\\\\\"_id\\\\\":\\\\\"troloro123456\\\\\",\\\\\"_mtime\\\\\":12345789,\\\\\"data\\\\\":\\\\\"this is attached information\\\\\"}]\"}'\ntests[\"Body is response\"] = responseBody === expected;" } } ], @@ -262,7 +320,7 @@ "listen": "test", "script": { "type": "text/javascript", - "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"[{\\\\\"id\\\\\":\\\\\"troloro12345\\\\\",\\\\\"mtime\\\\\":12345},{\\\\\"id\\\\\":\\\\\"troloro123456\\\\\",\\\\\"mtime\\\\\":12345}]\"}'\ntests[\"Body is response\"] = responseBody === expected;" + "exec": "var expected = '{\"id\":\"2\",\"jsonrpc\":\"2.0\",\"result\":\"[{\\\\\"id\\\\\":\\\\\"troloro12345\\\\\",\\\\\"mtime\\\\\":12345},{\\\\\"id\\\\\":\\\\\"troloro123456\\\\\",\\\\\"mtime\\\\\":12345789}]\"}'\ntests[\"Body is response\"] = responseBody === expected;" } } ],