Last active
December 25, 2024 10:00
-
-
Save omissis/9bda4baf0b242fcfbdd518ace5a066b2 to your computer and use it in GitHub Desktop.
go table tests example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shorturl_test | |
import ( | |
"fmt" | |
"github.com/omissis/ristretto/internal/domain/shorturl" | |
"github.com/omissis/ristretto/internal/test/mock" | |
"reflect" | |
"testing" | |
) | |
func TestNewResolveService(t *testing.T) { | |
r := &mock.RecordRepository{} | |
s := shorturl.NewResolveService(r) | |
if s == nil { | |
t.Fatalf("NewResolveService() does not return a ResolveService") | |
} | |
} | |
//////////////////////// Table Test Style /////////////////////// | |
func TestResolveService_Execute(t *testing.T) { | |
type test struct { | |
name string | |
setup func() *shorturl.ResolveService | |
input string | |
wantRecord *shorturl.Record | |
wantError error | |
} | |
for _, tt := range []test{ | |
{ | |
name: "Returns an existing record", | |
setup: func() *shorturl.ResolveService { | |
r := &mock.RecordRepository{} | |
if err := r.Add(shorturl.NewRecord("http://tenwarp.com/", "FoObAr")); err != nil { | |
t.Fatalf("Add() got = %v, want nil", err) | |
} | |
return shorturl.NewResolveService(r) | |
}, | |
input: "FoObAr", | |
wantRecord: shorturl.NewRecord("http://tenwarp.com/", "FoObAr"), | |
wantError: nil, | |
}, | |
{ | |
name: "Returns nothing", | |
setup: func() *shorturl.ResolveService { | |
r := &mock.RecordRepository{} | |
return shorturl.NewResolveService(r) | |
}, | |
input: "NotFound", | |
wantRecord: nil, | |
wantError: nil, | |
}, | |
{ | |
name: "ReturnsError", | |
setup: func() *shorturl.ResolveService { | |
r := &mock.RecordRepository{} | |
return shorturl.NewResolveService(r) | |
}, | |
input: "ERROR", | |
wantRecord: nil, | |
wantError: fmt.Errorf("Forced error"), | |
}, | |
} { | |
t.Run(tt.name, func (t *testing.T) { | |
s := tt.setup() | |
record, err := s.Execute(tt.input) | |
if !reflect.DeepEqual(err, tt.wantError) { | |
t.Errorf("Add() got error = %v, want %v", err, tt.wantError) | |
} | |
if !reflect.DeepEqual(record, tt.wantRecord) { | |
t.Errorf("Add() got record = %v, want %v", record, tt.wantRecord) | |
} | |
}) | |
} | |
} | |
//////////////////// End of Table Test Style //////////////////// | |
////////////////////// Classic Test Style /////////////////////// | |
func TestResolveService_Execute_ReturnsAnExistingRecord(t *testing.T) { | |
r := &mock.RecordRepository{} | |
if err := r.Add(shorturl.NewRecord("http://tenwarp.com/", "FoObAr")); err != nil { | |
t.Fatalf("Add() got = %v, want nil", err) | |
} | |
s := shorturl.NewResolveService(r) | |
record, err := s.Execute("FoObAr") | |
if err != nil { | |
t.Fatalf("Add() got error = %v, want nil", err) | |
} | |
if record == nil { | |
t.Fatalf("Add() got record = nil, want shorturl.Record") | |
} | |
} | |
func TestResolveService_Execute_ReturnsNothing(t *testing.T) { | |
r := &mock.RecordRepository{} | |
s := shorturl.NewResolveService(r) | |
record, err := s.Execute("NotFound") | |
if err != nil { | |
t.Fatalf("Add() got error = %v, want nil", err) | |
} | |
if record != nil { | |
t.Fatalf("Add() got record != nil, want nil") | |
} | |
} | |
func TestResolveService_Execute_ReturnsError(t *testing.T) { | |
r := &mock.RecordRepository{} | |
s := shorturl.NewResolveService(r) | |
_, err := s.Execute("ERROR") | |
if err == nil { | |
t.Fatalf("Add() got error = nil, want error") | |
} | |
} | |
/////////////////// End of Classic Test Style /////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment