D
D
darkoment2021-11-27 02:58:14
JSON
darkoment, 2021-11-27 02:58:14

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": []
      }

but on the client, let's say two keys are missing
"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?

UPD: How to make comparison on all keys, but not on each separately? By the way, I need to write a test exclusively on the client side

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hungryee, 2021-11-27
@Hungryee

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 question

Ask a Question

731 491 924 answers to any question