testdb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

godoc codecov Go Report Card

testdb - go db testing utilities

A collection of utilities for simple unit testing with a sqlite db

Installation

> go get github.com/go-corelibs/testdb@latest

Examples

NewTestDB

func main() {
    tdb, err := testdb.NewTestDB()
    defer tdb.Close()               // shutdown and delete temporary things
    sqlDB := tdb.DBH()              // *sql.DB
    file := tdb.SqliteDB()          // absolute path to temporary db file
    present := tdb.HasTable("blah") // check if a table is present
    
    // lots of other methods too, see the godoc for more detail
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package testdb provides go db testing utilities

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrResultColumnNotFound = errors.New("result column not found")
)

Functions

This section is empty.

Types

type TestDB

type TestDB interface {
	// Open starts a new sqlite database, destroying any previous one with
	// TestDB.Close first
	Open() (err error)
	// Close stops the existing sqlite connection and if the database is not
	// ":memory:", removes the db file
	Close()
	// DBH returns the sql.DB instance opened during NewTestDBWith
	DBH() *sql.DB
	// SqliteDB returns the main database path from the "pragma_database_list"
	// table
	SqliteDB() (file string)
	// Tables returns the list of non-sqlite tables from the "sqlite_schema"
	// table
	Tables() (names []string)
	// HasTable returns true if the given table name is present in the
	// "sqlite_schema" table
	HasTable(name string) (present bool)
	// TableSchema returns the SQL definition from the "sqlite_schema" table
	// for the named table
	TableSchema(name string) (schema string)
	// Indexes returns the list of non-sqlite indexes from the "sqlite_schema"
	// table
	Indexes() (names []string)
	// HasIndex returns true if the given table name is present in the
	// "sqlite_schema" table
	HasIndex(name string) (present bool)
	// IndexSchema returns the SQL definition from the "sqlite_schema" table
	// for the named index
	IndexSchema(name string) (schema string)
	// Select is a very simple wrapper around a sql.DB Query call, gathering
	// all the results in a simple mapping of column names to interface{}
	// values
	Select(query string, argv ...interface{}) (results []map[string]interface{}, err error)
	// SelectOne is a wrapper around Select and returning just the first result's
	// specific column value
	SelectOne(column, query string, argv ...interface{}) (value interface{}, err error)
	// SelectList is a wrapper around Select and returning a list of just the
	// specific column values
	SelectList(column, query string, argv ...interface{}) (values []interface{}, err error)
}

TestDB is the interface for Sqlite3 based test databases. These databases are intended to be ephemeral and easily reset for the purposes of unit testing within other projects

func NewTestDB

func NewTestDB() (tdb TestDB, err error)

NewTestDB is a wrapper around a default call to NewTestDBWith (creating an in-memory db)

func NewTestDBWith

func NewTestDBWith(file string) (tdb TestDB, err error)

NewTestDBWith opens the given database file and returns a new TestDB instance, if the file argument is empty, an in-memory database is used

Note that if the database file given is actually a file, the file will be deleted when the Close method is called

Jump to

Keyboard shortcuts

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