diff --git a/.gitignore b/.gitignore index 015b4af..b63178a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ lumberjack +.env diff --git a/handler_tagvalue.go b/handler_tagvalue.go index 73978c8..9f57b81 100644 --- a/handler_tagvalue.go +++ b/handler_tagvalue.go @@ -52,19 +52,20 @@ func (a *App) getTagValues(w http.ResponseWriter, r *http.Request) { } func (a *App) createTagValue(w http.ResponseWriter, r *http.Request) { - var c TagValue + var t TagValue decoder := json.NewDecoder(r.Body) - if err := decoder.Decode(&c); err != nil { + + if err := decoder.Decode(&t); err != nil { respondWithError(w, http.StatusBadRequest, "Invalid request payload") return } defer r.Body.Close() - if err := c.createTagValue(a.DB); err != nil { + if err := t.createTagValue(a.DB); err != nil { respondWithError(w, http.StatusInternalServerError, err.Error()) return } - respondWithJSON(w, http.StatusCreated, c) + respondWithJSON(w, http.StatusCreated, t) } func (a *App) updateTagValue(w http.ResponseWriter, r *http.Request) { diff --git a/main.go b/main.go index 9551dbf..1c2dd1f 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,19 @@ package main -import "os" +import ( + "log" + "os" + + "github.com/joho/godotenv" +) func main() { a := App{} + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } a.Initialize( os.Getenv("APP_DB_USERNAME"), os.Getenv("APP_DB_PASSWORD"), diff --git a/model_tagvalue.go b/model_tagvalue.go index 72e5b89..7b425bb 100644 --- a/model_tagvalue.go +++ b/model_tagvalue.go @@ -11,8 +11,8 @@ type TagValue struct { ID int `json:"id"` TagID int `json:"tagId"` Val string `json:"val"` - TagName int `json:"tagName"` - Units int `json:"units"` + TagName string `json:"tagName"` + Units string `json:"units"` DataType string `json:"dataType"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` @@ -39,15 +39,14 @@ func ensureTagValueTableExists(db *sql.DB) { // getTagValue : used during GET command func (t *TagValue) getTagValue(db *sql.DB) error { - sqlQuery := `SELECT - tagValues.tagId, - tagValues.val, + sqlQuery := `SELECT tagValues.tagId, + tagValues.val, tags.name, tags.units, dataTypes.dataType, - tagValues.createdAt, - tagValues.updatedAt - FROM tagValues + tagValues.createdAt, + tagValues.updatedAt + FROM tagValues JOIN tags ON tagValues.tagId = tags.id JOIN dataTypes ON tags.dataTypeId = dataTypes.id WHERE tagValues.id=?` @@ -86,16 +85,16 @@ func (t *TagValue) createTagValue(db *sql.DB) error { // getTagValues : used during GET command for all func getTagValues(db *sql.DB, start, count int) (TagValues, error) { - sqlQuery := `SELECT + sqlQuery := `SELECT tagValues.id, - tagValues.tagId, - tagValues.val, + tagValues.tagId, + tagValues.val, tags.name, tags.units, dataTypes.dataType, - tagValues.createdAt, - tagValues.updatedAt - FROM tagValues + tagValues.createdAt, + tagValues.updatedAt + FROM tagValues JOIN tags ON tagValues.tagId = tags.id JOIN dataTypes ON tags.dataTypeId = dataTypes.id LIMIT ? OFFSET ?` diff --git a/test_test.go b/test_test.go index 5dc8e7d..5933854 100644 --- a/test_test.go +++ b/test_test.go @@ -1,16 +1,23 @@ package main import ( + "log" "net/http" "net/http/httptest" "os" "testing" + + "github.com/joho/godotenv" ) var a App func TestMain(m *testing.M) { a = App{} + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } a.Initialize( os.Getenv("TEST_DB_USERNAME"), os.Getenv("TEST_DB_PASSWORD"), diff --git a/testtag_test.go b/testtag_test.go index 656ba81..4d005ce 100644 --- a/testtag_test.go +++ b/testtag_test.go @@ -113,18 +113,18 @@ func addTags(count int) { count = 1 } sqlQuery := `INSERT INTO tags ( - name, - tagName, - tagClassId, - deviceId, - description, - dataTypeId, - changeThreshold, - guaranteeSec, - mapFunction, - units, - minExpected, - maxExpected, + name, + tagName, + tagClassId, + deviceId, + description, + dataTypeId, + changeThreshold, + guaranteeSec, + mapFunction, + units, + minExpected, + maxExpected, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` insStmt, insErr := a.DB.Prepare(sqlQuery) diff --git a/testtagvalue_test.go b/testtagvalue_test.go index 0772641..f79ad55 100644 --- a/testtagvalue_test.go +++ b/testtagvalue_test.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/json" - "fmt" "net/http" "strconv" "testing" @@ -45,23 +44,26 @@ func TestGetNonExistentTagValue(t *testing.T) { func TestCreateTagValue(t *testing.T) { clearTagValueTable() + seedDataTypeData(a.DB) + seedDeviceTypeData(a.DB) + seedTagClassData(a.DB) + seedDeviceData(a.DB) addTags(1) - payload := []byte(`{"tagId":1,"val":"1234.56"}`) + payload := []byte(`{"tagId":2,"val":"1234.56"}`) req, _ := http.NewRequest("POST", "/api/v1/tagValue", bytes.NewBuffer(payload)) response := executeRequest(req) - fmt.Printf("\n\n%s\n\n", response.Body) checkResponseCode(t, http.StatusCreated, response.Code) var m map[string]interface{} json.Unmarshal(response.Body.Bytes(), &m) - if m["tagId"] != 1.0 { - t.Errorf("Expected tagValue name to be '1'. Got '%v'", m["name"]) + if m["tagId"] != 2.0 { + t.Errorf("Expected tagValue tagId to be '2'. Got '%v'", m["tagId"]) } if m["val"] != "1234.56" { - t.Errorf("Expected tagValue address to be '1234.56'. Got '%v'", m["address"]) + t.Errorf("Expected tagValue val to be '1234.56'. Got '%v'", m["val"]) } } @@ -87,7 +89,7 @@ func addTagValues(count int) { defer insStmt.Close() // Close the statement when we leave main() / the program terminates for i := 0; i < count; i++ { - insStmt.Exec(1, strconv.Itoa(i*10), time.Now(), time.Now()) + insStmt.Exec(2, strconv.Itoa(i*10), time.Now(), time.Now()) } } @@ -100,7 +102,7 @@ func TestUpdateTagValue(t *testing.T) { var originalTagValue map[string]interface{} json.Unmarshal(response.Body.Bytes(), &originalTagValue) - payload := []byte(`{"tagId":1,"val":"111.111"}`) + payload := []byte(`{"tagId":2,"val":"111.111"}`) req, _ = http.NewRequest("PUT", "/api/v1/tagValue/1", bytes.NewBuffer(payload)) response = executeRequest(req)