Skip to content

Commit

Permalink
test(client): add tests for buildNFTAnnotations (#11954)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Mar 11, 2022
1 parent b355c55 commit 06f04d2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
126 changes: 126 additions & 0 deletions packages/client/src/__tests__/buildNFTAnnotations.test.ts
@@ -0,0 +1,126 @@
import { ClientEngineType } from '@prisma/sdk'

import { buildNFTAnnotations } from '../generation/utils/buildNFTAnnotations'

function normalizePaths(snapshot: string): string {
if (process.platform === 'win32') {
return snapshot.replace(/\\\\/g, '/')
}
return snapshot
}

describe('library', () => {
it('generates annotations for a schema and a single engine', () => {
const annotations = buildNFTAnnotations(ClientEngineType.Library, ['debian-openssl-1.1.x'], 'out')

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`
path.join(__dirname, "libquery_engine-TEST_PLATFORM.LIBRARY_TYPE.node");
path.join(process.cwd(), "out/libquery_engine-TEST_PLATFORM.so.node")
path.join(__dirname, "schema.prisma");
path.join(process.cwd(), "out/schema.prisma")
`)
})

it('generates annotations for a schema and multiple engines', () => {
const annotations = buildNFTAnnotations(
ClientEngineType.Library,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
)

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`
path.join(__dirname, "libquery_engine-TEST_PLATFORM.LIBRARY_TYPE.node");
path.join(process.cwd(), "out/libquery_engine-TEST_PLATFORM.so.node")
path.join(__dirname, "libquery_engine-TEST_PLATFORM.dylib.node");
path.join(process.cwd(), "out/libquery_engine-TEST_PLATFORM.dylib.node")
path.join(__dirname, "query_engine-TEST_PLATFORM.dll.node");
path.join(process.cwd(), "out/query_engine-TEST_PLATFORM.dll.node")
path.join(__dirname, "schema.prisma");
path.join(process.cwd(), "out/schema.prisma")
`)
})
})

describe('binary', () => {
it('generates annotations for a schema and a single engine', () => {
const annotations = buildNFTAnnotations(ClientEngineType.Binary, ['debian-openssl-1.1.x'], 'out')

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`
path.join(__dirname, "query-engine-TEST_PLATFORM");
path.join(process.cwd(), "out/query-engine-TEST_PLATFORM")
path.join(__dirname, "schema.prisma");
path.join(process.cwd(), "out/schema.prisma")
`)
})

it('generates annotations for a schema and multiple engines', () => {
const annotations = buildNFTAnnotations(
ClientEngineType.Binary,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
)

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`
path.join(__dirname, "query-engine-TEST_PLATFORM");
path.join(process.cwd(), "out/query-engine-TEST_PLATFORM")
path.join(__dirname, "query-engine-TEST_PLATFORM");
path.join(process.cwd(), "out/query-engine-TEST_PLATFORM")
path.join(__dirname, "query-engine-TEST_PLATFORM");
path.join(process.cwd(), "out/query-engine-TEST_PLATFORM")
path.join(__dirname, "schema.prisma");
path.join(process.cwd(), "out/schema.prisma")
`)
})
})

describe('dataproxy', () => {
it('generates no annotations', () => {
const annotations = buildNFTAnnotations(
ClientEngineType.DataProxy,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
)

// TODO: when using .toMatchInlineSnapshot(), this fails after updating snapshots.
// Probably an issue with the snapshot serializer?
expect(normalizePaths(annotations)).toBe(`
`)
})
})

describe('special cases', () => {
/**
* The build image (Debian) is different from the runtime image (RHEL) on Netlify,
* so the build-time targets are replaced with what will actually be required at run time.
*/
it('replaces platforms with ["rhel-openssl-1.0.x"] on Netlify', () => {
process.env.NETLIFY = 'true'

const annotations = buildNFTAnnotations(
ClientEngineType.Library,
['debian-openssl-1.1.x', 'darwin', 'windows'],
'out',
)

delete process.env.NETLIFY

expect(normalizePaths(annotations)).toMatchInlineSnapshot(`
path.join(__dirname, "libquery_engine-TEST_PLATFORM.LIBRARY_TYPE.node");
path.join(process.cwd(), "out/libquery_engine-TEST_PLATFORM.so.node")
path.join(__dirname, "schema.prisma");
path.join(process.cwd(), "out/schema.prisma")
`)

expect(annotations).toContain('rhel-openssl-1.0.x')
})
})
2 changes: 1 addition & 1 deletion packages/client/src/generation/TSClient/TSClient.ts
Expand Up @@ -37,7 +37,7 @@ export interface TSClientOptions {
browser?: boolean
datasources: InternalDatasource[]
generator?: GeneratorConfig
platforms?: Platform[]
platforms?: Platform[] // TODO: consider making it non-nullable
sqliteDatasourceOverrides?: DatasourceOverwrite[]
schemaDir: string
outputDir: string
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/generation/utils/buildNFTAnnotations.ts
Expand Up @@ -21,6 +21,8 @@ export function buildNFTAnnotations(
relativeOutdir: string,
) {
if (platforms === undefined) {
// TODO: should we still build the schema annotations in this case?
// Or, even better, make platforms non-nullable in TSClientOptions to avoid this check.
return ''
}

Expand Down

0 comments on commit 06f04d2

Please sign in to comment.