Files
Lumberjack-Go-Backend/testtagvalue_test.go

142 lines
3.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"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()
addTags(1)
payload := []byte(`{"tagId":1,"val":"1234.56"}`)
req, _ := http.NewRequest("POST", "/api/v1/tagValue", bytes.NewBuffer(payload))
response := executeRequest(req)
fmt.Printf("\n\n%s\n\n", response.Body)
checkResponseCode(t, http.StatusCreated, response.Code)
var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m)
if m["tagId"] != 1.0 {
t.Errorf("Expected tagValue name to be '1'. Got '%v'", m["name"])
}
if m["val"] != "1234.56" {
t.Errorf("Expected tagValue address to be '1234.56'. Got '%v'", m["address"])
}
}
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(1, 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":1,"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)
}