Commit cc117c64 authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Fix : test error and DB error on opening

parent fc694a9d
Pipeline #4492 failed with stages
in 3 minutes and 9 seconds
package models
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/jinzhu/gorm"
// Needed for sqlite
......@@ -21,13 +20,14 @@ type DataHandler struct {
// NewDataHandler init a DataHandler and returns a pointer to it
func NewDataHandler() *DataHandler {
db, err := gorm.Open("sqlite3", "../../data/test.db")
db, err := gorm.Open("sqlite3", "./data/test.db")
if err != nil {
panic("failed to connect database")
}
db.LogMode(true)
db.Model(&Operation{}).AddForeignKey("creditor", "bank_account(id)", "RESTRICT", "RESTRICT")
// Add FK with SQLite doesn't work
// db.Model(&Operation{}).AddForeignKey("creditor", "bank_account(id)", "Set Null", "Set Null")
// Migrate the schema
db.AutoMigrate(&UserClient{})
......
......@@ -12,7 +12,9 @@ import (
func TestHandleUserClients(t *testing.T) {
// Remove the test db to start clean
os.Remove("../../data/test.db")
os.Remove("./data/test.db")
os.Mkdir("data", os.ModePerm)
os.Create("./data/test.db")
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleClients))
......@@ -196,4 +198,7 @@ func TestBankAccountDeletion(t *testing.T) {
// Test that bank account have been deleted after client deletion
do("GET", "/api/BankAccounts/3", noH, "", 404, `id does not exist`)
// remove db test
os.RemoveAll("./data")
}
......@@ -33,8 +33,8 @@ func CreateRootMux(port int, staticDir string) RootMux {
// Bank API endpoints
dh := models.NewDataHandler()
mainMux.HandleFunc("/api/UserClients/", dh.HandleClients)
mainMux.HandleFunc("/api/BankAccounts/", dh.HandleBankAccounts)
mainMux.HandleFunc("/api/Operations/", dh.HandleOperations)
// mainMux.HandleFunc("/api/BankAccounts/", dh.HandleBankAccounts)
// mainMux.HandleFunc("/api/Operations/", dh.HandleOperations)
// Common API endpoints
commonMux := http.NewServeMux()
......
......@@ -40,7 +40,7 @@ func TestAll(t *testing.T) {
// Create the mock API server
go http.ListenAndServe(":8091", mocks.CreateMockAPI())
// Set the constants with environment variables
os.Setenv("HOSTNAME", "vestibule.io")
os.Setenv("HOSTNAME", "sdk-go.io")
os.Setenv("ADMIN_ROLE", "ADMINS")
os.Setenv("CLIENT_ID", "clientid")
os.Setenv("CLIENT_SECRET", "clientsecret")
......@@ -187,6 +187,9 @@ func createAdminTests(t *testing.T) func(wg *sync.WaitGroup) {
}
func createTester(t *testing.T) (*httptest.Server, func(method string, url string, header tester.Header, payload string, expectedStatus int, expectedBody string) string, func(method string, url string, header tester.Header, payload string, expectedStatus int, expectedBody string) string) {
os.Remove("./data/test.db")
os.Mkdir("data", os.ModePerm)
os.Create("./data/test.db")
// Create the server
mux := CreateRootMux(1443, "../../web")
ts := httptest.NewServer(mux.Mux)
......@@ -196,6 +199,7 @@ func createTester(t *testing.T) (*httptest.Server, func(method string, url strin
mux.Manager.Hostname = "http://" + os.Getenv("HOSTNAME") + ":" + port
// Create the cookie jar
jar, _ := cookiejar.New(nil)
os.RemoveAll("./data")
// wrap the testing function
return ts, tester.CreateServerTester(t, port, os.Getenv("HOSTNAME"), jar), tester.CreateServerTester(t, port, os.Getenv("HOSTNAME"), nil)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment