Skip to main content

Testing Relations in Lesan

On the What Is the Relationship Really? page we saw how relations behave in Lesan and how they compare to SQL and NoSQL. Now let's see it in action. This page walks you through a full hands-on test of the relationship engine using the Playground's E2E feature, based on the country, city, and user models.

Clone and run E2Eโ€‹

You can clone the Lesan repo with git clone https://github.com/MiaadTeam/lesan.git, then go to the tests/playground folder and run the e2e.ts file with deno run -A e2e.ts. You should see this output:

HTTP webserver running.
please send a post request to http://localhost:1366/lesan
you can visit playground on http://localhost:1366/playground

Listening on http://localhost:1366/

Visit the Playgroundโ€‹

Now you can visit the playground at http://localhost:1366/playground and send requests to the server for the country, city, and user models:

Screenshot 2023-12-31 at 21-35-56 Lesan Playground

Use the addCountry, addCountries, updateCountry, getCountries, and deleteCountry methods for the country model:

Screenshot 2023-12-31 at 21-42-37 Lesan Playground

Also use addCity, updateCity, addCities, getCities, and addCityCountry for the city model:

Screenshot 2023-12-31 at 22-03-07 Lesan Playground

And also use addUser, addUsers, addUserLivedCities, addUserCountry, addUserCities, addMostLovedCity, removeMostLovedCity, removeLivedCities, updateUser, getUser, and getUsers for the user model:

Screenshot 2023-12-31 at 22-06-53 Lesan Playground

You can find the e2e.ts raw file here and see all the functions written in it.

Visit Schema and Actโ€‹

You can see all the schema information โ€” including pure, mainRelation, and relatedRelation โ€” inside the schema modal box in the playground by clicking the Schema button:

Screenshot 2023-12-31 at 22-33-26 Lesan Playground

Here is a screenshot of the schema modal box:

Screenshot 2023-12-31 at 22-37-13 Lesan Playground

Also you can see all the act information โ€” including service, model, act, and its inputs such as set and get โ€” inside the act modal box in the playground by clicking the Act button:

Screenshot 2023-12-31 at 22-38-46 Lesan Playground

Here is a screenshot of the act modal box:

Screenshot 2023-12-31 at 22-43-11 Lesan Playground

Visit the E2E test modalโ€‹

We have already prepared several E2E test series for the e2e.ts file, and you can go to the E2E modal box by clicking the E2E test button:

Screenshot 2023-12-31 at 22-52-21 Lesan Playground

Here you can import an E2E test config file by clicking the import button:

Screenshot 2023-12-31 at 22-55-06 Lesan Playground

We have these 3 json files next to the e2e.ts file, all three of which can be used for E2E testing:

Configdata E2E fileโ€‹

