ok

package module
v0.0.0-...-8467458 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2019 License: MIT Imports: 13 Imported by: 0

README

ClickHouse testing suite Build Status codecov GoDoc

This is a small framework to help test Go and ClickHouse applications.

Example:

package ok_test

import (
	"bytes"
	"database/sql"
	"encoding/csv"
	"testing"

	"github.com/ClickHouse-Ninja/ok"
	_ "github.com/kshvakov/clickhouse"
	"github.com/stretchr/testify/assert"
)

type App struct {
	conn *sql.DB
}

func (app *App) Count() (count int, err error) {
	if err = app.conn.QueryRow("SELECT COUNT() FROM tester.table").Scan(&count); err != nil {
		return 0, err
	}
	return count, nil
}

func TestExapmple(t *testing.T) {
	ok := ok.Connect(t, "tcp://127.0.0.1:9000?debug=0")
	if ok.DatabaseExists("tester") {
		t.Fatal("database 'tester' is already exists")
	}
	defer ok.Clear()
	const ddl = `
	CREATE DATABASE tester;
	CREATE TABLE tester.table (
		event_time   DateTime
		, event_type String
		, user_id    UInt64
		, value      UInt32
	) Engine Memory;
	`
	if err := ok.Exec(ddl); err != nil {
		t.Fatalf("an error occurred while creating the test table: %v", err)
	}
	var (
		buf    bytes.Buffer
		writer = csv.NewWriter(&buf)
	)
	writer.Comma = '\t'
	writer.WriteAll([][]string{
		[]string{"2019-03-08 21:00:00", "view", "1", "2"},
		[]string{"2019-03-08 21:00:01", "click", "1", "2"},
	})
	writer.Flush()
	if ok.CopyFromTSVReader(&buf, "INSERT INTO tester.table (event_time, event_type, user_id, value) VALUES") {
		app := App{
			conn: ok.DB(),
		}
		if count, err := app.Count(); assert.NoError(t, err) {
			assert.Equal(t, 2, count)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClickHouse

type ClickHouse interface {
	DB() *sql.DB
	Version() (*Version, error)
	Exec(query string) error
	ExecFromFile(path string) error
	SetSearchPath(path ...string)
	ShowDatabases() ([]string, error)
	ShowTables(from ...string) ([]string, error)
	DatabaseExists(database string) bool
	TableExists(database, table string) bool
	DictionaryExists(dictionary string) bool
	ReloadDictionary(dictionary string) bool
	CopyFromCSVReader(r io.Reader, sql string) bool
	CopyFromTSVReader(r io.Reader, sql string) bool
	CopyFromCSVFile(path, sql string) bool
	CopyFromTSVFile(path, sql string) bool
	DropDatabase(database string) bool
	DropTable(database, table string) bool
	Clear() bool
}

func Connect

func Connect(test *testing.T, dsn string) ClickHouse

type Version

type Version struct {
	Major int
	Minor int
	Patch int
}

func (*Version) Equal

func (v *Version) Equal(v2 *Version) bool

func (*Version) Less

func (v *Version) Less(v2 *Version) bool

func (*Version) String

func (v *Version) String() string

Jump to

Keyboard shortcuts

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