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:
Use the addCountry, addCountries, updateCountry, getCountries, and deleteCountry methods for the country model:
Also use addCity, updateCity, addCities, getCities, and addCityCountry for the city model:
And also use addUser, addUsers, addUserLivedCities, addUserCountry, addUserCities, addMostLovedCity, removeMostLovedCity, removeLivedCities, updateUser, getUser, and getUsers for the user model:
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:
Here is a screenshot of the schema modal box:
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:
Here is a screenshot of the act modal box:
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:
Here you can import an E2E test config file by clicking the import button:
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.)
-
Create a new country with
mainโcountryโaddCountry:- Here we used the
mainservice, thecountrymodel, and theaddCountryact. - We repeat this section 15 times.
- We captured
countryIdfrom the last response of this section. - Here we used faker to create
name,population, andabbfor the new country.
- Here we used the
-
Create multiple countries with one request using
mainโcountryโaddCountries:- Here we used the
mainservice, thecountrymodel, and theaddCountriesact. - Here we insert an array of
countryfor themultiCountrieskey inside thesetobject. - We captured some country IDs from the request response.
- Here we used the
-
Create a new city with
mainโcityโaddCity:- Here we used
iranId, the captured variable we got from the request-2 response. - We captured
body._idwith the nameharatIdfrom the response.
- Here we used
-
Create multiple cities with
mainโcityโaddCities:- Here we insert an array of
cityfor themultiCitieskey inside thesetobject. - Here we used
iraqId, the captured variable we got from the request-2 response.
- Here we insert an array of
-
Create multiple cities with
mainโcityโaddCities:- Here we insert an array of
cityfor themultiCitieskey inside thesetobject. We also usedfakerhere. - Here we used
afghanId, the captured variable we got from the request-2 response.
- Here we insert an array of
-
Change the country relation of a city with
mainโcityโaddCityCountry:- Here we used the
haratIdandafghanIdcaptured variables. Please check MongoDB Compass, because both sides of the relation are changed.
- Here we used the
-
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity:- Please note that we set the
isCapitalfield totrue, so the capital field of the related country is filled with this city.
- Please note that we set the
-
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Create a new city with
mainโcityโaddCity: -
Get a list of countries with
mainโcountryโgetCountries: -
Create a new user with
mainโuserโaddUser:- The country the user lived in is
Iran. - The user lived in two cities:
HamedanandTehran.
- The country the user lived in is
-
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Create a new user with
mainโuserโaddUser: -
Change the country relation of a user with
mainโuserโaddUserCountry:- 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.
-
Change the country relation of a user with
mainโuserโaddUserCountry: -
Add cities to the
livedCitiesrelation of a user withmainโuserโaddUserLivedCities:- 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.
-
Add cities to the
livedCitiesrelation of a user withmainโuserโaddUserLivedCities: -
Remove cities from the
livedCitiesrelation of a user withmainโuserโremoveLivedCities:- 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.
-
Add a city to the
mostLovedCityrelation of a user withmainโuserโaddMostLovedCity: -
Add a city to the
mostLovedCityrelation of a user withmainโuserโaddMostLovedCity: -
Remove a city from the
mostLovedCityrelation of a user withmainโuserโremoveLivedCities: -
Update a country with
mainโcountryโupdateCountry:- 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
countryfield in all the cities related to that country. - Update the
countryfield 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.
- 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 a city with
mainโcityโupdateCity: -
Update a user with
mainโuserโupdateUser:
After clicking the run E2E test button, you will go to the test results page:
If you scroll down a little, you can see the results of each sequence separately:
- With this button you can change the view of the panel from
body-header & DescriptiontoREQUEST & RESULT. - Shows some description about the sequence, including request number, timing, captured values, and so on.
- Shows the unparsed
headerandbodyyou sent to the backend. - Shows the
indexof each sequence. - Shows the
responsereceived back from the server. - Shows the parsed request you sent to the server, including the parsed
headerandbody. - 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:
You can play with this data in the playground and change everything you want:
fakerTest E2E fileโ
This file is not very important in this section; it's only used to test the faker functions in the E2E:
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.
-
Create a country with
mainโcountryโaddCountry: -
Create
50,000cities withmainโcityโaddCities: -
Create
50,000users withmainโuserโaddUsers:
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:
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:
In the cities, we have embedded the respective country:
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:
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:
Executing main โ country โ updateCountry:
After executing main โ country โ updateCountry:
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.