package main import ( "bytes" "encoding/json" "net/http" "strconv" "testing" "time" ) func clearDataTypeTable() { a.DB.Exec("DELETE FROM dataTypes") a.DB.Exec("ALTER TABLE dataTypes AUTO_INCREMENT = 1") } func TestEmptyDataTypeTable(t *testing.T) { clearDataTypeTable() req, _ := http.NewRequest("GET", "/api/v1/dataTypes", 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 TestGetNonExistentDataType(t *testing.T) { clearDataTypeTable() req, _ := http.NewRequest("GET", "/api/v1/dataType/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"] != "DataType not found" { t.Errorf("Expected the 'error' key of the response to be set to 'DataType not found'. Got '%s'", m["error"]) } } func TestCreateDataType(t *testing.T) { clearDataTypeTable() payload := []byte(`{"dataType":"Floating Point","plcType":"REAL"}`) req, _ := http.NewRequest("POST", "/api/v1/dataType", 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["dataType"] != "Floating Point" { t.Errorf("Expected dataType dataType to be 'Floating Point'. Got '%v'", m["dataType"]) } if m["plcType"] != "REAL" { t.Errorf("Expected dataType plcType to be 'REAL'. Got '%v'", m["plcType"]) } } func TestGetDataType(t *testing.T) { clearDataTypeTable() addDataTypes(1) req, _ := http.NewRequest("GET", "/api/v1/dataType/1", nil) response := executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) } func addDataTypes(count int) { if count < 1 { count = 1 } insStmt, insErr := a.DB.Prepare("INSERT INTO dataTypes(dataType, plcType, 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("TESTTYPE"+strconv.Itoa(i), "TESTPLCTYPE"+strconv.Itoa(i), time.Now(), time.Now()) } } func TestUpdateDataType(t *testing.T) { clearDataTypeTable() addDataTypes(1) req, _ := http.NewRequest("GET", "/api/v1/dataType/1", nil) response := executeRequest(req) var originalDataType map[string]interface{} json.Unmarshal(response.Body.Bytes(), &originalDataType) payload := []byte(`{"dataType":"Floating Point","plcType":"REAL"}`) req, _ = http.NewRequest("PUT", "/api/v1/dataType/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"] != originalDataType["id"] { t.Errorf("Expected the id to remain the same (%v). Got %v", originalDataType["id"], m["id"]) } if m["dataType"] == originalDataType["dataType"] { t.Errorf("Expected the dataType to change from '%v' to '%v'. Got '%v'", originalDataType["dataType"], m["dataType"], m["dataType"]) } if m["plcType"] == originalDataType["plcType"] { t.Errorf("Expected the plcType to change from '%v' to '%v'. Got '%v'", originalDataType["plcType"], m["plcType"], m["plcType"]) } } func TestDeleteDataType(t *testing.T) { clearDataTypeTable() addDataTypes(1) req, _ := http.NewRequest("GET", "/api/v1/dataType/1", nil) response := executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) req, _ = http.NewRequest("DELETE", "/api/v1/dataType/1", nil) response = executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) req, _ = http.NewRequest("GET", "/api/v1/dataType/1", nil) response = executeRequest(req) checkResponseCode(t, http.StatusNotFound, response.Code) }