Skip to content

Commit 03dfebd

Browse files
committed
Added unit tests.
1 parent 8e2ab24 commit 03dfebd

File tree

12 files changed

+1732
-83
lines changed

12 files changed

+1732
-83
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore files generated by the build process
2+
test_coverage.html
3+
coverage.out

firestruct_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package firestruct
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/bennovw/firestruct/internal/testutil"
8+
)
9+
10+
func TestFirestoreCloudEvent(t *testing.T) {
11+
var testEvent = testutil.TestFirebaseCloudEvents[0]
12+
var testCloudEventBytes, _ = json.Marshal(testEvent)
13+
var testCloudEvent = []byte(testCloudEventBytes)
14+
var FirestoreCloudEventTests = []testutil.TableTest{
15+
{
16+
Name: "data to simple struct",
17+
TargetType: "DataToSimple",
18+
Input: testCloudEvent,
19+
Expected: testutil.StructResults[0],
20+
},
21+
{
22+
Name: "data to tagged struct",
23+
TargetType: "DataToTagged",
24+
Input: testCloudEvent,
25+
Expected: testutil.StructResults[1],
26+
},
27+
{
28+
Name: "data to flattened map",
29+
TargetType: "ToMap",
30+
Input: testCloudEvent,
31+
Expected: testutil.FlattenedMapResults[12],
32+
},
33+
}
34+
35+
for _, test := range FirestoreCloudEventTests {
36+
thisMethodName := "FirestoreCloudEvent"
37+
38+
receivedCloudEvent := FirestoreCloudEvent{}
39+
err := json.Unmarshal(testCloudEvent, &receivedCloudEvent)
40+
if err != nil {
41+
t.Errorf("%v() test \"%v\" returned error: %v", thisMethodName, test.Name, err)
42+
}
43+
44+
switch test.TargetType {
45+
case "DataToSimple":
46+
var result testutil.TestSimpleStruct
47+
err = receivedCloudEvent.DataTo(&result)
48+
if err != nil {
49+
t.Errorf("%v() test \"%v\" returned error: %v", thisMethodName, test.Name, err)
50+
}
51+
testutil.IsDeepEqual(t, thisMethodName, test.Name, result, test.Expected)
52+
case "DataToTagged":
53+
var result testutil.TestTaggedStruct
54+
err = receivedCloudEvent.DataTo(&result)
55+
if err != nil {
56+
t.Errorf("%v() test \"%v\" returned error: %v", thisMethodName, test.Name, err)
57+
}
58+
testutil.IsDeepEqual(t, thisMethodName, test.Name, result, test.Expected)
59+
case "ToMap":
60+
result, err := receivedCloudEvent.ToMap()
61+
if err != nil {
62+
t.Errorf("%v() test \"%v\" returned error: %v", thisMethodName, test.Name, err)
63+
}
64+
testutil.IsDeepEqual(t, thisMethodName, test.Name, result, test.Expected)
65+
default:
66+
t.Errorf("%v() test \"%v\" method not covered", thisMethodName, test.Name)
67+
}
68+
}
69+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
require (
1717
github.com/cloudevents/sdk-go/v2 v2.15.2
1818
github.com/davecgh/go-spew v1.1.1
19+
github.com/fatih/structs v1.1.0
1920
github.com/golang/protobuf v1.5.4
2021
github.com/google/go-cmp v0.6.0
2122
google.golang.org/protobuf v1.34.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/cloudevents/sdk-go/v2 v2.15.2/go.mod h1:lL7kSWAE/V8VI4Wh0jbL2v/jvqsm6
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
7+
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
68
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
79
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
810
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

internal/testutil/cmp.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ package testutil
1717
import (
1818
"math"
1919
"math/big"
20+
"reflect"
21+
"testing"
2022

23+
"github.com/fatih/structs"
2124
"github.com/golang/protobuf/proto"
2225
"github.com/google/go-cmp/cmp"
2326
)
@@ -59,3 +62,34 @@ func Diff(x, y interface{}, opts ...cmp.Option) string {
5962
opts = append(opts[:len(opts):len(opts)], defaultCmpOptions...)
6063
return cmp.Diff(x, y, opts...)
6164
}
65+
66+
// IsDeepEqual performs both IsMapVersionDeepEqual and IsStringVersionDeepEqual checks
67+
func IsDeepEqual(t *testing.T, funcName string, testSectionName string, result interface{}, expected interface{}) {
68+
IsMapVersionDeepEqual(t, funcName, testSectionName, result, expected)
69+
IsStringVersionDeepEqual(t, funcName, testSectionName, result, expected)
70+
}
71+
72+
// IsMapVersionDeepEqual checks if the result is equal to the expected result
73+
func IsMapVersionDeepEqual(t *testing.T, funcName string, testSectionName string, result interface{}, expected interface{}) {
74+
// Convert to maps if not already maps
75+
expectedMap := asMap(expected)
76+
resultMap := asMap(result)
77+
78+
if !reflect.DeepEqual(resultMap, expectedMap) {
79+
t.Errorf("%v() test \"%v\" Result: %v Want: %v", funcName, testSectionName, result, expected)
80+
return
81+
}
82+
}
83+
84+
// asMap converts a value to a map representation.
85+
func asMap(v interface{}) interface{} {
86+
if v == nil {
87+
return nil
88+
}
89+
90+
t := reflect.TypeOf(v)
91+
if t != nil && t.Kind() == reflect.Map {
92+
return v
93+
}
94+
return structs.Map(v)
95+
}

0 commit comments

Comments
 (0)