Skip to content

Commit

Permalink
Convert tests to use db.provider over db.adapter (keystonejs#5387)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Apr 6, 2021
1 parent dce149a commit 406acca
Show file tree
Hide file tree
Showing 69 changed files with 415 additions and 422 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-seals-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/test-utils-legacy': major
---

Replaced type `AdapterName` with `ProviderName`. Updated all functions which accepted an `AdapterName` value named `adapterName` to accept a `ProviderName` argument named `provider`.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
fail-fast: false
matrix:
index: [0, 1, 2, 3, 4, 5, 6, 7, 8]
adapter: ['prisma_postgresql', 'prisma_sqlite']
adapter: ['postgresql', 'sqlite']
steps:
- name: Checkout Repo
uses: actions/checkout@v2
Expand Down Expand Up @@ -153,7 +153,7 @@ jobs:
CI_NODE_TOTAL: 9
CI_NODE_INDEX: ${{ matrix.index }}
TEST_ADAPTER: ${{ matrix.adapter }}
DATABASE_URL: ${{ matrix.adapter == 'prisma_sqlite' && 'file:./dev.db' || 'postgres://keystone5:k3yst0n3@localhost:5432/test_db' }}
DATABASE_URL: ${{ matrix.adapter == 'sqlite' && 'file:./dev.db' || 'postgres://keystone5:k3yst0n3@localhost:5432/test_db' }}

non-api-tests:
name: Package Unit Tests
Expand Down
8 changes: 4 additions & 4 deletions examples-next/ecommerce/tests/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { KeystoneContext } from '@keystone-next/types';
import {
multiAdapterRunners,
setupFromConfig,
AdapterName,
ProviderName,
testConfig,
} from '@keystone-next/test-utils-legacy';
import config from '../keystone';

function setupKeystone(adapterName: AdapterName) {
function setupKeystone(provider: ProviderName) {
return setupFromConfig({
adapterName,
provider,
config: testConfig({
lists: config.lists,
extendGraphqlSchema: config.extendGraphqlSchema,
Expand All @@ -23,7 +23,7 @@ const FAKE_ID = 12345;
const asUser = (context: KeystoneContext, itemId?: number) =>
context.exitSudo().withSession({ itemId, data: {} });

multiAdapterRunners('prisma_postgresql').map(({ runner }) =>
multiAdapterRunners('postgresql').map(({ runner }) =>
describe(`Custom mutations`, () => {
describe('checkout(token)', () => {
const token = 'TOKEN'; // This is not currently used by the mutation
Expand Down
1 change: 1 addition & 0 deletions examples-next/next-lite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# @keystone-next/example-next-lite

## 1.0.4

### Patch Changes

- Updated dependencies [[`901817fed`](https://github.com/keystonejs/keystone/commit/901817fedf4bcfb269416c3c68093ae0263f4d00), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`95fefaf81`](https://github.com/keystonejs/keystone/commit/95fefaf815204d6af6e407690f44750f500602e3), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`1a4db6c87`](https://github.com/keystonejs/keystone/commit/1a4db6c87c17706c8e5db2816e0a6b1b8f79e217), [`b40016301`](https://github.com/keystonejs/keystone/commit/b40016301dab71630068cc86c04828c5ee1683e8), [`5c4b48636`](https://github.com/keystonejs/keystone/commit/5c4b4863638cffa794dd1b02c445a87655a4178c), [`192393d0d`](https://github.com/keystonejs/keystone/commit/192393d0df67e123a694a42dd3f95ffa6d40042b), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`1886b4323`](https://github.com/keystonejs/keystone/commit/1886b43235e50bd2e070350d258f0a3145c19bbc), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`399e6db39`](https://github.com/keystonejs/keystone/commit/399e6db39c51cf9e8bbf3dde0887e5bf55dd1c4d), [`8665cfe66`](https://github.com/keystonejs/keystone/commit/8665cfe66016e0356681413e31f80a6d5586d364), [`1886b4323`](https://github.com/keystonejs/keystone/commit/1886b43235e50bd2e070350d258f0a3145c19bbc), [`399e6db39`](https://github.com/keystonejs/keystone/commit/399e6db39c51cf9e8bbf3dde0887e5bf55dd1c4d), [`d93bab17b`](https://github.com/keystonejs/keystone/commit/d93bab17b69c76e57580dc00e41314215da6d49b), [`5cd94b2a3`](https://github.com/keystonejs/keystone/commit/5cd94b2a32b3eddaf00ad77229f7e9664899c3b9), [`9e450d6b3`](https://github.com/keystonejs/keystone/commit/9e450d6b326e2ba5f46e49ecf53b6bd7a627e9ca), [`1e6d12f47`](https://github.com/keystonejs/keystone/commit/1e6d12f47076816d2a2441b42471176c5a7f2f8c), [`c28e765d1`](https://github.com/keystonejs/keystone/commit/c28e765d12655f802e324b82529fcf571d88c0c6), [`bc21855a7`](https://github.com/keystonejs/keystone/commit/bc21855a7ff6dd4dbc278b3e15c9157de765e6ba)]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const fieldName = 'orderNumber';
export const skipCreateTest = false;
export const skipUpdateTest = true;

export const unSupportedAdapterList = ['prisma_sqlite'];
export const unSupportedAdapterList = ['sqlite'];

// Be default, `AutoIncrement` are read-only. But for `isRequired` test purpose, we need to bypass these restrictions.
export const fieldConfig = (matrixValue: MatrixValue) => ({
Expand Down
10 changes: 5 additions & 5 deletions packages-next/fields/src/types/decimal/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdapterName } from '@keystone-next/test-utils-legacy';
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { decimal } from '..';
import { text } from '../../text';

Expand All @@ -8,7 +8,7 @@ export const exampleValue = () => '6.28';
export const exampleValue2 = () => '6.45';
export const supportsUnique = true;
export const fieldName = 'price';
export const unSupportedAdapterList = ['prisma_sqlite'];
export const unSupportedAdapterList = ['sqlite'];

export const getTestFields = () => ({
name: text(),
Expand Down Expand Up @@ -37,10 +37,10 @@ export const storedValues = () => [
{ name: 'price7', price: null },
];

export const supportedFilters = (adapterName: AdapterName) => [
export const supportedFilters = (provider: ProviderName) => [
'null_equality',
'equality',
'ordering',
adapterName !== 'prisma_postgresql' && 'in_empty_null',
adapterName !== 'prisma_postgresql' && 'in_equal',
provider !== 'postgresql' && 'in_empty_null',
provider !== 'postgresql' && 'in_equal',
];
10 changes: 5 additions & 5 deletions packages-next/fields/src/types/text/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdapterName } from '@keystone-next/test-utils-legacy';
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { text } from '..';

export const name = 'Text';
Expand Down Expand Up @@ -32,12 +32,12 @@ export const storedValues = () => [
{ name: 'g', testField: null },
];

export const supportedFilters = (adapterName: AdapterName) => [
export const supportedFilters = (provider: ProviderName) => [
'null_equality',
'equality',
adapterName !== 'prisma_sqlite' && 'equality_case_insensitive',
provider !== 'sqlite' && 'equality_case_insensitive',
'in_empty_null',
'in_value',
adapterName !== 'prisma_sqlite' && 'string',
adapterName !== 'prisma_sqlite' && 'string_case_insensitive',
provider !== 'sqlite' && 'string',
provider !== 'sqlite' && 'string_case_insensitive',
];
104 changes: 51 additions & 53 deletions packages-next/fields/src/types/timestamp/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getItems } from '@keystone-next/server-side-graphql-client-legacy';
import { AdapterName } from '@keystone-next/test-utils-legacy';
import { ProviderName } from '@keystone-next/test-utils-legacy';
import { KeystoneContext } from '@keystone-next/types';
import { text } from '../../text';
import { timestamp } from '..';
Expand Down Expand Up @@ -62,63 +62,61 @@ export const filterTests = (withKeystone: (args: any) => any) => {

test(
'Sorting: sortBy: lastOnline_ASC',
withKeystone(
({ context, adapterName }: { context: KeystoneContext; adapterName: AdapterName }) =>
match(
context,
undefined,
adapterName === 'prisma_sqlite'
? [
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
]
: [
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
],
'lastOnline_ASC'
)
withKeystone(({ context, provider }: { context: KeystoneContext; provider: ProviderName }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
]
: [
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
],
'lastOnline_ASC'
)
)
);

test(
'Sorting: sortBy: lastOnline_DESC',
withKeystone(
({ context, adapterName }: { context: KeystoneContext; adapterName: AdapterName }) =>
match(
context,
undefined,
adapterName === 'prisma_sqlite'
? [
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
]
: [
{ name: 'person7', lastOnline: null },
{ name: 'person6', lastOnline: null },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
],
'lastOnline_DESC'
)
withKeystone(({ context, provider }: { context: KeystoneContext; provider: ProviderName }) =>
match(
context,
undefined,
provider === 'sqlite'
? [
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
{ name: 'person6', lastOnline: null },
{ name: 'person7', lastOnline: null },
]
: [
{ name: 'person7', lastOnline: null },
{ name: 'person6', lastOnline: null },
{ name: 'person5', lastOnline: '2020-06-10T10:20:30.456Z' },
{ name: 'person4', lastOnline: '2000-01-20T00:08:00.000Z' },
{ name: 'person3', lastOnline: '1990-12-31T12:34:56.789Z' },
{ name: 'person2', lastOnline: '1980-10-01T23:59:59.999Z' },
{ name: 'person1', lastOnline: '1979-04-12T00:08:00.000Z' },
],
'lastOnline_DESC'
)
)
);
};
4 changes: 2 additions & 2 deletions packages/fields-cloudinary-image/src/test-fixtures.skip.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const storedValues = () => [
{ image: null, name: 'file6' },
];

export const supportedFilters = adapterName => [
export const supportedFilters = provider => [
'null_equality',
!['prisma_postgresql', 'prisma_sqlite'].includes(adapterName) && 'in_empty_null',
!['postgresql', 'sqlite'].includes(provider) && 'in_empty_null',
];
2 changes: 1 addition & 1 deletion packages/fields/src/types/Decimal/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Decimal extends Implementation {
return [
...this.equalityInputFields('String'),
...this.orderingInputFields('String'),
...(this.adapter.listAdapter.parentAdapter.name === 'prisma_postgresql'
...(this.adapter.listAdapter.parentAdapter.name === 'postgresql'
? []
: this.inInputFields('String')),
];
Expand Down
4 changes: 2 additions & 2 deletions packages/fields/src/types/File/test-fixtures.skip.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const afterAll = () => {
});
};

export const supportedFilters = adapterName => [
export const supportedFilters = provider => [
'null_equality',
!['prisma_postgresql'].includes(adapterName) && 'in_empty_null',
!['postgresql'].includes(provider) && 'in_empty_null',
];
42 changes: 21 additions & 21 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import {
import type { KeystoneConfig, BaseKeystone, KeystoneContext } from '@keystone-next/types';
import memoizeOne from 'memoize-one';

export type AdapterName = 'prisma_postgresql' | 'prisma_sqlite';
export type ProviderName = 'postgresql' | 'sqlite';

const hashPrismaSchema = memoizeOne(prismaSchema =>
crypto.createHash('md5').update(prismaSchema).digest('hex')
);

const argGenerator = {
prisma_postgresql: () => ({
postgresql: () => ({
url: process.env.DATABASE_URL!,
provider: 'postgresql' as const,
getDbSchemaName: () => null as any,
// Turn this on if you need verbose debug info
enableLogging: false,
}),
prisma_sqlite: () => ({
sqlite: () => ({
url: process.env.DATABASE_URL!,
provider: 'sqlite' as const,
// Turn this on if you need verbose debug info
Expand All @@ -49,13 +49,13 @@ export const testConfig = (config: TestKeystoneConfig) => config;
const alreadyGeneratedProjects = new Set<string>();

async function setupFromConfig({
adapterName,
provider,
config: _config,
}: {
adapterName: AdapterName;
provider: ProviderName;
config: TestKeystoneConfig;
}) {
const adapterArgs = await argGenerator[adapterName]();
const adapterArgs = await argGenerator[provider]();
const config = initConfig({
..._config,
db: adapterArgs,
Expand All @@ -66,7 +66,7 @@ async function setupFromConfig({
const { keystone, graphQLSchema } = createSystem(config);
const artifacts = await getCommittedArtifacts(graphQLSchema, keystone);
const hash = hashPrismaSchema(artifacts.prisma);
if (adapterName === 'prisma_postgresql') {
if (provider === 'postgresql') {
config.db.url = `${config.db.url}?schema=${hash.toString()}`;
}
const cwd = path.resolve('.api-test-prisma-clients', hash);
Expand Down Expand Up @@ -124,24 +124,24 @@ function networkedGraphqlRequest({

type Setup = { keystone: BaseKeystone; context: KeystoneContext; app: express.Application };

function _keystoneRunner(adapterName: AdapterName, tearDownFunction: () => Promise<void> | void) {
function _keystoneRunner(provider: ProviderName, tearDownFunction: () => Promise<void> | void) {
return function (
setupKeystoneFn: (adaptername: AdapterName) => Promise<Setup>,
setupKeystoneFn: (provider: ProviderName) => Promise<Setup>,
testFn?: (setup: Setup) => Promise<void>
) {
return async function () {
if (!testFn) {
// If a testFn is not defined then we just need
// to excute setup and tear down in isolation.
try {
await setupKeystoneFn(adapterName);
await setupKeystoneFn(provider);
} catch (error) {
await tearDownFunction();
throw error;
}
return;
}
const setup = await setupKeystoneFn(adapterName);
const setup = await setupKeystoneFn(provider);
const { keystone } = setup;
await keystone.connect();

Expand All @@ -155,13 +155,13 @@ function _keystoneRunner(adapterName: AdapterName, tearDownFunction: () => Promi
};
}

function _before(adapterName: AdapterName) {
function _before(provider: ProviderName) {
return async function (
setupKeystone: (
adapterName: AdapterName
provider: ProviderName
) => Promise<{ keystone: Keystone<string>; app: any; context: any }>
) {
const { keystone, context, app } = await setupKeystone(adapterName);
const { keystone, context, app } = await setupKeystone(provider);
await keystone.connect();
return { keystone, context, app };
};
Expand All @@ -177,18 +177,18 @@ function _after(tearDownFunction: () => Promise<void> | void) {
function multiAdapterRunners(only = process.env.TEST_ADAPTER) {
return [
{
runner: _keystoneRunner('prisma_postgresql', () => {}),
adapterName: 'prisma_postgresql' as const,
before: _before('prisma_postgresql'),
runner: _keystoneRunner('postgresql', () => {}),
provider: 'postgresql' as const,
before: _before('postgresql'),
after: _after(() => {}),
},
{
runner: _keystoneRunner('prisma_sqlite', () => {}),
adapterName: 'prisma_sqlite' as const,
before: _before('prisma_sqlite'),
runner: _keystoneRunner('sqlite', () => {}),
provider: 'sqlite' as const,
before: _before('sqlite'),
after: _after(() => {}),
},
].filter(a => typeof only === 'undefined' || a.adapterName === only);
].filter(a => typeof only === 'undefined' || a.provider === only);
}

export { setupFromConfig, multiAdapterRunners, networkedGraphqlRequest };
Loading

0 comments on commit 406acca

Please sign in to comment.