Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to the Source. #9

Merged
merged 4 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright © 2022 Meroxa, Inc. & Yalantis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this 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.

package clickhouse

import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"sync/atomic"
"testing"

"github.com/conduitio-labs/conduit-connector-clickhouse/config"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/matryer/is"
)

type driver struct {
sdk.ConfigurableAcceptanceTestDriver

counter int32
}

// GenerateRecord generates a random sdk.Record.
func (d *driver) GenerateRecord(t *testing.T, operation sdk.Operation) sdk.Record {
atomic.AddInt32(&d.counter, 1)

return sdk.Record{
Position: nil,
Operation: operation,
Metadata: map[string]string{
"clickhouse.table": d.Config.SourceConfig[config.Table],
},
Key: sdk.StructuredData{
"int_type": d.counter,
},
Payload: sdk.Change{After: sdk.RawData(
fmt.Sprintf(`{"int_type":%d,"string_type":"%s"}`, d.counter, uuid.NewString()),
)},
}
}

func TestAcceptance(t *testing.T) {
cfg := prepareConfig(t)

is := is.New(t)

sdk.AcceptanceTest(t, &driver{
ConfigurableAcceptanceTestDriver: sdk.ConfigurableAcceptanceTestDriver{
Config: sdk.ConfigurableAcceptanceTestDriverConfig{
Connector: Connector,
SourceConfig: cfg,
DestinationConfig: cfg,
BeforeTest: func(t *testing.T) {
err := createTable(cfg[config.URL], cfg[config.Table])
is.NoErr(err)
},
AfterTest: func(t *testing.T) {
err := dropTables(cfg[config.URL], cfg[config.Table])
is.NoErr(err)
},
},
},
})
}

// prepareConfig receives the connection URL from the environment variable
// and prepares configuration map.
func prepareConfig(t *testing.T) map[string]string {
url := os.Getenv("CLICKHOUSE_URL")
if url == "" {
t.Skip("CLICKHOUSE_URL env var must be set")

return nil
}

return map[string]string{
config.URL: url,
config.Table: fmt.Sprintf("CONDUIT_TEST_%s", randString(6)),
config.KeyColumns: "int_type",
config.OrderingColumn: "int_type",
}
}

// createTable creates test table.
func createTable(url, table string) error {
db, err := sqlx.Open("clickhouse", url)
if err != nil {
return fmt.Errorf("open connection: %w", err)
}
defer db.Close()

_, err = db.Exec(fmt.Sprintf(`
CREATE TABLE %s
(
int_type Int32,
string_type String
) ENGINE ReplacingMergeTree() PRIMARY KEY int_type;`, table))
if err != nil {
return fmt.Errorf("execute create table query: %w", err)
}

return nil
}

// dropTables drops test table and tracking test table if exists.
func dropTables(url, table string) error {
db, err := sqlx.Open("clickhouse", url)
if err != nil {
return fmt.Errorf("open connection: %w", err)
}
defer db.Close()

_, err = db.Exec(fmt.Sprintf("DROP TABLE %s", table))
if err != nil {
return fmt.Errorf("execute drop table query: %w", err)
}

return nil
}

