Files
Lumberjack-Go-Backend/testfile_test.go
2017-09-27 18:09:53 -05:00

148 lines
4.1 KiB
Go

package main
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"testing"
"time"
)
func clearFileTable() {
a.DB.Exec("DELETE FROM files")
a.DB.Exec("ALTER TABLE files AUTO_INCREMENT = 1")
}
func TestEmptyFileTable(t *testing.T) {
clearFileTable()
req, _ := http.NewRequest("GET", "/api/v1/files", 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 TestGetNonExistentFile(t *testing.T) {
clearFileTable()
req, _ := http.NewRequest("GET", "/api/v1/file/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"] != "File not found" {
t.Errorf("Expected the 'error' key of the response to be set to 'File not found'. Got '%s'", m["error"])
}
}
func TestCreateFile(t *testing.T) {
clearFileTable()
payload := []byte(`{"name":"test_filename","description":"Test Filename","location":"/this/path"}`)
req, _ := http.NewRequest("POST", "/api/v1/file", 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["name"] != "test_filename" {
t.Errorf("Expected file name to be 'test_filename'. Got '%v'", m["name"])
}
if m["description"] != "Test Filename" {
t.Errorf("Expected file description to be 'Test Filename'. Got '%v'", m["description"])
}
if m["location"] != "/this/path" {
t.Errorf("Expected file location to be '/this/path'. Got '%v'", m["location"])
}
}
func TestGetFile(t *testing.T) {
clearFileTable()
addFiles(1)
req, _ := http.NewRequest("GET", "/api/v1/file/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func addFiles(count int) {
if count < 1 {
count = 1
}
insStmt, insErr := a.DB.Prepare("INSERT INTO files(name, description, location, 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("testfile"+strconv.Itoa(i), "testfiledescription", "/this/location", time.Now(), time.Now())
}
}
func TestUpdateFile(t *testing.T) {
clearFileTable()
addFiles(1)
req, _ := http.NewRequest("GET", "/api/v1/file/1", nil)
response := executeRequest(req)
var originalFile map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &originalFile)
payload := []byte(`{"name":"test_filename1","description":"Test Filename1","location":"/this/path/1"}`)
req, _ = http.NewRequest("PUT", "/api/v1/file/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"] != originalFile["id"] {
t.Errorf("Expected the id to remain the same (%v). Got %v", originalFile["id"], m["id"])
}
if m["name"] == originalFile["name"] {
t.Errorf("Expected the name to change from '%v' to '%v'. Got '%v'", originalFile["name"], m["name"], m["name"])
}
if m["description"] == originalFile["description"] {
t.Errorf("Expected the description to change from '%v' to '%v'. Got '%v'", originalFile["description"], m["description"], m["description"])
}
if m["location"] == originalFile["location"] {
t.Errorf("Expected the location to change from '%v' to '%v'. Got '%v'", originalFile["location"], m["location"], m["location"])
}
}
func TestDeleteFile(t *testing.T) {
clearFileTable()
addFiles(1)
req, _ := http.NewRequest("GET", "/api/v1/file/1", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("DELETE", "/api/v1/file/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", "/api/v1/file/1", nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
}