Recount-Server/tests/testdb.go

161 lines
4.5 KiB
Go

package debug_mode
import (
"os"
"encoding/json"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type Transaction struct {
Id int `db:"trns_id" json:"Id"`
Amount string `db:"trns_amount" json:"Amount"`
Description sql.NullString `db:"trns_description" json:"Description"`
Account int `db:"trns_account" json:"Account"`
Bucket sql.NullInt64 `db:"trns_bucket" json:"Bucket"`
Date time.Time `db:"trns_date" json:"TransactionDate"`
}
func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("Could not get current working directory")
} else {
log.Trace().Msgf("Currect working directory is: %s", cwd)
}
_, err = os.Stat(cwd + DB_CONNECTION_STRING)
if err != nil {
log.Debug().Msg("Found existing test.db file. Attempting to delete")
err = os.Remove(DB_CONNECTION_STRING)
if err != nil {
log.Fatal().Err(err).Msg("Failed to delete testing db")
} else {
log.Debug().Msg("Deleted test.db file successfully")
}
} else {
log.Debug().Msg("No existing test.db file found")
}
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
log.Fatal().
Err(err).
Msg("Couldn't open test db")
}
defer db.Close()
init_sql := `
CREATE TABLE accounts (
acnt_id Integer PRIMARY KEY,
acnt_dsply_name varchar(50) NOT NULL,
acnt_description varchar(250) NULL
);
CREATE TABLE buckets (
bkt_id Integer PRIMARY KEY,
bkt_dsply_code varchar(5) NOT NULL,
bkt_dsply_name varchar(50) NULL,
bkt_description varchar(250) NULL
);
CREATE TABLE transactions (
trns_id Integer PRIMARY KEY,
trns_amount money NOT NULL,
trns_description varchar(250) NULL,
trns_account int4 NOT NULL,
trns_bucket int4 NULL,
trns_date Date not null,
FOREIGN KEY (trns_account)
REFERENCES accounts (acnt_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_bucket)
REFERENCES buckets (bkt_id)
ON UPDATE CASCADE ON DELETE SET NULL
);
CREATE TABLE transaction_breakdown (
trns_brkdwn_id Integer PRIMARY KEY,
trns_brkdwn_amount money NOT NULL,
trns_brkdwn_parent_transaction int4 NOT NULL,
trns_brkdwn_catagory int4 NULL,
trns_brkdwn_bucket int4 NULL,
FOREIGN KEY (trns_brkdwn_parent_transaction)
REFERENCES transactions (trns_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_brkdwn_catagory)
REFERENCES transaction_categories(trns_ctgry_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_brkdwn_bucket)
REFERENCES buckets (bkt_id)
ON UPDATE CASCADE ON DELETE SET NULL
);
CREATE TABLE transaction_categories (
trns_ctgry_id Integer PRIMARY KEY,
trns_ctgry_dsply_code varchar(5) NOT NULL,
trns_ctgry_dsply_name varchar(50) NOT NULL,
trns_ctgry_description varchar(250) NULL
);
INSERT INTO accounts (acnt_dsply_name, acnt_description) VALUES ("BECU Saving", "Savings Account");
INSERT INTO buckets (bkt_dsply_code, bkt_dsply_name, bkt_description) VALUES
("SVNGS", "Savings", "Savings Bucket");
INSERT INTO transactions (trns_amount, trns_description, trns_account, trns_bucket, trns_date) VALUES
("50.00", "Money", 1, 1, "2023-11-10");
`
tx := db.MustBegin()
tx.MustExec(init_sql)
err = tx.Commit()
if err != nil {
log.Fatal().
Err(err).
Msg("Could not commit transaction")
}
jsonExample := `{
"Id": 3,
"Amount": "100",
"Description": {
"String": "Transaction 3",
"Valid": true
},
"Account": 1,
"Bucket": {
"Int64": 1,
"Valid": true
},
"TransactionDate": "2023-11-11T00:00:00Z"
}`
var trns Transaction = Transaction{}
err = json.Unmarshal([]byte(jsonExample), &trns)
if err != nil {
log.Fatal().Err(err).Msg("could not unmarshal")
}
_, err = db.NamedExec("INSERT INTO transactions" +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
trns)
log.Debug().Msg("Test database initialized")
}