Exemplo n.º 1
0
func TestUpdateNotFound(t *testing.T) {
	crud := getSetup()

	dataForUpdate := map[string]interface{}{
		"name":   "Wichit",
		"height": 900,
		"weight": 44.44,
	}
	err := crud.Update(14, dataForUpdate)
	assert.Error(t, err)
	assert.True(t, errs.IsNotFound(err))
}
Exemplo n.º 2
0
func TestDelete(t *testing.T) {
	crud := getSetup()
	now := time.Now()
	v := testData{ID: 12, Name: "Xier", Time: now, Height: 1500, Weight: 25.55}

	err := crud.Create(&v)
	assert.NoError(t, err)

	err = crud.Delete(v.ID)
	assert.NoError(t, err)

	var tempData testData

	err = crud.db.Get(&tempData, "SELECT * FROM testTable WHERE id=12")
	err = errs.Sql(err)
	assert.Error(t, err)
	assert.True(t, errs.IsNotFound(err))
}
Exemplo n.º 3
0
func TestCRUD(t *testing.T) {
	assert := assert.New(t)

	session, dberr := mgo.Dial("192.168.1.178:27017")
	assert.NoError(dberr)
	defer session.Close()

	db := "test"
	c := "users"
	crud := NewCRUD(session, db, c)

	users := session.DB(db).C(c)

	users.RemoveAll(nil)

	data1 := &testData{Name: "Tom", Age: 44}
	err := crud.Insert(data1)
	assert.NoError(err)

	var data2 *testData
	//get none exist data
	err = users.FindId("2").One(&data2)
	assert.True(errors2.IsNotFound(errors2.Mgo(err)))
	assert.Nil(data2)

	//get exist data
	err = users.FindId(data1.Id).One(&data2)
	assert.NoError(err)
	assert.Equal(data2, data1)

	//check none exist data
	exist, err := crud.Exist("2")
	assert.NoError(err)
	assert.False(exist)

	//check exist with data
	exist, err = crud.Exist(data1.Id)
	assert.NoError(err)
	assert.True(exist)

	//update exist data
	args := make(map[string]interface{})
	args["age"] = 35
	err = crud.Update(data1.Id, args)
	assert.NoError(err)
	var data3 *testData
	err = users.FindId(data1.Id).One(&data3)
	assert.NoError(err)
	assert.NotEqual(data3, data2)

	err = crud.UpdateAll(data1.Id, data2)
	assert.NoError(err)
	err = users.FindId(data1.Id).One(&data3)
	assert.NoError(err)
	assert.Equal(data3, data2)

	//update none exist data
	err = crud.UpdateAll("2", data2)
	assert.True(errors2.IsNotFound(err))

	//delete exist data
	err = crud.Delete(data1.Id)
	assert.NoError(err)
	var data4 *testData
	err = users.FindId(data1.Id).One(&data4)
	assert.True(errors2.IsNotFound(errors2.Mgo(err)))
	assert.Nil(data4)

	//delete none exist data
	err = crud.Delete("2")
	assert.True(errors2.IsNotFound(err))
}
Exemplo n.º 4
0
func TestDeleteNotFound(t *testing.T) {
	crud := getSetup()
	err := crud.Delete("16")
	assert.Error(t, err)
	assert.True(t, errs.IsNotFound(err))
}
Exemplo n.º 5
0
func TestExistNotFound(t *testing.T) {
	crud := getSetup()
	_, err := crud.Exist("19")
	assert.Error(t, err)
	assert.True(t, errs.IsNotFound(err))
}