140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func clearConfigTable() {
|
|
a.DB.Exec("DELETE FROM configs")
|
|
a.DB.Exec("ALTER TABLE configs AUTO_INCREMENT = 1")
|
|
}
|
|
|
|
func TestEmptyConfigTable(t *testing.T) {
|
|
clearConfigTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/configs", nil)
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
if body := response.Body.String(); body != "[]" {
|
|
t.Errorf("Expected an empty array. Got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestGetNonExistentConfig(t *testing.T) {
|
|
clearConfigTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/config/11", nil)
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusNotFound, response.Code)
|
|
|
|
var m map[string]string
|
|
json.Unmarshal(response.Body.Bytes(), &m)
|
|
if m["error"] != "Config not found" {
|
|
t.Errorf("Expected the 'error' key of the response to be set to 'Config not found'. Got '%s'", m["error"])
|
|
}
|
|
}
|
|
|
|
func TestCreateConfig(t *testing.T) {
|
|
clearConfigTable()
|
|
|
|
payload := []byte(`{"parameter":"save_all","val":"true"}`)
|
|
|
|
req, _ := http.NewRequest("POST", "/api/v1/config", bytes.NewBuffer(payload))
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusCreated, response.Code)
|
|
|
|
var m map[string]interface{}
|
|
json.Unmarshal(response.Body.Bytes(), &m)
|
|
|
|
if m["parameter"] != "save_all" {
|
|
t.Errorf("Expected config parameter to be 'save_all'. Got '%v'", m["parameter"])
|
|
}
|
|
|
|
if m["val"] != "true" {
|
|
t.Errorf("Expected config val to be 'true'. Got '%v'", m["val"])
|
|
}
|
|
}
|
|
|
|
func TestGetConfig(t *testing.T) {
|
|
clearConfigTable()
|
|
addConfigs(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/config/1", nil)
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
}
|
|
|
|
func addConfigs(count int) {
|
|
if count < 1 {
|
|
count = 1
|
|
}
|
|
insStmt, insErr := a.DB.Prepare("INSERT INTO configs(parameter, val, createdAt, updatedAt) VALUES(?, ?, ?, ?)")
|
|
if insErr != nil {
|
|
panic(insErr.Error()) // proper error handling instead of panic in your app
|
|
}
|
|
defer insStmt.Close() // Close the statement when we leave main() / the program terminates
|
|
|
|
for i := 0; i < count; i++ {
|
|
insStmt.Exec("testparam"+strconv.Itoa(i), (i+1.0)*10, time.Now(), time.Now())
|
|
}
|
|
}
|
|
|
|
func TestUpdateConfig(t *testing.T) {
|
|
clearConfigTable()
|
|
addConfigs(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/config/1", nil)
|
|
response := executeRequest(req)
|
|
var originalConfig map[string]interface{}
|
|
json.Unmarshal(response.Body.Bytes(), &originalConfig)
|
|
|
|
payload := []byte(`{"parameter":"test param","val":"11.22"}`)
|
|
|
|
req, _ = http.NewRequest("PUT", "/api/v1/config/1", bytes.NewBuffer(payload))
|
|
response = executeRequest(req)
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
var m map[string]interface{}
|
|
json.Unmarshal(response.Body.Bytes(), &m)
|
|
|
|
if m["id"] != originalConfig["id"] {
|
|
t.Errorf("Expected the id to remain the same (%v). Got %v", originalConfig["id"], m["id"])
|
|
}
|
|
|
|
if m["parameter"] == originalConfig["parameter"] {
|
|
t.Errorf("Expected the parameter to change from '%v' to '%v'. Got '%v'", originalConfig["parameter"], m["parameter"], m["parameter"])
|
|
}
|
|
|
|
if m["val"] == originalConfig["val"] {
|
|
t.Errorf("Expected the val to change from '%v' to '%v'. Got '%v'", originalConfig["val"], m["val"], m["val"])
|
|
}
|
|
}
|
|
|
|
func TestDeleteConfig(t *testing.T) {
|
|
clearConfigTable()
|
|
addConfigs(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/config/1", nil)
|
|
response := executeRequest(req)
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("DELETE", "/api/v1/config/1", nil)
|
|
response = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("GET", "/api/v1/config/1", nil)
|
|
response = executeRequest(req)
|
|
checkResponseCode(t, http.StatusNotFound, response.Code)
|
|
}
|