testsql

package module
v0.0.0-...-75d045b Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 11 Imported by: 5

README

TestSQL

Build Status codecov.io Go Report Card GoDoc

TestSQL

Generate test data from SQL files before testing and clear it after finished.

Contents

Installation

go get github.com/zhulongcheng/testsql

Usage

Create a folder for the table-schema file and sql files:

testsql
├── fixtures
│   └── users.sql
└── schema.sql

The table-schema file include all tables schema, it would be like this:

CREATE TABLE `users` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(32) CHARACTER SET utf8mb4 NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The sql file would be like this:

INSERT INTO `users` (`id`, `name`)
VALUES
    (1, 'foo');

Your tests would be like this:

var TS *testsql.TestSQL

func newTS() *testsql.TestSQL {
    dsn := "user:password@tcp(host:port)/test_db_name"
    tableSchemaPath := "testsql/schema.sql"
    dirPath := "testsql/fixtures"
    ts := testsql.New(dsn, tableSchemaPath, dirPath)
    return ts
}

func initTestDB() {
    TS = newTS()
    
    // set sql-driver/orm to read/write data from TS's DSN
    // Driver = sql.Open(TS.Config.DSN)
    // ORM = ORM.New(TS.Config.DSN)
}

func TestMain(m *testing.M) {
    initTestDB()
    r := m.Run()
    TS.DropTestDB()
    os.Exit(r)
}

func TestUser(t *testing.T) {
    TS.Use("users.sql")
    defer TS.Clear()
    
    // user := GetUserByID(1)
    // if user.name != "foo" {
    //    t.Errorf("not equal, expected: %s, actual: %s", "foo", user.name) 
    // }
}

API Reference

TestSQL.Exec generates test data from sql string.

TestSQL.Use generates test data from sql files.

TestSQL.Clear deletes data of all tables

TestSQL.DropTestDB drops the database created by TestSQL.

FQA

Set sql-driver/orm to read/write data from TestSQL's DSN
    db := yourSQLDriver.Open(TS.Config.DSN)
    // or db := yourORM.Open(TS.Config.DSN)

    yourModel.SetDB(db)  // reset db

See also

Examples

Documentation

Overview

Package testsql generates test data from SQL files before testing and clears it after finished.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DSN             string
	TableSchemaPath string
	FixtureDirPath  string
}

Config represents a configuration for TestSQL.

type Set

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

Set represents a set container

func NewSet

func NewSet() *Set

NewSet returns a set.

func (*Set) Add

func (s *Set) Add(value string)

Add adds value to set.

func (*Set) Values

func (s *Set) Values() []string

Values returns a slice of values.

type TestSQL

type TestSQL struct {
	Config *Config
	DB     *sql.DB
	// contains filtered or unexported fields
}

TestSQL is the main object to generate/clear test data.

func New

func New(dsn string, tableSchemaPath string, fixtureDirPath string) *TestSQL

New initializes sql.DB, creates test database, and creates all tables.

func (*TestSQL) Clear

func (ts *TestSQL) Clear()

Clear deletes data of all tables.

func (*TestSQL) DropTestDB

func (ts *TestSQL) DropTestDB()

DropTestDB drops the database created by TestSQL.

func (*TestSQL) Exec

func (ts *TestSQL) Exec(sqlString string)

Exec generates data from sql string.

func (*TestSQL) Use

func (ts *TestSQL) Use(sqlFileNames ...string)

Use generates data from sql files.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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