示例#1
0
文件: helper.go 项目: gebv/magickeys
func UpdateModel(model models.Model, db *pgx.ConnPool, tx *pgx.Tx, where string, fields ...string) (err error) {
	if len(model.PrimaryValue()) == 0 {
		glog.Errorf("Update '%T'. Primary key = nil", model)
		return models.ErrNotValid
	}

	_fields := models.StringArray{}
	_fields.AddAsArray(fields)
	_fields.Del(model.PrimaryName())
	_fields.Del("removed_at")
	_fields.Del("created_at")
	_fields.Add("updated_at")

	return updateOrCreateModel(model, db, tx, false, false, where, _fields...)
}
示例#2
0
func TestModeEventsCreated(t *testing.T) {
	key1 := uuid.NewV1().String()
	key2 := uuid.NewV1().String()

	var origValueIndexes = make(map[uuid.UUID]int)

	for i, _ := range strings.Repeat("+", 10) {
		dto := models.NewValueDTO()
		dto.Keys.Add(key2)
		dto.Keys.Add(key1)
		dto.Value["string"] = strings.Join([]string{key1, key2, strconv.Itoa(i)}, ":")
		dto.Value["iter"] = i
		dto.Value["iter_inc"] = i + 1
		dto.Value[strconv.Itoa(i)+":special_value"] = i + 2
		dto.Value["enabled"] = true
		dto.Value["array"] = []string{"tag:" + strconv.Itoa(i), "tag1", "tag2"}

		value, err := _s.Get("value").(*ValueStore).Create(dto)

		if err != nil {
			t.Error(err)
			return
		}

		origValueIndexes[value.PrimaryValue()] = i
	}

	dto := models.NewValueDTO()
	dto.Keys.Add(key2)
	dto.Keys.Add(key1)
	values, err := _s.Get("value").(*ValueStore).FindByKeys(dto, "=")

	if err != nil {
		t.Error(err)
		return
	}

	for _, value := range values {
		iterValue, exist := origValueIndexes[value.PrimaryValue()]

		if !exist {
			t.Fatal("not exist item")
			return
		}

		if value.Value["string"].(string) != strings.Join([]string{key1, key2, strconv.Itoa(iterValue)}, ":") {
			t.Errorf("values Value is not correct for %v", value.PrimaryValue().String())
			return
		}

		if int(value.Value["iter"].(float64)) != iterValue {
			t.Errorf("values Value[\"iter\"] is not correct for %v", value.PrimaryValue().String())
			return
		}

		if int(value.Value["iter_inc"].(float64)) != iterValue+1 {
			t.Errorf("values Value[\"iter_inc\"] is not correct for %v", value.PrimaryValue().String())
			return
		}

		if int(value.Value[strconv.Itoa(iterValue)+":special_value"].(float64)) != iterValue+2 {
			t.Errorf("values Value[\"#:special_value\"] is not correct for %v", value.PrimaryValue().String())
			return
		}

		_array := models.StringArray{}
		_array.FromArray(value.Value["array"].([]interface{}))

		if !_array.IsExist("tag:" + strconv.Itoa(iterValue)) {
			t.Errorf("values Flags is not correct for %v", value.PrimaryValue().String())
			return
		}

		if value.Value["enabled"].(bool) != true {
			t.Errorf("values Value[\"enabled\"] is not correct for %v", value.PrimaryValue().String())
			return
		}
	}
}