In the config.json file, all the functions written in e2e.ts have been tested. In fact, all the important functions โ€” including all the functions of the ODM section in Lesan โ€” have been tested in this file. Let's look at all the parts of this E2E test one by one. (The point is that in almost all the functions written in the ODM, relationships are important, and Lesan must manage them.)

  1. Create a new country with main โ†’ country โ†’ addCountry:

    Screenshot 2024-01-04 at 11-22-31 Lesan Playground

    1. Here we used the main service, the country model, and the addCountry act.
    2. We repeat this section 15 times.
    3. We captured countryId from the last response of this section.
    4. Here we used faker to create name, population, and abb for the new country.
  2. Create multiple countries with one request using main โ†’ country โ†’ addCountries:

    Screenshot 2024-01-04 at 13-53-29 Lesan Playground

    1. Here we used the main service, the country model, and the addCountries act.
    2. Here we insert an array of country for the multiCountries key inside the set object.
    3. We captured some country IDs from the request response.
  3. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 14-20-22 Lesan Playground

    1. Here we used iranId, the captured variable we got from the request-2 response.
    2. We captured body._id with the name haratId from the response.
  4. Create multiple cities with main โ†’ city โ†’ addCities:

    Screenshot 2024-01-04 at 14-42-12 Lesan Playground

    1. Here we insert an array of city for the multiCities key inside the set object.
    2. Here we used iraqId, the captured variable we got from the request-2 response.
  5. Create multiple cities with main โ†’ city โ†’ addCities:

    Screenshot 2024-01-04 at 14-56-21 Lesan Playground

    1. Here we insert an array of city for the multiCities key inside the set object. We also used faker here.
    2. Here we used afghanId, the captured variable we got from the request-2 response.
  6. Change the country relation of a city with main โ†’ city โ†’ addCityCountry:

    Screenshot 2024-01-04 at 15-04-46 Lesan Playground

    1. Here we used the haratId and afghanId captured variables. Please check MongoDB Compass, because both sides of the relation are changed.
  7. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 15-14-21 Lesan Playground

  8. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 15-17-21 Lesan Playground

    1. Please note that we set the isCapital field to true, so the capital field of the related country is filled with this city.
  9. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-39-21 Lesan Playground

  10. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-42-27 Lesan Playground

  11. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-43-55 Lesan Playground

  12. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-45-38 Lesan Playground

  13. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-48-19 Lesan Playground

  14. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 16-50-30 Lesan Playground

  15. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 17-04-33 Lesan Playground

  16. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 17-07-41 Lesan Playground

  17. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 17-08-14 Lesan Playground

  18. Create a new city with main โ†’ city โ†’ addCity:

    Screenshot 2024-01-04 at 17-09-14 Lesan Playground

  19. Get a list of countries with main โ†’ country โ†’ getCountries:

    Screenshot 2024-01-04 at 17-10-22 Lesan Playground

  20. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-19-46 Lesan Playground

    1. The country the user lived in is Iran.
    2. The user lived in two cities: Hamedan and Tehran.
  21. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-28-58 Lesan Playground

  22. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-29-39 Lesan Playground

  23. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-30-13 Lesan Playground

  24. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-30-44 Lesan Playground

  25. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-31-20 Lesan Playground

  26. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-31-54 Lesan Playground

  27. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-32-28 Lesan Playground

  28. Create a new user with main โ†’ user โ†’ addUser:

    Screenshot 2024-01-04 at 17-33-04 Lesan Playground

  29. Change the country relation of a user with main โ†’ user โ†’ addUserCountry:

    Screenshot 2024-01-04 at 19-20-28 Lesan Playground

    1. We just send a country ID with a user ID, and with a simple function all the magic happens on both sides of the relation.
  30. Change the country relation of a user with main โ†’ user โ†’ addUserCountry:

    Screenshot 2024-01-04 at 19-34-16 Lesan Playground

  31. Add cities to the livedCities relation of a user with main โ†’ user โ†’ addUserLivedCities:

    Screenshot 2024-01-04 at 19-37-00 Lesan Playground

    1. We just send a list of city IDs with a user ID, and with a simple function all the magic happens on both sides of the relation.
  32. Add cities to the livedCities relation of a user with main โ†’ user โ†’ addUserLivedCities:

    Screenshot 2024-01-04 at 19-51-56 Lesan Playground

  33. Remove cities from the livedCities relation of a user with main โ†’ user โ†’ removeLivedCities:

    Screenshot 2024-01-04 at 19-54-10 Lesan Playground

    1. We just send a list of city IDs with a user ID, and with a simple function all the magic happens on both sides of the relation.
  34. Add a city to the mostLovedCity relation of a user with main โ†’ user โ†’ addMostLovedCity:

    Screenshot 2024-01-04 at 19-59-44 Lesan Playground

  35. Add a city to the mostLovedCity relation of a user with main โ†’ user โ†’ addMostLovedCity:

    Screenshot 2024-01-04 at 20-05-52 Lesan Playground

  36. Remove a city from the mostLovedCity relation of a user with main โ†’ user โ†’ removeLivedCities:

    Screenshot 2024-01-04 at 20-11-13 Lesan Playground

  37. Update a country with main โ†’ country โ†’ updateCountry:

    Screenshot 2024-01-04 at 20-12-26 Lesan Playground

    1. We send the ID of a country along with the rest of the pure fields (the pure fields are optional), and in a simple function the following happens:
      • Update the country itself.
      • Update the country field in all the cities related to that country.
      • Update the country field in all the users related to that country.

    The point to mention here is that you should not send any field other than pure fields for updating, because the relationships must be completely managed by Lesan itself.

  38. Update a city with main โ†’ city โ†’ updateCity:

    Screenshot 2024-01-04 at 20-26-49 Lesan Playground

  39. Update a user with main โ†’ user โ†’ updateUser:

    Screenshot 2024-01-04 at 20-27-19 Lesan Playground

