DL-1 use dotenv, tagValue passes tests

This commit is contained in:
Patrick McDonagh
2017-10-05 16:20:37 -05:00
parent 877cc2ae6c
commit 3f20496dfa
7 changed files with 58 additions and 39 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
lumberjack lumberjack
.env

View File

@@ -52,19 +52,20 @@ func (a *App) getTagValues(w http.ResponseWriter, r *http.Request) {
} }
func (a *App) createTagValue(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) 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") respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return return
} }
defer r.Body.Close() 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()) respondWithError(w, http.StatusInternalServerError, err.Error())
return return
} }
respondWithJSON(w, http.StatusCreated, c) respondWithJSON(w, http.StatusCreated, t)
} }
func (a *App) updateTagValue(w http.ResponseWriter, r *http.Request) { func (a *App) updateTagValue(w http.ResponseWriter, r *http.Request) {

11
main.go
View File

@@ -1,10 +1,19 @@
package main package main
import "os" import (
"log"
"os"
"github.com/joho/godotenv"
)
func main() { func main() {
a := App{} a := App{}
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
a.Initialize( a.Initialize(
os.Getenv("APP_DB_USERNAME"), os.Getenv("APP_DB_USERNAME"),
os.Getenv("APP_DB_PASSWORD"), os.Getenv("APP_DB_PASSWORD"),

View File

@@ -11,8 +11,8 @@ type TagValue struct {
ID int `json:"id"` ID int `json:"id"`
TagID int `json:"tagId"` TagID int `json:"tagId"`
Val string `json:"val"` Val string `json:"val"`
TagName int `json:"tagName"` TagName string `json:"tagName"`
Units int `json:"units"` Units string `json:"units"`
DataType string `json:"dataType"` DataType string `json:"dataType"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
@@ -39,8 +39,7 @@ func ensureTagValueTableExists(db *sql.DB) {
// getTagValue : used during GET command // getTagValue : used during GET command
func (t *TagValue) getTagValue(db *sql.DB) error { func (t *TagValue) getTagValue(db *sql.DB) error {
sqlQuery := `SELECT sqlQuery := `SELECT tagValues.tagId,
tagValues.tagId,
tagValues.val, tagValues.val,
tags.name, tags.name,
tags.units, tags.units,

View File

@@ -1,16 +1,23 @@
package main package main
import ( import (
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"testing" "testing"
"github.com/joho/godotenv"
) )
var a App var a App
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
a = App{} a = App{}
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
a.Initialize( a.Initialize(
os.Getenv("TEST_DB_USERNAME"), os.Getenv("TEST_DB_USERNAME"),
os.Getenv("TEST_DB_PASSWORD"), os.Getenv("TEST_DB_PASSWORD"),

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"testing" "testing"
@@ -45,23 +44,26 @@ func TestGetNonExistentTagValue(t *testing.T) {
func TestCreateTagValue(t *testing.T) { func TestCreateTagValue(t *testing.T) {
clearTagValueTable() clearTagValueTable()
seedDataTypeData(a.DB)
seedDeviceTypeData(a.DB)
seedTagClassData(a.DB)
seedDeviceData(a.DB)
addTags(1) 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)) req, _ := http.NewRequest("POST", "/api/v1/tagValue", bytes.NewBuffer(payload))
response := executeRequest(req) response := executeRequest(req)
fmt.Printf("\n\n%s\n\n", response.Body)
checkResponseCode(t, http.StatusCreated, response.Code) checkResponseCode(t, http.StatusCreated, response.Code)
var m map[string]interface{} var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m) json.Unmarshal(response.Body.Bytes(), &m)
if m["tagId"] != 1.0 { if m["tagId"] != 2.0 {
t.Errorf("Expected tagValue name to be '1'. Got '%v'", m["name"]) t.Errorf("Expected tagValue tagId to be '2'. Got '%v'", m["tagId"])
} }
if m["val"] != "1234.56" { 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 defer insStmt.Close() // Close the statement when we leave main() / the program terminates
for i := 0; i < count; i++ { 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{} var originalTagValue map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &originalTagValue) 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)) req, _ = http.NewRequest("PUT", "/api/v1/tagValue/1", bytes.NewBuffer(payload))
response = executeRequest(req) response = executeRequest(req)