Skip to content

Commit

Permalink
Filter adapter specific fields before running test
Browse files Browse the repository at this point in the history
- Some of the field type, for example `AutoIncrement`, is not supported
  by `mongoose` adapter. This was causing some test failures.

  This change add `unSupportedAdapterList`, allowing running tests for
  supported adapters.
  • Loading branch information
singhArmani committed Aug 20, 2020
1 parent fd2dd50 commit d60c7b7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 75 deletions.
118 changes: 48 additions & 70 deletions api-tests/fields.test.js
Expand Up @@ -3,81 +3,59 @@ const path = require('path');
const { multiAdapterRunners, setupServer } = require('@keystonejs/test-utils');
const { createItems } = require('@keystonejs/server-side-graphql-client');

const runTestModules = (testModules, runner, adapterName) => {
testModules.map(require).forEach(mod => {
const listKey = 'test';
const keystoneTestWrapper = (testFn = () => {}) =>
runner(
() => {
const createLists = keystone => {
// Create a list with all the fields required for testing
const fields = mod.getTestFields();

keystone.createList(listKey, { fields });
};
return setupServer({ adapterName, createLists });
},
async ({ keystone, ...rest }) => {
// Populate the database before running the tests
await createItems({
keystone,
listKey,
items: mod.initItems().map(x => ({ data: x })),
});
return testFn({ keystone, listKey, adapterName, ...rest });
}
);

describe(`${mod.name} field`, () => {
if (mod.crudTests) {
describe(`CRUD operations`, () => {
mod.crudTests(keystoneTestWrapper);
});
} else {
test.todo('CRUD operations - tests missing');
}

if (mod.filterTests) {
describe(`Filtering`, () => {
mod.filterTests(keystoneTestWrapper);
});
} else {
test.todo('Filtering - tests missing');
}
});
});
};

describe('Fields', () => {
const testModules = globby.sync(
[`packages/**/src/**/test-fixtures.js`, `!packages/fields-auto-increment/src/test-fixtures.js`],
{
absolute: true,
}
);
const testModules = globby.sync(`packages/**/src/**/test-fixtures.js`, {
absolute: true,
});
testModules.push(path.resolve('packages/fields/tests/test-fixtures.js'));

multiAdapterRunners().map(({ runner, adapterName }) =>
describe(`${adapterName} adapter`, () => {
runTestModules(testModules, runner, adapterName);
testModules
.map(require)
.filter(({ unSupportedAdapterList = [] }) => !unSupportedAdapterList.includes(adapterName))
.forEach(mod => {
const listKey = 'test';
const keystoneTestWrapper = (testFn = () => {}) =>
runner(
() => {
const createLists = keystone => {
// Create a list with all the fields required for testing
const fields = mod.getTestFields();

keystone.createList(listKey, { fields });
};
return setupServer({ adapterName, createLists });
},
async ({ keystone, ...rest }) => {
// Populate the database before running the tests
await createItems({
keystone,
listKey,
items: mod.initItems().map(x => ({ data: x })),
});
return testFn({ keystone, listKey, adapterName, ...rest });
}
);

describe(`${mod.name} field`, () => {
if (mod.crudTests) {
describe(`CRUD operations`, () => {
mod.crudTests(keystoneTestWrapper);
});
} else {
test.todo('CRUD operations - tests missing');
}

if (mod.filterTests) {
describe(`Filtering`, () => {
mod.filterTests(keystoneTestWrapper);
});
} else {
test.todo('Filtering - tests missing');
}
});
});
})
);
});

// Some fields types are not supported by all adapters.
// For example, 'AutoIncrement' is not supported by 'mongoose'.
// Thus, we need to filter those modules out, and run them individually by specifying the supported adapter.
describe('Fields: Adapter specific', () => {
const testModules = globby.sync(`packages/fields-auto-increment/src/test-fixtures.js`, {
absolute: true,
});

// Filtering out mongoose
multiAdapterRunners()
.filter(({ adapterName }) => adapterName !== 'mongoose')
.map(({ runner, adapterName }) =>
describe(`${adapterName} adapter`, () => {
runTestModules(testModules, runner, adapterName);
})
);
});
9 changes: 7 additions & 2 deletions api-tests/required.test.js
Expand Up @@ -3,12 +3,17 @@ const { multiAdapterRunners, setupServer } = require('@keystonejs/test-utils');
const { Text } = require('@keystonejs/fields');

describe('Test isRequired flag for all field types', () => {
const testModules = globby.sync(`packages/**/src/**/test-fixtures.js`, { absolute: true });
const testModules = globby.sync(`packages/**/src/**/test-fixtures.js`, {
absolute: true,
});
multiAdapterRunners().map(({ runner, adapterName }) =>
describe(`Adapter: ${adapterName}`, () => {
testModules
.map(require)
.filter(({ skipRequiredTest }) => !skipRequiredTest)
.filter(
({ skipRequiredTest, unSupportedAdapterList = [] }) =>
!skipRequiredTest && !unSupportedAdapterList.includes(adapterName)
)
.forEach(mod => {
describe(`Test isRequired flag for module: ${mod.name}`, () => {
const keystoneTestWrapper = testFn =>
Expand Down
5 changes: 4 additions & 1 deletion api-tests/uniqueness/unique.test.js
Expand Up @@ -8,7 +8,10 @@ describe('Test isUnique flag for all field types', () => {
describe(`Adapter: ${adapterName}`, () => {
testModules
.map(require)
.filter(({ supportsUnique }) => supportsUnique)
.filter(
({ supportsUnique, unSupportedAdapterList = [] }) =>
supportsUnique && !unSupportedAdapterList.includes(adapterName)
)
.forEach(mod => {
describe(`Test isUnique flag for module: ${mod.name}`, () => {
const keystoneTestWrapper = testFn =>
Expand Down
7 changes: 5 additions & 2 deletions packages/fields-auto-increment/src/test-fixtures.js
Expand Up @@ -8,8 +8,11 @@ export const exampleValue = '1';
export const exampleValue2 = '2';
export const supportsUnique = true;

//NOTE: The AutoIncrement field type behaves in a way that isn't supported by
// our current uniqueness tests.
// `AutoIncrement` field type is not supported by `mongoose`. So, we need to filter it out while performing `API` tests.
export const unSupportedAdapterList = ['mongoose'];

// Be default, `AutoIncrement` are read-only. But for `isRequired` test purpose, we need to bypass these restrictions.
export const fieldConfig = { access: { create: true, update: true } };

export const getTestFields = () => {
return {
Expand Down

0 comments on commit d60c7b7

Please sign in to comment.