-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cockroachdb.test.ts
127 lines (106 loc) · 5.23 KB
/
cockroachdb.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// describeIf is making eslint unhappy about the test names
/* eslint-disable jest/no-identical-title */
import { jestConsoleContext, jestContext, jestProcessContext } from '@prisma/get-platform'
import path from 'path'
import { DbPull } from '../../commands/DbPull'
import { setupCockroach, tearDownCockroach } from '../../utils/setupCockroach'
const isMacOrWindowsCI = Boolean(process.env.CI) && ['darwin', 'win32'].includes(process.platform)
if (isMacOrWindowsCI) {
jest.setTimeout(60_000)
}
const describeIf = (condition: boolean) => (condition ? describe : describe.skip)
const ctx = jestContext.new().add(jestConsoleContext()).add(jestProcessContext()).assemble()
// To avoid the loading spinner locally
process.env.CI = 'true'
const originalEnv = { ...process.env }
describeIf(!process.env.TEST_SKIP_COCKROACHDB)('cockroachdb', () => {
if (!process.env.TEST_SKIP_COCKROACHDB && !process.env.TEST_COCKROACH_URI_MIGRATE) {
throw new Error('You must set a value for process.env.TEST_COCKROACH_URI_MIGRATE. See TESTING.md')
}
const connectionString = process.env.TEST_COCKROACH_URI_MIGRATE?.replace(
'tests-migrate',
'tests-migrate-db-pull-cockroachdb',
)
const setupParams = {
connectionString: connectionString!,
// Note: at this location there is a setup.sql file
// which will be executed a SQL file so the database is not empty
dirname: path.join(__dirname, '..', '..', '__tests__', 'fixtures', 'introspection', 'cockroachdb'),
}
beforeAll(async () => {
await tearDownCockroach(setupParams).catch((e) => {
console.error(e)
})
})
beforeEach(async () => {
await setupCockroach(setupParams).catch((e) => {
console.error(e)
})
// Back to original env vars
process.env = { ...originalEnv }
// Update env var because it's the one that is used in the schemas tested
process.env.TEST_COCKROACH_URI_MIGRATE = connectionString
})
afterEach(async () => {
// Back to original env vars
process.env = { ...originalEnv }
await tearDownCockroach(setupParams).catch((e) => {
console.error(e)
})
})
test('basic introspection (with cockroachdb provider)', async () => {
ctx.fixture('introspection/cockroachdb')
const introspect = new DbPull()
const result = introspect.parse(['--print'])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
test('basic introspection (with postgresql provider) should fail', async () => {
ctx.fixture('introspection/cockroachdb')
const introspect = new DbPull()
const result = introspect.parse(['--print', '--schema', 'with-postgresql-provider.prisma'])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
You are trying to connect to a CockroachDB database, but the provider in your Prisma schema is \`postgresql\`. Please change it to \`cockroachdb\`.
`)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
test('basic introspection (no schema) --url', async () => {
ctx.fixture('introspection/cockroachdb')
const introspect = new DbPull()
const result = introspect.parse(['--print', '--url', setupParams.connectionString])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
test('basic introspection (with cockroach provider) --url ', async () => {
ctx.fixture('introspection/cockroachdb')
const introspect = new DbPull()
const result = introspect.parse(['--print', '--url', setupParams.connectionString])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
test('basic introspection (with postgresql provider) --url should fail', async () => {
ctx.fixture('introspection/cockroachdb')
const introspect = new DbPull()
const result = introspect.parse([
'--print',
'--url',
setupParams.connectionString,
'--schema',
'with-postgresql-provider.prisma',
])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
You are trying to connect to a CockroachDB database, but the provider in your Prisma schema is \`postgresql\`. Please change it to \`cockroachdb\`.
`)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
})