Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
func (cc *Controller) ParseContext(ctx climax.Context) (*models.Feature, error) {
	name, _ := ctx.Get("name")
	val, _ := ctx.Get("value")
	cmt, _ := ctx.Get("comment")
	scp, _ := ctx.Get("scope")

	if name == "" {
		return nil, errNameRequired
	}

	var v interface{}
	var ft models.FeatureType

	if val != "" {
		v, ft = models.ParseValueAndFeatureType(val)

		if ft == models.Invalid {
			return nil, errInvalidFeatureType
		}

		if ft == models.Percentile {
			if v.(float64) > 1.0 || v.(float64) < 0 {
				return nil, errInvalidRange
			}
		}
	}

	f := models.NewFeature(name, v, cmt, cc.Config.Username, scp, cc.Config.Namespace)
	f.FeatureType = ft

	return f, nil
}
Exemplo n.º 4
0
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)
}
Exemplo n.º 5
0
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)
}
Exemplo n.º 6
0
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)
}
Exemplo n.º 7
0
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)
}
Exemplo n.º 8
0
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)
}
Exemplo n.º 9
0
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)
}
Exemplo n.º 10
0
func (cc *Controller) Import(ctx climax.Context) int {
	bts, err := ioutil.ReadAll(os.Stdin)

	if err != nil {
		printer.SayErr("%v", err)
		return 1
	}

	var kvs map[string]interface{}
	err = json.Unmarshal(bts, &kvs)

	if err != nil {
		printer.SayErr("%v", err)
		return 1
	}

	scope, _ := ctx.Get("scope")

	if scope == "" {
		scope = models.DefaultScope
	}

	for k, v := range kvs {
		f := models.NewFeature(k, v, "", "", scope, cc.Config.Namespace)
		err = cc.Client.Set(f)

		if err != nil {
			printer.SayErr("%v", err)
			return 1
		}

		printer.Say("set %s to %+v", k, v)
	}

	return 1
}