From 8f4c48ef8a5f0c5097301a2f0eaebae4a2680548 Mon Sep 17 00:00:00 2001 From: steebchen Date: Sat, 14 Sep 2024 22:57:12 -0400 Subject: [PATCH] test(casing): add case sensitivity tests Includes tests for case-insensitive name queries in MongoDB and PostgreSQL. Also adds Prisma schema for User model. --- test/features/casing/default_test.go | 83 ++++++++++++++++++++++++++++ test/features/casing/schema.prisma | 16 ++++++ 2 files changed, 99 insertions(+) create mode 100644 test/features/casing/default_test.go create mode 100644 test/features/casing/schema.prisma diff --git a/test/features/casing/default_test.go b/test/features/casing/default_test.go new file mode 100644 index 00000000..df87de22 --- /dev/null +++ b/test/features/casing/default_test.go @@ -0,0 +1,83 @@ +package db + +import ( + "context" + "testing" + + "github.com/steebchen/prisma-client-go/test" + "github.com/steebchen/prisma-client-go/test/helpers/massert" +) + +type cx = context.Context +type Func func(t *testing.T, client *PrismaClient, ctx cx) + +func str(v string) *string { + return &v +} + +func i(v int) *int { + return &v +} + +func TestCaseSensitivity(t *testing.T) { + tests := []struct { + name string + before []string + run Func + }{{ + name: "case sensitivity", + before: nil, + run: func(t *testing.T, client *PrismaClient, ctx cx) { + user, err := client.User.CreateOne( + User.Name.Set("THIS is me"), + User.ID.Set("123"), + ).Exec(ctx) + if err != nil { + t.Fatal(err) + } + + expected := &UserModel{ + InnerUser: InnerUser{ + ID: "123", + Name: "THIS is me", + }, + } + + massert.Equal(t, expected, user) + + user, err = client.User.FindFirst( + User.And( + User.Name.Contains("THIS"), + User.Name.Mode(QueryModeInsensitive), + ), + ).Exec(ctx) + if err != nil { + t.Fatal(err) + } + + massert.Equal(t, expected, user) + + user, err = client.User.FindFirst( + User.Name.Contains("this"), + User.Name.Mode(QueryModeInsensitive), + ).Exec(ctx) + if err != nil { + t.Fatal(err) + } + + massert.Equal(t, expected, user) + }, + }} + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + test.RunSerial(t, []test.Database{test.MongoDB, test.PostgreSQL}, func(t *testing.T, db test.Database, ctx context.Context) { + client := NewClient() + mockDBName := test.Start(t, db, client.Engine, tt.before) + defer test.End(t, db, client.Engine, mockDBName) + tt.run(t, client, context.Background()) + }) + }) + } +} diff --git a/test/features/casing/schema.prisma b/test/features/casing/schema.prisma new file mode 100644 index 00000000..e6a6197e --- /dev/null +++ b/test/features/casing/schema.prisma @@ -0,0 +1,16 @@ +datasource db { + provider = "postgresql" + url = env("__REPLACE__") +} + +generator db { + provider = "go run github.com/steebchen/prisma-client-go" + output = "." + disableGoBinaries = true + package = "db" +} + +model User { + id String @id @default(cuid()) @map("_id") + name String +}