-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
979 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
package db | ||
|
||
import "database/sql" | ||
|
||
type DB interface { | ||
Table(name string) Query | ||
} | ||
|
||
type Query interface { | ||
Delete() error | ||
First(dest any) error | ||
Get(dest any) error | ||
Insert() error | ||
Update() error | ||
Insert(data any) (*Result, error) | ||
Where(query any, args ...any) Query | ||
} | ||
|
||
type Result struct { | ||
RowsAffected int64 | ||
} | ||
|
||
type Builder interface { | ||
Exec(query string, args ...any) (sql.Result, error) | ||
Get(dest any, query string, args ...any) error | ||
Select(dest any, query string, args ...any) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goravel/framework/contracts/database" | ||
contractsdriver "github.com/goravel/framework/contracts/database/driver" | ||
"github.com/goravel/framework/errors" | ||
mocksconfig "github.com/goravel/framework/mocks/config" | ||
mocksdriver "github.com/goravel/framework/mocks/database/driver" | ||
) | ||
|
||
func TestBuildDB(t *testing.T) { | ||
var ( | ||
mockConfig *mocksconfig.Config | ||
mockDriver *mocksdriver.Driver | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
connection string | ||
setup func() | ||
expectedError error | ||
}{ | ||
{ | ||
name: "Success", | ||
connection: "mysql", | ||
setup: func() { | ||
driverCallback := func() (contractsdriver.Driver, error) { | ||
return mockDriver, nil | ||
} | ||
mockConfig.On("Get", "database.connections.mysql.via").Return(driverCallback) | ||
mockDriver.On("DB").Return(&sql.DB{}, nil) | ||
mockDriver.On("Config").Return(database.Config{Driver: "mysql"}) | ||
}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "Config Not Found", | ||
connection: "invalid", | ||
setup: func() { | ||
mockConfig.On("Get", "database.connections.invalid.via").Return(nil) | ||
}, | ||
expectedError: errors.DatabaseConfigNotFound, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
mockConfig = mocksconfig.NewConfig(t) | ||
mockDriver = mocksdriver.NewDriver(t) | ||
test.setup() | ||
|
||
db, err := BuildDB(mockConfig, test.connection) | ||
if test.expectedError != nil { | ||
assert.Equal(t, test.expectedError, err) | ||
assert.Nil(t, db) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, db) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.