Skip to content

Commit

Permalink
tests: Set Up Tests for ClickHouse
Browse files Browse the repository at this point in the history
WORK IN PROGRESS
  • Loading branch information
kangoo13 committed Oct 5, 2020
1 parent 33938fa commit 28693e8
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ _testmain.go
.env
/testfixtures
/dist
.idea
8 changes: 7 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ tasks:
- task: test-db
vars: {DATABASE: postgresql}

test:clickhouse:
desc: Test ClickHouse
cmds:
- task: test-db
vars: { DATABASE: clickhousedb }

test:mysql:
desc: Test MySQL
cmds:
Expand Down Expand Up @@ -62,4 +68,4 @@ tasks:
docker:test:
cmds:
- docker-compose down -v
- docker-compose run testfixtures go test -v -tags 'postgresql sqlite mysql sqlserver cockroachdb'
- docker-compose run testfixtures go test -v -tags 'postgresql sqlite mysql sqlserver cockroachdb clickhousedb'
8 changes: 7 additions & 1 deletion clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testfixtures
import (
"database/sql"
"fmt"
_ "github.com/ClickHouse/clickhouse-go"
)

type clickhouse struct {
Expand Down Expand Up @@ -82,7 +83,6 @@ func (h *clickhouse) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction
}
defer tx.Rollback()


err = loadFn(tx)
if err != nil {
return err
Expand Down Expand Up @@ -132,3 +132,9 @@ func (h *clickhouse) getChecksum(q queryable, tableName string) (int64, error) {

return checksum.Int64, nil
}

// splitter is a batchSplitter interface implementation. We need it for
// ClickHouseDB because clickhouse doesn't support multi-statements.
func (*clickhouse) splitter() []byte {
return []byte(";\n")
}
19 changes: 19 additions & 0 deletions clickhouse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build mysql

package testfixtures

import (
"os"
"testing"

_ "github.com/go-sql-driver/mysql"
)

func TestClickHouse(t *testing.T) {
testLoader(
t,
"clickhouse",
os.Getenv("CLICKHOUSE_CONN_STRING"),
"testdata/schema/clickhouse.sql",
)
}
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ services:
- mysql
- sqlserver
- cockroachdb
- clickhousedb
environment:
PGPASSWORD: postgres
PG_CONN_STRING: host=postgresql user=postgres dbname=testfixtures_test port=5432 sslmode=disable

MYSQL_CONN_STRING: root:mysql@tcp(mysql)/testfixtures_test?multiStatements=true

CLICKHOUSE_CONN_STRING: tcp://clickhousedb:9000?debug=0&allowMultiQueries=true&parseTime=true&username=default&password=&database=testfixtures_test

SQLITE_CONN_STRING: testfixtures_test.sqlite3

SQLSERVER_CONN_STRING: server=sqlserver;database=master;user id=sa;password=SQL@1server;encrypt=disable
Expand All @@ -33,6 +36,11 @@ services:
MYSQL_DATABASE: testfixtures_test
MYSQL_ROOT_PASSWORD: mysql

clickhousedb:
image: yandex/clickhouse-server:20.9.2.20
environment:
CLICKHOUSE_DB: testfixtures_test

sqlserver:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/go-testfixtures/testfixtures/v3

require (
github.com/ClickHouse/clickhouse-go v1.4.3
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
github.com/go-sql-driver/mysql v1.4.1
github.com/jackc/pgx/v4 v4.6.0
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
github.com/ClickHouse/clickhouse-go v1.4.3 h1:iAFMa2UrQdR5bHJ2/yaSLffZkxpcOYQMCUuKeNXGdqc=
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/bkaradzic/go-lz4 v1.0.0 h1:RXc4wYsyz985CkXXeX04y4VnZFGG8Rd43pRaHsOXAKk=
github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand All @@ -10,6 +16,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73 h1:OGNva6WhsKst5OZf7eZOklDztV3hwtTHovdrLHV+MsA=
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -59,6 +66,7 @@ github.com/jackc/pgx/v4 v4.6.0/go.mod h1:vPh43ZzxijXUVJ+t/EmXBtFmbFVO72cuneCT9oA
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand All @@ -80,8 +88,11 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
4 changes: 4 additions & 0 deletions mocks_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (h *MockHelper) databaseName(queryable) (string, error) {
return h.dbName, nil
}

func (h *MockHelper) cleanTable(*sql.Tx, string) error {
return nil
}

// NewMockHelper returns MockHelper
func NewMockHelper(dbName string) *MockHelper {
return &MockHelper{dbName: dbName}
Expand Down
56 changes: 56 additions & 0 deletions testdata/schema/clickhouse.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
DROP TABLE IF EXISTS votes;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS posts_tags;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS users;

CREATE TABLE posts
(
id UInt64,
title String,
content String,
created_at DateTime DEFAULT '2000-01-01 00:00:00',
updated_at DateTime DEFAULT '2000-01-01 00:00:00'
) ENGINE MergeTree ORDER BY created_at;

CREATE TABLE tags
(
id UInt64,
name String,
created_at DateTime DEFAULT '2000-01-01 00:00:00',
updated_at DateTime DEFAULT '2000-01-01 00:00:00'
) ENGINE MergeTree ORDER BY created_at;

CREATE TABLE posts_tags
(
post_id UInt64,
tag_id UInt64,
created_at DateTime DEFAULT now()
) ENGINE MergeTree ORDER BY created_at;

CREATE TABLE comments
(
id UInt64,
post_id UInt64,
author_name String,
author_email String,
content String,
created_at DateTime DEFAULT '2000-01-01 00:00:00',
updated_at DateTime DEFAULT '2000-01-01 00:00:00'
) ENGINE MergeTree ORDER BY created_at;

CREATE TABLE votes
(
id UInt64,
comment_id UInt64,
created_at DateTime DEFAULT '2000-01-01 00:00:00',
updated_at DateTime DEFAULT '2000-01-01 00:00:00'
) ENGINE MergeTree ORDER BY created_at;

CREATE TABLE users
(
id UInt64,
attributes String,
created_at DateTime DEFAULT now()
) ENGINE MergeTree ORDER BY created_at
1 change: 1 addition & 0 deletions testfixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
Expand Down

0 comments on commit 28693e8

Please sign in to comment.