From ab1804176c4da7c0065466e70c601e9bea966bd2 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Tue, 21 Jan 2020 10:52:44 +0100 Subject: [PATCH 01/21] Transaction History Store --- BankAccount/pkg/store/transactionhistory.go | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 72440ea..113e153 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -1 +1,46 @@ package store + +import ( + "bankacc/pkg/config" + "bankacc/pkg/entities" + "database/sql" +) + +type TransactionHistoryStore interface { + Insert() +} + +type TransactionHistoryModel struct { + Db *sql.DB +} + +func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { + return &TransactionHistoryModel{ + Db: db, + } +} + +func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount float64, Action string, Created string) (*entities.TransactionHistory, error) { + db, err := config.GetMySQLDB() + if err != nil { + return nil, err + } else { + TransactionModel := TransactionHistoryModel{ + Db: db, + } + + transaction := entities.TransactionHistory{ + UserId: UserId, + AccountId: AccountId, + Amount: Amount, + Action: Action, + Created: Created, + } + _, err := TransactionModel.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, Created) + + if err != nil { + return nil, err + } + return &transaction, nil + } +} From d2152e5276f3eb8f571ab358974ae68aa0fefa5c Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Tue, 21 Jan 2020 11:17:04 +0100 Subject: [PATCH 02/21] Transaction History Store Insert --- BankAccount/pkg/store/transactionhistory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 113e153..c70f646 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -7,7 +7,7 @@ import ( ) type TransactionHistoryStore interface { - Insert() + Insert(UserId int, AccountId int, Amount float64, Action string, Created string) (*entities.TransactionHistory, error) } type TransactionHistoryModel struct { From a89ce212f530aa77a0dc29a9fcb7c202da56f84a Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Tue, 21 Jan 2020 13:37:26 +0100 Subject: [PATCH 03/21] Transaction History Store GetTransactionsById --- BankAccount/pkg/store/transactionhistory.go | 60 +++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index c70f646..05f4de3 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -7,7 +7,9 @@ import ( ) type TransactionHistoryStore interface { - Insert(UserId int, AccountId int, Amount float64, Action string, Created string) (*entities.TransactionHistory, error) + Insert(UserId int, AccountId int, Amount float64, Action string, CreatedAt string) (*entities.TransactionHistory, error) + GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) + GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) } type TransactionHistoryModel struct { @@ -20,7 +22,7 @@ func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { } } -func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount float64, Action string, Created string) (*entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount float64, Action string, CreatedAt string) (*entities.TransactionHistory, error) { db, err := config.GetMySQLDB() if err != nil { return nil, err @@ -34,9 +36,9 @@ func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount f AccountId: AccountId, Amount: Amount, Action: Action, - Created: Created, + CreatedAt: CreatedAt, } - _, err := TransactionModel.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, Created) + _, err := TransactionModel.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, CreatedAt) if err != nil { return nil, err @@ -44,3 +46,53 @@ func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount f return &transaction, nil } } + +func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) { + var transactions []entities.TransactionHistory + db, err := config.GetMySQLDB() + if err != nil { + return nil, err + } else { + TransactionModel := TransactionHistoryModel{ + Db: db, + } + result, err := TransactionModel.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ?", Id) + if err != nil { + return nil, err + } + var transaction entities.TransactionHistory + for result.Next() { + err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) + if err != nil { + return nil, err + } + transactions = append(transactions, transaction) + } + return &transactions, nil + } +} + +func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) { + var transactions []entities.TransactionHistory + db, err := config.GetMySQLDB() + if err != nil { + return nil, err + } else { + TransactionModel := TransactionHistoryModel{ + Db: db, + } + result, err := TransactionModel.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ? and created_at between ? and ?", Id, FromDate, ToDate) + if err != nil { + return nil, err + } + var transaction entities.TransactionHistory + for result.Next() { + err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) + if err != nil { + return nil, err + } + transactions = append(transactions, transaction) + } + return &transactions, nil + } +} From 2bf4a20db3f522f6885004ae056cad9fe888631c Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Tue, 21 Jan 2020 13:49:48 +0100 Subject: [PATCH 04/21] Formatted code and updated variables --- BankAccount/pkg/store/transactionhistory.go | 74 +++++++-------------- 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 05f4de3..8cf1691 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -1,7 +1,6 @@ package store import ( - "bankacc/pkg/config" "bankacc/pkg/entities" "database/sql" ) @@ -23,76 +22,51 @@ func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { } func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount float64, Action string, CreatedAt string) (*entities.TransactionHistory, error) { - db, err := config.GetMySQLDB() + transaction := entities.TransactionHistory{ + UserId: UserId, + AccountId: AccountId, + Amount: Amount, + Action: Action, + CreatedAt: CreatedAt, + } + _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, CreatedAt) + if err != nil { return nil, err - } else { - TransactionModel := TransactionHistoryModel{ - Db: db, - } - - transaction := entities.TransactionHistory{ - UserId: UserId, - AccountId: AccountId, - Amount: Amount, - Action: Action, - CreatedAt: CreatedAt, - } - _, err := TransactionModel.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, CreatedAt) - - if err != nil { - return nil, err - } - return &transaction, nil } + return &transaction, nil } func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - db, err := config.GetMySQLDB() + result, err := store.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ?", Id) if err != nil { return nil, err - } else { - TransactionModel := TransactionHistoryModel{ - Db: db, - } - result, err := TransactionModel.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ?", Id) + } + var transaction entities.TransactionHistory + for result.Next() { + err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) if err != nil { return nil, err } - var transaction entities.TransactionHistory - for result.Next() { - err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) - if err != nil { - return nil, err - } - transactions = append(transactions, transaction) - } - return &transactions, nil + transactions = append(transactions, transaction) } + return &transactions, nil } func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - db, err := config.GetMySQLDB() + result, err := store.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ? and created_at between ? and ?", Id, FromDate, ToDate) if err != nil { return nil, err - } else { - TransactionModel := TransactionHistoryModel{ - Db: db, - } - result, err := TransactionModel.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ? and created_at between ? and ?", Id, FromDate, ToDate) + } + var transaction entities.TransactionHistory + for result.Next() { + err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) if err != nil { return nil, err } - var transaction entities.TransactionHistory - for result.Next() { - err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) - if err != nil { - return nil, err - } - transactions = append(transactions, transaction) - } - return &transactions, nil + transactions = append(transactions, transaction) } + return &transactions, nil } From b2b953cf918dbfded493055fa26d88186dfc51cd Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Tue, 21 Jan 2020 14:11:32 +0100 Subject: [PATCH 05/21] Updated queries --- BankAccount/pkg/store/transactionhistory.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 8cf1691..5f2d494 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -39,7 +39,7 @@ func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount f func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ?", Id) + result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", Id) if err != nil { return nil, err } @@ -56,7 +56,7 @@ func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.T func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM BankAccount.TransactionHistory WHERE user_id = ? and created_at between ? and ?", Id, FromDate, ToDate) + result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", Id, FromDate, ToDate) if err != nil { return nil, err } From d321ce5a1a247b960c3683bac146799fbf41ae6f Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 22 Jan 2020 09:36:20 +0100 Subject: [PATCH 06/21] Renaming function arguments to GO convention --- BankAccount/pkg/store/transactionhistory.go | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 5f2d494..5a6ab39 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -6,9 +6,9 @@ import ( ) type TransactionHistoryStore interface { - Insert(UserId int, AccountId int, Amount float64, Action string, CreatedAt string) (*entities.TransactionHistory, error) - GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) - GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) + Insert(userId int, accountId int, amount float64, action string, createdAt string) (*entities.TransactionHistory, error) + GetTransactionsById(id int) (*[]entities.TransactionHistory, error) + GetTransactionsByIdFromToDate(id int, fromDate string, toDate string) (*[]entities.TransactionHistory, error) } type TransactionHistoryModel struct { @@ -21,15 +21,15 @@ func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { } } -func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount float64, Action string, CreatedAt string) (*entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string, createdAt string) (*entities.TransactionHistory, error) { transaction := entities.TransactionHistory{ - UserId: UserId, - AccountId: AccountId, - Amount: Amount, - Action: Action, - CreatedAt: CreatedAt, + UserId: userId, + AccountId: accountId, + Amount: amount, + Action: action, + CreatedAt: createdAt, } - _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", UserId, AccountId, Amount, Action, CreatedAt) + _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, createdAt) if err != nil { return nil, err @@ -37,9 +37,9 @@ func (store *TransactionHistoryModel) Insert(UserId int, AccountId int, Amount f return &transaction, nil } -func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) GetTransactionsById(id int) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", Id) + result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", id) if err != nil { return nil, err } @@ -54,9 +54,9 @@ func (store *TransactionHistoryModel) GetTransactionsById(Id int) (*[]entities.T return &transactions, nil } -func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(Id int, FromDate string, ToDate string) (*[]entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate string, toDate string) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", Id, FromDate, ToDate) + result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", id, fromDate, toDate) if err != nil { return nil, err } From 9cbeffa252648140123aebcc71ce5d278ef45eee Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 22 Jan 2020 10:36:54 +0100 Subject: [PATCH 07/21] Resolved variable types --- BankAccount/pkg/store/transactionhistory.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index 5a6ab39..ea8eaa1 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -3,12 +3,13 @@ package store import ( "bankacc/pkg/entities" "database/sql" + "time" ) type TransactionHistoryStore interface { - Insert(userId int, accountId int, amount float64, action string, createdAt string) (*entities.TransactionHistory, error) + Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) GetTransactionsById(id int) (*[]entities.TransactionHistory, error) - GetTransactionsByIdFromToDate(id int, fromDate string, toDate string) (*[]entities.TransactionHistory, error) + GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) (*[]entities.TransactionHistory, error) } type TransactionHistoryModel struct { @@ -21,15 +22,15 @@ func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { } } -func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string, createdAt string) (*entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) { transaction := entities.TransactionHistory{ UserId: userId, AccountId: accountId, Amount: amount, Action: action, - CreatedAt: createdAt, + CreatedAt: time.Now(), } - _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, createdAt) + _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, time.Now().Format("2006-01-02T15:04:05")) if err != nil { return nil, err @@ -54,7 +55,7 @@ func (store *TransactionHistoryModel) GetTransactionsById(id int) (*[]entities.T return &transactions, nil } -func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate string, toDate string) (*[]entities.TransactionHistory, error) { +func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) (*[]entities.TransactionHistory, error) { var transactions []entities.TransactionHistory result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", id, fromDate, toDate) if err != nil { From 3ad66c0c1c651d9b2358b91201e0d5000803d953 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 22 Jan 2020 11:14:36 +0100 Subject: [PATCH 08/21] Added time.Now() in a variable --- BankAccount/pkg/store/transactionhistory.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index ea8eaa1..a76391c 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -23,14 +23,15 @@ func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { } func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) { + now := time.Now() transaction := entities.TransactionHistory{ UserId: userId, AccountId: accountId, Amount: amount, Action: action, - CreatedAt: time.Now(), + CreatedAt: now, } - _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, time.Now().Format("2006-01-02T15:04:05")) + _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, now) if err != nil { return nil, err From 65fd5427deae26e74bf0de9afd21c40c59431f58 Mon Sep 17 00:00:00 2001 From: Filip Date: Wed, 22 Jan 2020 16:33:49 +0100 Subject: [PATCH 09/21] account store --- BankAccount/pkg/store/account.go | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 BankAccount/pkg/store/account.go diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go new file mode 100644 index 0000000..03beb03 --- /dev/null +++ b/BankAccount/pkg/store/account.go @@ -0,0 +1,78 @@ +package store + +import ( + "database/sql" + "time" + + "bankacc/pkg/entities" +) + +type AccountStore interface { + InsertAccount(userId int, balance float64, currency string, createdAt time.Time, updatedAt time.Time) (*entities.Account, error) + GetAccountsByUserId(userId int) (*[]entities.Account, error) + UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.User, error) + CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) +} + +type AccountModel struct { + Db *sql.DB +} + +func NewAccountStoreModel(db *sql.DB) *AccountModel { + return &AccountModel{ + Db: db, + } +} + +func (store *AccountModel) InsertAccount(userId int, balance float64, currency string, createdAt time.Time, updatedAt time.Time) (*entities.Account, error) { + createdAt = time.Now() + updatedAt = time.Now() + account := entities.Account{ + UserId: userId, + Balance: balance, + Currency: currency, + Status: true, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + } + _, err := store.Db.Exec("INSERT INTO Account (user_id, balance, currency, status, created_at, updated_at) VALUES(?, ?, ?, 1, ?, ?)", userId, balance, currency, createdAt, updatedAt) + if err != nil { + return nil, err + } + return &account, nil +} + +func (store *AccountModel) GetAccountsByUserId(userId int) (*[]entities.Account, error) { + var accounts []entities.Account + result, err := store.Db.Query("SELECT * FROM BankAccount.Account WHERE user_id=?", userId) + if err != nil { + return nil, err + } + var account entities.Account + for result.Next() { + err := result.Scan(&account.Id, &account.UserId, &account.Balance, &account.Currency, &account.Status, &account.CreatedAt, &account.UpdatedAt) + if err != nil { + return nil, err + } + accounts = append(accounts, account) + } + return &accounts, nil +} + +func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.User, error) { + updatedAt = time.Now() + _, err := store.Db.Exec("UPDATE BankAccount.Account SET balance = ?, currency =?, updated_at = ? WHERE user_id = ? AND id =?", balance, currency, updatedAt, userId, id) + if err != nil { + return nil, err + } + return nil, err +} + +func (store *AccountModel) CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) { + updatedAt = time.Now() + _, err := store.Db.Exec("UPDATE BankAccount.Account SET status = 0, updated_at = ? WHERE user_id = ? AND id = ?", updatedAt, userId, id) + if err != nil { + return nil, err + } + return nil, err +} From 845e94395eac36461c86c324a0bd57d6f0b8683c Mon Sep 17 00:00:00 2001 From: Filip Date: Thu, 23 Jan 2020 10:26:19 +0100 Subject: [PATCH 10/21] account-store --- BankAccount/pkg/store/account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index 03beb03..745aa97 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -69,7 +69,7 @@ func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, cu } func (store *AccountModel) CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) { - updatedAt = time.Now() + updatedAt = time.Now() _, err := store.Db.Exec("UPDATE BankAccount.Account SET status = 0, updated_at = ? WHERE user_id = ? AND id = ?", updatedAt, userId, id) if err != nil { return nil, err From 7e462dd4fc034d7b40b3a7d9dbb26c53911be11c Mon Sep 17 00:00:00 2001 From: Filip Date: Thu, 23 Jan 2020 10:40:17 +0100 Subject: [PATCH 11/21] removed createdAt, updatedAt in function --- BankAccount/pkg/store/account.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index 745aa97..956cdd0 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -8,9 +8,9 @@ import ( ) type AccountStore interface { - InsertAccount(userId int, balance float64, currency string, createdAt time.Time, updatedAt time.Time) (*entities.Account, error) + InsertAccount(userId int, balance float64, currency string) (*entities.Account, error) GetAccountsByUserId(userId int) (*[]entities.Account, error) - UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.User, error) + UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.Account, error) CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) } @@ -24,9 +24,9 @@ func NewAccountStoreModel(db *sql.DB) *AccountModel { } } -func (store *AccountModel) InsertAccount(userId int, balance float64, currency string, createdAt time.Time, updatedAt time.Time) (*entities.Account, error) { - createdAt = time.Now() - updatedAt = time.Now() +func (store *AccountModel) InsertAccount(userId int, balance float64, currency string) (*entities.Account, error) { + createdAt := time.Now() + updatedAt := time.Now() account := entities.Account{ UserId: userId, Balance: balance, @@ -59,8 +59,8 @@ func (store *AccountModel) GetAccountsByUserId(userId int) (*[]entities.Account, return &accounts, nil } -func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.User, error) { - updatedAt = time.Now() +func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, currency string) (*entities.Account, error) { + updatedAt := time.Now() _, err := store.Db.Exec("UPDATE BankAccount.Account SET balance = ?, currency =?, updated_at = ? WHERE user_id = ? AND id =?", balance, currency, updatedAt, userId, id) if err != nil { return nil, err @@ -68,8 +68,8 @@ func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, cu return nil, err } -func (store *AccountModel) CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) { - updatedAt = time.Now() +func (store *AccountModel) CloseAccount(id int, userId int) (*entities.Account, error) { + updatedAt := time.Now() _, err := store.Db.Exec("UPDATE BankAccount.Account SET status = 0, updated_at = ? WHERE user_id = ? AND id = ?", updatedAt, userId, id) if err != nil { return nil, err From 41f2ff97f0c5d8dfa2d78b333249b2cc6e7054aa Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 13:43:47 +0100 Subject: [PATCH 12/21] Changes to functions return types --- BankAccount/pkg/store/transactionhistory.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go index a76391c..d91f443 100644 --- a/BankAccount/pkg/store/transactionhistory.go +++ b/BankAccount/pkg/store/transactionhistory.go @@ -39,8 +39,8 @@ func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount f return &transaction, nil } -func (store *TransactionHistoryModel) GetTransactionsById(id int) (*[]entities.TransactionHistory, error) { - var transactions []entities.TransactionHistory +func (store *TransactionHistoryModel) GetTransactionsById(id int) ([]*entities.TransactionHistory, error) { + var transactions []*entities.TransactionHistory result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", id) if err != nil { return nil, err @@ -51,13 +51,13 @@ func (store *TransactionHistoryModel) GetTransactionsById(id int) (*[]entities.T if err != nil { return nil, err } - transactions = append(transactions, transaction) + transactions = append(transactions, &transaction) } - return &transactions, nil + return transactions, nil } -func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) (*[]entities.TransactionHistory, error) { - var transactions []entities.TransactionHistory +func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) ([]*entities.TransactionHistory, error) { + var transactions []*entities.TransactionHistory result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", id, fromDate, toDate) if err != nil { return nil, err @@ -68,7 +68,7 @@ func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, from if err != nil { return nil, err } - transactions = append(transactions, transaction) + transactions = append(transactions, &transaction) } - return &transactions, nil + return transactions, nil } From 01f507b6c729f99e21ecec32fd5b3537df61899f Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 13:49:44 +0100 Subject: [PATCH 13/21] Changed functions return types --- BankAccount/pkg/store/account.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index 956cdd0..802d9ea 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -42,8 +42,8 @@ func (store *AccountModel) InsertAccount(userId int, balance float64, currency s return &account, nil } -func (store *AccountModel) GetAccountsByUserId(userId int) (*[]entities.Account, error) { - var accounts []entities.Account +func (store *AccountModel) GetAccountsByUserId(userId int) ([]*entities.Account, error) { + var accounts []*entities.Account result, err := store.Db.Query("SELECT * FROM BankAccount.Account WHERE user_id=?", userId) if err != nil { return nil, err @@ -54,9 +54,9 @@ func (store *AccountModel) GetAccountsByUserId(userId int) (*[]entities.Account, if err != nil { return nil, err } - accounts = append(accounts, account) + accounts = append(accounts, &account) } - return &accounts, nil + return accounts, nil } func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, currency string) (*entities.Account, error) { From a88709dc35926ed990ff112f8b47c765456e2cb5 Mon Sep 17 00:00:00 2001 From: vpatcev18 <59229299+vpatcev18@users.noreply.github.com> Date: Wed, 29 Jan 2020 13:51:29 +0100 Subject: [PATCH 14/21] Delete transactionhistory.go --- BankAccount/pkg/store/transactionhistory.go | 74 --------------------- 1 file changed, 74 deletions(-) delete mode 100644 BankAccount/pkg/store/transactionhistory.go diff --git a/BankAccount/pkg/store/transactionhistory.go b/BankAccount/pkg/store/transactionhistory.go deleted file mode 100644 index d91f443..0000000 --- a/BankAccount/pkg/store/transactionhistory.go +++ /dev/null @@ -1,74 +0,0 @@ -package store - -import ( - "bankacc/pkg/entities" - "database/sql" - "time" -) - -type TransactionHistoryStore interface { - Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) - GetTransactionsById(id int) (*[]entities.TransactionHistory, error) - GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) (*[]entities.TransactionHistory, error) -} - -type TransactionHistoryModel struct { - Db *sql.DB -} - -func NewTransactionHistoryStoreModel(db *sql.DB) *TransactionHistoryModel { - return &TransactionHistoryModel{ - Db: db, - } -} - -func (store *TransactionHistoryModel) Insert(userId int, accountId int, amount float64, action string) (*entities.TransactionHistory, error) { - now := time.Now() - transaction := entities.TransactionHistory{ - UserId: userId, - AccountId: accountId, - Amount: amount, - Action: action, - CreatedAt: now, - } - _, err := store.Db.Exec("INSERT INTO TransactionHistory(user_id, account_id, amount, action, created_at) VALUES(?, ?, ?, ?, ?)", userId, accountId, amount, action, now) - - if err != nil { - return nil, err - } - return &transaction, nil -} - -func (store *TransactionHistoryModel) GetTransactionsById(id int) ([]*entities.TransactionHistory, error) { - var transactions []*entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ?", id) - if err != nil { - return nil, err - } - var transaction entities.TransactionHistory - for result.Next() { - err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) - if err != nil { - return nil, err - } - transactions = append(transactions, &transaction) - } - return transactions, nil -} - -func (store *TransactionHistoryModel) GetTransactionsByIdFromToDate(id int, fromDate time.Time, toDate time.Time) ([]*entities.TransactionHistory, error) { - var transactions []*entities.TransactionHistory - result, err := store.Db.Query("SELECT * FROM TransactionHistory WHERE user_id = ? and created_at BETWEEN ? and ?", id, fromDate, toDate) - if err != nil { - return nil, err - } - var transaction entities.TransactionHistory - for result.Next() { - err := result.Scan(&transaction.Id, &transaction.UserId, &transaction.AccountId, &transaction.Amount, &transaction.Action, &transaction.CreatedAt) - if err != nil { - return nil, err - } - transactions = append(transactions, &transaction) - } - return transactions, nil -} From 3c8fe1769dbe6fb5be92617737a0ab4898942f4b Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 13:54:49 +0100 Subject: [PATCH 15/21] Changed function return types --- BankAccount/pkg/store/account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index 802d9ea..a6774f6 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -9,7 +9,7 @@ import ( type AccountStore interface { InsertAccount(userId int, balance float64, currency string) (*entities.Account, error) - GetAccountsByUserId(userId int) (*[]entities.Account, error) + GetAccountsByUserId(userId int) ([]*entities.Account, error) UpdateAccount(id int, userId int, balance float64, currency string, updatedAt time.Time) (*entities.Account, error) CloseAccount(id int, userId int, updatedAt time.Time) (*entities.Account, error) } From 6d0af18a2ae289cf9d0a4c302c67fbe5d82edf95 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 14:29:26 +0100 Subject: [PATCH 16/21] Added lastInsertId in InsertAccount func --- BankAccount/pkg/store/account.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index a6774f6..6612b10 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -27,7 +27,17 @@ func NewAccountStoreModel(db *sql.DB) *AccountModel { func (store *AccountModel) InsertAccount(userId int, balance float64, currency string) (*entities.Account, error) { createdAt := time.Now() updatedAt := time.Now() + + result, err := store.Db.Exec("INSERT INTO Account (user_id, balance, currency, status, created_at, updated_at) VALUES(?, ?, ?, 1, ?, ?)", userId, balance, currency, createdAt, updatedAt) + if err != nil { + return nil, err + } + res, err := result.LastInsertId() + if err != nil { + return nil, err + } account := entities.Account{ + Id: int(res), UserId: userId, Balance: balance, Currency: currency, @@ -35,10 +45,6 @@ func (store *AccountModel) InsertAccount(userId int, balance float64, currency s CreatedAt: createdAt, UpdatedAt: updatedAt, } - _, err := store.Db.Exec("INSERT INTO Account (user_id, balance, currency, status, created_at, updated_at) VALUES(?, ?, ?, 1, ?, ?)", userId, balance, currency, createdAt, updatedAt) - if err != nil { - return nil, err - } return &account, nil } From c3beb6aeb32c6b7af9f04702e69a12c7f3e27ddd Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 15:45:47 +0100 Subject: [PATCH 17/21] Changed return values --- BankAccount/pkg/store/account.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/BankAccount/pkg/store/account.go b/BankAccount/pkg/store/account.go index 6612b10..2917f28 100644 --- a/BankAccount/pkg/store/account.go +++ b/BankAccount/pkg/store/account.go @@ -71,7 +71,14 @@ func (store *AccountModel) UpdateAccount(id int, userId int, balance float64, cu if err != nil { return nil, err } - return nil, err + account := entities.Account{ + Id: id, + UserId: userId, + Balance: balance, + Currency: currency, + Status: true, + } + return &account, nil } func (store *AccountModel) CloseAccount(id int, userId int) (*entities.Account, error) { @@ -80,5 +87,11 @@ func (store *AccountModel) CloseAccount(id int, userId int) (*entities.Account, if err != nil { return nil, err } - return nil, err + account := entities.Account{ + Id: id, + UserId: userId, + Status: false, + UpdatedAt: updatedAt, + } + return &account, err } From da33139747a4de9f4a2dbf17160c9f2534c5716d Mon Sep 17 00:00:00 2001 From: Filip Date: Wed, 29 Jan 2020 16:40:28 +0100 Subject: [PATCH 18/21] account store tests --- BankAccount/pkg/tests/accountstore_test.go | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 BankAccount/pkg/tests/accountstore_test.go diff --git a/BankAccount/pkg/tests/accountstore_test.go b/BankAccount/pkg/tests/accountstore_test.go new file mode 100644 index 0000000..6c07e76 --- /dev/null +++ b/BankAccount/pkg/tests/accountstore_test.go @@ -0,0 +1,65 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "bankacc/pkg/entities" +) + +func (suite *AccountTestSuite) TestUpdateAccount() { + store := store.NewAccountStoreModel(suite.Db) + var err error + var account *entities.Account + var accounts []*entities.Account + //time.Sleep(2 * time.Second) + for _, value := range suite.AccountsP { + account, err = store.UpdateAccount(value.Id, value.UserId, value.Balance, value.Currency) + if err != nil { + suite.T().Fatal("Unable to run UpdateAccount store func") + } + accounts = append(accounts, account) + } + for _, value := range suite.AccountsP { + for i := range accounts { + if value.Id == accounts[i].Id { + suite.Equal(value.UserId, accounts[i].UserId) + suite.Equal(value.Balance, accounts[i].Balance) + suite.Equal(value.Currency, accounts[i].Currency) + } + } + } +} +func (suite *AccountTestSuite) TestCloseAccount() { + store := store.NewAccountStoreModel(suite.Db) + var err error + var account *entities.Account + var accounts []*entities.Account + for _, value := range suite.AccountsP { + account, err = store.CloseAccount(value.Id, value.UserId) + if err != nil { + suite.T().Fatal("Unable to run CloseAccount store func") + } + accounts = append(accounts, account) + } + for _, value := range suite.AccountsP { + for i := range accounts { + if value.Id == accounts[i].Id { + suite.Equal(value.UserId, accounts[i].UserId) + suite.Equal(false, accounts[i].Status) + } + } + } +} +func TestAccountTestSuite(t *testing.T) { + suite.Run(t, new(AccountTestSuite)) +} +func (suite *AccountTestSuite) TearDownTest() { + for i := 0; i < len(suite.AccountsP); i++ { + _, err := suite.Db.Exec("DELETE FROM Account WHERE id=?", suite.AccountsP[i].Id) + if err != nil { + suite.T().Fatal("Unable to run delete query") + } + } +} From b5fa521130a7c992f4343c7c7023d9a9ae5365fd Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 29 Jan 2020 16:44:38 +0100 Subject: [PATCH 19/21] AccountStore Tests --- BankAccount/pkg/tests/accountstore_test.go | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 BankAccount/pkg/tests/accountstore_test.go diff --git a/BankAccount/pkg/tests/accountstore_test.go b/BankAccount/pkg/tests/accountstore_test.go new file mode 100644 index 0000000..b54b098 --- /dev/null +++ b/BankAccount/pkg/tests/accountstore_test.go @@ -0,0 +1,82 @@ +package tests + +import ( + "bankacc/pkg/entities" + "bankacc/pkg/store" + "database/sql" + + _ "github.com/go-sql-driver/mysql" + "github.com/stretchr/testify/suite" +) + +type TearDownTestSuite interface { + TearDownTest() +} + +type AccountTestSuite struct { + suite.Suite + Account *entities.Account + Accounts []entities.Account + AccountsP []*entities.Account + AccountStore store.AccountModel + Db *sql.DB +} + +func (suite *AccountTestSuite) SetupTest() { + var err error + suite.Db = MySQLInit() + account := store.NewAccountStoreModel(suite.Db) + suite.Accounts = []entities.Account{ + { + UserId: 1, + Balance: 1000, + Currency: "Euros", + }, + { + UserId: 2, + Balance: 500, + Currency: "$", + }, + { + UserId: 3, + Balance: 1000, + Currency: "Euros", + }, + { + UserId: 1, + Balance: 2000, + Currency: "$", + }, + } + + for _, value := range suite.Accounts { + suite.Account, err = account.InsertAccount(value.UserId, value.Balance, value.Currency) + if err != nil { + suite.T().Fatal("Unable to run InsertAccount store func") + } + suite.AccountsP = append(suite.AccountsP, suite.Account) + } +} + +func (suite *AccountTestSuite) TestGetAccountsById() { + store := store.NewAccountStoreModel(suite.Db) + var err error + var accounts []*entities.Account + for _, value := range suite.AccountsP { + accounts, err = store.GetAccountsByUserId(value.UserId) + if err != nil { + suite.T().Fatal("Unable to run GetAccountsById store func") + } + } + + for _, value := range suite.AccountsP { + for i := range accounts { + if value.Id == accounts[i].Id { + suite.Equal(value.UserId, accounts[i].UserId) + suite.Equal(value.Balance, accounts[i].Balance) + suite.Equal(value.Currency, accounts[i].Currency) + suite.Equal(value.Status, accounts[i].Status) + } + } + } +} From 2afa6a6870413c981bd3c4d0200e61e20e33fcec Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Thu, 30 Jan 2020 09:17:56 +0100 Subject: [PATCH 20/21] AccountStore Tests --- BankAccount/pkg/tests/accountstore_test.go | 49 ++++++++++++++-------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/BankAccount/pkg/tests/accountstore_test.go b/BankAccount/pkg/tests/accountstore_test.go index c237108..47d08d3 100644 --- a/BankAccount/pkg/tests/accountstore_test.go +++ b/BankAccount/pkg/tests/accountstore_test.go @@ -1,15 +1,28 @@ package tests import ( -<<<<<<< HEAD "bankacc/pkg/entities" "bankacc/pkg/store" "database/sql" + "log" + "testing" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/suite" ) +func MySQLInit() *sql.DB { + dbDriver := "mysql" + dbUser := "root" + dbPass := "Password1!" + dbName := "BankAccount?parseTime=true" + db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) + if err != nil { + log.Println(err) + } + return db +} + type TearDownTestSuite interface { TearDownTest() } @@ -70,13 +83,17 @@ func (suite *AccountTestSuite) TestGetAccountsById() { } } -======= - "testing" - - "github.com/stretchr/testify/suite" - - "bankacc/pkg/entities" -) + for _, value := range suite.AccountsP { + for i := range accounts { + if value.Id == accounts[i].Id { + suite.Equal(value.UserId, accounts[i].UserId) + suite.Equal(value.Balance, accounts[i].Balance) + suite.Equal(value.Currency, accounts[i].Currency) + suite.Equal(value.Status, accounts[i].Status) + } + } + } +} func (suite *AccountTestSuite) TestUpdateAccount() { store := store.NewAccountStoreModel(suite.Db) @@ -91,6 +108,7 @@ func (suite *AccountTestSuite) TestUpdateAccount() { } accounts = append(accounts, account) } + for _, value := range suite.AccountsP { for i := range accounts { if value.Id == accounts[i].Id { @@ -101,11 +119,13 @@ func (suite *AccountTestSuite) TestUpdateAccount() { } } } + func (suite *AccountTestSuite) TestCloseAccount() { store := store.NewAccountStoreModel(suite.Db) var err error var account *entities.Account var accounts []*entities.Account + for _, value := range suite.AccountsP { account, err = store.CloseAccount(value.Id, value.UserId) if err != nil { @@ -113,27 +133,21 @@ func (suite *AccountTestSuite) TestCloseAccount() { } accounts = append(accounts, account) } ->>>>>>> da33139747a4de9f4a2dbf17160c9f2534c5716d + for _, value := range suite.AccountsP { for i := range accounts { if value.Id == accounts[i].Id { suite.Equal(value.UserId, accounts[i].UserId) -<<<<<<< HEAD - suite.Equal(value.Balance, accounts[i].Balance) - suite.Equal(value.Currency, accounts[i].Currency) - suite.Equal(value.Status, accounts[i].Status) -======= suite.Equal(false, accounts[i].Status) ->>>>>>> da33139747a4de9f4a2dbf17160c9f2534c5716d } } } } -<<<<<<< HEAD -======= + func TestAccountTestSuite(t *testing.T) { suite.Run(t, new(AccountTestSuite)) } + func (suite *AccountTestSuite) TearDownTest() { for i := 0; i < len(suite.AccountsP); i++ { _, err := suite.Db.Exec("DELETE FROM Account WHERE id=?", suite.AccountsP[i].Id) @@ -142,4 +156,3 @@ func (suite *AccountTestSuite) TearDownTest() { } } } ->>>>>>> da33139747a4de9f4a2dbf17160c9f2534c5716d From d6182e8db45cc76e75f4d1b0e5925bba66b13583 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Thu, 30 Jan 2020 09:20:29 +0100 Subject: [PATCH 21/21] AccountStore Tests --- BankAccount/pkg/tests/accountstore_test.go | 63 ---------------------- 1 file changed, 63 deletions(-) diff --git a/BankAccount/pkg/tests/accountstore_test.go b/BankAccount/pkg/tests/accountstore_test.go index 47d08d3..9c67c30 100644 --- a/BankAccount/pkg/tests/accountstore_test.go +++ b/BankAccount/pkg/tests/accountstore_test.go @@ -5,7 +5,6 @@ import ( "bankacc/pkg/store" "database/sql" "log" - "testing" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/suite" @@ -94,65 +93,3 @@ func (suite *AccountTestSuite) TestGetAccountsById() { } } } - -func (suite *AccountTestSuite) TestUpdateAccount() { - store := store.NewAccountStoreModel(suite.Db) - var err error - var account *entities.Account - var accounts []*entities.Account - //time.Sleep(2 * time.Second) - for _, value := range suite.AccountsP { - account, err = store.UpdateAccount(value.Id, value.UserId, value.Balance, value.Currency) - if err != nil { - suite.T().Fatal("Unable to run UpdateAccount store func") - } - accounts = append(accounts, account) - } - - for _, value := range suite.AccountsP { - for i := range accounts { - if value.Id == accounts[i].Id { - suite.Equal(value.UserId, accounts[i].UserId) - suite.Equal(value.Balance, accounts[i].Balance) - suite.Equal(value.Currency, accounts[i].Currency) - } - } - } -} - -func (suite *AccountTestSuite) TestCloseAccount() { - store := store.NewAccountStoreModel(suite.Db) - var err error - var account *entities.Account - var accounts []*entities.Account - - for _, value := range suite.AccountsP { - account, err = store.CloseAccount(value.Id, value.UserId) - if err != nil { - suite.T().Fatal("Unable to run CloseAccount store func") - } - accounts = append(accounts, account) - } - - for _, value := range suite.AccountsP { - for i := range accounts { - if value.Id == accounts[i].Id { - suite.Equal(value.UserId, accounts[i].UserId) - suite.Equal(false, accounts[i].Status) - } - } - } -} - -func TestAccountTestSuite(t *testing.T) { - suite.Run(t, new(AccountTestSuite)) -} - -func (suite *AccountTestSuite) TearDownTest() { - for i := 0; i < len(suite.AccountsP); i++ { - _, err := suite.Db.Exec("DELETE FROM Account WHERE id=?", suite.AccountsP[i].Id) - if err != nil { - suite.T().Fatal("Unable to run delete query") - } - } -}