Skip to content

Commit

Permalink
tests: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Nov 4, 2023
1 parent d579977 commit 09da695
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
26 changes: 14 additions & 12 deletions db_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func ExampleOpen() {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
handleExampleError(err)
return
Expand All @@ -16,7 +16,7 @@ func ExampleOpen() {
// Work with the database...
}

func openTestConnection() (*DB, error) {
func openTestConnection(resetSchema bool) (*DB, error) {
// Database code.
schema := NewSchema()
schema.MustRegister("customers", Customer{}) // Register the Customer struct as a table named "customers".
Expand All @@ -30,17 +30,19 @@ func openTestConnection() (*DB, error) {
}
// Let the caller close the database connection pool: defer db.Close()

// Let's clear the schema, so we can run the tests even if already ran once in the past.
if err = db.DeleteSchema(context.Background()); err != nil { // DON'T DO THIS ON PRODUCTION.
return nil, fmt.Errorf("delete schema: %w", err)
}
if resetSchema {
// Let's clear the schema, so we can run the tests even if already ran once in the past.
if err = db.DeleteSchema(context.Background()); err != nil { // DON'T DO THIS ON PRODUCTION.
return nil, fmt.Errorf("delete schema: %w", err)
}

if err = db.CreateSchema(context.Background()); err != nil { // Create the schema in the database if it does not exist.
return nil, fmt.Errorf("create schema: %w", err)
}
if err = db.CreateSchema(context.Background()); err != nil { // Create the schema in the database if it does not exist.
return nil, fmt.Errorf("create schema: %w", err)
}

if err = db.CheckSchema(context.Background()); err != nil { // Check if the schema in the database matches the schema in the code.
return nil, fmt.Errorf("check schema: %w", err)
if err = db.CheckSchema(context.Background()); err != nil { // Check if the schema in the database matches the schema in the code.
return nil, fmt.Errorf("check schema: %w", err)
}
}

return db, nil
Expand All @@ -53,7 +55,7 @@ func openEmptyTestConnection() (*DB, error) { // without a schema.
}

func createTestConnectionSchema() error {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion db_information_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func handleExampleError(err error) {
}

func ExampleDB_ListColumns() {
db, err := openEmptyTestConnection()
db, err := openTestConnection(false)
if err != nil {
handleExampleError(err)
return
Expand Down
4 changes: 2 additions & 2 deletions db_table_listener_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func ExampleDB_ListenTable() {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
handleExampleError(err)
return
Expand Down Expand Up @@ -54,7 +54,7 @@ func ExampleDB_ListenTable() {
fmt.Println(err)
return
}
time.Sleep(5 * time.Second) // give it sometime to receive the notifications.
time.Sleep(8 * time.Second) // give it sometime to receive the notifications.
// Output:
// table: customers, event: INSERT, old: null
// table: customers, event: UPDATE
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// as the entities to be stored and manipulated in the database. It also prints "OK" if everything succeeds,
// or an error message otherwise.
func Example() {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
fmt.Println(err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion repository_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *Registry) BlogPosts() *Repository[BlogPost] {
}

func ExampleNewRepository() {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
handleExampleError(err)
return
Expand Down
4 changes: 2 additions & 2 deletions repository_table_listener_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func ExampleRepository_ListenTable() {
db, err := openTestConnection()
db, err := openTestConnection(true)
if err != nil {
handleExampleError(err)
return
Expand Down Expand Up @@ -48,7 +48,7 @@ func ExampleRepository_ListenTable() {
fmt.Println(err)
return
}
time.Sleep(5 * time.Second) // give it sometime to receive the notifications.
time.Sleep(8 * time.Second) // give it sometime to receive the notifications.
// Output:
// table: customers, event: INSERT, old name: new name: Makis
// table: customers, event: UPDATE, old name: Makis new name: Makis_UPDATED
Expand Down

0 comments on commit 09da695

Please sign in to comment.