Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(635)

Unified Diff: server/config/format_test.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Update MultiResolver interface, add test for MultiError. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: server/config/format_test.go
diff --git a/server/config/format_test.go b/server/config/format_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..7b95316c48578a12195bc5da3563ef59da61578d
--- /dev/null
+++ b/server/config/format_test.go
@@ -0,0 +1,112 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package config
+
+import (
+ "testing"
+
+ "golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+// retainingBackend is a simple Backend implementation that retains the last set
+// of data that it received.
+type retainingBackend struct {
+ Backend
+
+ lastItems []*Item
+ lastErr error
+}
+
+func (b *retainingBackend) Get(c context.Context, configSet, path string, p Params) (*Item, error) {
+ var item *Item
+ item, b.lastErr = b.Backend.Get(c, configSet, path, p)
+ b.lastItems = []*Item{item}
+ return item, b.lastErr
+}
+
+// GetAll implements Backend.
+func (b *retainingBackend) GetAll(c context.Context, t GetAllType, path string, p Params) ([]*Item, error) {
+ b.lastItems, b.lastErr = b.Backend.GetAll(c, t, path, p)
+ return b.lastItems, b.lastErr
+}
+
+type customFormatter string
+
+func (cf customFormatter) FormatItem(c string, fd string) (string, error) {
+ return string(cf), nil
+}
+
+type retainingResolver struct {
+ format string
+ item *Item
+}
+
+func (rr *retainingResolver) Format() (string, string) { return rr.format, "" }
+func (rr *retainingResolver) Resolve(it *Item) error {
+ rr.item = it
+ return nil
+}
+
+type panicFormatter struct{}
+
+func (pf panicFormatter) FormatItem(string, string) (string, error) { panic("panic") }
+
+func TestFormatBackend(t *testing.T) {
+ t.Parallel()
+
+ Convey(`A testing environment`, t, func() {
+ tb := testingBackend{
+ items: []*Item{
+ {Meta: Meta{"projects/foo", "path", "####", "v1"}, Content: "foo"},
+ {Meta: Meta{"projects/bar", "path", "####", "v1"}, Content: "bar"},
+ },
+ }
+
+ // Pass all things from the backend through the formatter.
+ var fr FormatterRegistry
+ fb := FormatBackend{
+ Backend: &tb,
+ GetRegistry: func(context.Context) *FormatterRegistry { return &fr },
+ }
+
+ // Retain all raw Items that were returned for examination.
+ rb := retainingBackend{
+ Backend: &fb,
+ }
+
+ c := context.Background()
+ c = WithBackend(c, &rb)
+
+ Convey(`Will panic if an attempt to register empty key is made.`, func() {
+ So(func() { fr.Register("", nil) }, ShouldPanic)
+ })
+
+ Convey(`Will ignore items that have already been formatted.`, func() {
+ rr := retainingResolver{"test", nil}
+ fr.Register("test", panicFormatter{})
+
+ // Confirm that this setup correctly attempts to format the item.
+ So(func() { Get(c, AsService, "", "", &rr, nil) }, ShouldPanic)
+
+ // Now, pretend the item is already formatter. We should not get a panic.
+ tb.items[0].Format = "something"
+ So(Get(c, AsService, "", "", &rr, nil), ShouldBeNil)
+ })
+
+ Convey(`Testing custom formatter`, func() {
+ cf := customFormatter("content")
+ fr.Register("test", cf)
+
+ // Confirm that this setup correctly attempts to format the item.
+ rr := retainingResolver{"test", nil}
+ So(Get(c, AsService, "", "", &rr, nil), ShouldBeNil)
+ So(rr.item, ShouldNotBeNil)
+ So(rr.item.Format, ShouldEqual, "test")
+ So(rr.item.Content, ShouldEqual, "content")
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698