Files
directus/tests/api/items/many-to-many.test.ts
Jay Cammarano 90bb9d4879 End-to-end tests for filters (#9078)
* reorganized for sanity

* filter tests for _eq/_neq

* logical operators

* update one one to many

* /:collection PATCH one to many

* base tables.id => uuids to minimize collisions

* tests pass

* added python to dockerfile

* tests passing

* ci?

* ci...

* hanging async
2021-11-01 14:21:44 -04:00

307 lines
9.8 KiB
TypeScript

import axios from 'axios';
import request from 'supertest';
import config from '../../config';
import { getDBsToTest } from '../../get-dbs-to-test';
import knex, { Knex } from 'knex';
import { createArtist, createEvent, createMany, seedTable, Item } from '../../setup/utils/factories';
describe('/items', () => {
const databases = new Map<string, Knex>();
beforeAll(async () => {
const vendors = getDBsToTest();
for (const vendor of vendors) {
databases.set(vendor, knex(config.knexConfig[vendor]!));
}
});
afterAll(async () => {
for (const [_vendor, connection] of databases) {
await connection.destroy();
}
});
describe('/:collection/:id GET', () => {
it.each(getDBsToTest())(`%p retrieves an artist and an event off the artists_events table`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist);
await seedTable(databases.get(vendor)!, 1, 'events', event);
const relation = await seedTable(
databases.get(vendor)!,
1,
'artists_events',
{
artists_id: artist.id,
events_id: event.id,
},
{
select: ['id'],
where: ['artists_id', artist.id],
}
);
const response = await request(url)
.get(
`/items/artists_events/${relation[relation.length - 1].id}?fields[]=artists_id.name&fields[]=events_id.cost`
)
.set('Authorization', 'Bearer AdminToken')
.expect('Content-Type', /application\/json/)
.expect(200);
expect(response.body.data).toMatchObject({
artists_id: { name: expect.any(String) },
events_id: { cost: expect.any(Number) },
});
});
});
describe('/:collection/:id PATCH', () => {
it.each(getDBsToTest())(`%p updates one artists_events to a different artist`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const insertedArtist = await seedTable(databases.get(vendor)!, 1, 'artists', createArtist(), {
select: ['id'],
});
const insertedEvent = await seedTable(databases.get(vendor)!, 1, 'events', createEvent(), {
select: ['id'],
});
const relation = await seedTable(
databases.get(vendor)!,
1,
'artists_events',
{
artists_id: insertedArtist[insertedArtist.length - 1].id,
events_id: insertedEvent[insertedEvent.length - 1].id,
},
{ select: ['id'], where: ['events_id', insertedEvent[insertedEvent.length - 1].id] }
);
const body = { artists_id: insertedArtist[0].id };
const response: any = await axios.patch(`${url}/items/artists_events/${relation[0].id}`, body, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
});
expect(response.data.data).toMatchObject({
artists_id: insertedArtist[0].id,
});
});
});
describe('/:collection/:id DELETE', () => {
it.each(getDBsToTest())(`%p deletes an artists_events without deleting the artist or event`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
const item = await seedTable(
databases.get(vendor)!,
1,
'artists_events',
{
artists_id: artist.id,
events_id: event.id,
},
{ select: ['id'], where: ['events_id', event.id] }
);
const response: any = await axios.delete(`${url}/items/artists_events/${item[item.length - 1].id}`, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
});
expect(response.data.data).toBe(undefined);
expect(await databases.get(vendor)!('artists_events').select('*').where('id', item[0].id)).toStrictEqual([]);
expect(await databases.get(vendor)!('artists').select('name').where('id', artist.id)).toMatchObject([
{ name: artist.name },
]);
expect(await databases.get(vendor)!('events').select('cost').where('id', event.id)).toMatchObject([
{ cost: event.cost },
]);
});
});
describe('/:collection GET', () => {
it.each(getDBsToTest())(`%p retrieves artists and events for each entry in artists_events`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 10, 'artists_events', {
artists_id: artist.id,
events_id: event.id,
});
const response = await request(url)
.get(`/items/artists_events?fields[]=artists_id.name&fields[]=events_id.cost`)
.set('Authorization', 'Bearer AdminToken')
.expect('Content-Type', /application\/json/)
.expect(200);
expect(response.body.data[0]).toMatchObject({
artists_id: { name: expect.any(String) },
events_id: { cost: expect.any(Number) },
});
expect(response.body.data.length).toBeGreaterThanOrEqual(10);
});
});
describe('/:collection POST', () => {
describe('createOne', () => {
it.each(getDBsToTest())(`%p creates an artist_events entry`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
const body = {
artists_id: artist.id,
events_id: event.id,
};
const response: any = await axios.post(`${url}/items/artists_events`, body, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
});
expect(response.data.data).toMatchObject({
id: expect.any(Number),
artists_id: expect.any(String),
events_id: expect.any(String),
});
});
});
describe('createMany', () => {
it.each(getDBsToTest())(`%p creates 5 artist_events entries`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
const body = createMany({}, 10, { artists_id: artist.id, events_id: event.id });
const response: any = await axios.post(`${url}/items/artists_events`, body, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
});
expect(response.data.data.length).toBeGreaterThanOrEqual(10);
expect(response.data.data[0]).toMatchObject({
artists_id: expect.any(String),
events_id: expect.any(String),
});
});
});
});
describe('/:collection PATCH', () => {
it.each(getDBsToTest())(`%p updates many artists_events to a different artist`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
const items = await seedTable(
databases.get(vendor)!,
10,
'artists_events',
{
artists_id: artist.id,
events_id: event.id,
},
{ select: ['id'], where: ['events_id', event.id] }
);
const keys: any[] = [];
Object.values(items).forEach((item: any) => {
keys.push(item.id);
});
const body = {
keys: keys,
data: { events_id: event.id },
};
const response: any = await axios.patch(`${url}/items/artists_events/?fields=events_id.cost`, body, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
});
for (let row = 0; row < response.data.data.length; row++) {
expect(response.data.data[row].events_id).toMatchObject({
cost: event.cost,
});
}
expect(response.data.data.length).toBe(keys.length);
});
});
describe('/:collection DELETE', () => {
it.each(getDBsToTest())(`%p deletes many artists_events without deleting the artists or events`, async (vendor) => {
const url = `http://localhost:${config.ports[vendor]!}`;
const artist = createArtist();
const event = createEvent();
await seedTable(databases.get(vendor)!, 1, 'artists', artist, {
select: ['id'],
});
await seedTable(databases.get(vendor)!, 1, 'events', event, {
select: ['id'],
});
const items = await seedTable(
databases.get(vendor)!,
10,
'artists_events',
{
artists_id: artist.id,
events_id: event.id,
},
{ select: ['id'], where: ['events_id', event.id] }
);
const body: any[] = [];
items
.sort(function (a: any, b: any) {
return b.id - a.id;
})
.forEach((item: Item) => {
body.push(item.id);
});
const response: any = await axios.delete(`${url}/items/artists_events/`, {
headers: {
Authorization: 'Bearer AdminToken',
'Content-Type': 'application/json',
},
data: JSON.stringify(body),
});
expect(response.data.data).toBe(undefined);
for (let row = 0; row < items.length; row++) {
expect(await databases.get(vendor)!('artists_events').select('*').where('id', items[row].id)).toStrictEqual([]);
}
expect((await databases.get(vendor)!('artists').select('name').where('id', artist.id))[0].name).toBe(artist.name);
expect((await databases.get(vendor)!('events').select('cost').where('id', event.id))[0].cost).toBe(event.cost);
});
});
});