Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add failing json test #729

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 174 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,181 @@ import (
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
setup(t)

DB.Create(&user)
tests := []struct {
name string
inputQuery string
inputArgs []any
wantLen int
wantAccountNumber string
}{
// These all fail
{
name: "single arrow true bool arg",
inputQuery: "config->'$.enabled' = ?",
inputArgs: []any{true},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "json_extract true bool arg",
inputQuery: "json_extract(config, '$.enabled') = ?",
inputArgs: []any{true},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "single arrow false bool arg",
inputQuery: "config->'$.enabled' = ?",
inputArgs: []any{false},
wantLen: 1,
wantAccountNumber: "AC67890",
},
{
name: "json_extract false bool arg",
inputQuery: "json_extract(config, '$.enabled') = ?",
inputArgs: []any{false},
wantLen: 1,
wantAccountNumber: "AC67890",
},

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
// These work
{
name: "single arrow no args with true bool",
inputQuery: "config->'$.enabled' = true",
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "single arrow no args with false bool",
inputQuery: "config->'$.enabled' = false",
wantLen: 1,
wantAccountNumber: "AC67890",
},
{
name: "double arrow with true string arg",
inputQuery: "config->>'$.enabled' = ?",
inputArgs: []any{"true"},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "double arrow with false string arg",
inputQuery: "config->>'$.enabled' = ?",
inputArgs: []any{"false"},
wantLen: 1,
wantAccountNumber: "AC67890",
},
{
name: "double arrow no args with string",
inputQuery: "config->>'$.enabled' = 'true'",
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "json_extract no args with true bool",
inputQuery: "json_extract(config, '$.enabled') = true",
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "json_extract no args with false bool",
inputQuery: "json_extract(config, '$.enabled') = false",
wantLen: 1,
wantAccountNumber: "AC67890",
},

// More passing tests to ensure other types like int and string work
{
name: "single arrow with int arg",
inputQuery: "config->'$.bar' = ?",
inputArgs: []any{123},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "double arrow with int arg",
inputQuery: "config->>'$.bar' = ?",
inputArgs: []any{123},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "single arrow with string arg",
inputQuery: "config->'$.foo' = ?",
inputArgs: []any{"abc"},
wantLen: 1,
wantAccountNumber: "AC12345",
},
{
name: "double arrow with string arg",
inputQuery: "config->>'$.foo' = ?",
inputArgs: []any{"abc"},
wantLen: 1,
wantAccountNumber: "AC12345",
},

// More passing tests expecting 0 results
{
name: "single arrow with int arg zero results",
inputQuery: "config->'$.bar' = ?",
inputArgs: []any{987},
wantLen: 0,
},
{
name: "single arrow with int arg zero results",
inputQuery: "config->'$.foo' = ?",
inputArgs: []any{"xyz"},
wantLen: 0,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var results []Account
if err := DB.Where(tc.inputQuery, tc.inputArgs...).Find(&results).Error; err != nil {
t.Fatalf("Failed, got error: %v", err)
}

if len(results) != tc.wantLen {
t.Fatalf("Failed, len(results) want=%d got=%d", tc.wantLen, len(results))
}

if tc.wantLen == 0 {
return
}

if results[0].Number != tc.wantAccountNumber {
t.Fatalf("Failed, account.Number want=%s got=%s", tc.wantAccountNumber, results[0].Number)
}
})
}
}

func setup(t *testing.T) {
err := DB.Exec(`TRUNCATE TABLE accounts`).Error
if err != nil {
t.Fatalf("unable to truncate table: %v", err)
}

accounts := []Account{
{
Number: "AC12345",
Config: AccountConfig{
Enabled: true,
Foo: "abc",
Bar: 123,
},
},
{
Number: "AC67890",
Config: AccountConfig{
Enabled: false,
Foo: "def",
Bar: 456,
},
},
}
DB.Create(&accounts)
}
7 changes: 7 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type Account struct {
gorm.Model
UserID sql.NullInt64
Number string
Config AccountConfig `gorm:"type:json;serializer:json"`
}

type AccountConfig struct {
Enabled bool `json:"enabled"`
Foo string `json:"foo"`
Bar int64 `json:"bar"`
}

type Pet struct {
Expand Down
Loading