-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Database diff for entities & attributes
- Loading branch information
1 parent
7f39fb4
commit c799d85
Showing
4 changed files
with
325 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,129 @@ | ||
import {describe, expect, test} from "@jest/globals"; | ||
import {databaseDiff} from "./databaseDiff"; | ||
import {Database} from "./database"; | ||
import {attributesDiff, databaseDiff} from "./databaseDiff"; | ||
import {Attribute, Database} from "./database"; | ||
|
||
describe('databaseDiff', () => { | ||
test('empty', () => { | ||
expect(databaseDiff({}, {})).toEqual({}) | ||
}) | ||
describe('entities', () => { | ||
test('create', () => { | ||
expect(databaseDiff({}, {entities: [{name: 'users'}]})).toEqual({entities: {created: [{i: 0, name: 'users'}]}}) | ||
}) | ||
test('delete', () => { | ||
expect(databaseDiff({entities: [{name: 'users'}]}, {})).toEqual({entities: {deleted: [{i: 0, name: 'users'}]}}) | ||
}) | ||
test('same', () => { | ||
const before = {entities: [{name: 'users'}]} | ||
const after = {entities: [{name: 'users'}]} | ||
const diff = {entities: {unchanged: [{i: 0, name: 'users'}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update def', () => { | ||
const before = {entities: [{name: 'users', def: 'SELECT * FROM users'}]} | ||
const after = {entities: [{name: 'users', def: 'SELECT id, name FROM users'}]} | ||
const diff = {entities: {updated: [{name: 'users', def: {before: 'SELECT * FROM users', after: 'SELECT id, name FROM users'}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update doc', () => { | ||
const before = {entities: [{name: 'users', doc: 'store users'}]} | ||
const after = {entities: [{name: 'users', doc: 'list users'}]} | ||
const diff = {entities: {updated: [{name: 'users', doc: {before: 'store users', after: 'list users'}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update extra', () => { | ||
const before = {entities: [{name: 'users', extra: {}}]} | ||
const after = {entities: [{name: 'users', extra: {deprecated: null}}]} | ||
const diff = {entities: {updated: [{name: 'users', extra: {deprecated: {before: undefined, after: null}}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
}) | ||
describe('types', () => { | ||
test('create type', () => { | ||
test('create', () => { | ||
expect(databaseDiff({}, {types: [{name: 'position'}]})).toEqual({types: {created: [{i: 0, name: 'position'}]}}) | ||
}) | ||
test('delete type', () => { | ||
test('delete', () => { | ||
expect(databaseDiff({types: [{name: 'position'}]}, {})).toEqual({types: {deleted: [{i: 0, name: 'position'}]}}) | ||
}) | ||
test('same type', () => { | ||
test('same', () => { | ||
const before = {types: [{name: 'position'}]} | ||
const after = {types: [{name: 'position'}]} | ||
const diff = {types: {unchanged: [{i: 0, name: 'position'}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change alias', () => { | ||
test('update alias', () => { | ||
const before = {types: [{name: 'position', alias: 'p'}]} | ||
const after = {types: [{name: 'position', alias: 'pos'}]} | ||
const diff = {types: {updated: [{name: 'position', alias: {before: 'p', after: 'pos'}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change values', () => { | ||
test('update values', () => { | ||
const before: Database = {types: [{name: 'status', values: ['draft', 'public']}]} | ||
const after: Database = {types: [{name: 'status', values: ['draft', 'private']}]} | ||
const diff = {types: {updated: [{name: 'status', values: {before: ['draft', 'public'], after: ['draft', 'private']}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type add attribute', () => { | ||
const before: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}]}]} | ||
const after: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}, {name: 'z', type: 'int'}]}]} | ||
const diff = {types: {updated: [{name: 'position', attrs: {unchanged: [{i: 0, name: 'x', type: 'int'}, {i: 1, name: 'y', type: 'int'}], created: [{i: 2, name: 'z', type: 'int'}]}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type drop attribute', () => { | ||
const before: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}]}]} | ||
const after: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}]}]} | ||
const diff = {types: {updated: [{name: 'position', attrs: {unchanged: [{i: 0, name: 'x', type: 'int'}], deleted: [{i: 1, name: 'y', type: 'int'}]}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change attribute type', () => { | ||
const before: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}]}]} | ||
const after: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'bigint'}]}]} | ||
const diff = {types: {updated: [{name: 'position', attrs: {unchanged: [{i: 0, name: 'x', type: 'int'}], updated: [{name: 'y', type: {before: 'int', after: 'bigint'}}]}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type rename attribute', () => { | ||
const before: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}]}]} | ||
const after: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'z', type: 'int'}]}]} | ||
const diff = {types: {updated: [{name: 'position', attrs: {unchanged: [{i: 0, name: 'x', type: 'int'}], created: [{i: 1, name: 'z', type: 'int'}], deleted: [{i: 1, name: 'y', type: 'int'}]}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change attribute position', () => { | ||
const before: Database = {types: [{name: 'position', attrs: [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}]}]} | ||
const after: Database = {types: [{name: 'position', attrs: [{name: 'y', type: 'int'}, {name: 'x', type: 'int'}]}]} | ||
const diff = {types: {updated: [{name: 'position', attrs: {updated: [{i: {before: 0, after: 1}, name: 'x'}, {i: {before: 1, after: 0}, name: 'y'}]}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change definition', () => { | ||
test('update definition', () => { | ||
const before = {types: [{name: 'position', definition: 'range(0..10)'}]} | ||
const after = {types: [{name: 'position', definition: 'range(0..100)'}]} | ||
const diff = {types: {updated: [{name: 'position', definition: {before: 'range(0..10)', after: 'range(0..100)'}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change doc', () => { | ||
test('update doc', () => { | ||
const before = {types: [{name: 'position', doc: 'store position'}]} | ||
const after = {types: [{name: 'position', doc: 'save position'}]} | ||
const diff = {types: {updated: [{name: 'position', doc: {before: 'store position', after: 'save position'}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
test('type change extra', () => { | ||
test('update extra', () => { | ||
const before = {types: [{name: 'position', extra: {}}]} | ||
const after = {types: [{name: 'position', extra: {deprecated: null}}]} | ||
const diff = {types: {updated: [{name: 'position', extra: {deprecated: {before: undefined, after: null}}}]}} | ||
expect(databaseDiff(before, after)).toEqual(diff) | ||
}) | ||
}) | ||
describe('attributes', () => { | ||
test('create', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}, {name: 'z', type: 'int'}] | ||
const diff = {unchanged: [{i: 0, name: 'x', type: 'int'}, {i: 1, name: 'y', type: 'int'}], created: [{i: 2, name: 'z', type: 'int'}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('delete', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'x', type: 'int'}] | ||
const diff = {unchanged: [{i: 0, name: 'x', type: 'int'}], deleted: [{i: 1, name: 'y', type: 'int'}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('rename', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'x', type: 'int'}, {name: 'z', type: 'int'}] | ||
const diff = {unchanged: [{i: 0, name: 'x', type: 'int'}], created: [{i: 1, name: 'z', type: 'int'}], deleted: [{i: 1, name: 'y', type: 'int'}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update position', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'y', type: 'int'}, {name: 'x', type: 'int'}] | ||
const diff = {updated: [{i: {before: 0, after: 1}, name: 'x'}, {i: {before: 1, after: 0}, name: 'y'}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update type', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'bigint'}] | ||
const diff = {unchanged: [{i: 0, name: 'x', type: 'int'}], updated: [{name: 'y', type: {before: 'int', after: 'bigint'}}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update null', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int', null: false}] | ||
const after: Attribute[] = [{name: 'x', type: 'int', null: true}, {name: 'y', type: 'int'}] | ||
const diff = {updated: [{name: 'x', null: {before: undefined, after: true}}, {name: 'y', null: {before: false, after: undefined}}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
test('update default', () => { | ||
const before: Attribute[] = [{name: 'x', type: 'int', default: 0}, {name: 'y', type: 'int'}] | ||
const after: Attribute[] = [{name: 'x', type: 'int'}, {name: 'y', type: 'int', default: '5'}] | ||
const diff = {updated: [{name: 'x', default: {before: 0, after: undefined}}, {name: 'y', default: {before: undefined, after: '5'}}]} | ||
expect(attributesDiff(before, after)).toEqual(diff) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.