mlabs-haskell / TuxedoDapp

Cryptokitties on Polkadot using UTXO
Apache License 2.0
2 stars 1 forks source link

Create unit tests for APIs: Part 1 #49

Closed philoniare closed 2 months ago

philoniare commented 2 months ago
NadigerAmit commented 2 months ago

@AltiMario and @philoniare

I have implemented unit tests for the following APIs in their respective service handlers in the web service:

Pre-requisite for UT to pass: Keep running the blockchain, use the sled db, and local keystore as they are.

If we need to mock the blockchain, sled db, and local keystore, then I believe it's a significant effort. We would need to mock all the APIs and inject the mock blockchain, mock db, and mock keystore as dependencies to the Axum server insted of realones when UT is executing. This approach need actual code modification and I think it is not recommanded at this point when we are trying to conclude the project.

So, I opted for the approach of the above pre-requisite.

The following APIs have UTs that are passing:

get_block mint_coins create_kitty

However, for the remaining APIs, I find it challenging to implement the UTs due to dependencies between APIs.

For example, if I want to list a kitty for sale, the following prerequisites are necessary:

  1. The owner's public key needs to be inserted into LKS.
  2. create_kitty from the public key of the owner.
  3. Once the kitty is created, the local db needs to sync with the blockchain db, so that the newly created kitty is present in the local db.
  4. Then we can trigger/test the list kitty for sale.

I have been able to satisfy all the above prerequisites except the third one, i.e., syncing the db is not done immediately after the kitty is created. Hence, when the actual API, i.e., get_txn_and_inpututxolist_for_list_kitty_for_sale(), looks for the kitty to be listed for sale in the local db, it cannot find it and the test case fails. Even if I make the test case sleep for 5 to 10 seconds after the kitty is created and before syncing the db, the local db is still not able to add the newly created kitty, causing the test case to fail. Please see the code : https://github.com/mlabs-haskell/Tuxedo/blob/b8780da09bee6cf004cb12ed7c75bdb9b91fc034/webservice-wallet/src/service_handlers/kitty_handler/kitty_service_handler.rs#L1357-L1377

This situation is the same for all the remaining APIs.

Additionally, the prerequisites for the next APIs will be more extensive and will lead to more test code:

For example, the prerequisites for buying a kitty will be:

  1. Add the public keys of both the buyer and seller.
  2. Create the kitty from the seller's public key.
  3. Mint coins from the buyer's public key.
  4. List the kitty for sale from the seller's public key.
  5. Then trigger the buy kitty API.

So, I think it is going to be a significant effort as we need to write pre-requisite code for all the above steps. Of course, we can reuse the pre-requisite functions if we divide them into functions. But the main blocking point is how to sync the db synchronously before proceeding to the next step.

Below is test result: image