@fabrika/db 0.9.0-dev.10 → 0.9.0-dev.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/testing-shared.ts +163 -42
- package/src/testing-sqlite.ts +8 -2
- package/src/testing.ts +8 -2
- package/types/src/testing-shared.d.ts +29 -10
- package/types/src/testing-sqlite.d.ts +1 -1
- package/types/src/testing.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"test": "bun test"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
95
|
+
"@fabrika/auth-core": "0.9.0-dev.11",
|
|
96
|
+
"@fabrika/platform": "0.9.0-dev.11",
|
|
97
|
+
"@fabrika/platform-node": "0.9.0-dev.11",
|
|
98
98
|
"kysely": "0.29.6",
|
|
99
99
|
"kysely-postgres-js": "5.0.1",
|
|
100
100
|
"zod": "^3.25.76"
|
package/src/testing-shared.ts
CHANGED
|
@@ -46,6 +46,11 @@ export interface WorldRow {
|
|
|
46
46
|
* Each occurs in no other fixture, so one found in an answer to another scope is a leaked row.
|
|
47
47
|
*/
|
|
48
48
|
readonly markers: readonly string[]
|
|
49
|
+
/**
|
|
50
|
+
* The values of the text columns of a row the table already held under a known scope value (`seedWorlds`' `scopes`),
|
|
51
|
+
* which no fixture wrote and other rows may share; empty for a row the fixture inserted.
|
|
52
|
+
*/
|
|
53
|
+
readonly stored: readonly string[]
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
/** One seeded row of an application table, which lies in no dimension. */
|
|
@@ -73,6 +78,8 @@ export interface SeededApplicationRows {
|
|
|
73
78
|
export interface SeededRow {
|
|
74
79
|
readonly key: string
|
|
75
80
|
readonly markers: readonly string[]
|
|
81
|
+
/** Deletes the row, the rows its foreign keys made the fixture insert, and every row that refers to them. */
|
|
82
|
+
readonly remove: () => Promise<void>
|
|
76
83
|
}
|
|
77
84
|
|
|
78
85
|
/**
|
|
@@ -94,12 +101,19 @@ export interface IsolationWorlds {
|
|
|
94
101
|
readonly own: WorldRow
|
|
95
102
|
readonly other: WorldRow
|
|
96
103
|
/**
|
|
97
|
-
* Deletes both rows of the table
|
|
98
|
-
*
|
|
104
|
+
* Deletes both rows of the table it inserted, every container row it inserted when it was given known scope values,
|
|
105
|
+
* and every row that refers to them, however deep; the other containers stay. A case whose rows hold pinned values or
|
|
106
|
+
* known scope values removes them, so the next case can seed the same values again under a unique index.
|
|
99
107
|
*/
|
|
100
108
|
readonly remove: () => Promise<void>
|
|
101
109
|
}
|
|
102
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Known values of dimensions, by dimension: the own world's and the other world's, which differ. A dimension whose values
|
|
113
|
+
* are a fixed set (an enum the input names it by) takes these instead of fresh ones.
|
|
114
|
+
*/
|
|
115
|
+
export type ScopeValues = Readonly<Record<string, { readonly own: string; readonly other: string }>>
|
|
116
|
+
|
|
103
117
|
/** What a test kit seeds through, over its migrated schema. */
|
|
104
118
|
export interface TestSeeding {
|
|
105
119
|
/**
|
|
@@ -108,13 +122,17 @@ export interface TestSeeding {
|
|
|
108
122
|
* marker, so a seed of one, or of a column that places the row, throws. `pins` (column → value) give a text column of
|
|
109
123
|
* `table` a known value instead of its marker: the value in the own row, and `<value>-other` in the other row, which
|
|
110
124
|
* counts among the other row's markers, so a leak of it is still found. A pin of a column that places the row, or of
|
|
111
|
-
* no text column, throws.
|
|
125
|
+
* no text column, throws. `scopes` place each world in known values of a dimension instead of fresh ones: a row of
|
|
126
|
+
* the table that owns it with that key is used as it is stored (a migration wrote it), its containers' values with it,
|
|
127
|
+
* and is inserted otherwise. A stored row of `table` itself takes no `values` or `pins`: they throw. A seed that throws
|
|
128
|
+
* removes what it inserted.
|
|
112
129
|
*/
|
|
113
130
|
seedWorlds<DB, P>(
|
|
114
131
|
tables: Tables<DB, P>,
|
|
115
132
|
table: string,
|
|
116
133
|
values?: Readonly<Record<string, SampleValue>>,
|
|
117
134
|
pins?: Readonly<Record<string, string>>,
|
|
135
|
+
scopes?: ScopeValues,
|
|
118
136
|
): Promise<IsolationWorlds>
|
|
119
137
|
/**
|
|
120
138
|
* Seeds one row of the application table `table`: its key, version and creator stamped, `values` (column → value)
|
|
@@ -133,11 +151,11 @@ export interface TestSeeding {
|
|
|
133
151
|
rows: Readonly<Record<string, Readonly<Record<string, SampleValue>>>>,
|
|
134
152
|
): Promise<SeededApplicationRows>
|
|
135
153
|
/**
|
|
136
|
-
* Seeds one row of `table`, a child or dimension table that lies in the object
|
|
137
|
-
* seeded
|
|
138
|
-
* the markers it holds.
|
|
154
|
+
* Seeds one row of `table`, a child or dimension table that lies in the object `under`'s chain starts at (a row
|
|
155
|
+
* `seedWorlds` seeded, or a value of its chain with the values outside it), placed in that chain: the row a fixture
|
|
156
|
+
* names by id, such as a note's comment. Answers its key and the markers it holds.
|
|
139
157
|
*/
|
|
140
|
-
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: WorldRow): Promise<SeededRow>
|
|
158
|
+
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: Pick<WorldRow, 'chain'>): Promise<SeededRow>
|
|
141
159
|
/**
|
|
142
160
|
* Values for the policy's insert columns of `table` that its schema accepts: every required one, sampled; a file
|
|
143
161
|
* column a seeded file. `beside` is a row `seedWorlds` seeded that the inserted row will lie beside, in the same
|
|
@@ -245,8 +263,12 @@ export interface SeedingBackend {
|
|
|
245
263
|
* (`checkLimit`, `foreignKeyLimit`, `uniqueValueLimit`).
|
|
246
264
|
*/
|
|
247
265
|
insert(table: string, values: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>>>
|
|
248
|
-
/** One row of `table
|
|
249
|
-
firstRow(
|
|
266
|
+
/** One row of `table` that holds `where` (column → value; every row when empty), of `columns`, or `undefined` when none does. */
|
|
267
|
+
firstRow(
|
|
268
|
+
table: string,
|
|
269
|
+
columns: readonly string[],
|
|
270
|
+
where?: ReadonlyMap<string, SampleValue>,
|
|
271
|
+
): Promise<Readonly<Record<string, unknown>> | undefined>
|
|
250
272
|
insertFile(file: SeededFile): Promise<void>
|
|
251
273
|
/**
|
|
252
274
|
* Deletes `rows` (each found by its `match`; one already gone is skipped) and every row that refers to one of them,
|
|
@@ -673,35 +695,132 @@ export function testSeeding(backend: SeedingBackend): TestSeeding {
|
|
|
673
695
|
/** The value a pin gives the row of `world`: as written in the own row, distinct in the other. */
|
|
674
696
|
const pinnedValue = (value: string, world: string): string => (world === 'own' ? value : `${value}-${world}`)
|
|
675
697
|
|
|
698
|
+
/**
|
|
699
|
+
* The dimension values the row of `owner` keyed `key` lies in, as the table stores it: its own, then each container's;
|
|
700
|
+
* `undefined` when the table holds no such row.
|
|
701
|
+
*/
|
|
702
|
+
async function storedChain(owner: TableModel, key: string): Promise<ReadonlyMap<string, string> | undefined> {
|
|
703
|
+
const scope = owner.scope
|
|
704
|
+
if (scope.mode !== 'dimension') return undefined
|
|
705
|
+
const row = await backend.firstRow(owner.name, [scope.key, ...owner.chain.map((link) => link.column)], new Map([[scope.key, key]]))
|
|
706
|
+
if (row === undefined) return undefined
|
|
707
|
+
const chain = new Map([[scope.dimension, key]])
|
|
708
|
+
for (const link of owner.chain) chain.set(link.dimension, String(keyValue(row[link.column], `${owner.name}.${link.column}`)))
|
|
709
|
+
return chain
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* The dimension values a row of `model` lies in for `world`, as far as `scopes` know them: each known value, and every
|
|
714
|
+
* value a stored row of a known one holds. `stored` are the dimensions whose row a table holds already.
|
|
715
|
+
*/
|
|
716
|
+
async function knownValues(
|
|
717
|
+
models: TableModels,
|
|
718
|
+
model: TableModel,
|
|
719
|
+
world: 'own' | 'other',
|
|
720
|
+
scopes: ScopeValues,
|
|
721
|
+
): Promise<{ readonly values: Map<string, string>; readonly stored: ReadonlySet<string> }> {
|
|
722
|
+
const own = model.scope.mode === 'dimension' ? [model.scope.dimension] : []
|
|
723
|
+
const dimensions = [...own, ...model.chain.map((link) => link.dimension)].filter((dimension) => Object.hasOwn(scopes, dimension))
|
|
724
|
+
const values = new Map<string, string>()
|
|
725
|
+
for (const dimension of dimensions) {
|
|
726
|
+
const known = scopes[dimension]
|
|
727
|
+
if (known !== undefined) values.set(dimension, world === 'own' ? known.own : known.other)
|
|
728
|
+
}
|
|
729
|
+
const stored = new Set<string>()
|
|
730
|
+
for (const dimension of dimensions) {
|
|
731
|
+
const owner = ownerOf(models, dimension)
|
|
732
|
+
const value = values.get(dimension)
|
|
733
|
+
const chain = owner === undefined || value === undefined ? undefined : await storedChain(owner, value)
|
|
734
|
+
for (const [type, found] of chain ?? []) {
|
|
735
|
+
const expected = values.get(type)
|
|
736
|
+
if (expected !== undefined && expected !== found) {
|
|
737
|
+
throw new Error(`the stored ${dimension} '${value}' lies in the ${type} '${found}', not in the known '${expected}'`)
|
|
738
|
+
}
|
|
739
|
+
values.set(type, found)
|
|
740
|
+
stored.add(type)
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return { values, stored }
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** The values of the text columns that would hold a marker in the stored row of `model` keyed `key`. */
|
|
747
|
+
async function storedTexts(model: TableModel, key: string): Promise<string[]> {
|
|
748
|
+
const scope = model.scope
|
|
749
|
+
if (scope.mode !== 'dimension') return []
|
|
750
|
+
const names: string[] = []
|
|
751
|
+
for (const column of columnsOf(model.name)) {
|
|
752
|
+
if (await holdsMarker(model, model.name, column)) names.push(column.name)
|
|
753
|
+
}
|
|
754
|
+
if (names.length === 0) return []
|
|
755
|
+
const row = await backend.firstRow(model.name, names, new Map([[scope.key, key]]))
|
|
756
|
+
return names.flatMap((name) => {
|
|
757
|
+
const value = row?.[name]
|
|
758
|
+
return typeof value === 'string' && value !== '' ? [value] : []
|
|
759
|
+
})
|
|
760
|
+
}
|
|
761
|
+
|
|
676
762
|
async function seedWorld(
|
|
677
763
|
models: TableModels,
|
|
678
764
|
model: TableModel,
|
|
679
|
-
world:
|
|
765
|
+
world: 'own' | 'other',
|
|
680
766
|
seed: Readonly<Record<string, SampleValue>>,
|
|
681
767
|
pins: Readonly<Record<string, string>>,
|
|
768
|
+
scopes: ScopeValues,
|
|
682
769
|
inserted: InsertedRow[],
|
|
683
770
|
): Promise<{ readonly world: Omit<WorldRow, 'updates'>; readonly row: ReadonlyMap<string, SampleValue> }> {
|
|
684
771
|
const markers: string[] = []
|
|
685
|
-
const values = new Map<string, string>()
|
|
686
|
-
const links: readonly Container[] = model.chain
|
|
687
|
-
for (const link of links) {
|
|
688
|
-
values.set(link.dimension, scopeValue(model.name, link.column, link.dimension, world))
|
|
689
|
-
}
|
|
690
772
|
const scope = model.scope
|
|
691
773
|
if (scope.mode !== 'dimension' && scope.mode !== 'child') {
|
|
692
774
|
throw new Error(`${model.name} is not a dimension or child table`)
|
|
693
775
|
}
|
|
694
|
-
const
|
|
695
|
-
|
|
696
|
-
|
|
776
|
+
const { values, stored } = await knownValues(models, model, world, scopes)
|
|
777
|
+
const links: readonly Container[] = model.chain
|
|
778
|
+
for (const link of links) {
|
|
779
|
+
if (!values.has(link.dimension)) values.set(link.dimension, scopeValue(model.name, link.column, link.dimension, world))
|
|
780
|
+
}
|
|
781
|
+
const own = scope.mode === 'dimension' ? scope.dimension : undefined
|
|
782
|
+
const key = (own === undefined ? undefined : values.get(own)) ?? scopeValue(model.name, scope.key, own ?? model.name, world)
|
|
783
|
+
if (own !== undefined) {
|
|
784
|
+
values.set(own, key)
|
|
785
|
+
}
|
|
786
|
+
const worldRow = (row: ReadonlyMap<string, SampleValue>, stored: readonly string[]) => {
|
|
787
|
+
const chain = [
|
|
788
|
+
...(own === undefined ? [] : [{ type: own, value: key }]),
|
|
789
|
+
...links.flatMap((link) => {
|
|
790
|
+
const value = values.get(link.dimension)
|
|
791
|
+
return value === undefined ? [] : [{ type: link.dimension, value }]
|
|
792
|
+
}),
|
|
793
|
+
]
|
|
794
|
+
const seeded = {
|
|
795
|
+
key,
|
|
796
|
+
parent: scope.mode === 'child' ? (values.get(links[0]?.dimension ?? '') ?? null) : null,
|
|
797
|
+
scope: values.get(grantDimension(models, model)) ?? key,
|
|
798
|
+
chain,
|
|
799
|
+
markers,
|
|
800
|
+
stored,
|
|
801
|
+
}
|
|
802
|
+
return { world: seeded, row }
|
|
697
803
|
}
|
|
698
|
-
|
|
804
|
+
if (own !== undefined && stored.has(own)) {
|
|
805
|
+
if (Object.keys(seed).length > 0 || Object.keys(pins).length > 0) {
|
|
806
|
+
throw new Error(
|
|
807
|
+
`${model.name} already holds the ${own} '${key}' (a migration wrote it), so a case writes no seed or pin into it; seed the state of a row the case inserts`,
|
|
808
|
+
)
|
|
809
|
+
}
|
|
810
|
+
return worldRow(new Map(), await storedTexts(model, key))
|
|
811
|
+
}
|
|
812
|
+
// Containers first, outermost first: a composite foreign key needs its target row. A stored one is used as it is.
|
|
699
813
|
for (let index = links.length - 1; index >= 0; index--) {
|
|
700
814
|
const link = links[index]
|
|
701
815
|
const owner = link === undefined ? undefined : ownerOf(models, link.dimension)
|
|
702
816
|
const value = link === undefined ? undefined : values.get(link.dimension)
|
|
703
|
-
if (owner
|
|
704
|
-
|
|
817
|
+
if (link === undefined || owner === undefined || owner.name === model.name || value === undefined || stored.has(link.dimension)) continue
|
|
818
|
+
// With known values, every container goes with the worlds: one of a known value, so the next case can insert it
|
|
819
|
+
// again, and one inside a stored row, which no removal of that row reaches.
|
|
820
|
+
const tracked = Object.keys(scopes).length > 0 ? inserted : undefined
|
|
821
|
+
await insert(models, owner.name, placement(owner, values, value), markers, 0, tracked)
|
|
822
|
+
if (tracked !== undefined && owner.scope.mode === 'dimension') {
|
|
823
|
+
tracked.push({ table: owner.name, match: new Map([[owner.scope.key, value]]) })
|
|
705
824
|
}
|
|
706
825
|
}
|
|
707
826
|
const assigned = placement(model, values, key)
|
|
@@ -731,22 +850,7 @@ export function testSeeding(backend: SeedingBackend): TestSeeding {
|
|
|
731
850
|
}
|
|
732
851
|
const { row } = await insert(models, model.name, assigned, markers)
|
|
733
852
|
inserted.push({ table: model.name, match: new Map([[scope.key, key]]) })
|
|
734
|
-
|
|
735
|
-
const chain = [
|
|
736
|
-
...(scope.mode === 'dimension' ? [{ type: scope.dimension, value: key }] : []),
|
|
737
|
-
...links.flatMap((link) => {
|
|
738
|
-
const value = values.get(link.dimension)
|
|
739
|
-
return value === undefined ? [] : [{ type: link.dimension, value }]
|
|
740
|
-
}),
|
|
741
|
-
]
|
|
742
|
-
const seeded = {
|
|
743
|
-
key,
|
|
744
|
-
parent: scope.mode === 'child' ? (values.get(links[0]?.dimension ?? '') ?? null) : null,
|
|
745
|
-
scope: values.get(dimension) ?? key,
|
|
746
|
-
chain,
|
|
747
|
-
markers,
|
|
748
|
-
}
|
|
749
|
-
return { world: seeded, row }
|
|
853
|
+
return worldRow(row, [])
|
|
750
854
|
}
|
|
751
855
|
|
|
752
856
|
/** A seeded row's values of the policy's update columns: as written, or a sample where the database filled the column. */
|
|
@@ -759,12 +863,27 @@ export function testSeeding(backend: SeedingBackend): TestSeeding {
|
|
|
759
863
|
}
|
|
760
864
|
|
|
761
865
|
return {
|
|
762
|
-
async seedWorlds(tables, table, values = {}, pins = {}) {
|
|
866
|
+
async seedWorlds(tables, table, values = {}, pins = {}, scopes = {}) {
|
|
763
867
|
const { models, model } = scopedModel(tables, table)
|
|
764
868
|
const dimension = grantDimension(models, model)
|
|
869
|
+
for (const [name, known] of Object.entries(scopes)) {
|
|
870
|
+
if (known.own === known.other) throw new Error(`the two worlds share the ${name} '${known.own}'; each needs a value of its own`)
|
|
871
|
+
}
|
|
765
872
|
const inserted: InsertedRow[] = []
|
|
766
|
-
|
|
767
|
-
|
|
873
|
+
let own: Awaited<ReturnType<typeof seedWorld>>
|
|
874
|
+
let other: Awaited<ReturnType<typeof seedWorld>>
|
|
875
|
+
try {
|
|
876
|
+
own = await seedWorld(models, model, 'own', values, pins, scopes, inserted)
|
|
877
|
+
other = await seedWorld(models, model, 'other', values, pins, scopes, inserted)
|
|
878
|
+
} catch (error) {
|
|
879
|
+
// A row of a known value left behind would pass for one a migration wrote in every later case.
|
|
880
|
+
try {
|
|
881
|
+
await backend.remove(inserted)
|
|
882
|
+
} catch (cleanup) {
|
|
883
|
+
throw new AggregateError([error, cleanup], 'the isolation fixture could not seed the worlds, nor remove what it had seeded')
|
|
884
|
+
}
|
|
885
|
+
throw error
|
|
886
|
+
}
|
|
768
887
|
// Sampled once both rows are written: a number a unique index covers is the next above what the table holds.
|
|
769
888
|
return {
|
|
770
889
|
dimension,
|
|
@@ -830,8 +949,10 @@ export function testSeeding(backend: SeedingBackend): TestSeeding {
|
|
|
830
949
|
}
|
|
831
950
|
const key = scopeValue(table, scope.key, scope.mode === 'dimension' ? scope.dimension : table, 'under')
|
|
832
951
|
const markers: string[] = []
|
|
833
|
-
|
|
834
|
-
|
|
952
|
+
const inserted: InsertedRow[] = []
|
|
953
|
+
await insert(models, table, placement(model, values, key), markers, 0, inserted)
|
|
954
|
+
inserted.push({ table, match: new Map([[scope.key, key]]) })
|
|
955
|
+
return { key, markers, remove: () => backend.remove(inserted) }
|
|
835
956
|
},
|
|
836
957
|
async sampleInsert(tables, table, beside) {
|
|
837
958
|
const { models, model } = scopedModel(tables, table)
|
package/src/testing-sqlite.ts
CHANGED
|
@@ -45,6 +45,7 @@ export {
|
|
|
45
45
|
type ApplicationRow,
|
|
46
46
|
type IsolationWorlds,
|
|
47
47
|
type SampleValue,
|
|
48
|
+
type ScopeValues,
|
|
48
49
|
SEEDED_OBJECT_VERSION,
|
|
49
50
|
type SeededApplicationRows,
|
|
50
51
|
type SeededRow,
|
|
@@ -487,8 +488,13 @@ export async function testDatabase(options: TestDatabaseOptions): Promise<TestDa
|
|
|
487
488
|
throw seedLimit(error, table) ?? error
|
|
488
489
|
}
|
|
489
490
|
},
|
|
490
|
-
async firstRow(table, selected) {
|
|
491
|
-
|
|
491
|
+
async firstRow(table, selected, where = new Map()) {
|
|
492
|
+
const names = [...where.keys()]
|
|
493
|
+
const condition = names.length === 0 ? '' : ` WHERE ${names.map((name) => `${quoted(name)} = ?`).join(' AND ')}`
|
|
494
|
+
return exec(
|
|
495
|
+
`SELECT ${selected.map(quoted).join(', ')} FROM ${quoted(table)}${condition} LIMIT 1`,
|
|
496
|
+
...names.map((name) => binding(where.get(name) ?? null)),
|
|
497
|
+
)[0]
|
|
492
498
|
},
|
|
493
499
|
async insertFile(file) {
|
|
494
500
|
exec(
|
package/src/testing.ts
CHANGED
|
@@ -48,6 +48,7 @@ export {
|
|
|
48
48
|
type ApplicationRow,
|
|
49
49
|
type IsolationWorlds,
|
|
50
50
|
type SampleValue,
|
|
51
|
+
type ScopeValues,
|
|
51
52
|
SEEDED_OBJECT_VERSION,
|
|
52
53
|
type SeededApplicationRows,
|
|
53
54
|
type SeededRow,
|
|
@@ -448,8 +449,13 @@ export async function testDatabase(options: TestDatabaseOptions): Promise<TestDa
|
|
|
448
449
|
const [returned] = z.array(z.record(z.string(), z.unknown())).parse(stored)
|
|
449
450
|
return returned ?? {}
|
|
450
451
|
},
|
|
451
|
-
async firstRow(table, columns) {
|
|
452
|
-
const
|
|
452
|
+
async firstRow(table, columns, where = new Map()) {
|
|
453
|
+
const names = [...where.keys()]
|
|
454
|
+
const condition = names.length === 0 ? '' : ` WHERE ${names.map((name, index) => `${quoted(name)} = $${index + 1}`).join(' AND ')}`
|
|
455
|
+
const selected: unknown = await seeder.unsafe(
|
|
456
|
+
`SELECT ${columns.map(quoted).join(', ')} FROM ${quoted(schema)}.${quoted(table)}${condition} LIMIT 1`,
|
|
457
|
+
names.map((name) => where.get(name) ?? null),
|
|
458
|
+
)
|
|
453
459
|
return z.array(z.record(z.string(), z.unknown())).parse(selected)[0]
|
|
454
460
|
},
|
|
455
461
|
async insertFile(file) {
|
|
@@ -32,6 +32,11 @@ export interface WorldRow {
|
|
|
32
32
|
* Each occurs in no other fixture, so one found in an answer to another scope is a leaked row.
|
|
33
33
|
*/
|
|
34
34
|
readonly markers: readonly string[];
|
|
35
|
+
/**
|
|
36
|
+
* The values of the text columns of a row the table already held under a known scope value (`seedWorlds`' `scopes`),
|
|
37
|
+
* which no fixture wrote and other rows may share; empty for a row the fixture inserted.
|
|
38
|
+
*/
|
|
39
|
+
readonly stored: readonly string[];
|
|
35
40
|
}
|
|
36
41
|
/** One seeded row of an application table, which lies in no dimension. */
|
|
37
42
|
export interface ApplicationRow {
|
|
@@ -56,6 +61,8 @@ export interface SeededApplicationRows {
|
|
|
56
61
|
export interface SeededRow {
|
|
57
62
|
readonly key: string;
|
|
58
63
|
readonly markers: readonly string[];
|
|
64
|
+
/** Deletes the row, the rows its foreign keys made the fixture insert, and every row that refers to them. */
|
|
65
|
+
readonly remove: () => Promise<void>;
|
|
59
66
|
}
|
|
60
67
|
/**
|
|
61
68
|
* What the isolation fixture cannot seed for a schema: a CHECK its values fail, a required foreign key to a scoped table
|
|
@@ -75,11 +82,20 @@ export interface IsolationWorlds {
|
|
|
75
82
|
readonly own: WorldRow;
|
|
76
83
|
readonly other: WorldRow;
|
|
77
84
|
/**
|
|
78
|
-
* Deletes both rows of the table
|
|
79
|
-
*
|
|
85
|
+
* Deletes both rows of the table it inserted, every container row it inserted when it was given known scope values,
|
|
86
|
+
* and every row that refers to them, however deep; the other containers stay. A case whose rows hold pinned values or
|
|
87
|
+
* known scope values removes them, so the next case can seed the same values again under a unique index.
|
|
80
88
|
*/
|
|
81
89
|
readonly remove: () => Promise<void>;
|
|
82
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Known values of dimensions, by dimension: the own world's and the other world's, which differ. A dimension whose values
|
|
93
|
+
* are a fixed set (an enum the input names it by) takes these instead of fresh ones.
|
|
94
|
+
*/
|
|
95
|
+
export type ScopeValues = Readonly<Record<string, {
|
|
96
|
+
readonly own: string;
|
|
97
|
+
readonly other: string;
|
|
98
|
+
}>>;
|
|
83
99
|
/** What a test kit seeds through, over its migrated schema. */
|
|
84
100
|
export interface TestSeeding {
|
|
85
101
|
/**
|
|
@@ -88,9 +104,12 @@ export interface TestSeeding {
|
|
|
88
104
|
* marker, so a seed of one, or of a column that places the row, throws. `pins` (column → value) give a text column of
|
|
89
105
|
* `table` a known value instead of its marker: the value in the own row, and `<value>-other` in the other row, which
|
|
90
106
|
* counts among the other row's markers, so a leak of it is still found. A pin of a column that places the row, or of
|
|
91
|
-
* no text column, throws.
|
|
107
|
+
* no text column, throws. `scopes` place each world in known values of a dimension instead of fresh ones: a row of
|
|
108
|
+
* the table that owns it with that key is used as it is stored (a migration wrote it), its containers' values with it,
|
|
109
|
+
* and is inserted otherwise. A stored row of `table` itself takes no `values` or `pins`: they throw. A seed that throws
|
|
110
|
+
* removes what it inserted.
|
|
92
111
|
*/
|
|
93
|
-
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>, pins?: Readonly<Record<string, string
|
|
112
|
+
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>, pins?: Readonly<Record<string, string>>, scopes?: ScopeValues): Promise<IsolationWorlds>;
|
|
94
113
|
/**
|
|
95
114
|
* Seeds one row of the application table `table`: its key, version and creator stamped, `values` (column → value)
|
|
96
115
|
* as given, a text column too, every other column sampled. A value for a stamped column or a file column throws.
|
|
@@ -105,11 +124,11 @@ export interface TestSeeding {
|
|
|
105
124
|
*/
|
|
106
125
|
seedApplicationRows<DB, P>(tables: Tables<DB, P>, rows: Readonly<Record<string, Readonly<Record<string, SampleValue>>>>): Promise<SeededApplicationRows>;
|
|
107
126
|
/**
|
|
108
|
-
* Seeds one row of `table`, a child or dimension table that lies in the object
|
|
109
|
-
* seeded
|
|
110
|
-
* the markers it holds.
|
|
127
|
+
* Seeds one row of `table`, a child or dimension table that lies in the object `under`'s chain starts at (a row
|
|
128
|
+
* `seedWorlds` seeded, or a value of its chain with the values outside it), placed in that chain: the row a fixture
|
|
129
|
+
* names by id, such as a note's comment. Answers its key and the markers it holds.
|
|
111
130
|
*/
|
|
112
|
-
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: WorldRow): Promise<SeededRow>;
|
|
131
|
+
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: Pick<WorldRow, 'chain'>): Promise<SeededRow>;
|
|
113
132
|
/**
|
|
114
133
|
* Values for the policy's insert columns of `table` that its schema accepts: every required one, sampled; a file
|
|
115
134
|
* column a seeded file. `beside` is a row `seedWorlds` seeded that the inserted row will lie beside, in the same
|
|
@@ -206,8 +225,8 @@ export interface SeedingBackend {
|
|
|
206
225
|
* (`checkLimit`, `foreignKeyLimit`, `uniqueValueLimit`).
|
|
207
226
|
*/
|
|
208
227
|
insert(table: string, values: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>>>;
|
|
209
|
-
/** One row of `table
|
|
210
|
-
firstRow(table: string, columns: readonly string[]): Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
228
|
+
/** One row of `table` that holds `where` (column → value; every row when empty), of `columns`, or `undefined` when none does. */
|
|
229
|
+
firstRow(table: string, columns: readonly string[], where?: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
211
230
|
insertFile(file: SeededFile): Promise<void>;
|
|
212
231
|
/**
|
|
213
232
|
* Deletes `rows` (each found by its `match`; one already gone is skipped) and every row that refers to one of them,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TableProbe } from './store';
|
|
2
2
|
import { type TestDatabase } from './testing-shared';
|
|
3
3
|
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
4
|
-
export { type ApplicationRow, type IsolationWorlds, type SampleValue, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
4
|
+
export { type ApplicationRow, type IsolationWorlds, type SampleValue, type ScopeValues, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
5
5
|
export interface TestDatabaseOptions {
|
|
6
6
|
/** The application's `db/migrations/` directory. */
|
|
7
7
|
readonly migrations: string | URL;
|
package/types/src/testing.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TableProbe } from './store';
|
|
2
2
|
import { type TestDatabase } from './testing-shared';
|
|
3
3
|
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
4
|
-
export { type ApplicationRow, type IsolationWorlds, type SampleValue, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
4
|
+
export { type ApplicationRow, type IsolationWorlds, type SampleValue, type ScopeValues, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
5
5
|
export declare const TEST_POSTGRES_URL = "FABRIKA_TEST_POSTGRES_URL";
|
|
6
6
|
/** Set to `1` by the platform build: a missing test database is then a failure, not a skip. */
|
|
7
7
|
export declare const TEST_POSTGRES_REQUIRED = "FABRIKA_TEST_POSTGRES_REQUIRED";
|