Skip to content

Commit 6611843

Browse files
committed
Imporve unit test cover for supportive code
1 parent efc8ecc commit 6611843

4 files changed

Lines changed: 71 additions & 5 deletions

File tree

storage/dsn_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,24 @@ func TestDSN(t *testing.T) {
4343

4444
dsn.AddParam("key", "value")
4545
t.Logf("Test set add param: formatted = %s", dsn.Format())
46+
47+
dsn.AddParam("key", "")
48+
t.Logf("Test delete param by set empty to add param: formatted = %s", dsn.Format())
49+
if _, ok := dsn.GetParam("key"); ok {
50+
t.Errorf("Should not have deleted key")
51+
}
52+
}
53+
54+
invalidString1 := "file:test.db?p1"
55+
dsn, err := NewDSN(invalidString1)
56+
if err == nil {
57+
t.Errorf("Should occurred unrecognized parameter error: %v", dsn)
58+
}
59+
60+
dsn = &DSN{}
61+
dsn.AddParam("clone", "true")
62+
clone := dsn.Clone()
63+
if _, ok := clone.GetParam("clone"); !ok {
64+
t.Errorf("Should cloned params")
4665
}
4766
}

storage/storage_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,26 @@ func newNamedQuery(query string, args map[string]interface{}) (q Query) {
5353
}
5454

5555
func TestBadType(t *testing.T) {
56-
fl, err := ioutil.TempFile("", "sqlite3-")
56+
// invalid storage
57+
invalidString1 := "file:test.db?p1"
5758

58-
if err != nil {
59-
t.Fatalf("error occurred: %v", err)
59+
st, err := New(invalidString1)
60+
if err == nil {
61+
defer st.Close()
62+
t.Fatalf("should occurred error: %v", st)
6063
}
6164

62-
st, err := New(fmt.Sprintf("file:%s", fl.Name()))
65+
st = &Storage{
66+
dsn: invalidString1,
67+
db: nil,
68+
}
69+
err = st.Close()
70+
if err == nil {
71+
t.Fatalf("should occurred error: %v", st)
72+
}
73+
74+
// valid storage
75+
st, err = New("file::memory:")
6376
defer st.Close()
6477

6578
if err != nil {
@@ -83,6 +96,16 @@ func TestBadType(t *testing.T) {
8396
} else {
8497
t.Logf("Error occurred as expected: %v", err)
8598
}
99+
100+
// empty query
101+
_, _, _, err = st.Query(context.Background(), []Query{})
102+
if err != nil {
103+
t.Fatal("empty query should be successed")
104+
}
105+
_, err = st.Exec(context.Background(), []Query{})
106+
if err != nil {
107+
t.Fatal("empty exec should be successed")
108+
}
86109
}
87110

88111
func TestStorage(t *testing.T) {

types/account_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestUserPermissionFromRole(t *testing.T) {
3333
err = json.Unmarshal([]byte(`"Write"`), &r)
3434
So(err, ShouldBeNil)
3535
So(r, ShouldEqual, Write)
36-
err = json.Unmarshal([]byte(`"Read,Write"`), &r)
36+
err = r.UnmarshalJSON([]byte(`"Read,Write"`))
3737
So(err, ShouldBeNil)
3838
So(r, ShouldEqual, ReadWrite)
3939
})
@@ -42,8 +42,18 @@ func TestUserPermissionFromRole(t *testing.T) {
4242
So(r, ShouldEqual, Void)
4343
r.FromString(Read.String())
4444
So(r, ShouldEqual, Read)
45+
r.FromString(Void.String())
46+
So(r, ShouldEqual, Void)
4547
r.FromString(ReadWrite.String())
4648
So(r, ShouldEqual, ReadWrite)
49+
r.FromString(Admin.String())
50+
So(r, ShouldEqual, Admin)
51+
52+
// tricky case
53+
trickyStr := "Write,Super"
54+
r.FromString(trickyStr)
55+
So(r, ShouldEqual, Write|Super)
56+
So(r.String(), ShouldEqual, trickyStr)
4757
})
4858
}
4959

utils/log/logwrapper_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func TestWithField(t *testing.T) {
102102

103103
call0()
104104

105+
WithError(errors.New("new")).WithField("newfieldkey", "newfieldvalue").WithTime(time.Now()).Debug("debug")
105106
f := new(Fields)
106107
WithError(errors.New("new")).WithFields(*f).WithTime(time.Now()).Debug("debug")
107108

@@ -179,10 +180,23 @@ func TestSimpleLog(t *testing.T) {
179180
Debug("Debug")
180181
Debugln("Debugln")
181182
Debugf("Debugf %d", 1)
183+
}
184+
185+
func TestFatalLog(t *testing.T) {
186+
SetStringLevel("willusenextparam", ErrorLevel)
187+
if GetLevel() != ErrorLevel {
188+
t.Fail()
189+
}
182190

183191
logrus.StandardLogger().ExitFunc = func(code int) {}
184192
Fatal("Fatal")
185193
Fatalln("Fatalln")
186194
Fatalf("Fatalf %d", 1)
195+
196+
entry := WithError(errors.New("new"))
197+
entry.Fatal("entry Fatal")
198+
entry.Fatalln("entry Fatalln")
199+
entry.Fatalf("entry Fatal %d", 1)
200+
187201
logrus.StandardLogger().ExitFunc = nil
188202
}

0 commit comments

Comments
 (0)