Esempio n. 1
0
func TestCategory(t *testing.T) {
	//load mock values
	defer models.Db.Exec("DELETE FROM users")
	defer models.Db.Exec("DELETE FROM account")
	defer models.Db.Exec("DELETE FROM users_account")
	defer models.Db.Exec("DELETE FROM category")
	if err := models.ExecSQLScript("test_category.sql"); err != nil {
		panic(err)
	}

	categoryName := "testName"
	accountID := "3fd155dc-7a6f-4bac-a74b-902df936ed75"
	_, err := models.CreateCategoryFromName(categoryName, accountID)
	if err != nil {
		t.Errorf("Error unable to create category : %s", err)
	}

	categories, err := models.FindCategoryByAccount(accountID)
	if err != nil {
		t.Errorf("Error unable to fetch categories : %s", err)
	}

	if len(categories) != 1 {
		t.Errorf("Count categories failed %d != 1", len(categories))
	}
	if categories[0].Name != categoryName {
		t.Errorf("Bad category name '%s'", categories[0].Name)
	}
}
Esempio n. 2
0
func TestTransaction(t *testing.T) {
	defer models.Db.Exec("DELETE FROM users")
	defer models.Db.Exec("DELETE FROM account")
	defer models.Db.Exec("DELETE FROM users_account")
	defer models.Db.Exec("DELETE FROM category")
	defer models.Db.Exec("DELETE FROM transaction")
	if err := models.ExecSQLScript("test_transaction.sql"); err != nil {
		panic(err)
	}

	//Create
	accountID := "3fd155dc-7a6f-4bac-a74b-902df936ed75"
	body := `{
		"label": "myevent",
		"value": -210.65,
		"accountId": "3fd155dc-7a6f-4bac-a74b-902df936ed75",
    "categoryId": "fe36bfe6-bf22-45fe-babd-a3db9d324727"
	}`
	request, err := http.NewRequest("PUT", "/api/"+accountID+"/transaction", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")
	recorder := httptest.NewRecorder()
	mTransaction.ServeHTTP(recorder, request)
	if recorder.Code != http.StatusOK {
		t.Errorf("Bad response code : %d", recorder.Code)
	}

	//Get
	request, err = http.NewRequest("GET", "/api/"+accountID+"/transactions/0/10", nil)
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")
	if err != nil {
		panic(err)
	}
	recorder = httptest.NewRecorder()
	m.ServeHTTP(recorder, request)
	if recorder.Code != http.StatusOK {
		t.Errorf("Bad response code : %d", recorder.Code)
	}

	var transactions []models.Transaction
	err = json.Unmarshal(recorder.Body.Bytes(), &transactions)
	if err != nil {
		t.Errorf("Fail to decode account response %s", err)
	}
	if len(transactions) != 1 {
		t.Errorf("Bad len result : %d != 1", len(transactions))
		return
	}
	if transactions[0].Label != "myevent" {
		t.Errorf("Bad account label : ''%s'", transactions[0].Label)
	}
	if transactions[0].Value != -210.65 {
		t.Errorf("Bad account label : ''%s'", transactions[0].Label)
	}
}
Esempio n. 3
0
func TestCategory(t *testing.T) {
	defer models.Db.Exec("DELETE FROM users")
	defer models.Db.Exec("DELETE FROM account")
	defer models.Db.Exec("DELETE FROM users_account")
	defer models.Db.Exec("DELETE FROM category")
	if err := models.ExecSQLScript("test_category.sql"); err != nil {
		panic(err)
	}

	//Create
	accountID := "3fd155dc-7a6f-4bac-a74b-902df936ed75"
	body := `{
		"accountId": "3fd155dc-7a6f-4bac-a74b-902df936ed75",
		"name": "my_category"
	}`
	request, err := http.NewRequest("PUT", "/api/"+accountID+"/category", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")

	recorder := httptest.NewRecorder()
	m.ServeHTTP(recorder, request)
	if recorder.Code != http.StatusOK {
		t.Errorf("Bad response code : %d", recorder.Code)
	}

	//Get
	request, err = http.NewRequest("GET", "/api/"+accountID+"/categories", nil)
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")
	if err != nil {
		panic(err)
	}
	recorder = httptest.NewRecorder()
	m.ServeHTTP(recorder, request)
	if recorder.Code != http.StatusOK {
		t.Errorf("Bad response code : %d", recorder.Code)
	}

	var categories []models.Category
	err = json.Unmarshal(recorder.Body.Bytes(), &categories)
	if err != nil {
		t.Errorf("Fail to decode account response %s", err)
	}
	if len(categories) != 1 {
		t.Errorf("Bad len result : %d != 1", len(categories))
	}
	if categories[0].Name != "my_category" {
		t.Errorf("Bad account name : ''%s'", categories[0].Name)
	}

}
Esempio n. 4
0
func TestAccountHandlers(t *testing.T) {
	defer models.Db.Exec("DELETE FROM users")
	defer models.Db.Exec("DELETE FROM account")
	defer models.Db.Exec("DELETE FROM users_account")
	if err := models.ExecSQLScript("test_create_account.sql"); err != nil {
		panic(err)
	}

	//create account
	var accountJSON = `{
    "name": "testaccount",
		"value": 120
  }`
	request, err := http.NewRequest("PUT", "/api/account", strings.NewReader(accountJSON))
	if err != nil {
		panic(err)
	}
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")

	recorder := httptest.NewRecorder()
	api.CreateAccountHandler(recorder, request)

	//get account
	request, err = http.NewRequest("GET", "/api/account", nil)
	if err != nil {
		panic(err)
	}
	context.Set(request, "currentId", "dc45ef40-1763-4dde-ab0f-978c224495e6")
	recorder = httptest.NewRecorder()
	api.GetAccountsHandler(recorder, request)
	var account []models.Account
	err = json.Unmarshal(recorder.Body.Bytes(), &account)
	if err != nil {
		t.Errorf("Fail to decode account response %s", err)
	}
	if len(account) != 1 {
		t.Errorf("Bad len result : %d != 1", len(account))
	}
	if account[0].Name != "testaccount" {
		t.Errorf("Bad account name : ''%s'", account[0].Name)
	}
	if account[0].Value != 120 {
		t.Errorf("Bad account value : ''%d'", account[0].Value)
	}
}
Esempio n. 5
0
func TestTransaction(t *testing.T) {
	//load mock values
	defer models.Db.Exec("DELETE FROM users")
	defer models.Db.Exec("DELETE FROM account")
	defer models.Db.Exec("DELETE FROM users_account")
	defer models.Db.Exec("DELETE FROM category")
	defer models.Db.Exec("DELETE FROM transaction")
	if err := models.ExecSQLScript("test_transaction.sql"); err != nil {
		panic(err)
	}

	transactionLabel := "label"
	transactionValue := -120.0
	accountID := "3fd155dc-7a6f-4bac-a74b-902df936ed75"
	categoryID := "fe36bfe6-bf22-45fe-babd-a3db9d324727"
	testDate := time.Now()

	transaction := models.Transaction{
		Label:      transactionLabel,
		Date:       testDate,
		Value:      transactionValue,
		Validate:   false,
		AccountID:  accountID,
		CategoryID: categoryID,
	}

	_, err := models.CreateTransaction(&transaction)
	if err != nil {
		t.Errorf("Error unable to create transaction : %s", err)
	}

	transactions, err := models.FindTransactionsForAccount(accountID, 0, 10)
	if err != nil {
		t.Errorf("Error unable to fetch transactions : %s", err)
	}

	if len(transactions) != 1 {
		t.Errorf("Count transactions failed %d != 1", len(transactions))
		return
	}
	if transactions[0].Label != transactionLabel {
		t.Errorf("Bad transaction label '%s'", transactions[0].Label)
	}
	if transactions[0].Value != transactionValue {
		t.Errorf("Bad transaction label '%s'", transactions[0].Value)
	}
	if transactions[0].Date.Equal(testDate) {
		t.Errorf("Bad transaction label '%s' vs '%s'", transactions[0].Date, testDate)
	}

	//Test trigger
	rows, err := models.Db.Query("SELECT value FROM account")
	if err != nil {
		t.Error(err)
		return
	}
	if rows.Next() {
		var value float64
		err := rows.Scan(&value)
		if err != nil {
			t.Error(err)
			return
		}
		if value != -20.0 {
			t.Errorf("Error trigger : %f != -20.0", value)
		}
	} else {
		t.Errorf("bad query")
	}

}