i have the following function that returns a closure in golang, any idea/reference how can possibly write a test for it?
type (
OrderRepoInterface interface {
func save(msg Message) error
}
// OrderAggregation represents an event handler
EventHandler struct {
repo OrderRepoInterface // in main.go i pass a concrete repository here
}
VersionedEventHandler struct {
function func(msg *Message) error
}
Message struct {
version int
payload string
}
)
func (o *EventHandler) OnOrderWasCreated() VersionedEventHandler {
return func(msg *Message) error {
msg.version = 1
err := o.repo.save(msg)
return err
}
}
ps
this is not the real code, as i am using a couple of libraries, i drafted this question with the above code, hoping that it would give an idea what i am trying to achieve, so it may not compile
edit
what i am after is to see some idiomatic examples or ideas where a function that returns an anonymous function is tested in go.
so i don't necessary need a working solution.
It would help to have a working example, so I provided one.
For this simple case, it would probbably suffice to just have a dict which maps version numbers to functions which handle the saving of an order. But I have tried to implement closer to what you have provided, with an order handler interface.
For testing, you will want some sort of log to capture side effects, so that you can verify that the correct handler is being called. To do that, I added a global string array called eventLog which can be appended to. For testing, you will want to create more MultiVersionHandlers, and more test Messages.
You will want to verify that the handlers respond to the test messages in the way you imagine, by calling the save() method on the handlers and comparing the contents of the eventLog with what you expected. Also, you will want to create messages that should fail. These messages would not map to a version that the handler supports. You then verify that the proper error value is returned. I have done some of this for you.
package main
import "fmt"
type MultiVersionHandler struct {
handlers map[int]OrderRepoInterface
}
type Message struct {
version int
payload string
}
type OrderRepoInterface interface {
save(Message) error
}
type OrderHandler struct {
saveHandler func(Message) error
}
// let's implement the OrderRepoInterface for a regular order handler
func (oh OrderHandler) save(msg Message) error {
return oh.saveHandler(msg)
}
// let's implement the OrderRepoInterface for a multi version order handler
func (mh MultiVersionHandler) save(msg Message) error {
if handler, ok := mh.handlers[msg.version]; ok {
return handler.save(msg)
}
return fmt.Errorf("doesn't support version %d, payload %q",
msg.version,
msg.payload)
}
// We will use eventLog capture simulations of a log of events which
// happen via our handlers. Useful for verification.
var eventLog = []string{}
func main() {
multiHandler := MakeMultiHandler()
msg1 := Message{payload: "make me a burger", version: 1}
msg2 := Message{payload: "make me a cake", version: 2}
msg3 := Message{payload: "make me a robot", version: 3}
// Create a message which has no handler.
// This message for version 4, should cause an error.
msg4 := Message{payload: "make me a planet", version: 4}
err := multiHandler.save(msg1)
err = multiHandler.save(msg2)
err = multiHandler.save(msg3)
err = multiHandler.save(msg4)
if err != nil {
fmt.Printf("Expecting an error: %q\n", err.Error())
// Expecting an error:
// "does not have a handler for version 4.
// Cannot process payload "make me a planet""
}
fmt.Printf("Event Log:%#v", eventLog)
// Event Log:[]string{
// "Amy will make me a burger",
// "Brandy will make me a cake",
// "Caleb will make me a robot"}
}
// This makes a multi version handler for an example.
// You should create more of these for testing different scenarios.
func MakeMultiHandler() OrderRepoInterface {
amy := OrderHandler{
saveHandler: func(msg Message) error {
action := "Amy will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}
brandy := OrderHandler{
saveHandler: func(msg Message) error {
action := "Brandy will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}
caleb := OrderHandler{
saveHandler: func(msg Message) error {
action := "Caleb will " + msg.payload
eventLog = append(eventLog, action)
return nil
},
}
multiHandler := MultiVersionHandler{
handlers: map[int]OrderRepoInterface{
1: amy, // amy should handle version 1 message
2: brandy, // brandy should handle version 2 message
3: caleb, // caleb should handle version 3 message
},
}
return multiHandler
}
Related
I'm having a hard time to get the current Cognito user attributes from within my lambda function, that is written in Go. I'm currently doing:
userAttributes = request.RequestContext.Authorizer["claims"]
And if I want to get the email:
userEmail = request.RequestContext.Authorizer["claims"].(map[string]interface{})["email"].(string)
I don't think this is a good way or even an acceptable way - it must have a better way to do it.
You can use 3rd party library to convert map[string]interface{} to a concrete type. Check the mitchellh/mapstructure library, it will help you to implement in a better way.
So, you could improve your code with this code :
import "github.com/mitchellh/mapstructure"
type Claims struct {
Email string
// other fields
ID int
}
func claims(r request.Request) (Claims, error) {
input := r.RequestContext.Authorizer["claims"]
output := Claims{}
err := mapstructure.Decode(input, &output)
if err != nil {
return nil, err
}
return output, nil
}
And somewhere in your handlers, you could get your claims by calling this method
func someWhere(){
userClaims, err := claims(request)
if err != nil {
// handle
}
// you can now use : userClaims.Email, userClaims.ID
}
Don't forget to change func claims request parameter type according to yours (r parameter).
For some reason, I cannot seem to get ioutil.ReadAll(res.Body), where res is the *http.Response returned by res, err := hc.Do(redirectRequest) (for hc http.Client, redirectRequest *http.Request).
Testing strategy thus far
Any time I see hc.Do or http.Request in the SUT, my instinct is to spin up a fake server and point the appropriate application states to it. Such a server, for this test, looks like this :
badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// some stuff
w.Write([some bad bytes])
}))
defer badServer.Close()
I don't seem to have a way to control res.Body, which is literally the only thing keeping me from 100% test completion against the func this is all in.
I tried, in the errorThrowingServer's handler func, setting r.Body to a stub io.ReadCloser that throws an error when Read() is called, but that doesn't effect res.
You can mock the body. Basically body is an io.ReadCloser interface so, you can do something like this:
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockReadCloser struct {
mock.Mock
}
func (m *mockReadCloser) Read(p []byte) (n int, err error) {
args := m.Called(p)
return args.Int(0), args.Error(1)
}
func (m *mockReadCloser) Close() error {
args := m.Called()
return args.Error(0)
}
func TestTestingSomethingWithBodyError(t *testing.T) {
mockReadCloser := mockReadCloser{}
// if Read is called, it will return error
mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
// if Close is called, it will return error
mockReadCloser.On("Close").Return(fmt.Errorf("error closing"))
request := &http.Request{
// pass the mock address
Body: &mockReadCloser,
}
expected := "what you expected"
result := YourMethod(request)
assert.Equal(t, expected, result)
mockReadCloser.AssertExpectations(t)
}
To stop reading you can use:
mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, io.EOF).Once()
As far as I could find perusing the source files for all of the working parts, the only way to get http.Response.Body.Read() to fail is commented here:
https://golang.org/src/net/http/response.go#L53
The response body is streamed on demand as the Body field is read. If
the network connection fails or the server terminates the response,
Body.Read calls return an error.
Or there is the possibility in ioutil.ReadAll() for it to return bytes.ErrTooLarge here:
https://golang.org/src/io/ioutil/ioutil.go#L20
If the buffer overflows, we will get bytes.ErrTooLarge. Return that as
an error. Any other panic remains.
I am trying to learn how to write tests for my code in order to write better code, but I just seem to have the hardest time figuring out how to actually test some code I have written. I have read so many tutorials, most of which seem to only cover functions that add two numbers or mock some database or server.
I have a simple function I wrote below that takes a text template and a CSV file as input and executes the template using the values of the CSV. I have "tested" the code by trial and error, passing files, and printing values, but I would like to learn how to write proper tests for it. I feel that learning to test my own code will help me understand and learn faster and better. Any help is appreciated.
// generateCmds generates configuration commands from a text template using
// the values from a CSV file. Multiple commands in the text template must
// be delimited by a semicolon. The first row of the CSV file is assumed to
// be the header row and the header values are used for key access in the
// text template.
func generateCmds(cmdTmpl string, filename string) ([]string, error) {
t, err := template.New("cmds").Parse(cmdTmpl)
if err != nil {
return nil, fmt.Errorf("parsing template: %v", err)
}
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("reading file: %v", err)
}
defer f.Close()
records, err := csv.NewReader(f).ReadAll()
if err != nil {
return nil, fmt.Errorf("reading records: %v", err)
}
if len(records) == 0 {
return nil, errors.New("no records to process")
}
var (
b bytes.Buffer
cmds []string
keys = records[0]
vals = make(map[string]string, len(keys))
)
for _, rec := range records[1:] {
for k, v := range rec {
vals[keys[k]] = v
}
if err := t.Execute(&b, vals); err != nil {
return nil, fmt.Errorf("executing template: %v", err)
}
for _, s := range strings.Split(b.String(), ";") {
if cmd := strings.TrimSpace(s); cmd != "" {
cmds = append(cmds, cmd)
}
}
b.Reset()
}
return cmds, nil
}
Edit: Thanks for all the suggestions so far! My question was flagged as being too broad, so I have some specific questions regarding my example.
Would a test table be useful in a function like this? And, if so, would the test struct need to include the returned cmds string slice and the value of err? For example:
type tmplTest struct {
name string // test name
tmpl string // the text template
filename string // CSV file with template values
expected []string // expected configuration commands
err error // expected error
}
How do you handle errors that are supposed to be returned for specific test cases? For example, os.Open() returns an error of type *PathError if an error is encountered. How do I initialize a *PathError that is equivalent to the one returned by os.Open()? Same idea for template.Parse(), template.Execute(), etc.
Edit 2: Below is a test function I came up with. My two question from the first edit still stand.
package cmd
import (
"testing"
"strings"
"path/filepath"
)
type tmplTest struct {
name string // test name
tmpl string // text template to execute
filename string // CSV containing template text values
cmds []string // expected configuration commands
}
var tests = []tmplTest{
{"empty_error", ``, "", nil},
{"file_error", ``, "fake_file.csv", nil},
{"file_empty_error", ``, "empty.csv", nil},
{"file_fmt_error", ``, "fmt_err.csv", nil},
{"template_fmt_error", `{{ }{{`, "test_values.csv", nil},
{"template_key_error", `{{.InvalidKey}}`, "test_values.csv", nil},
}
func TestGenerateCmds(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmds, err := generateCmds(tc.tmpl, filepath.Join("testdata", tc.filename))
if err != nil {
// Unexpected error. Fail the test.
if !strings.Contains(tc.name, "error") {
t.Fatal(err)
}
// TODO: Otherwise, check that the function failed at the expected point.
}
if tc.cmds == nil && cmds != nil {
t.Errorf("expected no commands; got %d", len(cmds))
}
if len(cmds) != len(tc.cmds) {
t.Errorf("expected %d commands; got %d", len(tc.cmds), len(cmds))
}
for i := range cmds {
if cmds[i] != tc.cmds[i] {
t.Errorf("expected %q; got %q", tc.cmds[i], cmds[i])
}
}
})
}
}
You basically need to have some sample files with the contents you want to test, then in your test code you can call the generateCmds function passing in the template string and the files to then verify that the results are what you expect.
It is not so much different as the examples you probably saw for simpler cases.
You can place the files under a testdata folder inside the same package (testdata is a special name that the Go tools will ignore during build).
Then you can do something like:
func TestCSVProcessing(t *testing.T) {
templateStr := `<your template here>`
testFile := "testdata/yourtestfile.csv"
result, err := generateCmds(templateStr, testFile)
if err != nil {
// fail the test here, unless you expected an error with this file
}
// compare the "result" contents with what you expected
// failing the test if it does not match
}
EDIT
About the specific questions you added later:
Would a test table be useful in a function like this? And, if so, would the test struct need to include the returned cmds string slice and the value of err?
Yes, it'd make sense to include both the expected strings to be returned as well as the expected error (if any).
How do you handle errors that are supposed to be returned for specific test cases? For example, os.Open() returns an error of type *PathError if an error is encountered. How do I initialize a *PathError that is equivalent to the one returned by os.Open()?
I don't think you'll be able to "initialize" an equivalent error for each case. Sometimes the libraries might use internal types for their errors making this impossible. Easiest would be to "initialize" a regular error with the same value returned in its Error() method, then just compare the returned error's Error() value with the expected one.
I have implemented a type wrapping glog so that I can add a prefix to log message identifying the emitter of the log in my program and I can change the log level per emitter.
How could I implement the unit tests ? The problem is that glog outputs text to stdErr.
The code is trivial but I would like the have the unit test and 100% coverage like the rest of the code. This programming effort already payed.
Test which captures stderr:
package main
import (
"bytes"
"io"
"os"
"testing"
"github.com/golang/glog"
"strings"
)
func captureStderr(f func()) (string, error) {
old := os.Stderr // keep backup of the real stderr
r, w, err := os.Pipe()
if err != nil {
return "", err
}
os.Stderr = w
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
// calling function which stderr we are going to capture:
f()
// back to normal state
w.Close()
os.Stderr = old // restoring the real stderr
return <-outC, nil
}
func TestGlogError(t *testing.T) {
stdErr, err := captureStderr(func() {
glog.Error("Test error")
})
if err != nil {
t.Errorf("should not be error, instead: %+v", err)
}
if !strings.HasSuffix(strings.TrimSpace(stdErr), "Test error") {
t.Errorf("stderr should end by 'Test error' but it doesn't: %s", stdErr)
}
}
running test:
go test -v
=== RUN TestGlogError
--- PASS: TestGlogError (0.00s)
PASS
ok command-line-arguments 0.007s
Write an interface that describes your usage. This won't be very pretty if you use the V method, but you have a wrapper so you've already done the hard work that fixing that would entail.
For each package you need to test, define
type Logger interface {
Infoln(...interface{}) // the methods you actually use in this package
}
And then you can easily swap it out by not referring to glog types directly in your code.
I want to test the type of the error returned against a table test of expected results, like so:
var tabletest = []struct{
instruction string
want string
err error
}{
{"synonym for hi", "hello", nil}, // input, retval, errtype
{"synonym for hig", "", TranslationError{}},
{"sssnymm for hi", "", InstructionError{}},
}
func TestThesaurus(t *Testing) {
for _, testcase := range tabletest {
got, err := Thesaurus(testcase.instruction)
// check error type
// check result type
}
}
In the example above, different error sub-classes are returned based on the type of error that occurred. You may imagine that the caller of the made-up Thesaurus function would handle each error type differently.
What is the idiomatic way to assert that the type of error returned, and the type of error expected, are the same?
Use a type switch.
func TestThesaurus(t *Testing) {
for _, testcase := range tabletest {
got, err := Thesaurus(testcase.instruction)
// Don't use && because we want to trap all cases where err is nil
if err == nil {
if testcase.err != nil {
// failure
}
continue
}
switch err.(type) {
case TranslationError:
if _,ok := (testcase.err).(TranslationError); !ok {
// failure
}
case InstructionError:
if _,ok := (testcase.err).(InstructionError); !ok {
// failure
}
default:
// Unrecognized error, failure
}
}
It's definitely not as succinct as the reflect way of doing it, but I think it's more Go-ish and explicit.
There's also this idiom:
In Thesaurus...
import "errors"
var (
TranslationError = errors.New("")
InstructionError = errors.New("")
)
In Testcase...
if err != testcase.err {
}
However, I think in this idiom the errors must be defined in advance (i.e. the message cannot be changed).
reflect.TypeOf does the job:
import "reflect"
...
func TestThesaurus(t *Testing) {
for _, testcase := range tabletest {
got, err := Thesaurus(testcase.instruction)
// check error type
if goterr, wanterr := reflect.TypeOf(err), reflect.TypeOf(testcase.err);
goterr != wanterr {
t.Errorf("For instruction %q, unexpected error: %q. Wanted %q",
testcase.instruction, goterr, wanterr)
}
// check result type
if want := testcase.want; got != want {
t.Errorf("For instruction %q, got %q, want %q.",
testcase.instruction, got, want)
}
}
}
Whether or not it's idiomatic is for the community to decide.