Skip to content

Commit efc8ecc

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

7 files changed

Lines changed: 115 additions & 36 deletions

File tree

proto/nodeinfo_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ func TestAccountAddress_DatabaseID(t *testing.T) {
4343
So(string(d), ShouldEqual, target[i])
4444
}
4545
})
46+
47+
Convey("AccountAddress JSON Convert", t, func() {
48+
for i := range target {
49+
var a AccountAddress
50+
dbIDJson := []byte("\"" + target[i] + "\"")
51+
err := a.UnmarshalJSON(dbIDJson)
52+
So(err, ShouldBeNil)
53+
d := a.DatabaseID()
54+
So(string(d), ShouldEqual, target[i])
55+
}
56+
})
57+
4658
}
4759

4860
func TestNode_InitNodeCryptoInfo(t *testing.T) {

proto/proto_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ func TestDatabaseID_AccountAddress(t *testing.T) {
6868
So(err, ShouldBeNil)
6969
So(h[:], ShouldResemble, a[:])
7070
}
71+
})
7172

73+
Convey("AccountAddress invalid convert", t, func() {
74+
invalid := "invalid"
75+
dbID := DatabaseID(invalid)
76+
_, err := dbID.AccountAddress()
77+
So(err, ShouldNotBeNil)
7278
})
7379
}
7480

storage/storage_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func TestBadType(t *testing.T) {
6060
}
6161

6262
st, err := New(fmt.Sprintf("file:%s", fl.Name()))
63+
defer st.Close()
6364

