Skip to content

Commit 7f9a1f5

Browse files
committed
Add more backend unit tests
1 parent d94e304 commit 7f9a1f5

File tree

10 files changed

+836
-0
lines changed

10 files changed

+836
-0
lines changed

backend/internal/tags/filters_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package tags
2+
3+
import (
4+
"npm/internal/util"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetFilterSchema(t *testing.T) {
11+
m := struct {
12+
ID int `filter:"id"`
13+
Name string `filter:"name"`
14+
}{
15+
ID: 1,
16+
Name: "John",
17+
}
18+
19+
filterSchema := util.PrettyPrintJSON(GetFilterSchema(m))
20+
21+
expectedSchema := `{
22+
"type": "array",
23+
"items": {
24+
"oneOf": [
25+
{
26+
"type": "object",
27+
"properties": {
28+
"field": {
29+
"type": "string",
30+
"pattern": "^id$"
31+
},
32+
"modifier": {
33+
"type": "string",
34+
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
35+
},
36+
"value": {
37+
"oneOf": [
38+
{
39+
"type": "string",
40+
"minLength": 1
41+
},
42+
{
43+
"type": "array",
44+
"items": {
45+
"type": "string",
46+
"minLength": 1
47+
}
48+
}
49+
]
50+
}
51+
}
52+
},
53+
{
54+
"type": "object",
55+
"properties": {
56+
"field": {
57+
"type": "string",
58+
"pattern": "^name$"
59+
},
60+
"modifier": {
61+
"type": "string",
62+
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
63+
},
64+
"value": {
65+
"oneOf": [
66+
{
67+
"type": "string",
68+
"minLength": 1
69+
},
70+
{
71+
"type": "array",
72+
"items": {
73+
"type": "string",
74+
"minLength": 1
75+
}
76+
}
77+
]
78+
}
79+
}
80+
}
81+
]
82+
}
83+
}`
84+
85+
assert.Equal(t, expectedSchema, filterSchema)
86+
}
87+
88+
func TestGetFilterTagSchema(t *testing.T) {
89+
schema := util.PrettyPrintJSON(getFilterTagSchema("id,integer"))
90+
91+
expectedSchema := `{
92+
"type": "object",
93+
"properties": {
94+
"field": {
95+
"type": "string",
96+
"pattern": "^id$"
97+
},
98+
"modifier": {
99+
"type": "string",
100+
"pattern": "^(equals|not|contains|starts|ends|in|notin|min|max|greater|less)$"
101+
},
102+
"value": {
103+
"oneOf": [
104+
{
105+
"type": "string",
106+
"pattern": "^[0-9]+$"
107+
},
108+
{
109+
"type": "array",
110+
"items": {
111+
"type": "string",
112+
"pattern": "^[0-9]+$"
113+
}
114+
}
115+
]
116+
}
117+
}
118+
}`
119+
120+
assert.Equal(t, expectedSchema, schema)
121+
}
122+
123+
func TestBoolFieldSchema(t *testing.T) {
124+
schema := util.PrettyPrintJSON(boolFieldSchema("active"))
125+
126+
expectedSchema := `{
127+
"type": "object",
128+
"properties": {
129+
"field": {
130+
"type": "string",
131+
"pattern": "^active$"
132+
},
133+
"modifier": {
134+
"type": "string",
135+
"pattern": "^(equals|not)$"
136+
},
137+
"value": {
138+
"oneOf": [
139+
{
140+
"type": "string",
141+
"pattern": "^(TRUE|true|t|yes|y|on|1|FALSE|f|false|n|no|off|0)$"
142+
},
143+
{
144+
"type": "array",
145+
"items": {
146+
"type": "string",
147+
"pattern": "^(TRUE|true|t|yes|y|on|1|FALSE|f|false|n|no|off|0)$"
148+
}
149+
}
150+
]
151+
}
152+
}
153+
}`
154+
155+
assert.Equal(t, expectedSchema, schema)
156+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestDBDate_Value(t *testing.T) {
10+
// Create a DBDate instance with a specific time
11+
expectedTime := time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)
12+
dbDate := DBDate{Time: expectedTime}
13+
14+
// Call the Value method
15+
value, err := dbDate.Value()
16+
17+
// Assert the value and error
18+
if err != nil {
19+
t.Errorf("Unexpected error: %v", err)
20+
}
21+
22+
// Convert the value to int64
23+
unixTime := value.(int64)
24+
25+
// Convert the unix time back to time.Time
26+
actualTime := time.Unix(unixTime, 0)
27+
28+
// Compare the actual time with the expected time
29+
if !actualTime.Equal(expectedTime) {
30+
t.Errorf("Expected time '%v', got '%v'", expectedTime, actualTime)
31+
}
32+
}
33+
34+
func TestDBDate_Scan(t *testing.T) {
35+
// Simulate a value from the database (unix timestamp)
36+
unixTime := int64(1640995200)
37+
38+
// Create a DBDate instance
39+
dbDate := DBDate{}
40+
41+
// Call the Scan method
42+
err := dbDate.Scan(unixTime)
43+
44+
// Assert the error
45+
if err != nil {
46+
t.Errorf("Unexpected error: %v", err)
47+
}
48+
49+
// Convert the DBDate's time to unix timestamp for comparison
50+
actualUnixTime := dbDate.Time.Unix()
51+
52+
// Compare the actual unix time with the expected unix time
53+
if actualUnixTime != unixTime {
54+
t.Errorf("Expected unix time '%v', got '%v'", unixTime, actualUnixTime)
55+
}
56+
}
57+
58+
func TestDBDate_UnmarshalJSON(t *testing.T) {
59+
// Simulate a JSON input representing a unix timestamp
60+
jsonData := []byte("1640995200")
61+
62+
// Create a DBDate instance
63+
dbDate := DBDate{}
64+
65+
// Call the UnmarshalJSON method
66+
err := dbDate.UnmarshalJSON(jsonData)
67+
68+
// Assert the error
69+
if err != nil {
70+
t.Errorf("Unexpected error: %v", err)
71+
}
72+
73+
// Convert the DBDate's time to unix timestamp for comparison
74+
actualUnixTime := dbDate.Time.Unix()
75+
76+
// Compare the actual unix time with the expected unix time
77+
expectedUnixTime := int64(1640995200)
78+
if actualUnixTime != expectedUnixTime {
79+
t.Errorf("Expected unix time '%v', got '%v'", expectedUnixTime, actualUnixTime)
80+
}
81+
}
82+
83+
func TestDBDate_MarshalJSON(t *testing.T) {
84+
// Create a DBDate instance with a specific time
85+
expectedTime := time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)
86+
dbDate := DBDate{Time: expectedTime}
87+
88+
// Call the MarshalJSON method
89+
jsonData, err := dbDate.MarshalJSON()
90+
91+
// Assert the value and error
92+
if err != nil {
93+
t.Errorf("Unexpected error: %v", err)
94+
}
95+
96+
// Convert the JSON data to an integer
97+
var actualUnixTime int64
98+
err = json.Unmarshal(jsonData, &actualUnixTime)
99+
if err != nil {
100+
t.Errorf("Failed to unmarshal JSON data: %v", err)
101+
}
102+
103+
// Convert the unix time back to time.Time
104+
actualTime := time.Unix(actualUnixTime, 0)
105+
106+
// Compare the actual time with the expected time
107+
if !actualTime.Equal(expectedTime) {
108+
t.Errorf("Expected time '%v', got '%v'", expectedTime, actualTime)
109+
}
110+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNullableDBIntValue(t *testing.T) {
8+
var d NullableDBInt
9+
10+
// Test when Int is 0 (null)
11+
d.Int = 0
12+
value, err := d.Value()
13+
if value != nil || err != nil {
14+
t.Errorf("Expected Value() to return nil, nil but got %v, %v", value, err)
15+
}
16+
17+
// Test when Int is not null
18+
d.Int = 10
19+
value, err = d.Value()
20+
if value != int64(10) || err != nil {
21+
t.Errorf("Expected Value() to return 10, nil but got %v, %v", value, err)
22+
}
23+
}
24+
25+
func TestNullableDBIntScan(t *testing.T) {
26+
var d NullableDBInt
27+
28+
// Test when src is an int
29+
err := d.Scan(20)
30+
if d.Int != 20 || err != nil {
31+
t.Errorf("Expected Scan(20) to set d.Int to 20 and return nil but got d.Int = %d, err = %v", d.Int, err)
32+
}
33+
34+
// Test when src is an int64
35+
err = d.Scan(int64(30))
36+
if d.Int != 30 || err != nil {
37+
t.Errorf("Expected Scan(int64(30)) to set d.Int to 30 and return nil but got d.Int = %d, err = %v", d.Int, err)
38+
}
39+
40+
// Test when src is a float32
41+
err = d.Scan(float32(40))
42+
if d.Int != 40 || err != nil {
43+
t.Errorf("Expected Scan(float32(40)) to set d.Int to 40 and return nil but got d.Int = %d, err = %v", d.Int, err)
44+
}
45+
46+
// Test when src is a float64
47+
err = d.Scan(float64(50))
48+
if d.Int != 50 || err != nil {
49+
t.Errorf("Expected Scan(float64(50)) to set d.Int to 50 and return nil but got d.Int = %d, err = %v", d.Int, err)
50+
}
51+
52+
// Test when src is a string
53+
err = d.Scan("60")
54+
if d.Int != 60 || err != nil {
55+
t.Errorf("Expected Scan(\"60\") to set d.Int to 60 and return nil but got d.Int = %d, err = %v", d.Int, err)
56+
}
57+
}
58+
59+
func TestNullableDBIntUnmarshalJSON(t *testing.T) {
60+
var d NullableDBInt
61+
62+
// Test when data is an integer value
63+
err := d.UnmarshalJSON([]byte("10"))
64+
if d.Int != 10 || err != nil {
65+
t.Errorf("Expected UnmarshalJSON([]byte(\"10\")) to set d.Int to 10 and return nil but got d.Int = %d, err = %v", d.Int, err)
66+
}
67+
68+
// Test when data is null
69+
err = d.UnmarshalJSON([]byte("null"))
70+
if d.Int != 0 || err != nil {
71+
t.Errorf("Expected UnmarshalJSON([]byte(\"null\")) to set d.Int to 0 and return nil but got d.Int = %d, err = %v", d.Int, err)
72+
}
73+
}
74+
75+
func TestNullableDBIntMarshalJSON(t *testing.T) {
76+
var d NullableDBInt
77+
78+
// Test when Int is 0 (null)
79+
d.Int = 0
80+
result, err := d.MarshalJSON()
81+
if string(result) != "null" || err != nil {
82+
t.Errorf("Expected MarshalJSON() to return \"null\", nil but got %s, %v", result, err)
83+
}
84+
85+
// Test when Int is not null
86+
d.Int = 10
87+
result, err = d.MarshalJSON()
88+
if string(result) != "10" || err != nil {
89+
t.Errorf("Expected MarshalJSON() to return \"10\", nil but got %s, %v", result, err)
90+
}
91+
}

0 commit comments

Comments
 (0)