Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

schema.test.ts

164 lines4.6 KB

1import { Schema, Table, column } from '@powersync/common'
2import { describe, expect, it } from 'vitest'
3import { convertTableToSchema } from '../src/schema'
4import type { StandardSchemaV1 } from '@standard-schema/spec'
5​
6describe(`Schema Conversion`, () => {
7 describe(`convertTableToSchema`, () => {
8 it(`should convert a simple table with text and integer columns`, () => {
9 const table = new Table({
10 name: column.text,
11 age: column.integer,
12 })
13​
14 const schema = convertTableToSchema(table)
15​
16 // Test schema structure
17 expect(schema).toHaveProperty(`~standard`)
18 expect(schema[`~standard`].version).toBe(1)
19 expect(schema[`~standard`].vendor).toBe(`powersync`)
20 expect(schema[`~standard`].validate).toBeTypeOf(`function`)
21​
22 // Test validation with valid data
23 const validResult = schema[`~standard`].validate({
24 id: `123`,
25 name: `John`,
26 age: 25,
27 }) as StandardSchemaV1.SuccessResult<any>
28​
29 expect(validResult.issues).toBeUndefined()
30 expect(validResult.value).toEqual({
31 id: `123`,
32 name: `John`,
33 age: 25,
34 })
35​
36 // Test validation with invalid data
37 const invalidResult = schema[`~standard`].validate({
38 id: `123`,
39 name: 123, // wrong type
40 age: `25`, // wrong type
41 }) as StandardSchemaV1.FailureResult
42​
43 expect(invalidResult.issues).toHaveLength(2)
44 expect(invalidResult.issues[0]?.message).toContain(`must be a string`)
45 expect(invalidResult.issues[1]?.message).toContain(`must be a number`)
46 })
47​
48 it(`should handle null values correctly`, () => {
49 const table = new Table({
50 name: column.text,
51 age: column.integer,
52 })
53​
54 const schema = convertTableToSchema(table)
55​
56 // Test validation with null values
57 const result = schema[`~standard`].validate({
58 id: `123`,
59 name: null,
60 age: null,
61 }) as StandardSchemaV1.SuccessResult<any>
62​
63 expect(result.issues).toBeUndefined()
64 expect(result.value).toEqual({
65 id: `123`,
66 name: null,
67 age: null,
68 })
69 })
70​
71 it(`should handle optional values correctly`, () => {
72 const table = new Table({
73 name: column.text,
74 age: column.integer,
75 })
76​
77 const schema = convertTableToSchema(table)
78​
79 // Test validation with null values
80 const result = schema[`~standard`].validate({
81 id: `123`,
82 name: null,
83 // Don't specify age
84 }) as StandardSchemaV1.SuccessResult<any>
85​
86 expect(result.issues).toBeUndefined()
87 expect(result.value).toEqual({
88 id: `123`,
89 name: null,
90 })
91 expect(result.value.age).undefined
92 })
93​
94 it(`should require id field`, () => {
95 const table = new Table({
96 name: column.text,
97 })
98​
99 const schema = convertTableToSchema(table)
100​
101 // Test validation without id
102 const result = schema[`~standard`].validate({
103 name: `John`,
104 }) as StandardSchemaV1.FailureResult
105​
106 expect(result.issues).toHaveLength(1)
107 expect(result.issues[0]?.message).toContain(`id field must be a string`)
108 })
109​
110 it(`should handle all column types`, () => {
111 const table = new Table({
112 text_col: column.text,
113 int_col: column.integer,
114 real_col: column.real,
115 })
116​
117 const schema = convertTableToSchema(table)
118​
119 // Test validation with all types
120 const result = schema[`~standard`].validate({
121 id: `123`,
122 text_col: `text`,
123 int_col: 42,
124 real_col: 3.14,
125 }) as StandardSchemaV1.SuccessResult<any>
126​
127 expect(result.issues).toBeUndefined()
128 expect(result.value).toEqual({
129 id: `123`,
130 text_col: `text`,
131 int_col: 42,
132 real_col: 3.14,
133 })
134 })
135​
136 it(`should validate each table independently`, () => {
137 const schema = new Schema({
138 users: new Table({
139 name: column.text,
140 }),
141 posts: new Table({
142 views: column.integer,
143 }),
144 })
145​
146 const usersSchema = convertTableToSchema(schema.props.users)
147 const postsSchema = convertTableToSchema(schema.props.posts)
148​
149 // Test that invalid data in one table doesn't affect the other
150 const userInvalidResult = usersSchema[`~standard`].validate({
151 id: `123`,
152 name: 42, // wrong type
153 }) as StandardSchemaV1.FailureResult
154​
155 const postValidResult = postsSchema[`~standard`].validate({
156 id: `456`,
157 views: 100,
158 }) as StandardSchemaV1.SuccessResult<any>
159​
160 expect(userInvalidResult.issues).toHaveLength(1)
161 expect(postValidResult.issues).toBeUndefined()
162 })
163 })
164})