database

package
v0.0.0-...-610bbfe Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(db Datastore) func(http.Handler) http.Handler

Middleware packs a pointer to the datastore into context

Types

type ConnectorFunc

type ConnectorFunc func() (*gorm.DB, zerolog.Logger, error)

ConnectorFunc is used to inject a database connection method into NewDatabaseConnection

func NewPostgreSQLConnector

func NewPostgreSQLConnector(log zerolog.Logger) ConnectorFunc

NewPostgreSQLConnector opens a connection to a postgresql database

func NewSQLiteConnector

func NewSQLiteConnector(log zerolog.Logger) ConnectorFunc

NewSQLiteConnector opens a connection to a local sqlite database

type Datastore

type Datastore interface {
	CreateDevice(device *fiware.Device) (*models.Device, error)
	CreateDeviceModel(deviceModel *fiware.DeviceModel) (*models.DeviceModel, error)
	GetDeviceFromID(id string) (*models.Device, error)
	GetDevices() ([]models.Device, error)
	GetDeviceModels() ([]models.DeviceModel, error)
	GetDeviceModelFromID(id string) (*models.DeviceModel, error)
	GetDeviceModelFromPrimaryKey(id uint) (*models.DeviceModel, error)
	UpdateDeviceLocation(deviceID string, lat, lon float64) error
	UpdateDeviceValue(deviceID, value string) error
	UpdateDeviceState(deviceID, state string) error
}

Datastore is an interface that is used to inject the database into different handlers to improve testability

func GetFromContext

func GetFromContext(ctx context.Context) (Datastore, error)

GetFromContext extracts the database wrapper, if any, from the provided context

func NewDatabaseConnection

func NewDatabaseConnection(connect ConnectorFunc) (Datastore, error)

NewDatabaseConnection initializes a new connection to the database and wraps it in a Datastore

type DatastoreMock

type DatastoreMock struct {
	// CreateDeviceFunc mocks the CreateDevice method.
	CreateDeviceFunc func(device *fiware.Device) (*models.Device, error)

	// CreateDeviceModelFunc mocks the CreateDeviceModel method.
	CreateDeviceModelFunc func(deviceModel *fiware.DeviceModel) (*models.DeviceModel, error)

	// GetDeviceFromIDFunc mocks the GetDeviceFromID method.
	GetDeviceFromIDFunc func(id string) (*models.Device, error)

	// GetDeviceModelFromIDFunc mocks the GetDeviceModelFromID method.
	GetDeviceModelFromIDFunc func(id string) (*models.DeviceModel, error)

	// GetDeviceModelFromPrimaryKeyFunc mocks the GetDeviceModelFromPrimaryKey method.
	GetDeviceModelFromPrimaryKeyFunc func(id uint) (*models.DeviceModel, error)

	// GetDeviceModelsFunc mocks the GetDeviceModels method.
	GetDeviceModelsFunc func() ([]models.DeviceModel, error)

	// GetDevicesFunc mocks the GetDevices method.
	GetDevicesFunc func() ([]models.Device, error)

	// UpdateDeviceLocationFunc mocks the UpdateDeviceLocation method.
	UpdateDeviceLocationFunc func(deviceID string, lat float64, lon float64) error

	// UpdateDeviceStateFunc mocks the UpdateDeviceState method.
	UpdateDeviceStateFunc func(deviceID string, state string) error

	// UpdateDeviceValueFunc mocks the UpdateDeviceValue method.
	UpdateDeviceValueFunc func(deviceID string, value string) error
	// contains filtered or unexported fields
}

DatastoreMock is a mock implementation of Datastore.

func TestSomethingThatUsesDatastore(t *testing.T) {

	// make and configure a mocked Datastore
	mockedDatastore := &DatastoreMock{
		CreateDeviceFunc: func(device *fiware.Device) (*models.Device, error) {
			panic("mock out the CreateDevice method")
		},
		CreateDeviceModelFunc: func(deviceModel *fiware.DeviceModel) (*models.DeviceModel, error) {
			panic("mock out the CreateDeviceModel method")
		},
		GetDeviceFromIDFunc: func(id string) (*models.Device, error) {
			panic("mock out the GetDeviceFromID method")
		},
		GetDeviceModelFromIDFunc: func(id string) (*models.DeviceModel, error) {
			panic("mock out the GetDeviceModelFromID method")
		},
		GetDeviceModelFromPrimaryKeyFunc: func(id uint) (*models.DeviceModel, error) {
			panic("mock out the GetDeviceModelFromPrimaryKey method")
		},
		GetDeviceModelsFunc: func() ([]models.DeviceModel, error) {
			panic("mock out the GetDeviceModels method")
		},
		GetDevicesFunc: func() ([]models.Device, error) {
			panic("mock out the GetDevices method")
		},
		UpdateDeviceLocationFunc: func(deviceID string, lat float64, lon float64) error {
			panic("mock out the UpdateDeviceLocation method")
		},
		UpdateDeviceStateFunc: func(deviceID string, state string) error {
			panic("mock out the UpdateDeviceState method")
		},
		UpdateDeviceValueFunc: func(deviceID string, value string) error {
			panic("mock out the UpdateDeviceValue method")
		},
	}

	// use mockedDatastore in code that requires Datastore
	// and then make assertions.

}

func (*DatastoreMock) CreateDevice

func (mock *DatastoreMock) CreateDevice(device *fiware.Device) (*models.Device, error)

CreateDevice calls CreateDeviceFunc.

