1
0

Migrated to dep instead of govendor

This commit is contained in:
kolaente
2018-07-24 13:49:45 +02:00
committed by konrad
parent 0a2eae120e
commit 9c0c9474e8
92 changed files with 14968 additions and 14649 deletions

View File

@ -1,10 +1,9 @@
# Go Test Fixtures
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/go-testfixtures/testfixtures/blob/master/LICENSE)
[![Join the chat at https://gitter.im/go-testfixtures/testfixtures](https://badges.gitter.im/go-testfixtures/testfixtures.svg)](https://gitter.im/go-testfixtures/testfixtures?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![GoDoc](https://godoc.org/gopkg.in/testfixtures.v2?status.svg)](https://godoc.org/gopkg.in/testfixtures.v2)
[![Build Status](https://travis-ci.org/go-testfixtures/testfixtures.svg?branch=master)](https://travis-ci.org/go-testfixtures/testfixtures)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-testfixtures/testfixtures)](https://goreportcard.com/report/github.com/go-testfixtures/testfixtures)
[![Build Status](https://travis-ci.org/go-testfixtures/testfixtures.svg?branch=master)](https://travis-ci.org/go-testfixtures/testfixtures)
[![Build status](https://ci.appveyor.com/api/projects/status/d2h6gq37wxbus1x7?svg=true)](https://ci.appveyor.com/project/andreynering/testfixtures)
> ***Warning***: this package will wipe the database data before loading the
fixtures! It is supposed to be used on a test database. Please, double check
@ -28,25 +27,25 @@ the tests.
First, get it:
```bash
go get -u gopkg.in/testfixtures.v2
go get -u -v gopkg.in/testfixtures.v2
```
## Usage
Create a folder for the fixture files. Each file should contain data for a
single table and have the name `<table-name>.yml`:
single table and have the name `<table_name>.yml`:
```yml
myapp
- myapp.go
- myapp_test.go
- ...
- fixtures:
- posts.yml
- comments.yml
- tags.yml
- posts_tags.yml
- ...
```
myapp/
myapp.go
myapp_test.go
...
fixtures/
posts.yml
comments.yml
tags.yml
posts_tags.yml
...
```
The file would look like this (it can have as many record you want):
@ -270,7 +269,7 @@ Oracle is supported as well. Use:
&testfixtures.Oracle{}
```
## Generation fixtures for a existent database (experimental)
## Generating fixtures for a existing database (experimental)
The following code will generate a YAML file for each table of the database in
the given folder. It may be useful to boostrap a test scenario from a sample
@ -283,6 +282,23 @@ if err != nil {
}
```
Or
```go
err := testfixtures.GenerateFixturesForTables(
db,
[]*TableInfo{
&TableInfo{Name: "table_name", Where: "foo = 'bar'"},
// ...
},
&testfixtures.PostgreSQL{},
"testdata/fixtures",
)
if err != nil {
log.Fatalf("Error generating fixtures: %v", err)
}
```
> This was thought to run in small sample databases. It will likely break
if run in a production/big database.
@ -314,28 +330,11 @@ go test -tags 'sqlite postgresql mysql'
go test -v -bench=. -tags postgresql
```
Travis runs tests for PostgreSQL, MySQL and SQLite.
Travis runs tests for PostgreSQL, MySQL and SQLite. AppVeyor run for all
these and also Microsoft SQL Server.
To set the connection string of tests for each database, edit the `.env`
file, but do not include the changes a in pull request.
## Changes in v2
A context was created to allow cache of some SQL statements. See in the
documentation above how to use it.
The helpers were renamed to have a smaller name:
```go
PostgreSQLHelper{} -> PostgreSQL{}
MySQLHelper{} -> MySQL{}
SQLiteHelper{} -> SQLite{}
SQLServerHelper{} -> SQLServer{}
OracleHelper{} -> Oracle{}
```
The old functions and helpers are still available for backward compatibility.
See the file [deprecated.go](https://github.com/go-testfixtures/testfixtures/blob/master/deprecated.go)
To set the connection string of tests for each database, copy the `.sample.env`
file as `.env` and edit it according to your environment.
## Alternatives
@ -352,17 +351,8 @@ unit test database code without having to connect to a real database
- [dbcleaner][dbcleaner] - Clean database for testing, inspired by
database_cleaner for Ruby
There's also these other implementations of test fixtures for Go:
- [go-fixtures][gofixtures]: Django style fixtures for Go
- [mongofixtures][mongofixtures]: Fixtures for MongoDB
- [fixturer][fixturer]: Another fixture loader supporting MySQL
[railstests]: http://guides.rubyonrails.org/testing.html#the-test-database
[gotxdb]: https://github.com/DATA-DOG/go-txdb
[gosqlmock]: https://github.com/DATA-DOG/go-sqlmock
[gofixtures]: https://github.com/AreaHQ/go-fixtures
[mongofixtures]: https://github.com/OwlyCode/mongofixtures
[fixturer]: https://github.com/44hapa/fixturer
[factorygo]: https://github.com/bluele/factory-go
[dbcleaner]: https://github.com/khaiql/dbcleaner

View File

@ -1,62 +1,64 @@
# github.com/go-task/task
dl-deps:
desc: Download cli deps
cmds:
- go get -u github.com/golang/lint/golint
- go get -u github.com/go-task/task/cmd/task
version: '2'
lint:
desc: Runs golint
cmds:
- golint .
tasks:
dl-deps:
desc: Download cli deps
cmds:
- go get -u github.com/golang/lint/golint
test-free:
desc: Test free databases (PG, MySQL and SQLite)
cmds:
- task: test-pg
- task: test-mysql
- task: test-sqlite
lint:
desc: Runs golint
cmds:
- golint .
test-all:
desc: Test all databases (PG, MySQL, SQLite, SQLServer and Oracle)
cmds:
- task: test-pg
- task: test-mysql
- task: test-sqlite
- task: test-sqlserver
- task: test-oracle
test-free:
desc: Test free databases (PG, MySQL and SQLite)
cmds:
- task: test-pg
- task: test-mysql
- task: test-sqlite
test-pg:
desc: Test PostgreSQL
cmds:
- task: test-db
vars: {DATABASE: postgresql}
test-all:
desc: Test all databases (PG, MySQL, SQLite, SQLServer and Oracle)
cmds:
- task: test-pg
- task: test-mysql
- task: test-sqlite
- task: test-sqlserver
- task: test-oracle
test-mysql:
desc: Test MySQL
cmds:
- task: test-db
vars: {DATABASE: mysql}
test-pg:
desc: Test PostgreSQL
cmds:
- task: test-db
vars: {DATABASE: postgresql}
test-sqlite:
desc: Test SQLite
cmds:
- task: test-db
vars: {DATABASE: sqlite}
test-mysql:
desc: Test MySQL
cmds:
- task: test-db
vars: {DATABASE: mysql}
test-sqlserver:
desc: Test SQLServer
cmds:
- task: test-db
vars: {DATABASE: sqlserver}
test-sqlite:
desc: Test SQLite
cmds:
- task: test-db
vars: {DATABASE: sqlite}
test-oracle:
desc: Test Oracle
cmds:
- task: test-db
vars: {DATABASE: oracle}
test-sqlserver:
desc: Test SQLServer
cmds:
- task: test-db
vars: {DATABASE: sqlserver}
test-db:
cmds:
- go test -v -tags {{.DATABASE}}
test-oracle:
desc: Test Oracle
cmds:
- task: test-db
vars: {DATABASE: oracle}
test-db:
cmds:
- go test -v -tags {{.DATABASE}}

View File

@ -32,6 +32,16 @@ type queryable interface {
QueryRow(string, ...interface{}) *sql.Row
}
// batchSplitter is an interface with method which returns byte slice for
// splitting SQL batches. This need to split sql statements and run its
// separately.
//
// For Microsoft SQL Server batch splitter is "GO". For details see
// https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go
type batchSplitter interface {
splitter() []byte
}
var (
_ Helper = &MySQL{}
_ Helper = &PostgreSQL{}

View File

@ -40,7 +40,8 @@ func (h *MySQL) tableNames(q queryable) ([]string, error) {
query := `
SELECT table_name
FROM information_schema.tables
WHERE table_schema=?;
WHERE table_schema = ?
AND table_type = 'BASE TABLE';
`
dbName, err := h.databaseName(q)
if err != nil {

View File

@ -62,13 +62,12 @@ func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
var tables []string
sql := `
SELECT pg_namespace.nspname || '.' || pg_class.relname
FROM pg_class
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'r'
AND
pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema')
AND pg_namespace.nspname NOT LIKE 'pg_toast%';
SELECT pg_namespace.nspname || '.' || pg_class.relname
FROM pg_class
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'r'
AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema')
AND pg_namespace.nspname NOT LIKE 'pg_toast%';
`
rows, err := q.Query(sql)
if err != nil {
@ -121,11 +120,10 @@ func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, err
var constraints []pgConstraint
sql := `
SELECT table_schema || '.' || table_name,
constraint_name
SELECT table_schema || '.' || table_name, constraint_name
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
AND is_deferrable = 'NO'
AND is_deferrable = 'NO'
`
rows, err := q.Query(sql)
if err != nil {
@ -269,8 +267,8 @@ func (h *PostgreSQL) afterLoad(q queryable) error {
func (h *PostgreSQL) getChecksum(q queryable, tableName string) (string, error) {
sqlStr := fmt.Sprintf(`
SELECT md5(CAST((array_agg(t.*)) AS TEXT))
FROM %s AS t
SELECT md5(CAST((array_agg(t.*)) AS TEXT))
FROM %s AS t
`,
h.quoteKeyword(tableName),
)

View File

@ -29,7 +29,7 @@ func (*SQLite) tableNames(q queryable) ([]string, error) {
query := `
SELECT name
FROM sqlite_master
WHERE type='table';
WHERE type = 'table';
`
rows, err := q.Query(query)
if err != nil {

View File

@ -3,6 +3,7 @@ package testfixtures
import (
"database/sql"
"fmt"
"strings"
)
// SQLServer is the helper for SQL Server for this package.
@ -28,8 +29,12 @@ func (*SQLServer) paramType() int {
return paramTypeQuestion
}
func (*SQLServer) quoteKeyword(str string) string {
return fmt.Sprintf("[%s]", str)
func (*SQLServer) quoteKeyword(s string) string {
parts := strings.Split(s, ".")
for i, p := range parts {
parts[i] = fmt.Sprintf(`[%s]`, p)
}
return strings.Join(parts, ".")
}
func (*SQLServer) databaseName(q queryable) (string, error) {
@ -39,7 +44,7 @@ func (*SQLServer) databaseName(q queryable) (string, error) {
}
func (*SQLServer) tableNames(q queryable) ([]string, error) {
rows, err := q.Query("SELECT table_name FROM information_schema.tables")
rows, err := q.Query("SELECT table_schema + '.' + table_name FROM information_schema.tables")
if err != nil {
return nil, err
}
@ -59,14 +64,14 @@ func (*SQLServer) tableNames(q queryable) ([]string, error) {
return tables, nil
}
func (*SQLServer) tableHasIdentityColumn(q queryable, tableName string) bool {
func (h *SQLServer) tableHasIdentityColumn(q queryable, tableName string) bool {
sql := `
SELECT COUNT(*)
FROM SYS.IDENTITY_COLUMNS
WHERE OBJECT_NAME(OBJECT_ID) = ?
WHERE OBJECT_ID = OBJECT_ID(?)
`
var count int
q.QueryRow(sql, tableName).Scan(&count)
q.QueryRow(sql, h.quoteKeyword(tableName)).Scan(&count)
return count > 0
}
@ -120,3 +125,11 @@ func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction)
return tx.Commit()
}
// splitter is a batchSplitter interface implementation. We need it for
// SQL Server because commands like a `CREATE SCHEMA...` and a `CREATE TABLE...`
// could not be executed in the same batch.
// See https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175502(v=sql.105)#rules-for-using-batches
func (*SQLServer) splitter() []byte {
return []byte("GO\n")
}