Skip to content

Commit bf3c17e

Browse files
Add tests for Boolean DataType
1 parent 016821e commit bf3c17e

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

tests/boolean_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
** Copyright (c) 2025 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
39+
package tests
40+
41+
import (
42+
"database/sql"
43+
"testing"
44+
)
45+
46+
type BooleanTest struct {
47+
ID uint `gorm:"column:ID;primaryKey"`
48+
Flag bool `gorm:"column:FLAG"`
49+
Nullable *bool `gorm:"column:NULLABLE"`
50+
SQLBool sql.NullBool `gorm:"column:SQL_BOOL"`
51+
}
52+
53+
func TestBooleanBasicInsert(t *testing.T) {
54+
DB.Migrator().DropTable(&BooleanTest{})
55+
if err := DB.AutoMigrate(&BooleanTest{}); err != nil {
56+
t.Fatalf("failed to migrate: %v", err)
57+
}
58+
59+
valTrue := true
60+
valFalse := false
61+
62+
// Insert true
63+
bt1 := BooleanTest{Flag: true, Nullable: &valTrue}
64+
if err := DB.Create(&bt1).Error; err != nil {
65+
t.Fatalf("insert true failed: %v", err)
66+
}
67+
68+
// Insert false
69+
bt2 := BooleanTest{Flag: false, Nullable: &valFalse}
70+
if err := DB.Create(&bt2).Error; err != nil {
71+
t.Fatalf("insert false failed: %v", err)
72+
}
73+
74+
// Verify fetch
75+
var got1, got2 BooleanTest
76+
if err := DB.First(&got1, bt1.ID).Error; err != nil {
77+
t.Fatal(err)
78+
}
79+
if got1.Flag != true {
80+
t.Errorf("expected true, got %v", got1.Flag)
81+
}
82+
83+
if err := DB.First(&got2, bt2.ID).Error; err != nil {
84+
t.Fatal(err)
85+
}
86+
if got2.Flag != false {
87+
t.Errorf("expected false, got %v", got2.Flag)
88+
}
89+
}
90+
91+
func TestBooleanUpdate(t *testing.T) {
92+
DB.Migrator().DropTable(&BooleanTest{})
93+
DB.AutoMigrate(&BooleanTest{})
94+
95+
bt := BooleanTest{Flag: false}
96+
DB.Create(&bt)
97+
98+
// Update false → true
99+
if err := DB.Model(&bt).Update("Flag", true).Error; err != nil {
100+
t.Fatalf("update failed: %v", err)
101+
}
102+
103+
var got BooleanTest
104+
DB.First(&got, bt.ID)
105+
if got.Flag != true {
106+
t.Errorf("expected true after update, got %v", got.Flag)
107+
}
108+
}
109+
110+
func TestBooleanQueryFilters(t *testing.T) {
111+
DB.Migrator().DropTable(&BooleanTest{})
112+
DB.AutoMigrate(&BooleanTest{})
113+
114+
DB.Create(&BooleanTest{Flag: true})
115+
DB.Create(&BooleanTest{Flag: false})
116+
117+
var trues []BooleanTest
118+
if err := DB.Where("FLAG = ?", true).Find(&trues).Error; err != nil {
119+
t.Fatal(err)
120+
}
121+
for _, row := range trues {
122+
if !row.Flag {
123+
t.Errorf("expected only true rows, got false")
124+
}
125+
}
126+
}
127+
128+
func TestBooleanNegativeInvalidDBValue(t *testing.T) {
129+
// Insert invalid value directly (bypassing GORM)
130+
if err := DB.Exec("INSERT INTO BOOLEAN_TEST (ID, FLAG) VALUES (999, 2)").Error; err != nil {
131+
t.Logf("expected insert error: %v", err)
132+
return
133+
}
134+
135+
var got BooleanTest
136+
err := DB.First(&got, 999).Error
137+
if err == nil {
138+
t.Errorf("expected scan error for invalid boolean mapping, got %+v", got)
139+
}
140+
}

0 commit comments

Comments
 (0)