func (*DatastoreMock) CreateDeviceCalls

func (mock *DatastoreMock) CreateDeviceCalls() []struct {
	Device *fiware.Device
}

CreateDeviceCalls gets all the calls that were made to CreateDevice. Check the length with:

len(mockedDatastore.CreateDeviceCalls())

func (*DatastoreMock) CreateDeviceModel

func (mock *DatastoreMock) CreateDeviceModel(deviceModel *fiware.DeviceModel) (*models.DeviceModel, error)

CreateDeviceModel calls CreateDeviceModelFunc.

func (*DatastoreMock) CreateDeviceModelCalls

func (mock *DatastoreMock) CreateDeviceModelCalls() []struct {
	DeviceModel *fiware.DeviceModel
}

CreateDeviceModelCalls gets all the calls that were made to CreateDeviceModel. Check the length with:

len(mockedDatastore.CreateDeviceModelCalls())

func (*DatastoreMock) GetDeviceFromID

func (mock *DatastoreMock) GetDeviceFromID(id string) (*models.Device, error)

GetDeviceFromID calls GetDeviceFromIDFunc.

func (*DatastoreMock) GetDeviceFromIDCalls

func (mock *DatastoreMock) GetDeviceFromIDCalls() []struct {
	ID string
}

GetDeviceFromIDCalls gets all the calls that were made to GetDeviceFromID. Check the length with:

len(mockedDatastore.GetDeviceFromIDCalls())

func (*DatastoreMock) GetDeviceModelFromID

func (mock *DatastoreMock) GetDeviceModelFromID(id string) (*models.DeviceModel, error)

GetDeviceModelFromID calls GetDeviceModelFromIDFunc.

func (*DatastoreMock) GetDeviceModelFromIDCalls

func (mock *DatastoreMock) GetDeviceModelFromIDCalls() []struct {
	ID string
}

GetDeviceModelFromIDCalls gets all the calls that were made to GetDeviceModelFromID. Check the length with:

len(mockedDatastore.GetDeviceModelFromIDCalls())

func (*DatastoreMock) GetDeviceModelFromPrimaryKey

func (mock *DatastoreMock) GetDeviceModelFromPrimaryKey(id uint) (*models.DeviceModel, error)

GetDeviceModelFromPrimaryKey calls GetDeviceModelFromPrimaryKeyFunc.

func (*DatastoreMock) GetDeviceModelFromPrimaryKeyCalls

func (mock *DatastoreMock) GetDeviceModelFromPrimaryKeyCalls() []struct {
	ID uint
}

GetDeviceModelFromPrimaryKeyCalls gets all the calls that were made to GetDeviceModelFromPrimaryKey. Check the length with:

len(mockedDatastore.GetDeviceModelFromPrimaryKeyCalls())

func (*DatastoreMock) GetDeviceModels

func (mock *DatastoreMock) GetDeviceModels() ([]models.DeviceModel, error)

GetDeviceModels calls GetDeviceModelsFunc.

func (*DatastoreMock) GetDeviceModelsCalls

func (mock *DatastoreMock) GetDeviceModelsCalls() []struct {
}

GetDeviceModelsCalls gets all the calls that were made to GetDeviceModels. Check the length with:

len(mockedDatastore.GetDeviceModelsCalls())

func (*DatastoreMock) GetDevices

func (mock *DatastoreMock) GetDevices() ([]models.Device, error)

GetDevices calls GetDevicesFunc.

func (*DatastoreMock) GetDevicesCalls

func (mock *DatastoreMock) GetDevicesCalls() []struct {
}

GetDevicesCalls gets all the calls that were made to GetDevices. Check the length with:

len(mockedDatastore.GetDevicesCalls())

func (*DatastoreMock) UpdateDeviceLocation

func (mock *DatastoreMock) UpdateDeviceLocation(deviceID string, lat float64, lon float64) error

UpdateDeviceLocation calls UpdateDeviceLocationFunc.

func (*DatastoreMock) UpdateDeviceLocationCalls

func (mock *DatastoreMock) UpdateDeviceLocationCalls() []struct {
	DeviceID string
	Lat      float64
	Lon      float64
}

UpdateDeviceLocationCalls gets all the calls that were made to UpdateDeviceLocation. Check the length with:

len(mockedDatastore.UpdateDeviceLocationCalls())

func (*DatastoreMock) UpdateDeviceState

func (mock *DatastoreMock) UpdateDeviceState(deviceID string, state string) error

UpdateDeviceState calls UpdateDeviceStateFunc.

func (*DatastoreMock) UpdateDeviceStateCalls

func (mock *DatastoreMock) UpdateDeviceStateCalls() []struct {
	DeviceID string
	State    string
}

UpdateDeviceStateCalls gets all the calls that were made to UpdateDeviceState. Check the length with:

len(mockedDatastore.UpdateDeviceStateCalls())

func (*DatastoreMock) UpdateDeviceValue

func (mock *DatastoreMock) UpdateDeviceValue(deviceID string, value string) error

UpdateDeviceValue calls UpdateDeviceValueFunc.

func (*DatastoreMock) UpdateDeviceValueCalls

func (mock *DatastoreMock) UpdateDeviceValueCalls() []struct {
	DeviceID string
	Value    string
}

UpdateDeviceValueCalls gets all the calls that were made to UpdateDeviceValue. Check the length with:

len(mockedDatastore.UpdateDeviceValueCalls())

Jump to

Keyboard shortcuts

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