144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func clearTagValueTable() {
|
|
a.DB.Exec("DELETE FROM tagValues")
|
|
a.DB.Exec("ALTER TABLE tagValues AUTO_INCREMENT = 1")
|
|
}
|
|
|
|
func TestEmptyTagValueTable(t *testing.T) {
|
|
clearTagValueTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/tagValues", 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 TestGetNonExistentTagValue(t *testing.T) {
|
|
clearTagValueTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/tagValue/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"] != "TagValue not found" {
|
|
t.Errorf("Expected the 'error' key of the response to be set to 'TagValue not found'. Got '%s'", m["error"])
|
|
}
|
|
}
|
|
|
|
func TestCreateTagValue(t *testing.T) {
|
|
clearTagValueTable()
|
|
seedDataTypeData(a.DB)
|
|
seedDeviceTypeData(a.DB)
|
|
seedTagClassData(a.DB)
|
|
seedDeviceData(a.DB)
|
|
addTags(1)
|
|
|
|
payload := []byte(`{"tagId":2,"val":"1234.56"}`)
|
|
|
|
req, _ := http.NewRequest("POST", "/api/v1/tagValue", 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["tagId"] != 2.0 {
|
|
t.Errorf("Expected tagValue tagId to be '2'. Got '%v'", m["tagId"])
|
|
}
|
|
if m["val"] != "1234.56" {
|
|
t.Errorf("Expected tagValue val to be '1234.56'. Got '%v'", m["val"])
|
|
}
|
|
|
|
}
|
|
|
|
func TestGetTagValue(t *testing.T) {
|
|
clearTagValueTable()
|
|
addTagValues(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/tagValue/1", nil)
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
}
|
|
|
|
func addTagValues(count int) {
|
|
if count < 1 {
|
|
count = 1
|
|
}
|
|
insStmt, insErr := a.DB.Prepare("INSERT INTO tagValues(tagId, 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(2, strconv.Itoa(i*10), time.Now(), time.Now())
|
|
}
|
|
}
|
|
|
|
func TestUpdateTagValue(t *testing.T) {
|
|
clearTagValueTable()
|
|
addTagValues(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/tagValue/1", nil)
|
|
response := executeRequest(req)
|
|
var originalTagValue map[string]interface{}
|
|
json.Unmarshal(response.Body.Bytes(), &originalTagValue)
|
|
|
|
payload := []byte(`{"tagId":2,"val":"111.111"}`)
|
|
|
|
req, _ = http.NewRequest("PUT", "/api/v1/tagValue/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"] != originalTagValue["id"] {
|
|
t.Errorf("Expected the id to remain the same (%v). Got %v", originalTagValue["id"], m["id"])
|
|
}
|
|
|
|
if m["tagId"] != originalTagValue["tagId"] {
|
|
t.Errorf("Expected the id to remain the same (%v). Got %v", originalTagValue["tagId"], m["tagId"])
|
|
}
|
|
|
|
if m["val"] == originalTagValue["val"] {
|
|
t.Errorf("Expected the val to change from '%v' to '%v'. Got '%v'", originalTagValue["val"], m["val"], m["val"])
|
|
}
|
|
}
|
|
|
|
func TestDeleteTagValue(t *testing.T) {
|
|
clearTagValueTable()
|
|
addTagValues(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/tagValue/1", nil)
|
|
response := executeRequest(req)
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("DELETE", "/api/v1/tagValue/1", nil)
|
|
response = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("GET", "/api/v1/tagValue/1", nil)
|
|
response = executeRequest(req)
|
|
checkResponseCode(t, http.StatusNotFound, response.Code)
|
|
}
|