Skip to content

Get back the data you just inserted in its new state in a variable #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ibrahimhammani
Copy link

Hello all,

Sometimes I have some generated fields in my entity like ID, Creation date ...
So when I insert my data I want to get back my entity with the exact value of the generated field.
I add some steps that allows to do that as follows :

  Scenario: we can insert data and retrieve the persisted objects in a variable using table
    Given that the users table will contain the following data available in users_list:
      """yaml
      - firstName: Darth
        lastName: Vader
      """

    Then users_list contains:
    """yaml
    - id: 1
      firstName: Darth
      lastName: Vader
    """

Please check the PR for more details.

Regards

@CLAassistant
Copy link

CLAassistant commented Jun 15, 2025

CLA assistant check
All committers have signed the CLA.

@alexandrepa
Copy link
Collaborator

Does it not the same as doing :

    Then the users table contains:
    """yaml
    - id: 1
      firstName: Darth
      lastName: Vader
    """

?

@ibrahimhammani
Copy link
Author

Hello
Thanks for your reply!
It's not, this new step will save the data and set the saved data in a new variable right away.

Off course I'm talking about that step:

Given that the users table will contain the following data available in users_list:

In the exemple I gave the variable users_list will container the saved data, with the ID and any other generated data like création date.

If it's not clear please let me know

@alexandrepa
Copy link
Collaborator

@ibrahimhammani
Copy link
Author

They are similar, but not the same
since my step returns back the entities I just saved only

@alexandrepa
Copy link
Collaborator

alexandrepa commented Jun 19, 2025

In this case, I would prefer in the jpa step to do a generic
objects.add("_savedEntites", repository.saveAll(...));

And after you can use this steps to add it in a variable :

And that users_list is "{{[_savedEntites]}}"

Your test case become :

Scenario: we can insert data and retrieve the persisted objects in a variable using table
  Given that the users table will contain:
    """yaml
    - firstName: Darth
      lastName: Vader
    """
  And that users_list is "{{[_savedEntites]}}"

  Then users_list contains:
    """yaml
    - id: 1
    firstName: Darth
    lastName: Vader
    """

It is a little more verbose but more flexible for other use cases.
What do you think ?

@ibrahimhammani
Copy link
Author

ibrahimhammani commented Jun 20, 2025

This could be a good idea however, users might not remember that specific detail
Especially if not documented
And even if it is I'm afraid developers will lost it in README.MD files
And above it All I can use multiple variable names but If I use _savedEntites, It will override it the next step saving entities

PS: I will definitly add this: `objects.add("_savedEntites", repository.saveAll(...));` in another PR

@alexandrepa
Copy link
Collaborator

@jak78 What do you think about it ?

@jak78
Copy link
Contributor

jak78 commented Jun 20, 2025

Hey @ibrahimhammani thanks for the suggestion. Could you help me understand what the already implemented feature cannot do? I think the example you provided can be written as follows:

  Scenario: we can insert data and retrieve the persisted objects in a variable using table
    Given that the users table will contain:
      """yaml
      - firstName: Darth
        lastName: Vader
      """
    And users_list is the users table content
    Then users_list contains:
    """yaml
    - id: 1
      firstName: Darth
      lastName: Vader
    """

@ibrahimhammani
Copy link
Author

Thank you for your prompt response.

It appears that none of the existing steps can accommodate my specific requirement. Allow me to illustrate with a real-world example:

I'm working with a JPA entity that uses a complex ID generator. The generated ID follows a specific pattern, similar to PREFIX_UUID_SUFFIX.

When I insert a new record into the database, I need to retrieve it immediately with its generated ID. This is crucial because I then need to use this ID in next steps, such as calling an external API with that generated ID.

My objective isn't to test the value of the generated data (ID) , but rather to immediately obtain and utilize the new data, including the ID, generated by Hibernate/JPA.

Currently, if I use steps like:

Then the users table contains:
"""yaml
...
"""

or

And users_list is the users table content

These will retrieve all data from the table, making it impossible to identify the specific record I just inserted and need to work with. This is especially problematic when test backgrounds insert data into the same table or when I need to insert a large volume of data sequentially.

To summarize, my goal is to:

  • Immediately retrieve the data I just inserted.
  • Utilize the generated value (specifically the ID) directly, rather than just testing its value.
  • Obtain only the specific item I want, not the entire table content.

Please let me know if you have any questions.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants