Files
directus/packages/utils/shared/get-relation.test.ts
Nitwel 4d59d70897 Unify getRelation/getRelations usage across packages (#25053)
* restructure

* add changeset

* add schema builder for primary fields

* fmt

* add relational generation

* add more tests and clean schema.ts

* fmt

* add changeset

* add more tests and tidy up existing ones

* add more tests and clean code

* add standalone a2o relation

* add tests and clean up even more logic

* continue working on tests

* update more tests

* update tests and reorganize files

* more tests and take appart applyFilter

* update test for get-filter-type

* update export

* fmt

* unify get-relations

* fmt

* add changeset

* Update .changeset/dry-donuts-guess.md

* remove additional whitespace

* Update .changeset/dry-donuts-guess.md

* Update .changeset/dry-donuts-guess.md

* fix new test

* chore: organize import

* remove unnecessary type assertion

---------

Co-authored-by: daedalus <44623501+ComfortablyCoding@users.noreply.github.com>
Co-authored-by: ian <licitdev@gmail.com>
2025-04-22 16:59:20 -04:00

228 lines
5.7 KiB
TypeScript

import { SchemaBuilder } from '@directus/schema-builder';
import { expect, test } from 'vitest';
import { getRelation, getRelations } from './get-relation.js';
test('relations on m2o field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('author').m2o('users', 'articles');
})
.build();
const result = getRelations(schema.relations, 'article', 'author');
expect(result).toMatchInlineSnapshot(`
[
{
"collection": "article",
"field": "author",
"meta": {
"id": 0,
"junction_field": null,
"many_collection": "article",
"many_field": "author",
"one_allowed_collections": null,
"one_collection": "users",
"one_collection_field": null,
"one_deselect_action": "nullify",
"one_field": "articles",
"sort_field": null,
},
"related_collection": "users",
"schema": {
"column": "author",
"constraint_name": "article_author_foreign",
"foreign_key_schema": "public",
"foreign_key_table": "users",
"on_delete": "SET NULL",
"on_update": "NO ACTION",
"table": "article",
},
},
]
`);
expect(result).toEqual(getRelations(schema.relations, 'users', 'articles'));
});
test('relations on o2m field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelations(schema.relations, 'article', 'tags');
expect(result).toMatchInlineSnapshot(`
[
{
"collection": "tags",
"field": "article_id",
"meta": {
"id": 0,
"junction_field": null,
"many_collection": "tags",
"many_field": "article_id",
"one_allowed_collections": null,
"one_collection": "article",
"one_collection_field": null,
"one_deselect_action": "nullify",
"one_field": "tags",
"sort_field": null,
},
"related_collection": "article",
"schema": {
"column": "tags",
"constraint_name": "article_tags_foreign",
"foreign_key_schema": "public",
"foreign_key_table": "tags",
"on_delete": "SET NULL",
"on_update": "NO ACTION",
"table": "article",
},
},
]
`);
expect(result).toEqual(getRelations(schema.relations, 'tags', 'article_id'));
});
test('relation on m2o field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('author').m2o('users', 'articles');
})
.build();
const result = getRelation(schema.relations, 'article', 'author');
expect(result).toMatchInlineSnapshot(`
{
"collection": "article",
"field": "author",
"meta": {
"id": 0,
"junction_field": null,
"many_collection": "article",
"many_field": "author",
"one_allowed_collections": null,
"one_collection": "users",
"one_collection_field": null,
"one_deselect_action": "nullify",
"one_field": "articles",
"sort_field": null,
},
"related_collection": "users",
"schema": {
"column": "author",
"constraint_name": "article_author_foreign",
"foreign_key_schema": "public",
"foreign_key_table": "users",
"on_delete": "SET NULL",
"on_update": "NO ACTION",
"table": "article",
},
}
`);
expect(result).toEqual(getRelation(schema.relations, 'users', 'articles'));
});
test('relation on o2m field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelation(schema.relations, 'article', 'tags');
expect(result).toMatchInlineSnapshot(`
{
"collection": "tags",
"field": "article_id",
"meta": {
"id": 0,
"junction_field": null,
"many_collection": "tags",
"many_field": "article_id",
"one_allowed_collections": null,
"one_collection": "article",
"one_collection_field": null,
"one_deselect_action": "nullify",
"one_field": "tags",
"sort_field": null,
},
"related_collection": "article",
"schema": {
"column": "tags",
"constraint_name": "article_tags_foreign",
"foreign_key_schema": "public",
"foreign_key_table": "tags",
"on_delete": "SET NULL",
"on_update": "NO ACTION",
"table": "article",
},
}
`);
expect(result).toEqual(getRelation(schema.relations, 'tags', 'article_id'));
});
test('relations on wrong collection', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelations(schema.relations, 'wrong', 'tags');
expect(result).toEqual([]);
});
test('relations on wrong field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelations(schema.relations, 'article', 'wrong');
expect(result).toEqual([]);
});
test('relation on wrong collection', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelation(schema.relations, 'wrong', 'tags');
expect(result).toBeUndefined();
});
test('relation on wrong field', () => {
const schema = new SchemaBuilder()
.collection('article', (c) => {
c.field('id').id();
c.field('tags').o2m('tags', 'article_id');
})
.build();
const result = getRelation(schema.relations, 'article', 'wrong');
expect(result).toBeUndefined();
});