Adds Handlers and Tests for devicetypes, devices, and files
This commit is contained in:
131
testdevicetype_test.go
Normal file
131
testdevicetype_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func clearDeviceTypeTable() {
|
||||
a.DB.Exec("DELETE FROM deviceTypes")
|
||||
a.DB.Exec("ALTER TABLE deviceTypes AUTO_INCREMENT = 1")
|
||||
}
|
||||
|
||||
func TestEmptyDeviceTypeTable(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/api/v1/deviceTypes", 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 TestGetNonExistentDeviceType(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/api/v1/deviceType/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"] != "DeviceType not found" {
|
||||
t.Errorf("Expected the 'error' key of the response to be set to 'DeviceType not found'. Got '%s'", m["error"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateDeviceType(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
|
||||
payload := []byte(`{"name":"CLX"}`)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/api/v1/deviceType", 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"] != "CLX" {
|
||||
t.Errorf("Expected deviceType deviceType to be 'CLX'. Got '%v'", m["name"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDeviceType(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
addDeviceTypes(1)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/api/v1/deviceType/1", nil)
|
||||
response := executeRequest(req)
|
||||
|
||||
checkResponseCode(t, http.StatusOK, response.Code)
|
||||
}
|
||||
|
||||
func addDeviceTypes(count int) {
|
||||
if count < 1 {
|
||||
count = 1
|
||||
}
|
||||
insStmt, insErr := a.DB.Prepare("INSERT INTO deviceTypes(name, 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("TESTDEVICETYPE"+strconv.Itoa(i), time.Now(), time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateDeviceType(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
addDeviceTypes(1)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/api/v1/deviceType/1", nil)
|
||||
response := executeRequest(req)
|
||||
var originalDeviceType map[string]interface{}
|
||||
json.Unmarshal(response.Body.Bytes(), &originalDeviceType)
|
||||
|
||||
payload := []byte(`{"name":"E300"}`)
|
||||
|
||||
req, _ = http.NewRequest("PUT", "/api/v1/deviceType/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"] != originalDeviceType["id"] {
|
||||
t.Errorf("Expected the id to remain the same (%v). Got %v", originalDeviceType["id"], m["id"])
|
||||
}
|
||||
|
||||
if m["name"] == originalDeviceType["name"] {
|
||||
t.Errorf("Expected the name to change from '%v' to '%v'. Got '%v'", originalDeviceType["name"], m["name"], m["name"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteDeviceType(t *testing.T) {
|
||||
clearDeviceTypeTable()
|
||||
addDeviceTypes(1)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/api/v1/deviceType/1", nil)
|
||||
response := executeRequest(req)
|
||||
checkResponseCode(t, http.StatusOK, response.Code)
|
||||
|
||||
req, _ = http.NewRequest("DELETE", "/api/v1/deviceType/1", nil)
|
||||
response = executeRequest(req)
|
||||
|
||||
checkResponseCode(t, http.StatusOK, response.Code)
|
||||
|
||||
req, _ = http.NewRequest("GET", "/api/v1/deviceType/1", nil)
|
||||
response = executeRequest(req)
|
||||
checkResponseCode(t, http.StatusNotFound, response.Code)
|
||||
}
|
||||
Reference in New Issue
Block a user