db

package
v0.0.0-...-14cb7df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 8 Imported by: 0

README

Database layer

This layer contains handlers which interact directly with the database. Typically, other layers of our service, including service layer and transport layer, would interact with the database through this layer if they need to.

Migrations

  • To compare the models with database schema, add the models to /scripts/loader.go. This will help generate the migrations for your models.
  • To generate a new migration, run ./scripts/migrate.sh [name_of_your_migration]. For example: ./scripts/migrate.sh init will generate a new migration called "init" in ./migrations directory.
  • To compare the state/status of the migrations against the database schema, run ./scripts/status.sh. This will print which migrations are pending to be applied and which ones have been applied.
  • To apply all the pending migrations, run ./scripts/apply.sh.

Testing

Unit / Whitebox Tests

All the essential unit tests to be covered:

  • Create a new record in the database.
  • Retrieve and list all the records from the database with supported filters.
  • Get a record from the database using it's ID.
  • Update a record with new options in the database.
  • Delete a record from the database using it's ID.
Integration / Blackbox Tests

To write itnegration tests, you would typically want to mock this layer's interfaces and consume them outside the package.

To generate mock files of the database interface, use the following commands:

  1. Install mockgen.
    go install go.uber.org/mock/mockgen@latest
    
  2. Generate mocks.
    go generate ./...
    
    Or:
    mockgen -destination=mock.go -source=db.go -package=db
    

This will generate the file mock.go which will contain your mock database. You can import it in your tests with:

func TestFoo(t *testing.T) {
  ctrl := gomock.NewController(t)

  m := NewMockDatabase(ctrl)

  // Asserts that the first and only call to Bar() is passed 99.
  // Anything else will fail.
  m.
    EXPECT().
    Bar(gomock.Eq(99)).
    Return(101)

  YourUnitTest(m)
}

Documentation

Overview

Package db is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOptions  = fmt.Errorf("invalid options")
	ErrInvalidRecordID = fmt.Errorf("invalid record id")
	ErrInvalidUserID   = fmt.Errorf("invalid user id")
	ErrInvalidTitle    = fmt.Errorf("invalid title")
	ErrInvalidFilters  = fmt.Errorf("invalid filters")
	ErrNoRowsAffected  = fmt.Errorf("no rows affected")
)

Functions

This section is empty.

Types

type CreateOptions

type CreateOptions struct {

	//	Title of the record.
	Title string

	// ID of the user who is creating the record.
	UserID uuid.UUID
}

CreateOptions holds the options for creating a new record.

type DB

DB interface declares the signature of the database layer.

func NewSQLDB

func NewSQLDB(config *SQLDBConfig) DB

type ListOptions

type ListOptions struct {

	//	Title of the record.
	Title string
	//	Skip for pagination.
	Skip int
	//	Limit for pagination.
	Limit int
	//	Order by field.
	OrderBy string
	//	Order by direction.
	OrderDirection string
}

ListOptions holds the options for listing records.

type MockDB

type MockDB struct {
	// contains filtered or unexported fields
}

MockDB is a mock of DB interface.

func NewMockDB

func NewMockDB(ctrl *gomock.Controller) *MockDB

NewMockDB creates a new mock instance.

func (*MockDB) Create

func (m *MockDB) Create(arg0 context.Context, arg1 *CreateOptions) (*model.Record, error)

Create mocks base method.

func (*MockDB) Delete

func (m *MockDB) Delete(arg0 context.Context, arg1 uuid.UUID) error

Delete mocks base method.

func (*MockDB) EXPECT

func (m *MockDB) EXPECT() *MockDBMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockDB) Get

func (m *MockDB) Get(arg0 context.Context, arg1 uuid.UUID) (*model.Record, error)

Get mocks base method.

func (*MockDB) List

func (m *MockDB) List(arg0 context.Context, arg1 *ListOptions) ([]*model.Record, error)

List mocks base method.

func (*MockDB) Update

func (m *MockDB) Update(arg0 context.Context, arg1 uuid.UUID, arg2 *UpdateOptions) (*model.Record, error)

Update mocks base method.

type MockDBMockRecorder

type MockDBMockRecorder struct {
	// contains filtered or unexported fields
}

MockDBMockRecorder is the mock recorder for MockDB.

func (*MockDBMockRecorder) Create

func (mr *MockDBMockRecorder) Create(arg0, arg1 any) *gomock.Call

Create indicates an expected call of Create.

func (*MockDBMockRecorder) Delete

func (mr *MockDBMockRecorder) Delete(arg0, arg1 any) *gomock.Call

Delete indicates an expected call of Delete.

func (*MockDBMockRecorder) Get

func (mr *MockDBMockRecorder) Get(arg0, arg1 any) *gomock.Call

Get indicates an expected call of Get.

func (*MockDBMockRecorder) List

func (mr *MockDBMockRecorder) List(arg0, arg1 any) *gomock.Call

List indicates an expected call of List.

func (*MockDBMockRecorder) Update

func (mr *MockDBMockRecorder) Update(arg0, arg1, arg2 any) *gomock.Call

Update indicates an expected call of Update.

type SQLDBConfig

type SQLDBConfig struct {

	// Database connection.
	// The connection should already be open.
	//
	// This field is mandatory.
	DB *gorm.DB
}

type UpdateOptions

type UpdateOptions struct {

	//	Title of the record.
	Title string
}

UpdateOptions holds the options for updating a record.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL