149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func clearDeviceTable() {
|
|
a.DB.Exec("DELETE FROM devices")
|
|
a.DB.Exec("ALTER TABLE devices AUTO_INCREMENT = 1")
|
|
seedDeviceTypeData(a.DB)
|
|
}
|
|
|
|
func TestEmptyDeviceTable(t *testing.T) {
|
|
clearDeviceTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/devices", 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 TestGetNonExistentDevice(t *testing.T) {
|
|
clearDeviceTable()
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/device/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"] != "Device not found" {
|
|
t.Errorf("Expected the 'error' key of the response to be set to 'Device not found'. Got '%s'", m["error"])
|
|
}
|
|
}
|
|
|
|
func TestCreateDevice(t *testing.T) {
|
|
clearDeviceTable()
|
|
|
|
payload := []byte(`{"name":"TestDevice","deviceTypeId":1,"address":"192.168.1.10"}`)
|
|
|
|
req, _ := http.NewRequest("POST", "/api/v1/device", 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"] != "TestDevice" {
|
|
t.Errorf("Expected device name to be 'TestDevice'. Got '%v'", m["name"])
|
|
}
|
|
if m["address"] != "192.168.1.10" {
|
|
t.Errorf("Expected device address to be '192.168.1.10'. Got '%v'", m["address"])
|
|
}
|
|
|
|
if m["deviceTypeId"] != 1.0 {
|
|
t.Errorf("Expected device deviceTypeId to be '1'. Got '%v'", m["deviceTypeId"])
|
|
}
|
|
|
|
}
|
|
|
|
func TestGetDevice(t *testing.T) {
|
|
clearDeviceTable()
|
|
addDevices(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/device/1", nil)
|
|
response := executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
}
|
|
|
|
func addDevices(count int) {
|
|
if count < 1 {
|
|
count = 1
|
|
}
|
|
insStmt, insErr := a.DB.Prepare("INSERT INTO devices(name, deviceTypeId, address, 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("TESTDEVICE"+strconv.Itoa(i), 1, "192.168.1.10", time.Now(), time.Now())
|
|
}
|
|
}
|
|
|
|
func TestUpdateDevice(t *testing.T) {
|
|
clearDeviceTable()
|
|
addDevices(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/device/1", nil)
|
|
response := executeRequest(req)
|
|
var originalDevice map[string]interface{}
|
|
json.Unmarshal(response.Body.Bytes(), &originalDevice)
|
|
|
|
payload := []byte(`{"name":"TestDevice2","deviceTypeId":2,"address":"192.168.1.11"}`)
|
|
|
|
req, _ = http.NewRequest("PUT", "/api/v1/device/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"] != originalDevice["id"] {
|
|
t.Errorf("Expected the id to remain the same (%v). Got %v", originalDevice["id"], m["id"])
|
|
}
|
|
|
|
if m["name"] == originalDevice["name"] {
|
|
t.Errorf("Expected the name to change from '%v' to '%v'. Got '%v'", originalDevice["name"], m["name"], m["name"])
|
|
}
|
|
|
|
if m["deviceTypeId"] == originalDevice["deviceTypeId"] {
|
|
t.Errorf("Expected the deviceTypeId to change from '%v' to '%v'. Got '%v'", originalDevice["deviceTypeId"], m["deviceTypeId"], m["deviceTypeId"])
|
|
}
|
|
|
|
if m["address"] == originalDevice["address"] {
|
|
t.Errorf("Expected the address to change from '%v' to '%v'. Got '%v'", originalDevice["address"], m["address"], m["address"])
|
|
}
|
|
}
|
|
|
|
func TestDeleteDevice(t *testing.T) {
|
|
clearDeviceTable()
|
|
addDevices(1)
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/device/1", nil)
|
|
response := executeRequest(req)
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("DELETE", "/api/v1/device/1", nil)
|
|
response = executeRequest(req)
|
|
|
|
checkResponseCode(t, http.StatusOK, response.Code)
|
|
|
|
req, _ = http.NewRequest("GET", "/api/v1/device/1", nil)
|
|
response = executeRequest(req)
|
|
checkResponseCode(t, http.StatusNotFound, response.Code)
|
|
}
|