Skip to content

Commit

Permalink
fix(cli) Generated services use Date objects for DateTime types in th…
Browse files Browse the repository at this point in the history
…e tests (#6394)

* fix(cli) generate Date objects for DateTime types

* use Date object for update test

* added the missing millisecond to the comment

* undo changes to service scenarios generator

* return stringified scenario directly

* make DateTime types test simpler

* test the validity od the date

* use new date(...) only in the assertions

* refactor template

* better documentation on extractExpectedValue

* add tests for scalar types

* change documentation

* update documentation tests

* use a static date so that snapshots don't change

* service test: Mock timers

* Update test project fixture

* Revert changes to testing.md

* Date default constructor

Co-authored-by: Tobbe Lundberg <[email protected]>
  • Loading branch information
esteban-url and Tobbe authored Sep 30, 2022
1 parent 874bb71 commit 6e2cd52
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import type { StandardScenario } from './contacts.scenarios'

// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String7848705',
email: 'String9043010',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand All @@ -24,7 +24,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String1920513',
email: 'String3280784',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { posts, post, createPost, updatePost, deletePost } from './posts'
import type { StandardScenario } from './posts.scenarios'

// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export const standard = defineScenario<Prisma.UserCreateArgs>({
user: {
one: {
data: {
email: 'String7590248',
email: 'String4277909',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
},
},
two: {
data: {
email: 'String2966925',
email: 'String493680',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand Down
1 change: 1 addition & 0 deletions docs/docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ In the above examples, we can see expect doesn't preserve the floating numbers.
> For cases where using decimal is not optimal, see the [Jest Expect documentation](https://jestjs.io/docs/expect) for other options and methods.
#### DateTime

Prisma returns [DateTime](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#datetime) as ISO 8601-formatted strings. So, you can convert the date to ISO String in JavaScript:

```jsx {1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ exports[`handler can be called with PascalCase model name 3`] = `
"fileContent": "import { users, user, createUser, updateUser, deleteUser } from './users'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -205,7 +205,7 @@ exports[`handler can be called with PascalCase model name 7`] = `
} from './customDatums'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -353,7 +353,7 @@ exports[`handler can be called with camelCase model name 3`] = `
"fileContent": "import { users, user, createUser, updateUser, deleteUser } from './users'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -501,7 +501,7 @@ exports[`handler can be called with camelCase model name 7`] = `
} from './customDatums'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`in javascript mode creates a multi word service test file 1`] = `
"import { userProfiles } from './userProfiles'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand All @@ -38,7 +38,7 @@ exports[`in javascript mode creates a multi word service test file with crud act
} from './transactions'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -90,6 +90,78 @@ describe('transactions', () => {
"
`;

exports[`in javascript mode creates a multi word service test file with multiple scalar types 1`] = `
"import {
scalarTypes,
scalarType,
createScalarType,
updateScalarType,
deleteScalarType,
} from './scalarTypes'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
describe('scalarTypes', () => {
scenario('returns all scalarTypes', async (scenario) => {
const result = await scalarTypes()
expect(result.length).toEqual(Object.keys(scenario.scalarType).length)
})
scenario('returns a single scalarType', async (scenario) => {
const result = await scalarType({ id: scenario.scalarType.one.id })
expect(result).toEqual(scenario.scalarType.one)
})
scenario('creates a scalarType', async () => {
const result = await createScalarType({
input: {
email: 'String',
date: '2022-09-30T09:50:00.000Z',
bigInt: 1234567n,
integer: 1234567,
boolean: true,
},
})
expect(result.email).toEqual('String')
expect(result.date).toEqual(new Date('2022-09-30T09:50:00.000Z'))
expect(result.bigInt).toEqual(1234567n)
expect(result.integer).toEqual(1234567)
expect(result.boolean).toEqual(true)
})
scenario('updates a scalarType', async (scenario) => {
const original = await scalarType({
id: scenario.scalarType.one.id,
})
const result = await updateScalarType({
id: original.id,
input: { email: 'String2' },
})
expect(result.email).toEqual('String2')
})
scenario('deletes a scalarType', async (scenario) => {
const original = await deleteScalarType({
id: scenario.scalarType.one.id,
})
const result = await scalarType({ id: original.id })
expect(result).toEqual(null)
})
})
"
`;

exports[`in javascript mode creates a single word service file 1`] = `
"import { db } from 'src/lib/db'
Expand Down Expand Up @@ -238,7 +310,7 @@ exports[`in javascript mode creates a single word service test file 1`] = `
"import { users, user, createUser, updateUser, deleteUser } from './users'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -302,7 +374,7 @@ import { userProfiles } from './userProfiles'
import type { StandardScenario } from './userProfiles.scenarios'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -330,7 +402,7 @@ import {
import type { StandardScenario } from './transactions.scenarios'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down Expand Up @@ -383,6 +455,82 @@ describe('transactions', () => {
"
`;
exports[`in typescript mode creates a multi word service test file with multiple scalar types 1`] = `
"import type { ScalarType } from '@prisma/client'
import {
scalarTypes,
scalarType,
createScalarType,
updateScalarType,
deleteScalarType,
} from './scalarTypes'
import type { StandardScenario } from './scalarTypes.scenarios'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
describe('scalarTypes', () => {
scenario('returns all scalarTypes', async (scenario: StandardScenario) => {
const result = await scalarTypes()
expect(result.length).toEqual(Object.keys(scenario.scalarType).length)
})
scenario(
'returns a single scalarType',
async (scenario: StandardScenario) => {
const result = await scalarType({ id: scenario.scalarType.one.id })
expect(result).toEqual(scenario.scalarType.one)
}
)
scenario('creates a scalarType', async () => {
const result = await createScalarType({
input: {
email: 'String',
date: '2022-09-30T09:50:00.000Z',
bigInt: 1234567n,
integer: 1234567,
boolean: true,
},
})
expect(result.email).toEqual('String')
expect(result.date).toEqual(new Date('2022-09-30T09:50:00.000Z'))
expect(result.bigInt).toEqual(1234567n)
expect(result.integer).toEqual(1234567)
expect(result.boolean).toEqual(true)
})
scenario('updates a scalarType', async (scenario: StandardScenario) => {
const original = (await scalarType({
id: scenario.scalarType.one.id,
})) as ScalarType
const result = await updateScalarType({
id: original.id,
input: { email: 'String2' },
})
expect(result.email).toEqual('String2')
})
scenario('deletes a scalarType', async (scenario: StandardScenario) => {
const original = (await deleteScalarType({
id: scenario.scalarType.one.id,
})) as ScalarType
const result = await scalarType({ id: original.id })
expect(result).toEqual(null)
})
})
"
`;
exports[`in typescript mode creates a single word service file 1`] = `
"import type { QueryResolvers, MutationResolvers } from 'types/graphql'
Expand Down Expand Up @@ -549,7 +697,7 @@ import { users, user, createUser, updateUser, deleteUser } from './users'
import type { StandardScenario } from './users.scenarios'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float and DateTime types.
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ model Favorite {
post Post @relation(fields: [postId], references: [id])
likes BigInt
}

model ScalarType{
id Int @id @default(autoincrement())
email String
date DateTime
bigInt BigInt
integer Int
boolean Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ describe('the scenario generator', () => {
expect(typeof value).toBe('number')
})

test('scenarioFieldValue returns an ISO8601 timestamp string for DateTime types', () => {
test('scenarioFieldValue returns a valid Date for DateTime types', () => {
const field = { type: 'DateTime' }
const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
const value = service.scenarioFieldValue(field)

expect(service.scenarioFieldValue(field)).toMatch(iso8601Regex)
expect(value instanceof Date).toBe(true)
expect(!isNaN(value)).toBe(true)
})

test('scenarioFieldValue returns JSON for Json types', () => {
Expand Down
Loading

0 comments on commit 6e2cd52

Please sign in to comment.