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
.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) {
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) {

11
main.go
View File

@@ -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"),

View File

@@ -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,8 +39,7 @@ func ensureTagValueTableExists(db *sql.DB) {
// getTagValue : used during GET command
func (t *TagValue) getTagValue(db *sql.DB) error {
sqlQuery := `SELECT
tagValues.tagId,
sqlQuery := `SELECT tagValues.tagId,
tagValues.val,
tags.name,
tags.units,

View File

@@ -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"),

View File

@@ -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)