6 changed files with 161 additions and 4 deletions
-
2README.md
-
3lib/cache/cache.go
-
55lib/cache/cache_test.go
-
5lib/common/guid.go
-
57lib/database/database_test.go
-
43lib/response/response_test.go
@ -0,0 +1,55 @@ |
|||||
|
package cache |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
func TestCache(t *testing.T) { |
||||
|
appCache, err := Bootstrap() |
||||
|
if err != nil { |
||||
|
t.Errorf("bootstrap cache failed: %v\n", err) |
||||
|
} |
||||
|
if err := appCache.Set("key", "value"); err != nil { |
||||
|
t.Errorf("set cache failed: %v\n", err) |
||||
|
} |
||||
|
// real result
|
||||
|
value, err := appCache.Get("key") |
||||
|
if err != nil { |
||||
|
t.Errorf("get cache failed: %v\n", err) |
||||
|
} |
||||
|
if value == nil { |
||||
|
t.Error("get cache didn't fetch a value for set key") |
||||
|
} else if *value != "value" { |
||||
|
t.Errorf("get cache value should be \"value\" but is \"%s\"", *value) |
||||
|
} |
||||
|
// nil result
|
||||
|
value, err = appCache.Get("notExisting") |
||||
|
if err != nil { |
||||
|
t.Errorf("get cache failed: %v\n", err) |
||||
|
} |
||||
|
if value != nil { |
||||
|
t.Error("get cache must return nil for non existing key") |
||||
|
} |
||||
|
if err := appCache.SetWithTTL("ttl", "10", 3); err != nil { |
||||
|
t.Errorf("set cache failed: %v\n", err) |
||||
|
} |
||||
|
value, err = appCache.Get("ttl") |
||||
|
if err != nil { |
||||
|
t.Errorf("get cache failed: %v\n", err) |
||||
|
} |
||||
|
if value == nil { |
||||
|
t.Error("get cache didn't fetch a value for set key") |
||||
|
} else if *value != "10" { |
||||
|
t.Errorf("get cache value should be \"10\" but is \"%s\"", *value) |
||||
|
} |
||||
|
time.Sleep(3 * time.Second) |
||||
|
value, err = appCache.Get("ttl") |
||||
|
if err != nil { |
||||
|
t.Errorf("get cache failed: %v\n", err) |
||||
|
} |
||||
|
if value != nil { |
||||
|
t.Error("get cache must return nil for expired key") |
||||
|
} |
||||
|
appCache.Close() |
||||
|
} |
||||
@ -0,0 +1,57 @@ |
|||||
|
package database |
||||
|
|
||||
|
import ( |
||||
|
"strings" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
// FIXME make real unit tests
|
||||
|
func TestConnect(t *testing.T) { |
||||
|
if _, err := Connect(Credentials{ |
||||
|
Host: "127.0.0.1", |
||||
|
Port: 3308, |
||||
|
Dialect: "mySQL", |
||||
|
User: "unittest", |
||||
|
Password: "secret", |
||||
|
Database: "unittest", |
||||
|
}); err != nil { |
||||
|
if !strings.HasSuffix(err.Error(), "connect: connection refused") { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
} |
||||
|
if _, err := Connect(Credentials{ |
||||
|
Socket: "/tmp/unittest.socket", |
||||
|
Dialect: "mySQL", |
||||
|
User: "unittest", |
||||
|
Password: "secret", |
||||
|
Database: "unittest", |
||||
|
}); err != nil { |
||||
|
if !strings.HasSuffix(err.Error(), "no such file or directory") { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
} |
||||
|
if _, err := Connect(Credentials{ |
||||
|
Host: "127.0.0.1", |
||||
|
Port: 3309, |
||||
|
Dialect: "msSQL", |
||||
|
User: "unittest", |
||||
|
Password: "secret", |
||||
|
Database: "unittest", |
||||
|
}); err != nil { |
||||
|
if !strings.HasSuffix(err.Error(), "connect: connection refused") { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
} |
||||
|
if _, err := Connect(Credentials{ |
||||
|
Host: "127.0.0.1", |
||||
|
Port: 3310, |
||||
|
Dialect: "PostGRES", |
||||
|
User: "unittest", |
||||
|
Password: "secret", |
||||
|
Database: "unittest", |
||||
|
}); err != nil { |
||||
|
if !strings.HasSuffix(err.Error(), "connect: connection refused)") { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
package response |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func TestAppendError(t *testing.T) { |
||||
|
const errMsg = "expected error" |
||||
|
envelope := Envelope{} |
||||
|
e := envelope.AppendError(errors.New(errMsg)) |
||||
|
if e.Success { |
||||
|
t.Error("error response Success must be false") |
||||
|
} |
||||
|
if e.Result != nil { |
||||
|
t.Error("error response Result must be nil") |
||||
|
} |
||||
|
if len(e.Errors) != 1 { |
||||
|
t.Error("error response Errors must be set") |
||||
|
} else if e.Errors[0] != errMsg { |
||||
|
t.Errorf("error response error message must be \"%s\" but is \"%s\"", errMsg, e.Errors[0]) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func TestSetSuccess(t *testing.T) { |
||||
|
const result = "expected result" |
||||
|
envelope := Envelope{} |
||||
|
e := envelope.SetSuccess(result) |
||||
|
if !e.Success { |
||||
|
t.Error("success response Success must be true") |
||||
|
} |
||||
|
if e.Result == nil { |
||||
|
t.Error("success response Result must be set") |
||||
|
} |
||||
|
if msg, ok := e.Result.(string); ok { |
||||
|
if msg != result { |
||||
|
t.Errorf("success response result must be \"%s\" but is \"%s\"", result, msg) |
||||
|
} |
||||
|
} |
||||
|
if len(e.Errors) > 0 { |
||||
|
t.Error("success response Errors must be empty") |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue