Exemple #1
0
func TestGet(t *testing.T) {
	setup()
	defer tearDown()

	user := &testuser{}
	user.Name = "Thomas"
	user.Age = 28
	user.Rev = 1
	db.Create(user)

	req, _ := http.NewRequest("GET", idURL(user.ID), nil)

	// Get response
	client := &http.Client{}
	res, err := client.Do(req)

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

	if res.StatusCode != 200 {
		t.Error("Request Failed")
		return
	}

	defer res.Body.Close()
	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Error(err)
		return
	}

	var result testuser
	err = json.Unmarshal(content, &result)

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

	if result.ID != user.ID || result.Name != user.Name || result.Age != user.Age {
		t.Error("Response is invalid")
	}

	// Make sure data exists in Redis
	if goal.SharedCache != nil {
		key := goal.CacheKey(user)

		// Test data exists in Redis
		if exist, _ := goal.SharedCache.Exists(key); !exist {
			t.Error("Data should be saved into Redis")
		}

		var redisUser testuser
		goal.SharedCache.Get(key, &redisUser)
		if !reflect.DeepEqual(user, &redisUser) {
			t.Error("Incorrect data in redis, ", user, &redisUser)
		}
	}
}
Exemple #2
0
func TestPutConflict(t *testing.T) {
	setup()
	defer tearDown()

	user := &testuser{}
	user.Name = "Thomas"
	user.Age = 28
	user.Rev = 2
	db.Create(user)

	var json = []byte(`{"Name":"Thomas Dao", "ID":1, "Rev":1}`)
	req, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json))
	req.Close = true

	// Get response
	client := &http.Client{}
	res, err := client.Do(req)

	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 409 {
		t.Errorf("This should be conflict %d", res.StatusCode)
		return
	}

	var result testuser
	if db.Where("name = ?", "Thomas").First(&result).RecordNotFound() {
		t.Error("Update unsuccessful")
	}

	if result.ID != user.ID || result.Age != user.Age {
		t.Error("Incorrect update")
	}

	// Make sure data exists in Redis
	if goal.SharedCache != nil {
		key := goal.CacheKey(user)
		var redisUser testuser
		goal.SharedCache.Get(key, &redisUser)
		if !reflect.DeepEqual(result, redisUser) {
			t.Error("Incorrect data in redis, ", result, redisUser)
		}
	}

	json1 := []byte(`{"Name":"Thomas Dao", "ID":1}`)
	req1, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json1))
	res1, err1 := client.Do(req1)

	if err1 != nil {
		t.Error(err1)
	}

	if res1.StatusCode != 400 {
		t.Errorf("Revision should be required %d", res1.StatusCode)
		return
	}
}
Exemple #3
0
func TestPut(t *testing.T) {
	setup()
	defer tearDown()

	user := &testuser{}
	user.Name = "Thomas"
	user.Age = 28
	user.Rev = 1
	db.Create(user)

	var json = []byte(`{"Name":"Thomas Dao", "ID":1, "Rev":1}`)
	req, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json))
	req.Close = true

	// Get response
	client := &http.Client{}
	res, err := client.Do(req)

	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		body, _ := ioutil.ReadAll(res.Body)
		t.Errorf("Request Failed %d %s", res.StatusCode, string(body))
		return
	}

	var result testuser
	if db.Where("name = ?", "Thomas Dao").First(&result).RecordNotFound() {
		t.Error("Update unsuccessful")
	}

	if result.ID != user.ID || result.Age != user.Age || result.Rev != user.Rev+1 {
		t.Errorf("Incorrect update %+v", result)
	}

	// Make sure data exists in Redis
	if goal.SharedCache != nil {
		key := goal.CacheKey(user)
		var redisUser testuser
		goal.SharedCache.Get(key, &redisUser)
		if !reflect.DeepEqual(result, redisUser) {
			t.Error("Incorrect data in redis, ", result, redisUser)
		}
	}
}
Exemple #4
0
func TestCreate(t *testing.T) {
	setup()
	defer tearDown()

	var json = []byte(`{"Name":"Thomas", "Age": 28, "Rev": 1}`)
	req, _ := http.NewRequest("POST", userURL(), bytes.NewBuffer(json))

	// Get response
	client := &http.Client{}
	res, err := client.Do(req)

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

	if res.StatusCode != 200 {
		t.Error("Request Failed ", res.StatusCode)
		return
	}

	// Make sure db has one object
	var user testuser
	db.Where("name = ?", "Thomas").First(&user)
	if &user == nil {
		t.Error("Fail to save object to database")
		return
	}

	if user.Name != "Thomas" || user.Age != 28 {
		t.Error("Save wrong data or missing data")
	}

	// Make sure data exists in Redis
	if goal.SharedCache != nil {
		key := goal.CacheKey(user)
		var redisUser testuser
		goal.SharedCache.Get(key, &redisUser)
		if !reflect.DeepEqual(user, redisUser) {
			t.Error("Incorrect data in redis, ", user, redisUser)
		}
	}
}
Exemple #5
0
func TestDelete(t *testing.T) {
	setup()
	defer tearDown()

	user := &testuser{}
	user.Name = "Thomas"
	user.Age = 28
	db.Create(user)

	req, _ := http.NewRequest("DELETE", idURL(user.ID), nil)

	// Get response
	client := &http.Client{}
	res, err := client.Do(req)

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

	if res.StatusCode != 200 {
		t.Error("Request Failed")
		return
	}

	var result testuser
	if !db.Where("name = ?", "Thomas").First(&result).RecordNotFound() {
		t.Error("Delete is not successful. Expected result delete from db")
	}

	// Make sure no more data in redis
	if goal.SharedCache != nil {
		key := goal.CacheKey(user)
		if exist, _ := goal.SharedCache.Exists(key); exist {
			t.Error("Data should be deleted from Redis when object is deleted")
		}
	}

}