After clicking the run E2E test button, you will go to the test results page:

Screenshot 2024-01-06 at 13-34-30 Lesan Playground

If you scroll down a little, you can see the results of each sequence separately:

Screenshot 2024-01-06 at 14-13-04 Lesan Playground

Screenshot 2024-01-06 at 14-20-03 Lesan Playground

  1. With this button you can change the view of the panel from body-header & Description to REQUEST & RESULT.
  2. Shows some description about the sequence, including request number, timing, captured values, and so on.
  3. Shows the unparsed header and body you sent to the backend.
  4. Shows the index of each sequence.
  5. Shows the response received back from the server.
  6. Shows the parsed request you sent to the server, including the parsed header and body.
  7. Pagination for sequences with more than 1 request.

After finishing executing all the tests in configdata.json, you have nice data inserted into the sample collection in MongoDB:

Screenshot from 2024-01-06 15-10-49

You can play with this data in the playground and change everything you want:

Screenshot 2024-01-06 at 15-08-36 Lesan Playground

fakerTest E2E fileโ€‹

This file is not very important in this section; it's only used to test the faker functions in the E2E:

Screenshot 2024-01-06 at 16-47-45 Lesan Playground

stress E2E fileโ€‹

This file is used to test insertMany when it has a relation. Note that a very large number of server-side requests are sent, resulting in the creation of a country with 50,000 cities for that country and 50,000 users for that country.

  1. Create a country with main โ†’ country โ†’ addCountry:

    Screenshot 2024-01-06 at 17-33-04 Lesan Playground

  2. Create 50,000 cities with main โ†’ city โ†’ addCities:

    Screenshot 2024-01-06 at 17-35-45 Lesan Playground

  3. Create 50,000 users with main โ†’ user โ†’ addUsers:

    Screenshot 2024-01-06 at 17-39-02 Lesan Playground

After clicking the run E2E test button, 10001 requests should be sent to the server, and as a result, a country, 50,000 cities, and 50,000 users should be created:

Screenshot 2024-01-06 at 19-21-48 Lesan Playground

Pay attention to the inserted data โ€” although we used insertMany, all relationships are embedded. In the country, we have embedded the cities in 4 fields separately and with different conditions, and we have embedded users in two fields with different conditions:

Screenshot from 2024-01-06 19-07-59

In the cities, we have embedded the respective country:

Screenshot from 2024-01-06 19-14-11

In the user schema, for each user, we have embedded the cities he has lived in as a list, the city he is most interested in as an object, and the country of each user as an object:

Screenshot from 2024-01-06 19-16-33

The interesting thing about this E2E test is that after the database is filled, you can test a big update in the playground. If you update the country in the playground, 100,000 other documents must be updated along with the country record itself.

Before executing main โ†’ country โ†’ updateCountry:

Screenshot from 2024-01-06 19-40-02

Executing main โ†’ country โ†’ updateCountry:

Screenshot 2024-01-06 at 19-41-30 Lesan Playground

After executing main โ†’ country โ†’ updateCountry:

Screenshot from 2024-01-06 19-41-56

The 100,000-document update you just witnessed is the trade-off of embedding โ€” see The Bitterness of Relationships in Lesan and how Lesan queues these large updates.