Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunderl committed Apr 26, 2020
1 parent 3a757d5 commit 2f0b3b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sample-apps/blank-go/0-run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -eo pipefail
REGION=$(aws configure get region)
cd function
AWS_REGION=$REGION go test
46 changes: 46 additions & 0 deletions sample-apps/blank-go/function/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"os"
"encoding/json"
"time"
"context"
"testing"
"strings"
"io/ioutil"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-lambda-go/events"
)

func TestMain(t *testing.T) {
d := time.Now().Add(50 * time.Millisecond)
os.Setenv("AWS_LAMBDA_FUNCTION_NAME","blank-go")
ctx, _ := context.WithDeadline(context.Background(), d)
ctx = lambdacontext.NewContext(ctx, &lambdacontext.LambdaContext{
AwsRequestID: "495b12a8-xmpl-4eca-8168-160484189f99",
InvokedFunctionArn: "arn:aws:lambda:us-east-2:123456789012:function:blank-go",
})
inputJson := ReadJSONFromFile(t, "../event.json")
var event events.SQSEvent
err := json.Unmarshal(inputJson, &event)
if err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
//var inputEvent SQSEvent
result, err := handleRequest(ctx, event)
if err != nil {
t.Log(err)
}
t.Log(result)
if !strings.Contains(result, "FunctionCount") {
t.Errorf("Output does not contain FunctionCode.")
}
}
func ReadJSONFromFile(t *testing.T, inputFile string) []byte {
inputJSON, err := ioutil.ReadFile(inputFile)
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

return inputJSON
}

0 comments on commit 2f0b3b8

Please sign in to comment.