jhipster / jhipster-dotnetcore

JHipster.NET blueprint
Apache License 2.0
309 stars 93 forks source link

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

Open ivanmonteiro opened 3 years ago

ivanmonteiro commented 3 years ago
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
kennethlong commented 2 years ago

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

ivanmonteiro commented 2 years ago

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:

irwansyahwii commented 1 month ago

@ivanmonteiro can I take this?

ivanmonteiro commented 1 month ago

The idea would be to create unit tests that

Sure, if you have questions let me know

irwansyahwii commented 1 month ago

@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 commented 1 month ago

@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: https://github.com/jhipster/jhipster-dotnetcore/blob/e9d40ccec16c5ab4efea8645a73fa53a9d6a620a/generators/dotnetcore/templates/dotnetcore/test/Project.Test/Controllers/_persistClass_ControllerIntTest.cs.ejs#L206

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:

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 commented 1 month ago

@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?