Skip to content

Commit

Permalink
Merge pull request #26 from Pomog/den_dev
Browse files Browse the repository at this point in the history
Den dev
  • Loading branch information
Pomog authored Dec 1, 2023
2 parents 3acec10 + 9ab041e commit b2a6086
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 4 deletions.
30 changes: 30 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Pomog/ForumFFF/internal/handler"
"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
"github.com/Pomog/ForumFFF/internal/repository/dbrepo"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -47,6 +48,35 @@ func main() {

}

func createDB() {
db_driver.MakeDBTables()

Check failure on line 52 in cmd/web/main.go

View workflow job for this annotation

GitHub Actions / build

undefined: db_driver

newUser := models.User{
UserName: "test1",
Password: "123",
FirstName: "testFirstName1",
LastName: "testLastName1",
Email: "test1@mail.com",
}

db, _ := db_driver.GetDB()

Check failure on line 62 in cmd/web/main.go

View workflow job for this annotation

GitHub Actions / build

undefined: db_driver

repo := dbrepo.NewSQLiteRepo(db, &app)

repo.CreatetUser(newUser)

userNameToFind := "test1"
userNameToFind2 := "noSuchUser"
userEmailToFind := "test1@mail.com"

isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind, isPresent)
fmt.Println("--------------------------------------------------")
isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
log.Printf("User %s present - %v", userNameToFind2, isPresent2)

}

func run() error {
fmt.Println("Starting application")

Expand Down
17 changes: 13 additions & 4 deletions db_driver/db_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ func MakeDBTables() error {
database, err := GetDB()
if err != nil {
log.Println(err)
return err
return database, err

Check failure on line 15 in db_driver/db_funcs.go

View workflow job for this annotation

GitHub Actions / build

too many return values
}
defer database.Close()

sqlQuerys := getQuerys()

Expand All @@ -28,17 +27,27 @@ func MakeDBTables() error {

_, err = statement.Exec()
if err != nil {
return err
return database, err

Check failure on line 30 in db_driver/db_funcs.go

View workflow job for this annotation

GitHub Actions / build

too many return values
}
}

return nil
return database, nil

Check failure on line 34 in db_driver/db_funcs.go

View workflow job for this annotation

GitHub Actions / build

too many return values
}

func GetDB() (*sql.DB, error) {
database, err := sql.Open("sqlite3", "./mainDB.db")
if err != nil {
return nil, err
}
errDB := testDB(database)
if errDB != nil {
return nil, errDB
}
return database, nil
}

func testDB(db *sql.DB) error {
//Ping verifies a connection to the database is still alive, establishing a connection if necessary.
err := db.Ping()
return err
}
102 changes: 102 additions & 0 deletions db_driver/db_funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package db_driver

import (
"os"
"testing"
)

var ObligatoryTables = []string{
"users", "thread", "votes", "post",
}

func Test_GetDB(t *testing.T) {

db, err := MakeDBTables()

if err != nil {
t.Errorf("Could not %s", "sql.Open(sqlite3, ./mainDB.db)")
}
defer db.Close()

rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table';")
if err != nil {
t.Error(err)
}
defer rows.Close()

var tableName string
var allTables []string
for rows.Next() {
if err := rows.Scan(&tableName); err != nil {
t.Error(err)
}
allTables = append(allTables, tableName)
}

if err := rows.Err(); err != nil {
t.Error(err)
}

if !compareTableNames(ObligatoryTables, allTables) {
t.Errorf("\nExpected %v\nreceived %v", (ObligatoryTables), (allTables))
}
filepath := "./mainDB.db"
if _, err := os.Stat(filepath); err == nil {
// Delete the file
err := os.Remove(filepath)
if err != nil {
t.Error("Error:", err)
}
}

}

func compareTableNames(want, get []string) bool {
if len(want) != len(get) {
return false
}
for _, elem := range want {
if !containsString(get, elem) {
return false
}
}
return true
}

// containsString checks if a string is present in a slice of strings
func containsString(slice []string, element string) bool {
for _, value := range slice {
if value == element {
return true
}
}
return false
}

// db_driver.MakeDBTables()

// newUser := models.User{
// UserName: "test1",
// Password: "123",
// FirstName: "testFirstName1",
// LastName: "testLastName1",
// Email: "test1@mail.com",
// }

// db, _ := db_driver.GetDB()

// repo := dbrepo.NewSQLiteRepo(db, &app)

// repo.CreatetUser(newUser)

// userNameToFind := "test1"
// userNameToFind2 := "noSuchUser"
// userEmailToFind := "test1@mail.com"

// isPresent, _ := repo.UserPresent(userNameToFind, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind, isPresent)
// fmt.Println("--------------------------------------------------")
// isPresent2, _ := repo.UserPresent(userNameToFind2, userEmailToFind)
// log.Printf("User %s present - %v", userNameToFind2, isPresent2)

// -------------------------------------------------------------
7 changes: 7 additions & 0 deletions db_driver/struct_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package db_driver

import "database/sql"

type DataBase struct{
SQL *sql.DB
}
Empty file added h
Empty file.

0 comments on commit b2a6086

Please sign in to comment.