|
| 1 | +import Parser from '../../../src/parse/Parser'; |
| 2 | +import { test, beforeAll, describe, expect } from 'vitest'; |
| 3 | + |
| 4 | +const DBML = ` |
| 5 | + Table users { |
| 6 | + id integer [pk] |
| 7 | + name varchar |
| 8 | + } |
| 9 | +
|
| 10 | + Table posts { |
| 11 | + id integer [pk] |
| 12 | + content text |
| 13 | + } |
| 14 | +
|
| 15 | + Records users(id, name) { |
| 16 | + 1, "Alice" |
| 17 | + 2, "Bob" |
| 18 | + } |
| 19 | +
|
| 20 | + Records posts(id, content) { |
| 21 | + 1, "Hello World" |
| 22 | + } |
| 23 | +`; |
| 24 | + |
| 25 | +describe('@dbml/core - model_structure - records', () => { |
| 26 | + let database: ReturnType<Parser['parse']>; |
| 27 | + |
| 28 | + beforeAll(() => { |
| 29 | + database = new Parser().parse(DBML, 'dbmlv2'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('database.records contains all records', () => { |
| 33 | + expect(database.records.length).toBe(2); |
| 34 | + }); |
| 35 | + |
| 36 | + test('table.records is populated after linking', () => { |
| 37 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 38 | + const postsTable = database.schemas[0].tables.find((t) => t.name === 'posts'); |
| 39 | + |
| 40 | + expect(usersTable!.records.length).toBe(1); |
| 41 | + expect(postsTable!.records.length).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + test('table.records holds the same object references as database.records', () => { |
| 45 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 46 | + |
| 47 | + expect(database.records).toContain(usersTable!.records[0]); |
| 48 | + }); |
| 49 | + |
| 50 | + test('linked record has correct tableName and values', () => { |
| 51 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 52 | + const record = usersTable!.records[0]; |
| 53 | + |
| 54 | + expect(record.tableName).toBe('users'); |
| 55 | + expect(record.values.length).toBe(2); |
| 56 | + }); |
| 57 | + |
| 58 | + test('linked record has tableId set to the table id', () => { |
| 59 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 60 | + expect(usersTable!.records[0].tableId).toBe(usersTable!.id); |
| 61 | + }); |
| 62 | + |
| 63 | + test('table without records has empty records array', () => { |
| 64 | + const db = new Parser().parse(` |
| 65 | + Table empty_table { |
| 66 | + id integer [pk] |
| 67 | + } |
| 68 | + `, 'dbmlv2'); |
| 69 | + |
| 70 | + expect(db.schemas[0].tables[0].records).toEqual([]); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('normalized model', () => { |
| 74 | + test('table has recordIds in normalized model', () => { |
| 75 | + const normalizedModel = database.normalize(); |
| 76 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 77 | + const postsTable = database.schemas[0].tables.find((t) => t.name === 'posts'); |
| 78 | + |
| 79 | + expect(normalizedModel.tables[usersTable!.id].recordIds).toEqual([usersTable!.records[0].id]); |
| 80 | + expect(normalizedModel.tables[postsTable!.id].recordIds).toEqual([postsTable!.records[0].id]); |
| 81 | + }); |
| 82 | + |
| 83 | + test('normalized records contain tableId', () => { |
| 84 | + const normalizedModel = database.normalize(); |
| 85 | + const usersTable = database.schemas[0].tables.find((t) => t.name === 'users'); |
| 86 | + const recordId = usersTable!.records[0].id; |
| 87 | + |
| 88 | + expect(normalizedModel.records[recordId].tableId).toBe(usersTable!.id); |
| 89 | + }); |
| 90 | + |
| 91 | + test('table without records has empty recordIds in normalized model', () => { |
| 92 | + const db = new Parser().parse(` |
| 93 | + Table empty_table { |
| 94 | + id integer [pk] |
| 95 | + } |
| 96 | + `, 'dbmlv2'); |
| 97 | + |
| 98 | + const normalizedModel = db.normalize(); |
| 99 | + const table = db.schemas[0].tables[0]; |
| 100 | + expect(normalizedModel.tables[table.id].recordIds).toEqual([]); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments