Test Database Functions

This commit is contained in:
hoernschen 2020-10-03 00:15:52 +02:00
parent 253b020733
commit 0cc882cff0
4 changed files with 116 additions and 14 deletions

BIN
sqlite.db

Binary file not shown.

View file

@ -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
}

View file

@ -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)

View file

@ -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)