Skip to content

Commit

Permalink
Added new and updated utils methods for constrains. Also, added integ…
Browse files Browse the repository at this point in the history
…ration tests. Restructured some other parts.
  • Loading branch information
jnbdz committed Nov 24, 2024
1 parent 129a009 commit 606b6cd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 68 deletions.
33 changes: 18 additions & 15 deletions cache/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ type IDatabase interface {
}

// SDatabase implements IDatabase
type SDatabase struct{}
type SDatabase struct {
queries *[]string
}

var (
db *sql.DB
Expand All @@ -42,7 +44,7 @@ func (s *SDatabase) Initialize() error {
return nil // Already initialized successfully
}

dbPath := "/tmp/amadla.cache"
dbPath := "/tmp/hery.test.cache"
var err error
db, err = sql.Open("sqlite3", dbPath)
if err != nil {
Expand Down Expand Up @@ -105,10 +107,10 @@ func (s *SDatabase) IsInitialized() bool {
}

// CreateTable creates a new table
func (s *SDatabase) CreateTable(table Table) string {
func (s *SDatabase) CreateTable(table Table) {
var sqlColumns string
for _, column := range table.Columns {
sqlColumn := fmt.Sprintf(",\n%s %s %s", column.ColumnName, column.DataType, column.Constraint)
sqlColumn := fmt.Sprintf(",\n%s %s %s", column.ColumnName, column.DataType, column.Constraints)
sqlColumns = fmt.Sprintf("%s %s", sqlColumns, sqlColumn)
}

Expand Down Expand Up @@ -139,17 +141,17 @@ func (s *SDatabase) CreateTable(table Table) string {
sqlIndexes = fmt.Sprintf("%s\n%s", sqlIndexes, sqlIndexe)
}

return fmt.Sprintf("%s\n%s", createTableSQL, sqlIndexes)
*s.queries = append(*s.queries, fmt.Sprintf("%s\n%s", createTableSQL, sqlIndexes))
}

// Insert inserts records into the table
func (s *SDatabase) Insert(table Table, names []string) string {
return fmt.Sprintf(`INSERT INTO %s (name) VALUES (?)`, table.Name)
func (s *SDatabase) Insert(table Table, names []string) {
*s.queries = append(*s.queries, fmt.Sprintf(`INSERT INTO %s (name) VALUES (?)`, table.Name))
}

// Update updates a record in the table
func (s *SDatabase) Update(table Table, id int, newName string) string {
return fmt.Sprintf(`UPDATE %s SET name = ? WHERE id = ?`, table.Name)
func (s *SDatabase) Update(table Table, id int, newName string) {
*s.queries = append(*s.queries, fmt.Sprintf(`UPDATE %s SET name = ? WHERE id = ?`, table.Name))
}

// Select retrieves a record from the table
Expand All @@ -172,25 +174,26 @@ func (s *SDatabase) Select(table Table, name string) (string, error) {
}

// Delete deletes a record from the table
func (s *SDatabase) Delete(table Table, id int) string {
return fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, table.Name)
func (s *SDatabase) Delete(table Table, id int) {
*s.queries = append(*s.queries, fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, table.Name))
}

// DropTable drops the table from the database
func (s *SDatabase) DropTable(table Table) string {
return fmt.Sprintf(`DROP TABLE IF EXISTS %s`, table.Name)
func (s *SDatabase) DropTable(table Table) {
*s.queries = append(*s.queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s`, table.Name))
}

// Apply all the SQL scripts that are in a string array that are combined into one SQL script
func (s *SDatabase) Apply(sqlQueries *[]string) error {
func (s *SDatabase) Apply() error {
if !s.IsInitialized() {
return fmt.Errorf(ErrorDatabaseNotInitialized)
}

stmt, err := db.Prepare(mergeSqlQueries(sqlQueries))
stmt, err := db.Prepare(mergeSqlQueries(s.queries))
if err != nil {
return fmt.Errorf("error preparing insert statement: %v", err)
}

defer func(stmt *sql.Stmt) {
err := stmt.Close()
if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions cache/database/database_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package database

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_Integration_Initialize(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

isInitialized := databaseService.IsInitialized()
assert.True(t, isInitialized)

err = databaseService.Close()
assert.NoError(t, err)
}

func Test_Integration_IsInitialized(t *testing.T) {}

func Test_Integration_CreateTable(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.CreateTable(Table{Name: "test"})
assert.NoError(t, err)

err = databaseService.Close()
assert.NoError(t, err)
}

func Test_Integration_Insert(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.Insert(Table{Name: "test"}, []string{"Joe", "Jack"})
assert.NoError(t, err)

err = databaseService.Close()
assert.NoError(t, err)
}

func Test_Integration_Select(t *testing.T) {}

func Test_Integration_Delete(t *testing.T) {}

func Test_Integration_DropTable(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.DropTable(Table{Name: "test"})
assert.NoError(t, err)

err = databaseService.Close()
assert.NoError(t, err)
}
53 changes: 3 additions & 50 deletions cache/database/database_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
package database

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestInitialize(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

isInitialized := databaseService.IsInitialized()
assert.True(t, isInitialized)

err = databaseService.Close()
assert.NoError(t, err)
}

func TestClose(t *testing.T) {}

func TestIsInitialized(t *testing.T) {}

func TestCreateTable(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.CreateTable(Table{Name: "test"})
assert.NoError(t, err)

err = databaseService.Close()
assert.NoError(t, err)
}

func TestInsert(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.Insert(Table{Name: "test"}, []string{"Joe", "Jack"})
assert.NoError(t, err)

err = databaseService.Close()
assert.NoError(t, err)
}

func TestDelete(t *testing.T) {}
func TestInsert(t *testing.T) {}

func TestUpdate(t *testing.T) {}

func TestSelect(t *testing.T) {}

func TestDropTable(t *testing.T) {
databaseService := NewDatabaseService()
err := databaseService.Initialize()
assert.NoError(t, err)

err = databaseService.DropTable(Table{Name: "test"})
assert.NoError(t, err)
func TestDelete(t *testing.T) {}

err = databaseService.Close()
assert.NoError(t, err)
}
func TestDropTable(t *testing.T) {}
1 change: 0 additions & 1 deletion cache/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ type Column struct {
ColumnName string
DataType DataType
Constraints []Constraint
Default string
}

// Relationships so to create relationships
Expand Down
15 changes: 13 additions & 2 deletions cache/database/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ func mergeSqlQueries(sqlQueries *[]string) string {
return strings.Replace(strings.Join(*sqlQueries, ";\n")+";", ";;", ";", -1)
}

func (c Constraint) ToSQL(columnName string) string {
// ToSQL for Column
func (col Column) ToSQL() string {
var constraints []string
for _, constraint := range col.Constraints {
constraints = append(constraints, constraint.ToSQL())
}

return fmt.Sprintf("%s %s %s", col.ColumnName, col.DataType, strings.Join(constraints, " "))
}

// ToSQL for Constraint
func (c Constraint) ToSQL() string {
switch c.Type {
case ConstraintNotNull:
return "NOT NULL"
Expand All @@ -28,7 +39,7 @@ func (c Constraint) ToSQL(columnName string) string {
}
case ConstraintForeignKey:
if c.References != "" {
return fmt.Sprintf("FOREIGN KEY (%s)", c.References)
return fmt.Sprintf("FOREIGN KEY REFERENCES %s", c.References)
}
case ConstraintAutoincrement:
return "AUTOINCREMENT"
Expand Down

0 comments on commit 606b6cd

Please sign in to comment.