Files
fn-serverless/api/models/trigger_test.go
Tom Coupland d7139358ce List Cursor management moved into datastore layer. (#1102)
* Don't try to delete an app that wasn't successfully created in the case of failure

* Allow datastore implementations to inject additional annotations on objects

* Allow for datastores transparently adding annotations on apps, fns and triggers. Change NameIn filter to Name for apps.

* Move *List types including JSON annotations for App, Fn and Trigger into models

* Change return types for GetApps, GetFns and GetTriggers on datastore to
be models.*List and ove cursor generation into datastore

* Trigger cursor handling fixed into db layer

Also changes the name generation so that it is not in the same order
as the id (well is random), this means we are now testing our name ordering.

* GetFns now respects cursors

* Apps now feeds cursor back

* Mock fixes

* Fixing up api level cursor decoding

* Tidy up treatment of cursors in the db layer

* Adding conditions for non nil items lists

* fix mock test
2018-06-29 19:14:13 +01:00

65 lines
1.7 KiB
Go

package models
import (
"encoding/json"
"testing"
)
var openEmptyJson = `{"id":"","name":"","app_id":"","fn_id":"","created_at":"0001-01-01T00:00:00.000Z","updated_at":"0001-01-01T00:00:00.000Z","type":"","source":""`
var triggerJsonCases = []struct {
val *Trigger
valString string
}{
{val: &Trigger{}, valString: openEmptyJson + "}"},
}
func TestTriggerJsonMarshalling(t *testing.T) {
for _, tc := range triggerJsonCases {
v, err := json.Marshal(tc.val)
if err != nil {
t.Fatalf("Failed to marshal json into %s: %v", tc.valString, err)
}
if string(v) != tc.valString {
t.Errorf("Invalid trigger value, expected %s, got %s", tc.valString, string(v))
}
}
}
func TestTriggerListJsonMarshalling(t *testing.T) {
emptyList := &TriggerList{Items: []*Trigger{}}
expected := "{\"items\":[]}"
v, err := json.Marshal(emptyList)
if err != nil {
t.Fatalf("Failed to marshal json into %s: %v", expected, err)
}
if string(v) != expected {
t.Errorf("Invalid trigger value, expected %s, got %s", expected, string(v))
}
}
var httpTrigger = &Trigger{Name: "name", AppID: "foo", FnID: "bar", Type: "http", Source: "baz"}
var invalidTrigger = &Trigger{Name: "name", AppID: "foo", FnID: "bar", Type: "error", Source: "baz"}
var triggerValidateCases = []struct {
val *Trigger
valid bool
}{
{val: &Trigger{}, valid: false},
{val: invalidTrigger, valid: false},
{val: httpTrigger, valid: true},
}
func TestTriggerValidate(t *testing.T) {
for _, tc := range triggerValidateCases {
v := tc.val.Validate()
if v != nil && tc.valid {
t.Errorf("Expected Trigger to be valid, but err (%s) returned. Trigger: %#v", v, tc.val)
}
if v == nil && !tc.valid {
t.Errorf("Expected Trigger to be invalid, but no err returned. Trigger: %#v", tc.val)
}
}
}