// generates a random string of length n.
func randString(n int) string {
b := make([]byte, n)
rand.Read(b) //nolint:errcheck // does not actually fail

return hex.EncodeToString(b)
}
6 changes: 3 additions & 3 deletions config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Source struct {
func ParseSource(cfg map[string]string) (Source, error) {
config, err := parseGeneral(cfg)
if err != nil {
return Source{}, err
return Source{}, fmt.Errorf("parse general config: %w", err)
}

sourceConfig := Source{
Expand All @@ -75,7 +75,7 @@ func ParseSource(cfg map[string]string) (Source, error) {

err = validateColumns(sourceConfig.OrderingColumn, sourceConfig.KeyColumns, sourceConfig.Columns)
if err != nil {
return Source{}, err
return Source{}, fmt.Errorf("validate config columns: %w", err)
}
}

Expand All @@ -88,7 +88,7 @@ func ParseSource(cfg map[string]string) (Source, error) {

err = validate(sourceConfig)
if err != nil {
return Source{}, err
return Source{}, fmt.Errorf("validate source config: %w", err)
}

return sourceConfig, nil
Expand Down
17 changes: 9 additions & 8 deletions config/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"errors"
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "id",
Columns: "id,name,age",
},
err: errors.New(`columns must include orderingColumn`),
err: errors.New("validate config columns: columns must include orderingColumn"),
},
{
name: "invalid config, missed key",
Expand All @@ -140,7 +141,7 @@ func TestParseSource(t *testing.T) {
Columns: "id,name,age",
OrderingColumn: "id",
},
err: errors.New(`"keyColumns" value must be set`),
err: errors.New(`validate source config: "keyColumns" value must be set`),
},
{
name: "invalid config, invalid batch size",
Expand All @@ -162,7 +163,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "name",
Columns: "name,age",
},
err: errOrderingColumnInclude,
err: fmt.Errorf("validate config columns: %s", errOrderingColumnInclude),
},
{
name: "invalid config, missed keyColumn in columns",
Expand All @@ -173,7 +174,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "name",
Columns: "id,age",
},
err: errKeyColumnsInclude,
err: fmt.Errorf("validate config columns: %w", errKeyColumnsInclude),
},
{
name: "invalid config, keyColumn is required",
Expand All @@ -184,7 +185,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: ",",
Columns: "id,age",
},
err: errRequired(KeyColumns),
err: fmt.Errorf("validate source config: %w", errRequired(KeyColumns)),
},
{
name: "invalid config, BatchSize is too big",
Expand All @@ -195,7 +196,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "id",
BatchSize: "100001",
},
err: errOutOfRange(BatchSize),
err: fmt.Errorf("validate source config: %w", errOutOfRange(BatchSize)),
},
{
name: "invalid config, BatchSize is zero",
Expand All @@ -206,7 +207,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "id",
BatchSize: "0",
},
err: errOutOfRange(BatchSize),
err: fmt.Errorf("validate source config: %w", errOutOfRange(BatchSize)),
},
{
name: "invalid config, BatchSize is negative",
Expand All @@ -217,7 +218,7 @@ func TestParseSource(t *testing.T) {
KeyColumns: "id",
BatchSize: "-1",
},
err: errOutOfRange(BatchSize),
err: fmt.Errorf("validate source config: %w", errOutOfRange(BatchSize)),
},
}

Expand Down
12 changes: 7 additions & 5 deletions destination/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type Writer interface {
type Destination struct {
sdk.UnimplementedDestination

config config.Destination
db *sqlx.DB
writer Writer
cfg config.Destination
}

// NewDestination initialises a new Destination.
Expand Down Expand Up @@ -75,7 +75,7 @@ func (d *Destination) Parameters() map[string]sdk.Parameter {
func (d *Destination) Configure(ctx context.Context, cfg map[string]string) (err error) {
sdk.Logger(ctx).Info().Msg("Configuring ClickHouse Destination...")

d.cfg, err = config.ParseDestination(cfg)
d.config, err = config.ParseDestination(cfg)
if err != nil {
return fmt.Errorf("parse destination config: %w", err)
}
Expand All @@ -87,7 +87,7 @@ func (d *Destination) Configure(ctx context.Context, cfg map[string]string) (err
func (d *Destination) Open(ctx context.Context) (err error) {
sdk.Logger(ctx).Info().Msg("Opening a ClickHouse Destination...")

db, err := sqlx.Open("clickhouse", d.cfg.URL)
db, err := sqlx.Open("clickhouse", d.config.URL)
if err != nil {
return fmt.Errorf("open connection: %w", err)
}
Expand All @@ -97,10 +97,12 @@ func (d *Destination) Open(ctx context.Context) (err error) {
return fmt.Errorf("ping: %w", err)
}

d.db = db

d.writer, err = writer.NewWriter(ctx, writer.Params{
DB: db,
Table: d.cfg.Table,
KeyColumns: d.cfg.KeyColumns,
Table: d.config.Table,
KeyColumns: d.config.KeyColumns,
})
if err != nil {
return fmt.Errorf("new writer: %w", err)
Expand Down
Loading