Answer the question
In order to leave comments, you need to log in
How to test json by its keys?
I have an application which is divided into backend and client (Desktop application). The exchange takes place among themselves through json format (I use nlohmann/json). Every time there are changes on the backend, these changes must be made in the client immediately (otherwise, the entire connection between the client and the backend will break). The question arose about writing a test / tests for this case, and then the question arose: how can json be tested at all and is it possible to do a test using its keys in order to make a comparison later? Is there some universal method that would send a request to the backend, receive a response in order to compare all the keys, and not each key individually?
For example I have:
"courtcase": [
{
"id": 2,
"office": 2,
"cid": "4222",
"commentary": "",
"date_received": "2021-07-06T12:17:16.355Z",
"purchases": [],
"researches": [],
"participants": [],
"court_transfer_date": "2021-07-06T12:17:10Z",
"court_handover_date": null,
"date_expected_payment": "2021-07-06T12:17:12Z",
"billing_date": null,
"payment_date": null,
"extra_docs_requested_date": null,
"extra_docs_received_date": null,
"courrier_handover_date": null,
"court": [],
"judges": [],
"related_collectors": [],
"related_courriers": [],
"related_departments": [],
"related_experts": [],
"related_lawyers": [],
"related_managers": []
}
"related_lawyers": []and
"related_managers": []And so that each time you don’t have to manually look at what is on the client, what is on the backend, is it possible to write a test that would immediately show what is on the backend and what is not on the client?
Answer the question
In order to leave comments, you need to log in
Generally speaking, I see no reason to compare what is on the client and what is on the server
Json from the server should always take precedence and replace the state of the client.
But if you really need it, then you need to compare the content of both objects anyway.
At least, compare the lengths of both objects using (js) Object.keys(myObject).length;
If they are not equal, then replace the client json with the server json. If equal, then check the content of each field
For each nested field, perform the same recursive check
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question