feat(testing): session tests consider platform errors now

pull/10616/head
Leonardo Di Donato 2018-12-19 16:24:57 +01:00 committed by Leonardo Di Donato
parent 19eb016c5f
commit 486e25a46f
1 changed files with 29 additions and 36 deletions

View File

@ -41,14 +41,18 @@ type SessionFields struct {
Users []*platform.User
}
type sessionServiceFunc func(
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
t *testing.T,
)
// SessionService tests all the service functions.
func SessionService(
init func(SessionFields, *testing.T) (platform.SessionService, func()), t *testing.T,
init func(SessionFields, *testing.T) (platform.SessionService, string, func()), t *testing.T,
) {
tests := []struct {
name string
fn func(init func(SessionFields, *testing.T) (platform.SessionService, func()),
t *testing.T)
fn sessionServiceFunc
}{
{
name: "CreateSession",
@ -72,7 +76,7 @@ func SessionService(
// CreateSession testing
func CreateSession(
init func(SessionFields, *testing.T) (platform.SessionService, func()),
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
t *testing.T,
) {
type args struct {
@ -116,19 +120,11 @@ func CreateSession(
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
s, opPrefix, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
session, err := s.CreateSession(ctx, tt.args.user)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
if diff := cmp.Diff(session, tt.wants.session, sessionCmpOptions...); diff != "" {
t.Errorf("sessions are different -got/+want\ndiff %s", diff)
@ -139,7 +135,7 @@ func CreateSession(
// FindSession testing
func FindSession(
init func(SessionFields, *testing.T) (platform.SessionService, func()),
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
t *testing.T,
) {
type args struct {
@ -182,24 +178,29 @@ func FindSession(
},
},
},
{
name: "look for not exising session",
args: args{
key: "abc123xyz",
},
wants: wants{
err: &platform.Error{
Code: platform.ENotFound,
Op: platform.OpFindSession,
Msg: platform.ErrSessionNotFound,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
s, opPrefix, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
session, err := s.FindSession(ctx, tt.args.key)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
}
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
if diff := cmp.Diff(session, tt.wants.session, sessionCmpOptions...); diff != "" {
t.Errorf("session is different -got/+want\ndiff %s", diff)
@ -210,7 +211,7 @@ func FindSession(
// ExpireSession testing
func ExpireSession(
init func(SessionFields, *testing.T) (platform.SessionService, func()),
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
t *testing.T,
) {
type args struct {
@ -257,20 +258,12 @@ func ExpireSession(
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
s, opPrefix, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
err := s.ExpireSession(ctx, tt.args.key)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected errors to be equal '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
}
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
session, err := s.FindSession(ctx, tt.args.key)
if err == nil {