Files
Lumberjack-Go-Backend/testtag_test.go
2017-10-05 16:20:37 -05:00

231 lines
7.5 KiB
Go

package main
import (
"bytes"
"encoding/json"
"net/http"
"reflect"
"strconv"
"testing"
"time"
)
func clearTagTable() {
a.DB.Exec("DELETE FROM tags")
a.DB.Exec("ALTER TABLE tags AUTO_INCREMENT = 1")
}
func TestEmptyTagTable(t *testing.T) {
clearTagTable()
req, _ := http.NewRequest("GET", "/api/v1/tags", 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 TestGetNonExistentTag(t *testing.T) {
clearTagTable()
req, _ := http.NewRequest("GET", "/api/v1/tag/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"] != "Tag not found" {
t.Errorf("Expected the 'error' key of the response to be set to 'Tag not found'. Got '%s'", m["error"])
}
}
func TestCreateTag(t *testing.T) {
clearTagTable()
seedDataTypeData(a.DB)
seedDeviceTypeData(a.DB)
seedTagClassData(a.DB)
seedDeviceData(a.DB)
payload := []byte(`{"name":"TestTag","tagName":"testTag","tagClassId":1,"deviceId":1,"description":"This is a test tag","dataTypeId":1,"changeThreshold":4.5,"guaranteeSec":600,"mapFunction":"double","units":"in.","minExpected":0.0,"maxExpected":1000.0}`)
req, _ := http.NewRequest("POST", "/api/v1/tag", 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["name"] != "TestTag" {
t.Errorf("Expected tag name to be 'TestTag'. Got '%v'", m["name"])
}
if m["tagName"] != "testTag" {
t.Errorf("Expected tag tagName to be 'testTag'. Got '%v'", m["tagName"])
}
if m["tagClassId"] != 1.0 {
t.Errorf("Expected tag tagClassId to be '1'. Got '%v'", m["tagClassId"])
}
if m["deviceId"] != 1.0 {
t.Errorf("Expected tag deviceId to be '1'. Got '%v'", m["deviceId"])
}
if m["description"] != "This is a test tag" {
t.Errorf("Expected tag description to be 'This is a test tag'. Got '%v'", m["description"])
}
if m["dataTypeId"] != 1.0 {
t.Errorf("Expected tag dataTypeId to be 'testTag'. Got '%v'", m["dataTypeId"])
}
if m["changeThreshold"] != 4.5 {
t.Errorf("Expected tag changeThreshold to be '4.5'. Got '%v'", m["changeThreshold"])
}
if m["guaranteeSec"] != 600.0 {
t.Errorf("Expected tag guaranteeSec to be '600'. Got '%v'", m["guaranteeSec"])
}
if m["mapFunction"] != "double" {
t.Errorf("Expected tag mapFunction to be 'double'. Got '%v'", m["mapFunction"])
}
if m["units"] != "in." {
t.Errorf("Expected tag units to be 'in.'. Got '%v'", m["units"])
}
if m["minExpected"] != 0.0 {
t.Errorf("Expected tag minExpected to be '0.0'. Got '%v'", m["minExpected"])
}
if m["maxExpected"] != 1000.0 {
t.Errorf("Expected tag maxExpected to be '1000.0'. Got '%v'", m["maxExpected"])
}
}
func TestGetTag(t *testing.T) {
clearTagTable()
addTags(1)
req, _ := http.NewRequest("GET", "/api/v1/tag/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func addTags(count int) {
if count < 1 {
count = 1
}
sqlQuery := `INSERT INTO tags (
name,
tagName,
tagClassId,
deviceId,
description,
dataTypeId,
changeThreshold,
guaranteeSec,
mapFunction,
units,
minExpected,
maxExpected,
createdAt,
updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
insStmt, insErr := a.DB.Prepare(sqlQuery)
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("Name"+strconv.Itoa(i), "tag"+strconv.Itoa(i), 1, 1,
"Description"+strconv.Itoa(i), 1, i*100.0, 600,
"mapFn"+strconv.Itoa(i), "in.", 0.0, 1000.0, time.Now(), time.Now())
}
}
func TestUpdateTag(t *testing.T) {
clearTagTable()
addTags(1)
req, _ := http.NewRequest("GET", "/api/v1/tag/1", nil)
response := executeRequest(req)
var originalTag map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &originalTag)
payload := []byte(`{"name":"TestTag","tagName":"testTag","tagClassId":1,"deviceId":1,"description":"This is a test tag","dataTypeId":1,"changeThreshold":4.5,"guaranteeSec":600,"mapFunction":"double","units":"in.2","minExpected":1.0,"maxExpected":1001.0}`)
req, _ = http.NewRequest("PUT", "/api/v1/tag/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"] != originalTag["id"] {
t.Errorf("Expected the id to remain the same (%v). Got %v", originalTag["id"], m["id"])
}
if m["name"] == originalTag["name"] {
t.Errorf("Expected the name to change from '%v' to '%v'. Got '%v'", originalTag["name"], m["name"], m["name"])
}
if m["tagName"] == originalTag["tagName"] {
t.Errorf("Expected the tagName to change from '%v' to '%v'. Got '%v'", originalTag["tagName"], m["tagName"], m["tagName"])
}
if int(m["tagClassId"].(float64)) == originalTag["tagClassId"] {
t.Errorf("Expected the tagClassId to change from '%v' to '%v'. Got '%v'", originalTag["tagClassId"], m["tagClassId"], m["tagClassId"])
}
if int(m["deviceId"].(float64)) == originalTag["deviceId"] {
t.Errorf("Expected the deviceId to change from '%v' to '%v'. Got '%v'", originalTag["deviceId"], m["deviceId"], m["deviceId"])
}
if m["description"] == originalTag["description"] {
t.Errorf("Expected the description to change from '%v' to '%v'. Got '%v'", originalTag["description"], m["description"], m["description"])
}
if int(m["dataTypeId"].(float64)) == originalTag["dataTypeId"] {
t.Errorf("Expected the dataTypeId to change from '%v' to '%v'. Got '%v'", originalTag["dataTypeId"], m["dataTypeId"], m["dataTypeId"])
}
if m["changeThreshold"] == originalTag["changeThreshold"] {
t.Errorf("Expected the changeThreshold to change from '%v' to '%v'. Got '%v'", originalTag["changeThreshold"], m["changeThreshold"], m["changeThreshold"])
}
if int(m["guaranteeSec"].(float64)) == originalTag["guaranteeSec"] {
t.Errorf("Expected the guaranteeSec to change from '%v' to '%v'. Got '%v'", originalTag["guaranteeSec"], m["guaranteeSec"], m["guaranteeSec"])
}
if m["mapFunction"] == originalTag["mapFunction"] {
t.Errorf("Expected the mapFunction to change from '%v' to '%v'. Got '%v'", originalTag["changeThreshold"], m["changeThreshold"], m["changeThreshold"])
}
if m["units"] == originalTag["units"] {
t.Errorf("Expected the units to change from '%v' to '%v'. Got '%v'", reflect.TypeOf(originalTag["units"]), reflect.TypeOf(m["units"]), reflect.TypeOf(m["units"]))
}
if m["minExpected"] == originalTag["minExpected"] {
t.Errorf("Expected the minExpected to change from '%v' to '%v'. Got '%v'", originalTag["minExpected"], m["minExpected"], m["minExpected"])
}
if m["maxExpected"] == originalTag["maxExpected"] {
t.Errorf("Expected the maxExpected to change from '%v' to '%v'. Got '%v'", originalTag["maxExpected"], m["maxExpected"], m["maxExpected"])
}
}
func TestDeleteTag(t *testing.T) {
clearTagTable()
addTags(1)
req, _ := http.NewRequest("GET", "/api/v1/tag/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("DELETE", "/api/v1/tag/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", "/api/v1/tag/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
}