mirror of
https://github.com/LeRoid-hub/Bookholder-API.git
synced 2025-01-30 18:24:56 +00:00
test cases for database, some account testing
This commit is contained in:
parent
ca7d82a6e3
commit
82ef167a49
@ -1,33 +1,32 @@
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE TABLE accounts (
|
||||
name "char" NOT NULL,
|
||||
kind "char" NOT NULL,
|
||||
id integer NOT NULL
|
||||
id integer NOT NULL,
|
||||
name character varying NOT NULL,
|
||||
kind character varying NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE transaction (
|
||||
id uuid NOT NULL,
|
||||
CREATE TABLE transactions (
|
||||
id serial NOT NULL PRIMARY KEY,
|
||||
amount double precision NOT NULL,
|
||||
debit boolean NOT NULL,
|
||||
offset_account integer NOT NULL,
|
||||
account integer NOT NULL,
|
||||
date timestamp without time zone NOT NULL,
|
||||
description "char"
|
||||
description character varying
|
||||
);
|
||||
|
||||
CREATE TABLE users (
|
||||
name "char" NOT NULL,
|
||||
password "char" NOT NULL,
|
||||
id integer
|
||||
id UUID NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
name character varying UNIQUE NOT NULL,
|
||||
password character varying NOT NULL
|
||||
);
|
||||
|
||||
ALTER TABLE ONLY accounts
|
||||
ADD CONSTRAINT accounts_pkey PRIMARY KEY (id);
|
||||
|
||||
ALTER TABLE ONLY transaction
|
||||
ADD CONSTRAINT transaction_pkey PRIMARY KEY (id);
|
||||
|
||||
ALTER TABLE ONLY transaction
|
||||
ALTER TABLE ONLY transactions
|
||||
ADD CONSTRAINT "Account" FOREIGN KEY (account) REFERENCES accounts(id) NOT VALID;
|
||||
|
||||
ALTER TABLE ONLY transaction
|
||||
ALTER TABLE ONLY transactions
|
||||
ADD CONSTRAINT "Offset" FOREIGN KEY (offset_account) REFERENCES accounts(id) NOT VALID;
|
@ -132,6 +132,7 @@ func existAccount(database *sql.DB, id int) (bool, error) {
|
||||
}
|
||||
|
||||
func NewAccount(database *sql.DB, account Account) error {
|
||||
|
||||
exists, err := existAccount(database, int(account.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
@ -140,10 +141,11 @@ func NewAccount(database *sql.DB, account Account) error {
|
||||
return errors.New("account already exists")
|
||||
}
|
||||
|
||||
_, err = database.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account.ID, account.Name, account.Kind)
|
||||
_, err = database.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", int(account.ID), string(account.Name), account.Kind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -160,8 +162,8 @@ func UpdateAccount(database *sql.DB, account Account) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteAccount(database *sql.DB, id int) error {
|
||||
@ -185,10 +187,12 @@ func GetAccount(database *sql.DB, id int) (Account, error) {
|
||||
var account Account
|
||||
err := database.QueryRow("SELECT * FROM accounts WHERE id = $1", id).Scan(&account.ID, &account.Name, &account.Kind)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return account, errors.New("account does not exist")
|
||||
}
|
||||
return account, err
|
||||
}
|
||||
return account, nil
|
||||
|
||||
}
|
||||
|
||||
func existTransaction(database *sql.DB, id int) (bool, error) {
|
||||
@ -204,7 +208,19 @@ func existTransaction(database *sql.DB, id int) (bool, error) {
|
||||
}
|
||||
|
||||
func NewTransaction(database *sql.DB, transaction Transaction) error {
|
||||
_, err := database.Exec("INSERT INTO transactions (amount, debit, offset_account, account, time, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description)
|
||||
if transaction.OffsetAccount == transaction.Account {
|
||||
return errors.New("offset account and account cannot be the same")
|
||||
}
|
||||
|
||||
if transaction.Amount == 0 {
|
||||
return errors.New("amount cannot be 0")
|
||||
}
|
||||
|
||||
if transaction.Date.IsZero() {
|
||||
return errors.New("date is required")
|
||||
}
|
||||
|
||||
_, err := database.Exec("INSERT INTO transactions (amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -221,7 +237,7 @@ func UpdateTransaction(database *sql.DB, transaction Transaction) error {
|
||||
if !exists {
|
||||
return errors.New("transaction already exists")
|
||||
}
|
||||
_, err = database.Exec("UPDATE transactions SET amount = $1, debit = $2, offset_account = $3, account = $4, time = $5, description = $6 WHERE id = $7", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description, transaction.ID)
|
||||
_, err = database.Exec("UPDATE transactions SET amount = $1, debit = $2, offset_account = $3, account = $4, date = $5, description = $6 WHERE id = $7", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description, transaction.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -248,7 +264,7 @@ func DeleteTransaction(database *sql.DB, id int) error {
|
||||
|
||||
func GetTransaction(database *sql.DB, id int) (Transaction, error) {
|
||||
var transaction Transaction
|
||||
err := database.QueryRow("SELECT * FROM transactions WHERE id = $1", id).Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Date, &transaction.Description)
|
||||
err := database.QueryRow("SELECT id, amount, debit, offset_account, account, date, description FROM transactions WHERE id = $1", id).Scan(&transaction.ID, &transaction.Amount, &transaction.Debit, &transaction.OffsetAccount, &transaction.Account, &transaction.Date, &transaction.Description)
|
||||
if err != nil {
|
||||
return transaction, err
|
||||
}
|
||||
@ -263,11 +279,10 @@ func GetTransactions(database *sql.DB, account int, year int, month int) ([]Tran
|
||||
return nil, errors.New("year is required")
|
||||
}
|
||||
|
||||
// TODO: Extract is probably not used right
|
||||
if month == 0 {
|
||||
row, err = database.Query("SELECT * FROM transactions WHERE account = $1 AND EXTRACT(YEAR FROM time) = $2", account, year)
|
||||
row, err = database.Query("SELECT id, amount, debit, offset_account, account, date, description FROM transactions WHERE (account = $1 OR offset_account = $1) AND EXTRACT(YEAR FROM date) = $2", account, year)
|
||||
} else {
|
||||
row, err = database.Query("SELECT * FROM transactions WHERE account = $1 AND EXTRACT(YEAR FROM time) = $2 AND EXTRACT(MONTH FROM time) = $3", account, year, month)
|
||||
row, err = database.Query("SELECT id, amount, debit, offset_account, account, date, description FROM transactions WHERE (account = $1 OR offset_account = $1) AND EXTRACT(YEAR FROM date) = $2 AND EXTRACT(MONTH FROM date) = $3", account, year, month)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -306,7 +321,7 @@ func UpdateUser(database *sql.DB, user User) error {
|
||||
|
||||
}
|
||||
|
||||
func DeleteUser(database *sql.DB, id int) error {
|
||||
func DeleteUser(database *sql.DB, id string) error {
|
||||
_, err := database.Exec("DELETE FROM users WHERE id = $1", id)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -315,7 +330,7 @@ func DeleteUser(database *sql.DB, id int) error {
|
||||
|
||||
}
|
||||
|
||||
func GetUser(database *sql.DB, id int) (User, error) {
|
||||
func GetUser(database *sql.DB, id string) (User, error) {
|
||||
var user User
|
||||
err := database.QueryRow("SELECT * FROM users WHERE id = $1", id).Scan(&user.ID, &user.Name, &user.Password)
|
||||
if err != nil {
|
||||
@ -334,7 +349,7 @@ func GetUserByName(database *sql.DB, name string) (User, error) {
|
||||
|
||||
}
|
||||
|
||||
func AuthenicateUser(database *sql.DB, name string, password string) (User, error) {
|
||||
func AuthenticateUser(database *sql.DB, name string, password string) (User, error) {
|
||||
var user User
|
||||
err := database.QueryRow("SELECT * FROM users WHERE name = $1 AND password = $2", name, password).Scan(&user.ID, &user.Name, &user.Password)
|
||||
if err != nil {
|
||||
|
909
database/database_test.go
Normal file
909
database/database_test.go
Normal file
@ -0,0 +1,909 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ory/dockertest/v3"
|
||||
"github.com/ory/dockertest/v3/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
|
||||
pool, err := dockertest.NewPool("")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not construct pool: %s", err)
|
||||
}
|
||||
|
||||
err = pool.Client.Ping()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not connect to Docker: %s", err)
|
||||
}
|
||||
|
||||
// pulls an image, creates a container based on it and runs it
|
||||
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
||||
Repository: "postgres",
|
||||
Tag: "17",
|
||||
Env: []string{
|
||||
"POSTGRES_PASSWORD=secret",
|
||||
"POSTGRES_USER=user_name",
|
||||
"POSTGRES_DB=dbname",
|
||||
"listen_addresses = '*'",
|
||||
},
|
||||
}, func(config *docker.HostConfig) {
|
||||
// set AutoRemove to true so that stopped container goes away by itself
|
||||
config.AutoRemove = true
|
||||
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Could not start resource: %s", err)
|
||||
}
|
||||
|
||||
hostAndPort := resource.GetHostPort("5432/tcp")
|
||||
databaseUrl := fmt.Sprintf("postgres://user_name:secret@%s/dbname?sslmode=disable", hostAndPort)
|
||||
|
||||
log.Println("Connecting to database on url: ", databaseUrl)
|
||||
|
||||
resource.Expire(120) // Tell docker to hard kill the container in 120 seconds
|
||||
|
||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||
pool.MaxWait = 120 * time.Second
|
||||
if err = pool.Retry(func() error {
|
||||
db, err = sql.Open("pgx", databaseUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Ping()
|
||||
}); err != nil {
|
||||
log.Fatalf("Could not connect to docker: %s", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := pool.Purge(resource); err != nil {
|
||||
log.Fatalf("Could not purge resource: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// setup database
|
||||
|
||||
sqlFile, err := os.ReadFile("../database/bookholder.sql")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read sql file: %s", err)
|
||||
}
|
||||
|
||||
sqlStatements := strings.Split(string(sqlFile), ";")
|
||||
for _, statement := range sqlStatements {
|
||||
_, err = db.Exec(statement)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not create tables: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// load sample data
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES (1, 'Test Account', '1000.00')")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not insert sample data: %s", err)
|
||||
}
|
||||
|
||||
// run tests
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func cleanTables() {
|
||||
_, err := db.Exec("DELETE FROM transactions")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not clean tables: %s", err)
|
||||
}
|
||||
_, err = db.Exec("DELETE FROM accounts")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not clean tables: %s", err)
|
||||
}
|
||||
|
||||
_, err = db.Exec("DELETE FROM users")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not clean tables: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNewAccount(t *testing.T) {
|
||||
cleanTables()
|
||||
account := Account{ID: 1000, Name: "Test Account 2", Kind: "1000.00"}
|
||||
err := NewAccount(db, account)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if account was created
|
||||
var id uint
|
||||
err = db.QueryRow("SELECT id FROM accounts WHERE id = $1", account.ID).Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("Retrieved id:", id)
|
||||
t.Log("Expected id:", account.ID)
|
||||
assert.Equal(t, account.ID, id)
|
||||
}
|
||||
|
||||
func TestNewAccountAlreadyExists(t *testing.T) {
|
||||
cleanTables()
|
||||
iniAccount := Account{ID: 1, Name: "Test Account", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", iniAccount.ID, iniAccount.Name, iniAccount.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account := Account{ID: 1, Name: "Test Account 2", Kind: "1000.00"}
|
||||
err = NewAccount(db, account)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
t.Log("Error:", err)
|
||||
t.Log("Expected error: account already exists")
|
||||
assert.Equal(t, "account already exists", err.Error())
|
||||
}
|
||||
|
||||
func TestExistAccount(t *testing.T) {
|
||||
exists, err := existAccount(db, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Account exists:", exists)
|
||||
t.Log("Expected account exists: true")
|
||||
assert.True(t, exists)
|
||||
}
|
||||
|
||||
func TestExistAccountNotExists(t *testing.T) {
|
||||
exists, err := existAccount(db, 2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Account exists:", exists)
|
||||
t.Log("Expected account exists: false")
|
||||
assert.False(t, exists)
|
||||
}
|
||||
|
||||
func TestUpdateAccount(t *testing.T) {
|
||||
cleanTables()
|
||||
iniAccount := Account{ID: 3, Name: "Test Account", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", iniAccount.ID, iniAccount.Name, iniAccount.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account := Account{ID: 3, Name: "Test Account 3", Kind: "1000.00"}
|
||||
err = UpdateAccount(db, account)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if account was updated
|
||||
var name string
|
||||
err = db.QueryRow("SELECT name FROM accounts WHERE id = $1", account.ID).Scan(&name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("Retrieved name:", name)
|
||||
t.Log("Expected name:", account.Name)
|
||||
assert.Equal(t, account.Name, name)
|
||||
}
|
||||
|
||||
func TestUpdateAccountNotExists(t *testing.T) {
|
||||
account := Account{ID: 4, Name: "Test Account 4", Kind: "1000.00"}
|
||||
err := UpdateAccount(db, account)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
t.Log("Error:", err)
|
||||
t.Log("Expected error: account does not exist")
|
||||
assert.Equal(t, "account does not exist", err.Error())
|
||||
}
|
||||
|
||||
func TestDeleteAccount(t *testing.T) {
|
||||
cleanTables()
|
||||
account := Account{ID: 5, Name: "Test Account 5", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account.ID, account.Name, account.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = DeleteAccount(db, int(account.ID))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if account was deleted
|
||||
var id uint
|
||||
err = db.QueryRow("SELECT id FROM accounts WHERE id = $1", account.ID).Scan(&id)
|
||||
if err == nil {
|
||||
t.Error("account was not deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAccountNotExists(t *testing.T) {
|
||||
err := DeleteAccount(db, 6)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
t.Log("Error:", err)
|
||||
t.Log("Expected error: account does not exist")
|
||||
assert.Equal(t, "account does not exist", err.Error())
|
||||
}
|
||||
|
||||
func TestGetAccount(t *testing.T) {
|
||||
account := Account{ID: 7, Name: "Test Account 7", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account.ID, account.Name, account.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
acc, err := GetAccount(db, int(account.ID))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved account:", acc)
|
||||
t.Log("Expected account:", account)
|
||||
assert.Equal(t, account, acc)
|
||||
}
|
||||
|
||||
func TestGetAccountNotExists(t *testing.T) {
|
||||
_, err := GetAccount(db, 8)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
t.Log("Error:", err)
|
||||
t.Log("Expected error: account does not exist")
|
||||
assert.Equal(t, "account does not exist", err.Error())
|
||||
}
|
||||
|
||||
func TestExistTransaction(t *testing.T) {
|
||||
account1 := Account{ID: 9, Name: "Test Account 9", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 10, Name: "Test Account 10", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 0, Amount: 1234.56, Debit: true, OffsetAccount: 9, Account: 10, Date: time.Now()}
|
||||
_, err = db.Exec("INSERT INTO transactions (amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
exists, err := existTransaction(db, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Transaction exists:", exists)
|
||||
t.Log("Expected transaction exists: true")
|
||||
assert.True(t, exists)
|
||||
}
|
||||
|
||||
func TestExistTransactionNotExists(t *testing.T) {
|
||||
exists, err := existTransaction(db, 2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Transaction exists:", exists)
|
||||
t.Log("Expected transaction exists: false")
|
||||
assert.False(t, exists)
|
||||
}
|
||||
|
||||
func TestNewTransaction(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 11, Name: "Test Account 11", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 12, Name: "Test Account 12", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
err = NewTransaction(db, transaction)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var id uint
|
||||
err = db.QueryRow("SELECT id FROM transactions limit 1").Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if transaction was created
|
||||
var account uint
|
||||
err = db.QueryRow("SELECT account FROM transactions WHERE id = $1", id).Scan(&account)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("Retrieved id:", id)
|
||||
t.Log("Expected id:", transaction.ID)
|
||||
assert.Equal(t, transaction.Account, account)
|
||||
}
|
||||
|
||||
func TestNewTransactionAccountNotExists(t *testing.T) {
|
||||
transaction := Transaction{ID: 2, Amount: 1234.56, Debit: true, OffsetAccount: 13, Account: 14, Date: time.Now(), Description: "Test Transaction"}
|
||||
err := NewTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNewTransactionOffsetAccountNotExists(t *testing.T) {
|
||||
account := Account{ID: 15, Name: "Test Account 15", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account.ID, account.Name, account.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 3, Amount: 1234.56, Debit: true, OffsetAccount: 16, Account: account.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
err = NewTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNewTransactionOffsetAccountEqualsAccount(t *testing.T) {
|
||||
account := Account{ID: 17, Name: "Test Account 17", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account.ID, account.Name, account.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 4, Amount: 1234.56, Debit: true, OffsetAccount: account.ID, Account: account.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
err = NewTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNewTransactionAmountZero(t *testing.T) {
|
||||
account1 := Account{ID: 18, Name: "Test Account 18", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 19, Name: "Test Account 19", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 5, Amount: 0, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
err = NewTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNewTransactionDateZero(t *testing.T) {
|
||||
account1 := Account{ID: 20, Name: "Test Account 20", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 21, Name: "Test Account 21", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 6, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Time{}, Description: "Test Transaction"}
|
||||
err = NewTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUpdateTransaction(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 22, Name: "Test Account 22", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 23, Name: "Test Account 23", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
iniTransaction := Transaction{ID: 7, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
_, err = db.Exec("INSERT INTO transactions (id, amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6, $7)", iniTransaction.ID, iniTransaction.Amount, iniTransaction.Debit, iniTransaction.OffsetAccount, iniTransaction.Account, iniTransaction.Date, iniTransaction.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 7, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction 2"}
|
||||
err = UpdateTransaction(db, transaction)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if transaction was updated
|
||||
var description string
|
||||
err = db.QueryRow("SELECT description FROM transactions WHERE id = $1", transaction.ID).Scan(&description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("Retrieved description:", description)
|
||||
t.Log("Expected description:", transaction.Description)
|
||||
assert.Equal(t, transaction.Description, description)
|
||||
}
|
||||
|
||||
func TestUpdateTransactionNotExists(t *testing.T) {
|
||||
account1 := Account{ID: 24, Name: "Test Account 24", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 25, Name: "Test Account 25", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 8, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
err = UpdateTransaction(db, transaction)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDeleteTransaction(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 26, Name: "Test Account 26", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 27, Name: "Test Account 27", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 9, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
_, err = db.Exec("INSERT INTO transactions (id, amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6, $7)", transaction.ID, transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = DeleteTransaction(db, int(transaction.ID))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if transaction was deleted
|
||||
var id uint
|
||||
err = db.QueryRow("SELECT id FROM transactions WHERE id = $1", transaction.ID).Scan(&id)
|
||||
if err == nil {
|
||||
t.Error("transaction was not deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTransactionNotExists(t *testing.T) {
|
||||
err := DeleteTransaction(db, 10)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetTransaction(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 28, Name: "Test Account 28", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 29, Name: "Test Account 29", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: time.Now(), Description: "Test Transaction"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction.Amount, transaction.Debit, transaction.OffsetAccount, transaction.Account, transaction.Date, transaction.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var id int
|
||||
err = db.QueryRow("SELECT id FROM transactions limit 1").Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
result, err := GetTransaction(db, id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved transaction:", result.Amount)
|
||||
t.Log("Expected transaction:", transaction.Amount)
|
||||
assert.Equal(t, result.Amount, transaction.Amount)
|
||||
}
|
||||
|
||||
func TestGetTransactionNotExists(t *testing.T) {
|
||||
_, err := GetTransaction(db, 2)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetTransactionsAccountYear(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 30, Name: "Test Account 30", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 31, Name: "Test Account 31", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
date, _ := time.Parse("2006-01-02", "2024-01-01")
|
||||
|
||||
transaction1 := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 1"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction1.Amount, transaction1.Debit, transaction1.OffsetAccount, transaction1.Account, transaction1.Date, transaction1.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction2 := Transaction{ID: 2, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 2"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction2.Amount, transaction2.Debit, transaction2.OffsetAccount, transaction2.Account, transaction2.Date, transaction2.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transactions, err := GetTransactions(db, 31, 2024, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved transactions:", transactions)
|
||||
t.Log("Expected transactions:", []Transaction{transaction1, transaction2})
|
||||
assert.Equal(t, transaction1.Amount, transactions[0].Amount)
|
||||
assert.Equal(t, transaction2.Amount, transactions[1].Amount)
|
||||
}
|
||||
|
||||
func TestGetTransactionsOfffsetAccountYear(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 30, Name: "Test Account 30", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 31, Name: "Test Account 31", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
date, _ := time.Parse("2006-01-02", "2024-01-01")
|
||||
|
||||
transaction1 := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 1"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction1.Amount, transaction1.Debit, transaction1.OffsetAccount, transaction1.Account, transaction1.Date, transaction1.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction2 := Transaction{ID: 2, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 2"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction2.Amount, transaction2.Debit, transaction2.OffsetAccount, transaction2.Account, transaction2.Date, transaction2.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transactions, err := GetTransactions(db, 30, 2024, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved transactions:", transactions)
|
||||
t.Log("Expected transactions:", []Transaction{transaction1, transaction2})
|
||||
assert.Equal(t, transaction1.Amount, transactions[0].Amount)
|
||||
assert.Equal(t, transaction2.Amount, transactions[1].Amount)
|
||||
}
|
||||
|
||||
func TestGetTransactionsAccountYearMonth(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 30, Name: "Test Account 30", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 31, Name: "Test Account 31", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
date, _ := time.Parse("2006-01-02", "2024-01-01")
|
||||
|
||||
transaction1 := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 1"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction1.Amount, transaction1.Debit, transaction1.OffsetAccount, transaction1.Account, transaction1.Date, transaction1.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction2 := Transaction{ID: 2, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 2"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction2.Amount, transaction2.Debit, transaction2.OffsetAccount, transaction2.Account, transaction2.Date, transaction2.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transactions, err := GetTransactions(db, 31, 2024, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved transactions:", transactions)
|
||||
t.Log("Expected transactions:", []Transaction{transaction1, transaction2})
|
||||
assert.Equal(t, transaction1.Amount, transactions[0].Amount)
|
||||
assert.Equal(t, transaction2.Amount, transactions[1].Amount)
|
||||
}
|
||||
|
||||
func TestGetTransactionsOfffsetAccountYearMonth(t *testing.T) {
|
||||
cleanTables()
|
||||
account1 := Account{ID: 30, Name: "Test Account 30", Kind: "1000.00"}
|
||||
_, err := db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account1.ID, account1.Name, account1.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
account2 := Account{ID: 31, Name: "Test Account 31", Kind: "1000.00"}
|
||||
_, err = db.Exec("INSERT INTO accounts (id, name, kind) VALUES ($1, $2, $3)", account2.ID, account2.Name, account2.Kind)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
date, _ := time.Parse("2006-01-02", "2024-01-01")
|
||||
|
||||
transaction1 := Transaction{ID: 1, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 1"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction1.Amount, transaction1.Debit, transaction1.OffsetAccount, transaction1.Account, transaction1.Date, transaction1.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transaction2 := Transaction{ID: 2, Amount: 1234.56, Debit: true, OffsetAccount: account1.ID, Account: account2.ID, Date: date, Description: "Test Transaction 2"}
|
||||
_, err = db.Exec("INSERT INTO transactions ( amount, debit, offset_account, account, date, description) VALUES ($1, $2, $3, $4, $5, $6)", transaction2.Amount, transaction2.Debit, transaction2.OffsetAccount, transaction2.Account, transaction2.Date, transaction2.Description)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
transactions, err := GetTransactions(db, 30, 2024, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved transactions:", transactions)
|
||||
t.Log("Expected transactions:", []Transaction{transaction1, transaction2})
|
||||
assert.Equal(t, transaction1.Amount, transactions[0].Amount)
|
||||
assert.Equal(t, transaction2.Amount, transactions[1].Amount)
|
||||
}
|
||||
|
||||
func TestNewUser(t *testing.T) {
|
||||
cleanTables()
|
||||
user := User{ID: "1", Name: "Test User", Password: "password"}
|
||||
err := NewUser(db, user)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if user was created
|
||||
var Name string
|
||||
err = db.QueryRow("SELECT name FROM users WHERE name = $1", user.Name).Scan(&Name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved id:", Name)
|
||||
t.Log("Expected id:", user.Name)
|
||||
assert.Equal(t, user.Name, Name)
|
||||
}
|
||||
|
||||
func TestNewUserAlreadyExists(t *testing.T) {
|
||||
cleanTables()
|
||||
user1 := User{ID: "", Name: "Test User", Password: "password"}
|
||||
err := NewUser(db, user1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
user2 := User{ID: "", Name: "Test User", Password: "password"}
|
||||
err = NewUser(db, user2)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestUpdateUser(t *testing.T) {
|
||||
cleanTables()
|
||||
iniUser := User{ID: "2", Name: "Test User", Password: "password"}
|
||||
_, err := db.Exec("INSERT INTO users ( name, password) VALUES ($1, $2)", iniUser.Name, iniUser.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var id string
|
||||
err = db.QueryRow("SELECT id FROM users WHERE name = $1", iniUser.Name).Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
user := User{ID: id, Name: "Test User 2", Password: "password"}
|
||||
err = UpdateUser(db, user)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if user was updated
|
||||
var name string
|
||||
err = db.QueryRow("SELECT name FROM users WHERE name = $1", user.Name).Scan(&name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved name:", name)
|
||||
t.Log("Expected name:", user.Name)
|
||||
assert.Equal(t, user.Name, name)
|
||||
}
|
||||
|
||||
func TestDeleteUser(t *testing.T) {
|
||||
cleanTables()
|
||||
user := User{ID: "3", Name: "Test User", Password: "password"}
|
||||
_, err := db.Exec("INSERT INTO users ( name, password) VALUES ($1, $2)", user.Name, user.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var id string
|
||||
err = db.QueryRow("SELECT id FROM users WHERE name = $1", user.Name).Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = DeleteUser(db, id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// check if user was deleted
|
||||
err = db.QueryRow("SELECT id FROM users WHERE id = $1", user.ID).Scan(&id)
|
||||
if err == nil {
|
||||
t.Error("user was not deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUser(t *testing.T) {
|
||||
cleanTables()
|
||||
user := User{ID: "4", Name: "Test User", Password: "password"}
|
||||
_, err := db.Exec("INSERT INTO users ( name, password) VALUES ($1, $2)", user.Name, user.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var id string
|
||||
err = db.QueryRow("SELECT id FROM users WHERE name = $1", user.Name).Scan(&id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
result, err := GetUser(db, id)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved user:", result.Name)
|
||||
t.Log("Expected user:", user.Name)
|
||||
assert.Equal(t, user.Name, result.Name)
|
||||
}
|
||||
|
||||
func TestGetUserNotExists(t *testing.T) {
|
||||
_, err := GetUser(db, "5")
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetUserByName(t *testing.T) {
|
||||
cleanTables()
|
||||
user := User{ID: "6", Name: "Test User", Password: "password"}
|
||||
_, err := db.Exec("INSERT INTO users ( name, password) VALUES ($1, $2)", user.Name, user.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
result, err := GetUserByName(db, user.Name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved user:", result.Name)
|
||||
t.Log("Expected user:", user.Name)
|
||||
assert.Equal(t, user.Name, result.Name)
|
||||
}
|
||||
|
||||
func TestGetUserByNameNotExists(t *testing.T) {
|
||||
_, err := GetUserByName(db, "SHITTY USER NAME")
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestAuthenticateUser(t *testing.T) {
|
||||
cleanTables()
|
||||
user := User{ID: "7", Name: "Test User", Password: "password"}
|
||||
_, err := db.Exec("INSERT INTO users ( name, password) VALUES ($1, $2)", user.Name, user.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
result, err := AuthenticateUser(db, user.Name, user.Password)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Retrieved user:", result.Name)
|
||||
t.Log("Expected user:", user.Name)
|
||||
assert.Equal(t, user.Name, result.Name)
|
||||
}
|
@ -19,7 +19,7 @@ type Transaction struct {
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uint
|
||||
ID string
|
||||
Name string
|
||||
Password string
|
||||
}
|
||||
|
27
go.mod
27
go.mod
@ -5,19 +5,32 @@ go 1.22.1
|
||||
require github.com/joho/godotenv v1.5.1
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.1 // indirect
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
|
||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
|
||||
github.com/bytedance/sonic v1.12.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/containerd/continuity v0.4.5 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/docker/cli v27.4.1+incompatible // indirect
|
||||
github.com/docker/docker v27.4.1+incompatible // indirect
|
||||
github.com/docker/go-connections v0.5.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/gin-gonic/gin v1.10.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.23.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
|
||||
github.com/goccy/go-json v0.10.4 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.2 // indirect
|
||||
@ -27,13 +40,26 @@ require (
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/moby/sys/user v0.3.0 // indirect
|
||||
github.com/moby/term v0.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.0 // indirect
|
||||
github.com/opencontainers/runc v1.2.3 // indirect
|
||||
github.com/ory/dockertest/v3 v3.11.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
||||
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
|
||||
golang.org/x/arch v0.12.0 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
@ -41,5 +67,6 @@ require (
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.36.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
86
go.sum
86
go.sum
@ -1,15 +1,35 @@
|
||||
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
|
||||
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
|
||||
github.com/bytedance/sonic v1.12.6 h1:/isNmCUF2x3Sh8RAp/4mh4ZGkcFAX/hLrzrK3AvpRzk=
|
||||
github.com/bytedance/sonic v1.12.6/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E=
|
||||
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/containerd/continuity v0.4.5 h1:ZRoN1sXq9u7V6QoHMcVWGhOwDFqZ4B9i5H6un1Wh0x4=
|
||||
github.com/containerd/continuity v0.4.5/go.mod h1:/lNJvtJKUQStBzpVQ1+rasXO1LAWtUQssk28EZvJ3nE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/docker/cli v27.4.1+incompatible h1:VzPiUlRJ/xh+otB75gva3r05isHMo5wXDfPRi5/b4hI=
|
||||
github.com/docker/cli v27.4.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
||||
github.com/docker/docker v27.4.1+incompatible h1:ZJvcY7gfwHn1JF48PfbyXg7Jyt9ZCWDW+GGXOIxEwp4=
|
||||
github.com/docker/docker v27.4.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
|
||||
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
|
||||
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
@ -22,11 +42,17 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
|
||||
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
@ -39,6 +65,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
@ -49,15 +77,35 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/sys/user v0.3.0 h1:9ni5DlcW5an3SvRSx4MouotOygvzaXbaSrc/wGDFWPo=
|
||||
github.com/moby/sys/user v0.3.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
|
||||
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
|
||||
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
|
||||
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
|
||||
github.com/opencontainers/runc v1.2.3 h1:fxE7amCzfZflJO2lHXf4y/y8M1BoAqp+FVmG19oYB80=
|
||||
github.com/opencontainers/runc v1.2.3/go.mod h1:nSxcWUydXrsBZVYNSkTjoQ/N6rcyTtn+1SD5D4+kRIM=
|
||||
github.com/ory/dockertest/v3 v3.11.0 h1:OiHcxKAvSDUwsEVh2BjxQQc/5EHz9n0va9awCtNGuyA=
|
||||
github.com/ory/dockertest/v3 v3.11.0/go.mod h1:VIPxS1gwT9NpPOrfD3rACs8Y9Z7yhzO4SB194iUDnUI=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
@ -73,22 +121,60 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg=
|
||||
golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
|
||||
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -37,7 +37,7 @@ func getAccount(c *gin.Context) {
|
||||
acc, err := database.GetAccount(Database, idInt)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "internal server error",
|
||||
"message": "internal server error" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ func createUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if user.ID != 0 {
|
||||
if user.ID != "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "User already exists"})
|
||||
return
|
||||
}
|
||||
@ -72,7 +72,7 @@ func authenticateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if userFound.ID == 0 {
|
||||
if userFound.ID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "User not found"})
|
||||
return
|
||||
}
|
||||
@ -145,14 +145,14 @@ func checkAuth(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := database.GetUser(Database, int(claims["id"].(float64)))
|
||||
user, err := database.GetUser(Database, string(claims["id"].(string)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not found"})
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if user.ID == 0 {
|
||||
if user.ID == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not found"})
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user