func TestSet(t *testing.T) { ft := models.NewFeature("test", 0.5, "c", "u", "s", "n") cs := stores.NewMockStore(ft, nil) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) err := c.Set(ft) assert.Nil(t, err) }
func TestSetErrorOnNilValue(t *testing.T) { ft := models.NewFeature("test", nil, "c", "u", "s", "n") cs := stores.NewMockStore(nil, nil) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) err := c.Set(ft) assert.Equal(t, ErrNilValue, err) }
func TestSetWithError(t *testing.T) { ft := models.NewFeature("test", 0.5, "c", "u", "s", "n") e := errors.New("") cs := stores.NewMockStore(ft, e) c := New(cs, nil, config.DefaultConfig(), nil) err := c.Set(ft) assert.Equal(t, e, err) }
func TestTypeChangeErrorSet(t *testing.T) { orig := models.NewFeature("test", 0.5, "c", "u", "s", "n") bad := models.NewFeature("test", false, "c", "u", "s", "n") cs := stores.NewMockStore(orig, nil) c := New(cs, nil, config.DefaultConfig(), nil) err := c.Set(bad) assert.Equal(t, ErrTypeChange, err) }
func TestList(t *testing.T) { ft := models.NewFeature("test", 0.5, "c", "u", "s", "n") cs := stores.NewMockStore(ft, nil) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) fts, err := c.List("test", "") assert.Nil(t, err) assert.Equal(t, models.Features{*ft}, fts) }
func TestNilGet(t *testing.T) { cs := stores.NewMockStore(nil, nil) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) var f *models.Feature err := c.Get("test", &f) assert.EqualError(t, err, "dcdr/test not found") assert.Nil(t, f) }
func TestClientSetExisting(t *testing.T) { update := models.NewFeature("test", nil, "c", "u", "s", "n") orig := models.NewFeature("test", 0.5, "c", "u", "s", "n") c := New(stores.NewMockStore(orig, nil), &stores.MockRepo{}, config.DefaultConfig(), nil) err := c.Set(update) assert.NoError(t, err) }
func TestDeleteWithError(t *testing.T) { ft := models.NewFeature("test", 0.5, "c", "u", "s", "n") e := errors.New("") cs := stores.NewMockStore(ft, e) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) err := c.Delete(ft.Key, "") assert.Equal(t, e, err) }
func TestGet(t *testing.T) { ft := models.NewFeature("test", 0.5, "c", "u", "s", "n") cs := stores.NewMockStore(ft, nil) c := New(cs, &stores.MockRepo{}, config.DefaultConfig(), nil) var f *models.Feature err := c.Get("test", &f) assert.Nil(t, err) assert.Equal(t, f, ft) }
// NewDefault creates a new `Server` using `config.hcl`. func NewDefault() (srv *Server, err error) { cfg := config.DefaultConfig() cl, err := client.New(cfg) if err != nil { return nil, err } srv = New(cfg, cl) return }
func TestListEmptyFeatures(t *testing.T) { cfg := config.DefaultConfig() c := NewMockClient(nil, nil, nil) ctl := New(cfg, c) ctx := climax.Context{ Variable: map[string]string{}, } code := ctl.List(ctx) assert.Equal(t, Error, code) }
func TestListFeatures(t *testing.T) { cfg := config.DefaultConfig() fts := models.Features{ models.Feature{ Key: "test", Value: true, }, } c := NewMockClient(nil, fts, nil) ctl := New(cfg, c) ctx := climax.Context{ Variable: map[string]string{}, } code := ctl.List(ctx) assert.Equal(t, Success, code) }
func TestWatch(t *testing.T) { p := "/tmp/decider.json" fm, err := models.NewFeatureMap(JSONBytes) assert.NoError(t, err) err = ioutil.WriteFile(p, JSONBytes, 0644) assert.NoError(t, err) cfg := config.DefaultConfig() cfg.Watcher.OutputPath = p c, err := New(cfg) if err != nil { assert.NoError(t, err) } c.Watch() assert.Equal(t, fm, c.FeatureMap()) err = os.Remove(p) assert.NoError(t, err) }
func TestUpdateFeatures(t *testing.T) { json := []byte(`{ "dcdr": { "info": { "current_sha": "abcde" }, "features": { "ab": { "float": 0.2 }, "cc": { "cn": { "float": 0.1 } }, "default": { "float": 0, "default_bool": true } } } }`) update := []byte(`{ "dcdr": { "info": { "current_sha": "updated" }, "features": { "ab": { "float": 0.3, "new_ab_feature": true }, "cc": { "cn": { "float": 0.1 } }, "default": { "float": 0, "default_bool": true } } } }`) cfg := config.DefaultConfig() cfg.Git.RepoPath = "/tmp" c, _ := New(cfg) c.UpdateFeatures(json) scoped := c.WithScopes("ab") assert.False(t, scoped.FeatureExists("new_ab_feature")) assert.Equal(t, float64(0.2), scoped.Features()["float"]) assert.Equal(t, true, scoped.Features()["default_bool"]) scoped.UpdateFeatures(update) assert.Equal(t, float64(0.3), scoped.Features()["float"]) assert.True(t, scoped.FeatureExists("new_ab_feature")) assert.Equal(t, true, scoped.Features()["default_bool"]) }