cancel
Showing results for 
Search instead for 
Did you mean: 

Nested DataSource loops

Stalsy
Occasional Contributor

Nested DataSource loops

Hi, I am new to ReadAPI and API testing, so I’m learning as I go along…

I’m using data driven testing and have the following issue:

My rest request uses the customerID to get information about the customer’s accounts.  So, the response is a json array that in its simplest form would look something like below (the real one has much more information per account):

{

   "customerId" : “11111”,

      "accounts" : [

         {

            "accountId" : "1",

            "creationDate" : "2001-04-10",

            "currentBalance" : "10.00"

         },

         {

            "accountId" : "2",

            "creationDate" : "2003-01-30",

            "currentBalance" : "20.00"

         },

         {

            "accountId" : "23",

            "creationDate" : "2020-04-27",

            "currentBalance" : "30.00"

         }

      ]

   }

 

My problem is that one customer may have 1 account and the next one 30 accounts.  Data driven testing works with a fixed number of rows per iteration.  In previous tests I used the listagg function to collect the information of multiple rows in a single row, but listagg will not work here because there is a lot of information per account, plus I have customers with more than 100 accounts!

 

So the way I solved this is with nested dataSource loops:

  • The first SQL returns a bunch of customerIDs and the number of active accounts each customer has (dataSource1).
  • Then comes the REST request for a customer ID
  • Then I have a groovy script to test that the API ahs returned the number of accounts I expected
  • Then another SQL script to fetch the customer’s account information: accountID, creation Date and currentBalance (dataSource2).
  • Following is another groovy script to check that the accountID, creation Date and currentBalance of dataSource2 exist in the REST response
  • Loop to Datasource2
  • Loop to REST request

 

This solution works well but obviously apart from the initial query that it is fired only once,  it also fires a database query once per customerID, so it is not as efficient as my previous tests where a single DB query was enough to satisfy the whole test.

My question is, has anyone used an alternative method that might be more time efficient?

 

 

8 REPLIES 8
nmrao
Community Hero

Re: Nested DataSource loops

Please the screen shot of test case.

Is it that you need to create request per customer and send the request? or you can send all the customers into single request and send?

Is it that you fetch the data from DB in order create the request?


Regards,
Rao.
nmrao
Community Hero

Re: Nested DataSource loops

Is it possible to get the data in one pass Instead of two passes?


Regards,
Rao.
richie
Community Hero

Re: Nested DataSource loops

Hi,

Im probably not understanding something obvious here, but if the issues is the customers can have 1 to many accounts, could you not just have an initial query to retrieve x number or customer records where number or accounts is =1?

Or is it part of your testable requirement you need to cover off scenarios where there arw multiple accounts per customer?

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Stalsy
Occasional Contributor

Re: Nested DataSource loops

Most customers have multiple accounts. I want to test as many scenarios as possible.  I could write a test to select customers with 1 account, another to test customers with 2 accounts, another test with 3 and so on.  Where do I stop? How many tests will I need?  We have a customer with 130 accounts! All my tests work with random selections of customers so that as I run them over and again, I'm testing different customers.  If I fix the number of accounts, then I'll certainly excluding the customers that don't have that number of accounts! 

Stalsy
Occasional Contributor

Re: Nested DataSource loops

Of course I can get all the data in one pass, but it will look like the following, if say the Customer1 has one account, customer2 has 3 accounts, customer3 has one account and customer4 has four accounts :

Customer1, account11, balance11

customer2,account21,balance21

customer2,account22,balance22

customer2,account23,balance23

customer3,account31,balance31

customer4,account41,balance41

customer4,account42,balance42

customer4,account43,balance43

customer4,account44,balance44

I don't know how to process this data.  The API accepts one customerID and returns a json containing information on all the accounts for the customer.  

nmrao
Community Hero

Re: Nested DataSource loops

Based on the data you shared, it seems you already did good.

However, it is possible to do entirely in single groovy script too if one wishes to do so. It will be little lengthy.

Are building json after getting the the query result for each customer id?
And how do you verify the accounts for the each customer id? Compare against SQL result?


Regards,
Rao.
Stalsy
Occasional Contributor

Re: Nested DataSource loops

This is how my test looks like:

  1. Sterp1: Outer loop DataSource( returns customerId and number of accounts for each customer from DB) For the example I shared earlier, this select would return 4 lines:
  2. Customer1,1 (customer1 has one account)
  3. customer2, 3 (customer2 has three accounts)
  4. customer3, 1 (customer3 has one account)
  5. customer4, 4 (customer4 has four accounts)
  6. Step2: Call API using customer ID. API returns json with details for all the accounts of the customer.
  7. Step3: Assert that the API retuned the correct number of accounts
  8. Step4: Inner Loop Datasource (returns the account details for all the customer's accounts) 
  9. Step5: Assert that the account details of the current row exist in the json response
  10. Step6: Inner Data Source loop
  11. Step7: Outer Data Source Loop

So, in the outer loop I check that the API response json has the correct number of accounts for the customer, and in the inner loop I check that each account is correctly represented in the response json.

So for the above example, the outer loop would be executed 4 times and the API would be called 4 times, once per customer.  The inner loop would be executed once for Customer1, 3 times for customer2, once for customer3 and 4 times for customer4.

 

"However, it is possible to do entirely in single groovy script too if one wishes to do so. It will be little lengthy."  

I still can't see how to I can do this using just a simple loop!

nmrao
Community Hero

Re: Nested DataSource loops

Not sure with the datasource and one loop since you have varied number of rows per customer id.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors