From 0cc882cff0771989ac9da0c110cc2e2db4370a21 Mon Sep 17 00:00:00 2001 From: hoernschen Date: Sat, 3 Oct 2020 00:15:52 +0200 Subject: [PATCH] Test Database Functions --- sqlite.db | Bin 61440 -> 61440 bytes utils/database/roomDatabaseConnector.go | 53 +++++++++++++++++- .../database/transactionDatabaseConnector.go | 39 +++++++++++-- utils/database/userDatabaseConnector.go | 38 +++++++++++-- 4 files changed, 116 insertions(+), 14 deletions(-) diff --git a/sqlite.db b/sqlite.db index 59d87e36db1b8fb29d429ac05aa7f7797f55ae27..53619eacbc989a3d70816563fb0da1b0f93f3711 100644 GIT binary patch delta 55 zcmZp8z})bFc>{;RVh#ZXWW1R}fKh?*?PfuNXZ$?E;-V#~#U&xG!68PQ|Jbt#007H@ B5Iq0@ delta 45 zcmZp8z})bFc>{+56aTZ#f&%yXg{4HLMN3kPOF%fpH8{l3$awRg`z!*B1Qq}QZ2}M^ diff --git a/utils/database/roomDatabaseConnector.go b/utils/database/roomDatabaseConnector.go index 92890b7..6be7f11 100644 --- a/utils/database/roomDatabaseConnector.go +++ b/utils/database/roomDatabaseConnector.go @@ -27,9 +27,56 @@ func initRoomTable(db *sql.DB) (err error) { return } statement.Exec() - - //TODO: Test Queries - + /* + newRoom := &room.Room{Id: "test", Version: "test"} + err = CreateRoom(db, newRoom, "test") + if err != nil { + log.Printf("Error Create: %s", err) + return + } + err = CreateRoomMember(db, newRoom.Id, "test2") + if err != nil { + log.Printf("Error Create: %s", err) + return + } + roomMembers, err := ReadRoomMembers(db, newRoom.Id) + if err != nil { + log.Printf("Error Read Members: %s", err) + return + } + log.Println(roomMembers) + newRoom.Version = "test2" + err = UpdateRoom(db, newRoom) + if err != nil { + log.Printf("Error Update: %s", err) + return + } + err = DeleteRoomMember(db, newRoom.Id, "test") + if err != nil { + log.Printf("Error Delete: %s", err) + return + } + err = DeleteAllRoomMemberForUser(db, "test2") + if err != nil { + log.Printf("Error Delete: %s", err) + return + } + err = DeleteRoom(db, newRoom.Id) + if err != nil { + log.Printf("Error Delete: %s", err) + return + } + readRoom, err := ReadRoom(db, newRoom.Id) + if err != nil { + log.Printf("Error Read: %s", err) + return + } + if readRoom != nil { + log.Printf("Room ID: %s Version: %s Members: %s", readRoom.Id, readRoom.Version, readRoom.Members) + } else { + log.Printf("No Room found") + } + */ return } diff --git a/utils/database/transactionDatabaseConnector.go b/utils/database/transactionDatabaseConnector.go index 776972a..de312a4 100644 --- a/utils/database/transactionDatabaseConnector.go +++ b/utils/database/transactionDatabaseConnector.go @@ -20,12 +20,39 @@ func initTransactionTable(db *sql.DB) (err error) { } statement.Exec() - //TODO: Test Queries - + /* + newTransaction := &transaction.Transaction{Id: "test", Origin: "test.de", Timestamp: 1234} + err = CreateTransaction(db, newTransaction) + if err != nil { + log.Printf("Error Create: %s", err) + return + } + newTransaction.Origin = "test2.de" + err = UpdateTransaction(db, newTransaction) + if err != nil { + log.Printf("Error Update: %s", err) + return + } + err = DeleteTransaction(db, newTransaction.Id) + if err != nil { + log.Printf("Error Delete: %s", err) + return + } + readTransaction, err := ReadTransaction(db, newTransaction.Id) + if err != nil { + log.Printf("Error Read: %s", err) + return + } + if readTransaction != nil { + log.Printf("Transaction ID: %s Origin: %s Timestamp: %s PDUS: %s", readTransaction.Id, readTransaction.Origin, readTransaction.Timestamp, readTransaction.PDUS) + } else { + log.Printf("No Transaction found") + } + */ return } -func createTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) { +func CreateTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) { sqlStmt := fmt.Sprintf(`INSERT INTO txn (id, origin, timestamp) VALUES @@ -50,7 +77,7 @@ func createTransaction(db *sql.DB, transaction *transaction.Transaction) (err er return } -func readTransaction(db *sql.DB, id string) (foundTransaction *transaction.Transaction, err error) { +func ReadTransaction(db *sql.DB, id string) (foundTransaction *transaction.Transaction, err error) { queryStmt := fmt.Sprintf(`SELECT id, origin, timestamp FROM txn WHERE id = '%s'`, id) @@ -74,7 +101,7 @@ func readTransaction(db *sql.DB, id string) (foundTransaction *transaction.Trans return } -func updateTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) { +func UpdateTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) { sqlStmt := fmt.Sprintf(`UPDATE txn SET origin = ?, timestamp = ? @@ -100,7 +127,7 @@ func updateTransaction(db *sql.DB, transaction *transaction.Transaction) (err er return } -func deleteTransaction(db *sql.DB, id string) (err error) { +func DeleteTransaction(db *sql.DB, id string) (err error) { queryStmt := fmt.Sprintf(`DELETE FROM txn WHERE id = '%s'`, id) diff --git a/utils/database/userDatabaseConnector.go b/utils/database/userDatabaseConnector.go index a9da638..3ec2715 100644 --- a/utils/database/userDatabaseConnector.go +++ b/utils/database/userDatabaseConnector.go @@ -20,12 +20,40 @@ func initUserTable(db *sql.DB) (err error) { } statement.Exec() - //TODO: Test Queries + /* + newUser := &user.User{Id: "test", Name: "test", Password: "test"} + err = CreateUser(db, newUser) + if err != nil { + log.Printf("Error Create: %s", err) + return + } + newUser.Name = "TEST2" + err = UpdateUser(db, newUser) + if err != nil { + log.Printf("Error Update: %s", err) + return + } + err = DeleteUser(db, newUser.Id) + if err != nil { + log.Printf("Error Delete: %s", err) + return + } + readUser, err := ReadUser(db, newUser.Id) + if err != nil { + log.Printf("Error Read: %s", err) + return + } + if readUser != nil { + log.Printf("User ID: %s Name: %s Password: %s Devices: %s", readUser.Id, readUser.Name, readUser.Password, readUser.Devices) + } else { + log.Printf("No User found") + } + */ return } -func createUser(db *sql.DB, user *user.User) (err error) { +func CreateUser(db *sql.DB, user *user.User) (err error) { sqlStmt := fmt.Sprintf(`INSERT INTO user (id, name, password) VALUES @@ -50,7 +78,7 @@ func createUser(db *sql.DB, user *user.User) (err error) { return } -func readUser(db *sql.DB, id string) (foundUser *user.User, err error) { +func ReadUser(db *sql.DB, id string) (foundUser *user.User, err error) { queryStmt := fmt.Sprintf(`SELECT id, name, password FROM user WHERE id = '%s'`, id) @@ -74,7 +102,7 @@ func readUser(db *sql.DB, id string) (foundUser *user.User, err error) { return } -func updateUser(db *sql.DB, user *user.User) (err error) { +func UpdateUser(db *sql.DB, user *user.User) (err error) { sqlStmt := fmt.Sprintf(`UPDATE user SET name = ?, password = ? @@ -100,7 +128,7 @@ func updateUser(db *sql.DB, user *user.User) (err error) { return } -func deleteUser(db *sql.DB, id string) (err error) { +func DeleteUser(db *sql.DB, id string) (err error) { queryStmt := fmt.Sprintf(`DELETE FROM user WHERE id = '%s'`, id)