2024-01-01 22:18:43 -08:00
|
|
|
package debug_mode
|
2023-12-31 17:21:09 -08:00
|
|
|
|
|
|
|
import (
|
2024-01-11 20:07:47 -08:00
|
|
|
"encoding/json"
|
2024-02-27 18:38:00 -08:00
|
|
|
"nickiel.net/recount_server/types"
|
2024-01-11 20:07:47 -08:00
|
|
|
"os"
|
2024-01-01 22:18:43 -08:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/rs/zerolog"
|
2024-01-01 20:47:34 -08:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-12-31 17:21:09 -08:00
|
|
|
)
|
|
|
|
|
2024-01-01 22:18:43 -08:00
|
|
|
func SetLogLevel(level zerolog.Level) {
|
2024-01-11 20:07:47 -08:00
|
|
|
zerolog.SetGlobalLevel(level)
|
2024-01-01 22:18:43 -08:00
|
|
|
}
|
|
|
|
|
2023-12-31 17:21:09 -08:00
|
|
|
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
|
2024-01-01 22:18:43 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
cwd, err := os.Getwd()
|
2024-01-11 20:07:47 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Could not get current working directory")
|
|
|
|
} else {
|
2024-02-27 18:38:00 -08:00
|
|
|
cwd = cwd + "/"
|
2024-01-11 20:07:47 -08:00
|
|
|
log.Trace().Msgf("Currect working directory is: %s", cwd)
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
_, err = os.Stat(cwd + DB_CONNECTION_STRING)
|
2024-01-11 20:07:47 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msg("No existing test.db file found")
|
2024-02-08 18:11:18 -08:00
|
|
|
} else {
|
2024-02-27 18:38:00 -08:00
|
|
|
log.Debug().Msg("Found existing test.db file. Attempting to delete")
|
|
|
|
err = os.Remove(cwd + DB_CONNECTION_STRING)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Failed to delete testing db. Continueing anyways")
|
|
|
|
} else {
|
|
|
|
log.Debug().Msg("Deleted test.db file successfully")
|
|
|
|
}
|
2024-01-11 20:07:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 := `
|
2023-12-31 17:21:09 -08:00
|
|
|
CREATE TABLE accounts (
|
2024-01-01 20:59:23 -08:00
|
|
|
acnt_id Integer PRIMARY KEY,
|
2024-02-27 17:35:33 -08:00
|
|
|
acnt_display_name varchar(50) NOT NULL,
|
2024-01-01 20:59:23 -08:00
|
|
|
acnt_description varchar(250) NULL
|
2023-12-31 17:21:09 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE buckets (
|
2024-01-01 20:59:23 -08:00
|
|
|
bkt_id Integer PRIMARY KEY,
|
2024-02-27 17:35:33 -08:00
|
|
|
bkt_display_code varchar(5) NOT NULL,
|
|
|
|
bkt_display_name varchar(50) NULL,
|
2024-01-01 20:59:23 -08:00
|
|
|
bkt_description varchar(250) NULL
|
2023-12-31 17:21:09 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE transactions (
|
2024-01-01 20:59:23 -08:00
|
|
|
trns_id Integer PRIMARY KEY,
|
2023-12-31 17:21:09 -08:00
|
|
|
trns_amount money NOT NULL,
|
|
|
|
trns_description varchar(250) NULL,
|
|
|
|
trns_account int4 NOT NULL,
|
|
|
|
trns_bucket int4 NULL,
|
|
|
|
trns_date Date not null,
|
2024-01-01 20:59:23 -08:00
|
|
|
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
|
2023-12-31 17:21:09 -08:00
|
|
|
);
|
|
|
|
|
2024-01-01 20:59:23 -08:00
|
|
|
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
|
|
|
|
);
|
2023-12-31 17:21:09 -08:00
|
|
|
|
|
|
|
CREATE TABLE transaction_categories (
|
2024-01-01 20:59:23 -08:00
|
|
|
trns_ctgry_id Integer PRIMARY KEY,
|
2024-02-27 17:35:33 -08:00
|
|
|
trns_ctgry_display_code varchar(5) NOT NULL,
|
|
|
|
trns_ctgry_display_name varchar(50) NOT NULL,
|
2024-01-01 20:59:23 -08:00
|
|
|
trns_ctgry_description varchar(250) NULL
|
2023-12-31 17:21:09 -08:00
|
|
|
);
|
|
|
|
|
2024-02-27 17:35:33 -08:00
|
|
|
INSERT INTO accounts (acnt_display_name, acnt_description) VALUES ("BECU Saving", "Savings Account");
|
|
|
|
INSERT INTO buckets (bkt_display_code, bkt_display_name, bkt_description) VALUES
|
2024-01-01 22:18:43 -08:00
|
|
|
("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");
|
2023-12-31 17:21:09 -08:00
|
|
|
`
|
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
tx := db.MustBegin()
|
|
|
|
tx.MustExec(init_sql)
|
2023-12-31 17:21:09 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
err = tx.Commit()
|
2023-12-31 17:21:09 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not commit transaction")
|
|
|
|
}
|
2023-12-31 17:21:09 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
jsonExample := `{
|
2024-01-01 22:18:43 -08:00
|
|
|
"Id": 3,
|
|
|
|
"Amount": "100",
|
|
|
|
"Description": {
|
|
|
|
"String": "Transaction 3",
|
|
|
|
"Valid": true
|
|
|
|
},
|
|
|
|
"Account": 1,
|
|
|
|
"Bucket": {
|
|
|
|
"Int64": 1,
|
|
|
|
"Valid": true
|
|
|
|
},
|
|
|
|
"TransactionDate": "2023-11-11T00:00:00Z"
|
|
|
|
}`
|
|
|
|
|
2024-01-15 19:26:51 -08:00
|
|
|
var trns types.Transaction = types.Transaction{}
|
2024-01-11 20:07:47 -08:00
|
|
|
err = json.Unmarshal([]byte(jsonExample), &trns)
|
2024-01-01 22:18:43 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("could not unmarshal")
|
|
|
|
}
|
2023-12-31 17:21:09 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
_, 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)
|
2024-01-01 22:18:43 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
log.Debug().Msg("Test database initialized")
|
2023-12-31 17:21:09 -08:00
|
|
|
|
|
|
|
}
|