SQL Databases

๐Ÿ“Š SQL (Structured Query Language) databases, often referred to as relational databases, use structured tables to store data. They allow for complex queries and are ideal for situations where data relationships and integrity are essential.

๐Ÿงช Mock Implementations for Testing

For developers and testers, we offer mock implementation of SQL database client

package mocks

import (
	"database/sql"

	"gorm.io/gorm"
)

type (
	FirstDelegate  func(out interface{}, where ...interface{}) *gorm.DB
	CreateDelegate func(value interface{}) *gorm.DB
	DBDelegate     func() (*sql.DB, error)
)

type MockSQLdb struct {
	FirstFn  FirstDelegate
	CreateFn CreateDelegate
	DBFn     DBDelegate
}

func (m *MockSQLdb) First(out interface{}, where ...interface{}) *gorm.DB {
	if m.FirstFn != nil {
		return m.FirstFn(out, where)
	}

	return nil
}

func (m *MockSQLdb) Create(value interface{}) *gorm.DB {
	if m.CreateFn != nil {
		return m.CreateFn(value)
	}

	return nil
}

func (m *MockSQLdb) DB() (*sql.DB, error) {
	if m.DBFn != nil {
		return m.DBFn()
	}

	return nil, nil
}

Usage example:

mockDB := &mocks.MockSQLdb{
		FirstFn: func(out interface{}, where ...interface{}) *gorm.DB {
			return &gorm.DB{Error: errInternal} // Return a mock *gorm.DB with gorm.ErrRecordNotFound error
		},
	}

Last updated