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

Introduce prismock and use it in transaction agent spec #786

Merged
merged 13 commits into from
Feb 28, 2024

Conversation

skosito
Copy link
Collaborator

@skosito skosito commented Jan 29, 2024

Description

  • Adds prismock library. I used this instead of prisma-mock library which is mentioned in issue because it seems maintained more often, and they are doing the same thing anyways.

  • Reworks transaction agent specs to use this library and fix some tests there. NOTE: make sure to expand diff in transaction agent spec since it's hidden in diff by default.

  • Bumps nodejs version to 17.0.0 in workflow because prisma needs it.

  • Cleanup of NOT_FOUND messages in workstep and workflow modules.

  • Once approach is approved I will modify all unit tests in the repo in follow up PR.

Related Issue

#742

Motivation and Context

Better way to write unit tests.

How Has This Been Tested

This modifies tests only, so it is tested by default.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Request to be added as a Code Owner/Maintainer

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I commit to abide by the Responsibilities of Code Owners/Maintainers.

import { AuthAgent } from '../../auth/agent/auth.agent';
import { BpiSubjectAccount } from '../../identity/bpiSubjectAccounts/models/bpiSubjectAccount';
import { BpiSubject } from '../../identity/bpiSubjects/models/bpiSubject';
import { BpiSubjectAccount as ModelBpiSubjectAccount } from '../../identity/bpiSubjectAccounts/models/bpiSubjectAccount';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is the other way around in the prisma mapper when it comes to naming.
Domain objects imported without aliases.
PrismaModels have PrismaModel suffix.
Could we do the same here and use this as a convention throughout the codebase?

We can also turn it around, i do not care as long as we are consistent

// and implement various test data scenarios that can be selected with a single line of code.
// https://github.com/demonsters/prisma-mock
const existingWorkgroupId = uuid();
bpiSubject2 = await prisma.bpiSubject.create({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a lot of builiders implemented in builders.ts.
Could we can use them to define the test domain objects we want to work with and then just have separate methods that abstract away prisma.create calls? This way we can share those methods across the test code base.

This way we could probably ditch the as ModelBpiSubjectAccount syntax as well.

Convetion would be that we work with domain objects in tests, and prisma create calls are only there to avoid mocking the storage agents

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is advantage of using those builders compared to just calling constructors? seems like a redundant wrapper and one more place to maintain same thing as domain object constructor, every time new field is added, prisma schema, domain object and builder have to be updated

not sure i understand the point here, is this the flow:

  1. create object using builder
  2. then use that object to call prisma create (prisma create doesnt accept domain object as argument, we would need to map it or something like that)
  3. then use that same domain object again in tests?

i think these objects are mutable so using them around seems a bit weird, wouldn't it be simpler that every spec file sets up its own data? but no strong opinion here, i can modify if the flow i described above is what you meant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is mainly for readability and maintainability purposes. I agree it might be too much at this point. Maybe just open an issue and we can introduce them later if time allows.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok maybe lets merge this PR and i will apply this to all tests in follow up PR and we can see then what makes sense to be abstracted in builders etc? maybe we can also clean up builders after all tests are adapted

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skosito can you open an issue for this so we can keep track of it? cc @ognjenkurtic @Kasshern

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skosito dont forget to open an issue for this one pls.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure #789

'123',
fromBpiSubjectAccount,
undefined as unknown as BpiSubjectAccount,
workflow.id + '',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the + ''?

Copy link
Collaborator Author

@skosito skosito Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm not sure, but its not needed

workstepStorageAgentMock.getWorkstepById.mockResolvedValueOnce(
existingWorkstep1,
);

const tx = new Transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add transaction builder as well?

Copy link
Collaborator

@Therecanbeonlyone1969 Therecanbeonlyone1969 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modulo the builder comment from @ognjenkurtic which will be addressed in a separate PT I am good

Copy link
Collaborator

@biscuitdey biscuitdey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach looks good to me

@ognjenkurtic ognjenkurtic merged commit 958671d into main Feb 28, 2024
1 of 2 checks passed
@ognjenkurtic ognjenkurtic deleted the feature/742-prisma-mock branch February 28, 2024 21:42
@skosito skosito linked an issue Feb 28, 2024 that may be closed by this pull request
1 task
@skosito skosito mentioned this pull request Feb 28, 2024
2 tasks
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.

SRI - Implement mocking of the Prisma client for unit testing purposes
4 participants