6465
if err != nil {
6566
t.Fatalf("error occurred: %v", err)
@@ -92,6 +93,7 @@ func TestStorage(t *testing.T) {
9293
}
9394

9495
st, err := New(fmt.Sprintf("file:%s", fl.Name()))
96+
defer st.Close()
9597

9698
if err != nil {
9799
t.Fatalf("error occurred: %v", err)

utils/log/logwrapper.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ type CallerHook struct {
6969
StackLevels []logrus.Level
7070
}
7171

72+
func init() {
73+
AddHook(StandardCallerHook())
74+
}
75+
76+
//var (
77+
// // std is the name of the standard logger in stdlib `log`
78+
// std = logrus.New()
79+
//)
80+
81+
// StandardLogger returns the standard logger.
82+
func StandardLogger() *Logger {
83+
return (*Logger)(logrus.StandardLogger())
84+
}
85+
86+
// Printf logs a message at level Info on the standard logger.
87+
func (l *Logger) Printf(format string, args ...interface{}) {
88+
Printf(format, args...)
89+
}
90+
7291
// NewCallerHook creates new CallerHook
7392
func NewCallerHook(stackLevels []logrus.Level) *CallerHook {
7493
return &CallerHook{
@@ -175,20 +194,6 @@ func (hook *CallerHook) caller(entry *logrus.Entry) (relFuncName, caller string)
175194
return relFuncName, caller
176195
}
177196

178-
func init() {
179-
AddHook(StandardCallerHook())
180-
}
181-
182-
//var (
183-
// // std is the name of the standard logger in stdlib `log`
184-
// std = logrus.New()
185-
//)
186-
187-
// StandardLogger returns the standard logger.
188-
func StandardLogger() *Logger {
189-
return (*Logger)(logrus.StandardLogger())
190-
}
191-
192197
// SetOutput sets the standard logger output.
193198
func SetOutput(out io.Writer) {
194199
logrus.SetOutput(out)
@@ -288,28 +293,23 @@ func Error(args ...interface{}) {
288293
logrus.Error(args...)
289294
}
290295

291-
// Panic logs a message at level Panic on the standard logger.
292-
func Panic(args ...interface{}) {
293-
//std.WithField("Func", getFuncPath(2)).Panic(args...)
294-
logrus.Panic(args...)
295-
}
296-
297296
// Fatal logs a message at level Fatal on the standard logger.
298297
func Fatal(args ...interface{}) {
299298
//std.WithField("Func", getFuncPath(2)).Fatal(args...)
300299
logrus.Fatal(args...)
301300
}
302301

302+
// Panic logs a message at level Panic on the standard logger.
303+
func Panic(args ...interface{}) {
304+
//std.WithField("Func", getFuncPath(2)).Panic(args...)
305+
logrus.Panic(args...)
306+
}
307+
303308
// Debugf logs a message at level Debug on the standard logger.
304309
func Debugf(format string, args ...interface{}) {
305310
logrus.Debugf(format, args...)
306311
}
307312

308-
// Printf logs a message at level Info on the standard logger.
309-
func (l *Logger) Printf(format string, args ...interface{}) {
310-
Printf(format, args...)
311-
}
312-
313313
// Printf logs a message at level Info on the standard logger.
314314
func Printf(format string, args ...interface{}) {
315315
logrus.Printf(format, args...)
@@ -335,13 +335,13 @@ func Errorf(format string, args ...interface{}) {
335335
logrus.Errorf(format, args...)
336336
}
337337

338-
// Panicf logs a message at level Panic on the standard logger.
339-
func Panicf(format string, args ...interface{}) {
340-
logrus.Panicf(format, args...)
341-
}
342-
343338
// Fatalf logs a message at level Fatal on the standard logger.
344339
func Fatalf(format string, args ...interface{}) {
340+
logrus.Fatalf(format, args...)
341+
}
342+
343+
// Panicf logs a message at level Panic on the standard logger.
344+
func Panicf(format string, args ...interface{}) {
345345
logrus.Panicf(format, args...)
346346
}
347347

@@ -375,12 +375,12 @@ func Errorln(args ...interface{}) {
375375
logrus.Errorln(args...)
376376
}
377377

378-
// Panicln logs a message at level Panic on the standard logger.
379-
func Panicln(args ...interface{}) {
380-
logrus.Panicln(args...)
381-
}
382-
383378
// Fatalln logs a message at level Fatal on the standard logger.
384379
func Fatalln(args ...interface{}) {
385380
logrus.Fatalln(args...)
386381
}
382+
383+
// Panicln logs a message at level Panic on the standard logger.
384+
func Panicln(args ...interface{}) {
385+
logrus.Panicln(args...)
386+
}

utils/log/logwrapper_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ func TestWithField(t *testing.T) {
107107

108108
WithFields(*f).Debug("debug")
109109
WithTime(time.Now()).WithError(errors.New("new")).Debug("debug")
110-
NewEntry(StandardLogger()).WithTime(time.Now()).String()
110+
entry := NewEntry(StandardLogger())
111+
entry.WithTime(time.Now()).String()
112+
entry.Printf("entry printf %d", 1)
111113

112114
WithField("k", "v").Debug("debug")
113115
WithField("k", "v").Debugln("Debugln")
@@ -153,3 +155,34 @@ func TestWithField(t *testing.T) {
153155

154156
WithField("k", "v").Panic("panic")
155157
}
158+
159+
func TestSimpleLog(t *testing.T) {
160+
SetStringLevel("error", ErrorLevel)
161+
if GetLevel() != ErrorLevel {
162+
t.Fail()
163+
}
164+
Debug("Debug")
165+
Debugln("Debugln")
166+
Debugf("Debugf %d", 1)
167+
logger := StandardLogger()
168+
logger.Printf("StandardLogger Printf %d", 1)
169+
170+
SimpleLog = "Y"
171+
SetLevel(DebugLevel)
172+
Debug("Debug")
173+
Debugln("Debugln")
174+
Debugf("Debugf %d", 1)
175+
176+
SimpleLog = "N"
177+
SetOutput(&NilWriter{})
178+
SetFormatter(&NilFormatter{})
179+
Debug("Debug")
180+
Debugln("Debugln")
181+
Debugf("Debugf %d", 1)
182+
183+
logrus.StandardLogger().ExitFunc = func(code int) {}
184+
Fatal("Fatal")
185+
Fatalln("Fatalln")
186+
Fatalf("Fatalf %d", 1)
187+
logrus.StandardLogger().ExitFunc = nil
188+
}

utils/log/nillogger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ type NilFormatter struct{}
2727
func (f *NilFormatter) Format(entry *logrus.Entry) ([]byte, error) {
2828
return nil, nil
2929
}
30+
31+
// NilWriter just discards the log entry
32+
type NilWriter struct{}
33+
34+
// Write just return 0, nil for discarding log entry
35+
func (w *NilWriter) Write(p []byte) (n int, err error) {
36+
return 0, nil
37+
}

utils/msgpack_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,22 @@ func TestMsgPack_EncodeDecode(t *testing.T) {
5959
So(err, ShouldBeNil)
6060
So(*preValue, ShouldResemble, postValue)
6161
})
62+
63+
Convey("DecodeMsgPackPlain test", t, func() {
64+
log.SetLevel(log.DebugLevel)
65+
str := "test"
66+
buf, err := EncodeMsgPack(str)
67+
log.Debugf("string: test encoded len %d to %x", len(buf.Bytes()), buf.Bytes())
68+
So(err, ShouldBeNil)
69+
70+
var value string
71+
err = DecodeMsgPackPlain(buf.Bytes(), &value)
72+
So(err, ShouldBeNil)
73+
So(value, ShouldEqual, str)
74+
75+
var value2 string
76+
err = DecodeMsgPack(buf.Bytes(), &value2)
77+
So(err, ShouldBeNil)
78+
So(value2, ShouldEqual, str)
79+
})
6280
}

0 commit comments

Comments
 (0)