Skip to content
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

Add unit tests for related entities on the generated app #397

Open
1 task done
ivanmonteiro opened this issue Sep 30, 2020 · 7 comments
Open
1 task done

Add unit tests for related entities on the generated app #397

ivanmonteiro opened this issue Sep 30, 2020 · 7 comments
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: server ⚙️ theme: test up-for-grabs $100 https://www.jhipster.tech/bug-bounties/

Comments

@ivanmonteiro
Copy link
Contributor

Overview of the feature request

Add unit tests for related entities on the generated dotnet application.

As an example:
Unit tests for Employee should test adding and removing its Department relationship.

Motivation for or Use Case

Currently the unit tests only adds/removes the main entity.
Changes to the generator may cause issues regarding relationships and are not caught by tests.
This is important to make sure that new PR's are not breaking relationship functionality at this project.

Related issues or PR
  • Checking this box is mandatory (this is just to show you read everything)
@kennethlong
Copy link
Contributor

Does the Java template generate these test? I was going to peek at the implementation if it does.

@ivanmonteiro
Copy link
Contributor Author

Does the Java template generate these test? I was going to peek at the implementation if it does.

Last time I checked the main Jhipster generator didn't generate tests for relationships.
Currently the tests only check if relationship is required.

The idea would be to create unit tests that:

  • creates the main entity
  • also creates the dependant entities
  • saves the entity graph to the database
  • verifies that all entities were created/updated
  • also deletes dependant entities and checks if it was properly deleted

@irwansyahwii
Copy link

@ivanmonteiro can I take this?

@ivanmonteiro
Copy link
Contributor Author

The idea would be to create unit tests that

Sure, if you have questions let me know

@irwansyahwii
Copy link

@ivanmonteiro I just reviewed the generated codes and wants to confirm several things:

The only entities generated are User, Role, and UserRole. So that means we need to create/update/deletion tests that will create/update/delete the User and its Roles. Is this correct?

@ivanmonteiro
Copy link
Contributor Author

ivanmonteiro commented Jul 12, 2024

@ivanmonteiro I just reviewed the generated codes and wants to confirm several things:

The only entities generated are User, Role, and UserRole. So that means we need to create/update/deletion tests that will create/update/delete the User and its Roles. Is this correct?

When we create entities, currently the generator creates ControllerIntTest.cs tests:

It tests controllers actions like save/update/delete and check if the a child entity is null or not. It only checks if it is required.

But it does not create child entities and save together with the parent. Like if we have a Person entity that has an Address child entity, it does not create the the Person entity with its Address.

Ideally whe should have tests that both creates the parent AND child entity, and tests the relationship:

  • a one-to-many relationship: we create the parend with a few child entities, save it, the delete one of child then check if it was persisted correctly.
  • a one-to-one relationship: we create the parent with the child, try to modify a value in the child, save the parent, check if it was persisted correctly.

So the _persistClass_ControllerIntTest.cs.ejs template can be a starting point, but I have a suggestion:

Keep it simple, if you can.

The template file I mentioned (_persistClass_ControllerIntTest.cs.ejs) is more like an integration test (more complex). But this issue could probably be solved creating unit tests (simple), and could use the repository (or service classes) directly in the test instead of creating an httpClient, calling the controllers, etc...

If you have more questions feel free to ask.

@irwansyahwii
Copy link

@ivanmonteiro every time I tried to add a relationship to an entity it was always error:

================= Student =================
Fields
name (String) 

Relationships
lecturers (Lecturer) one-to-many



ERROR! An error occured while running jhipster-dotnetcore:entity#askForTableName
ERROR! ERROR! Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')
    at default.askForTableName (file:///Users/irwansyah/Documents/PROJECTS/jhipster-dotnetcore/test-ride-entities/node_modules/generator-jhipster-dotnetcore/generators/entity/prompts.js:205:21)
    at default.executeTask (file:///Users/irwansyah/Documents/PROJECTS/jhipster-dotnetcore/test-ride-entities/node_modules/yeoman-generator/dist/actions/lifecycle.js:244:26)
    at env.queueTask.once (file:///Users/irwansyah/Documents/PROJECTS/jhipster-dotnetcore/test-ride-entities/node_modules/yeoman-generator/dist/actions/lifecycle.js:218:56)
    at runLoop.add.once (file:///Users/irwansyah/.nvm/versions/node/v20.15.1/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/dist/environment-base.js:384:23)
    at Immediate.<anonymous> (/Users/irwansyah/.nvm/versions/node/v20.15.1/lib/node_modules/generator-jhipster/node_modules/grouped-queue/lib/subqueue.js:48:34)
    at process.processImmediate (node:internal/timers:478:21)
(base) irwansyah@Irwansyahs-MacBook-Pro test-ride-entities % 

This error happens in two scenarios:

  1. I add a new entity and specified a relationship to non existence entity
  2. I added two entities and when I tried to add a relationship to one of them

Should I fix the error first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: server ⚙️ theme: test up-for-grabs $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

No branches or pull requests

4 participants