Skip to content

Commit

Permalink
Add additional tests to validators
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Dec 11, 2024
1 parent 2265f01 commit 7af7983
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 9 deletions.
28 changes: 27 additions & 1 deletion drizzle-typebox/tests/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type as t } from '@sinclair/typebox';
import { type Equal, sql } from 'drizzle-orm';
import { int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { customType, int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
import { CONSTANTS } from '~/constants.ts';
Expand Down Expand Up @@ -207,6 +207,32 @@ test('refine table - select', (tc) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (tc) => {
const customText = customType({ dataType: () => 'text' });
const table = mysqlTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = t.String({ minLength: 1, maxLength: 100 });
const result = createSelectSchema(table, {
c2: (schema) => t.Integer({ minimum: schema.minimum, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});
const expected = t.Object({
c1: t.Union([intSchema, t.Null()]),
c2: t.Integer({ minimum: CONSTANTS.INT32_MIN, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});

expectSchemaShape(tc, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (tc) => {
const table = mysqlTable('test', {
c1: int(),
Expand Down
38 changes: 37 additions & 1 deletion drizzle-typebox/tests/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Type as t } from '@sinclair/typebox';
import { type Equal, sql } from 'drizzle-orm';
import { integer, pgEnum, pgMaterializedView, pgSchema, pgTable, pgView, serial, text } from 'drizzle-orm/pg-core';
import {
customType,
integer,
pgEnum,
pgMaterializedView,
pgSchema,
pgTable,
pgView,
serial,
text,
} from 'drizzle-orm/pg-core';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
import { CONSTANTS } from '~/constants.ts';
Expand Down Expand Up @@ -233,6 +243,32 @@ test('refine table - select', (tc) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (tc) => {
const customText = customType({ dataType: () => 'text' });
const table = pgTable('test', {
c1: integer(),
c2: integer().notNull(),
c3: integer().notNull(),
c4: customText(),
});

const customTextSchema = t.String({ minLength: 1, maxLength: 100 });
const result = createSelectSchema(table, {
c2: (schema) => t.Integer({ minimum: schema.minimum, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});
const expected = t.Object({
c1: t.Union([integerSchema, t.Null()]),
c2: t.Integer({ minimum: CONSTANTS.INT32_MIN, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});

expectSchemaShape(tc, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (tc) => {
const table = pgTable('test', {
c1: integer(),
Expand Down
28 changes: 27 additions & 1 deletion drizzle-typebox/tests/sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type as t } from '@sinclair/typebox';
import { type Equal, sql } from 'drizzle-orm';
import { int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { customType, int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { test } from 'vitest';
import { bufferSchema, jsonSchema } from '~/column.ts';
import { CONSTANTS } from '~/constants.ts';
Expand Down Expand Up @@ -186,6 +186,32 @@ test('refine table - select', (tc) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (tc) => {
const customText = customType({ dataType: () => 'text' });
const table = sqliteTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = t.String({ minLength: 1, maxLength: 100 });
const result = createSelectSchema(table, {
c2: (schema) => t.Integer({ minimum: schema.minimum, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});
const expected = t.Object({
c1: t.Union([intSchema, t.Null()]),
c2: t.Integer({ minimum: CONSTANTS.INT32_MIN, maximum: 1000 }),
c3: t.Integer({ minimum: 1, maximum: 10 }),
c4: customTextSchema,
});

expectSchemaShape(tc, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (tc) => {
const table = sqliteTable('test', {
c1: int(),
Expand Down
28 changes: 27 additions & 1 deletion drizzle-valibot/tests/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Equal, sql } from 'drizzle-orm';
import { int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { customType, int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import * as v from 'valibot';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -210,6 +210,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = mysqlTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = v.pipe(v.string(), v.minLength(1), v.maxLength(100));
const result = createSelectSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});
const expected = v.object({
c1: v.nullable(intSchema),
c2: v.pipe(intSchema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = mysqlTable('test', {
c1: int(),
Expand Down
38 changes: 37 additions & 1 deletion drizzle-valibot/tests/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { type Equal, sql } from 'drizzle-orm';
import { integer, pgEnum, pgMaterializedView, pgSchema, pgTable, pgView, serial, text } from 'drizzle-orm/pg-core';
import {
customType,
integer,
pgEnum,
pgMaterializedView,
pgSchema,
pgTable,
pgView,
serial,
text,
} from 'drizzle-orm/pg-core';
import * as v from 'valibot';
import { test } from 'vitest';
import { jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -234,6 +244,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = pgTable('test', {
c1: integer(),
c2: integer().notNull(),
c3: integer().notNull(),
c4: customText(),
});

const customTextSchema = v.pipe(v.string(), v.minLength(1), v.maxLength(100));
const result = createSelectSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});
const expected = v.object({
c1: v.nullable(integerSchema),
c2: v.pipe(integerSchema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = pgTable('test', {
c1: integer(),
Expand Down
28 changes: 27 additions & 1 deletion drizzle-valibot/tests/sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Equal, sql } from 'drizzle-orm';
import { int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { customType, int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import * as v from 'valibot';
import { test } from 'vitest';
import { bufferSchema, jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -187,6 +187,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = sqliteTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = v.pipe(v.string(), v.minLength(1), v.maxLength(100));
const result = createSelectSchema(table, {
c2: (schema) => v.pipe(schema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});
const expected = v.object({
c1: v.nullable(intSchema),
c2: v.pipe(intSchema, v.maxValue(1000)),
c3: v.pipe(v.string(), v.transform(Number)),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = sqliteTable('test', {
c1: int(),
Expand Down
28 changes: 27 additions & 1 deletion drizzle-zod/tests/mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Equal, sql } from 'drizzle-orm';
import { int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { customType, int, mysqlSchema, mysqlTable, mysqlView, serial, text } from 'drizzle-orm/mysql-core';
import { test } from 'vitest';
import { z } from 'zod';
import { jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -201,6 +201,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = mysqlTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = z.string().min(1).max(100);
const result = createSelectSchema(table, {
c2: (schema) => schema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});
const expected = z.object({
c1: intSchema.nullable(),
c2: intSchema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = mysqlTable('test', {
c1: int(),
Expand Down
38 changes: 37 additions & 1 deletion drizzle-zod/tests/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { type Equal, sql } from 'drizzle-orm';
import { integer, pgEnum, pgMaterializedView, pgSchema, pgTable, pgView, serial, text } from 'drizzle-orm/pg-core';
import {
customType,
integer,
pgEnum,
pgMaterializedView,
pgSchema,
pgTable,
pgView,
serial,
text,
} from 'drizzle-orm/pg-core';
import { test } from 'vitest';
import { z } from 'zod';
import { jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -234,6 +244,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = pgTable('test', {
c1: integer(),
c2: integer().notNull(),
c3: integer().notNull(),
c4: customText(),
});

const customTextSchema = z.string().min(1).max(100);
const result = createSelectSchema(table, {
c2: (schema) => schema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});
const expected = z.object({
c1: integerSchema.nullable(),
c2: integerSchema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = pgTable('test', {
c1: integer(),
Expand Down
28 changes: 27 additions & 1 deletion drizzle-zod/tests/sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Equal, sql } from 'drizzle-orm';
import { int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { customType, int, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
import { test } from 'vitest';
import { z } from 'zod';
import { bufferSchema, jsonSchema } from '~/column.ts';
Expand Down Expand Up @@ -182,6 +182,32 @@ test('refine table - select', (t) => {
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - select with custom data type', (t) => {
const customText = customType({ dataType: () => 'text' });
const table = sqliteTable('test', {
c1: int(),
c2: int().notNull(),
c3: int().notNull(),
c4: customText(),
});

const customTextSchema = z.string().min(1).max(100);
const result = createSelectSchema(table, {
c2: (schema) => schema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});
const expected = z.object({
c1: intSchema.nullable(),
c2: intSchema.max(1000),
c3: z.string().transform(Number),
c4: customTextSchema,
});

expectSchemaShape(t, expected).from(result);
Expect<Equal<typeof result, typeof expected>>();
});

test('refine table - insert', (t) => {
const table = sqliteTable('test', {
c1: int(),
Expand Down

0 comments on commit 7af7983

Please sign in to comment.