package main import ( "bytes" "encoding/json" "net/http" "strconv" "testing" "time" ) func clearTagClassTable() { a.DB.Exec("DELETE FROM tagClasses") a.DB.Exec("ALTER TABLE tagClasses AUTO_INCREMENT = 1") } func TestEmptyTagClassTable(t *testing.T) { clearTagClassTable() req, _ := http.NewRequest("GET", "/api/v1/tagClasses", 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 TestGetNonExistentTagClass(t *testing.T) { clearTagClassTable() req, _ := http.NewRequest("GET", "/api/v1/tagClass/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"] != "TagClass not found" { t.Errorf("Expected the 'error' key of the response to be set to 'TagClass not found'. Got '%s'", m["error"]) } } func TestCreateTagClass(t *testing.T) { clearTagClassTable() payload := []byte(`{"classType":"Normal","description":"Historical Data"}`) req, _ := http.NewRequest("POST", "/api/v1/tagClass", 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["classType"] != "Normal" { t.Errorf("Expected tagClass classType to be 'Normal'. Got '%v'", m["classType"]) } if m["description"] != "Historical Data" { t.Errorf("Expected tagClass description to be 'Historical Data'. Got '%v'", m["description"]) } } func TestGetTagClass(t *testing.T) { clearTagClassTable() addTagClasses(1) req, _ := http.NewRequest("GET", "/api/v1/tagClass/1", nil) response := executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) } func addTagClasses(count int) { if count < 1 { count = 1 } insStmt, insErr := a.DB.Prepare("INSERT INTO tagClasses(classType, description, 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), "TESTDESCRIPTION"+strconv.Itoa(i), time.Now(), time.Now()) } } func TestUpdateTagClass(t *testing.T) { clearTagClassTable() addTagClasses(1) req, _ := http.NewRequest("GET", "/api/v1/tagClass/1", nil) response := executeRequest(req) var originalTagClass map[string]interface{} json.Unmarshal(response.Body.Bytes(), &originalTagClass) payload := []byte(`{"classType":"handshake","description":"PLC Handshake"}`) req, _ = http.NewRequest("PUT", "/api/v1/tagClass/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"] != originalTagClass["id"] { t.Errorf("Expected the id to remain the same (%v). Got %v", originalTagClass["id"], m["id"]) } if m["classType"] == originalTagClass["tagClass"] { t.Errorf("Expected the classType to change from '%v' to '%v'. Got '%v'", originalTagClass["classType"], m["classType"], m["classType"]) } if m["description"] == originalTagClass["description"] { t.Errorf("Expected the description to change from '%v' to '%v'. Got '%v'", originalTagClass["description"], m["description"], m["description"]) } } func TestDeleteTagClass(t *testing.T) { clearTagClassTable() addTagClasses(1) req, _ := http.NewRequest("GET", "/api/v1/tagClass/1", nil) response := executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) req, _ = http.NewRequest("DELETE", "/api/v1/tagClass/1", nil) response = executeRequest(req) checkResponseCode(t, http.StatusOK, response.Code) req, _ = http.NewRequest("GET", "/api/v1/tagClass/1", nil) response = executeRequest(req) checkResponseCode(t, http.StatusNotFound, response.Code) }