-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer_test.go
92 lines (79 loc) · 1.87 KB
/
serializer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package serializer_test
import (
"strconv"
"testing"
"time"
jsonserializer "github.com/davetweetlive/jsonserializer"
"github.com/stretchr/testify/require"
)
var fileName = "tmp/user.json"
func TestCreateUsers(t *testing.T) {
t.Parallel()
users, err := jsonserializer.CreateUsers()
require.NoError(t, err)
require.Equal(t, len(users), 3)
require.Equal(t, users[1].FirstName, "Irak")
}
func TestWriteStructToJSONFile(t *testing.T) {
t.Parallel()
users, err := jsonserializer.CreateUsers()
require.NoError(t, err)
err = jsonserializer.WriteStructToJSONFile(users, fileName)
require.NoError(t, err)
}
func TestFromJSONFileToStruct(t *testing.T) {
t.Parallel()
users, err := jsonserializer.FromJSONFileToStruct(fileName)
require.NoError(t, err)
require.Equal(t, len(users), 3)
require.Equal(t, users[2].FirstName, "Imaginery")
}
func TestCreateUsersSubTest(t *testing.T) {
users, err := jsonserializer.CreateUsers()
require.NoError(t, err)
batch := []struct {
batch int
users []*jsonserializer.User
}{
{batch: 1, users: users},
{batch: 2, users: []*jsonserializer.User{
{
Id: 1,
FirstName: "ABC",
LastName: "Some Last Name",
Email: "abc@mail.com",
CreatedAt: time.Now(),
},
{
Id: 3,
FirstName: "PQR",
LastName: "Alphabet",
Email: "pa@gmail.com",
CreatedAt: time.Now(),
},
},
},
{
batch: 3,
users: []*jsonserializer.User{
{
Id: 3,
FirstName: "XYZ",
LastName: "Alphabet",
},
},
},
}
require.NotEqual(t, batch, nil)
for i, tc := range batch {
t.Run(strconv.Itoa(tc.batch), func(t *testing.T) {
t.Parallel()
if tc.batch < 1 || tc.batch > 3 {
require.NotEqual(t, tc.users[i].FirstName, "")
require.NotEqual(t, tc.users[i].LastName, "")
require.NotEqual(t, tc.users[i].Email, "")
}
require.NotEqual(t, len(tc.users), 0)
})
}
}