Skip to content

Commit

Permalink
use testify in all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Jun 23, 2024
1 parent d3cb2eb commit 1bb9dd8
Show file tree
Hide file tree
Showing 41 changed files with 785 additions and 1,108 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
SHELL ?= /bin/bash
SHELL ?= bash
CPU_CORES ?= $(shell nproc)

PARALLEL_FLAGS ?= --halt-on-error 2 --jobs=$(CPU_CORES) -v -u

TEST_FLAGS ?=
TEST_FLAGS ?= -v -failfast -race -timeout 20m

UPPER_DB_LOG ?= WARN

Expand Down
10 changes: 5 additions & 5 deletions adapter/cockroachdb/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SHELL ?= bash

COCKROACHDB_VERSION ?= v23.1.11
COCKROACHDB_SUPPORTED ?= $(COCKROACHDB_VERSION) v22.2.15 v21.2.16
PROJECT ?= upper_cockroachdb_$(COCKROACHDB_VERSION)
COCKROACHDB_VERSION ?= v23.2.6
COCKROACHDB_SUPPORTED ?= $(COCKROACHDB_VERSION) v22.2.19 v21.2.17
PROJECT ?= $(subst .,_,"upper_cockroachdb_$(COCKROACHDB_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 26257
Expand Down Expand Up @@ -30,7 +30,7 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 5 && \
psql -Uroot -h$(DB_HOST) -p$(DB_PORT) postgres -c "\
DROP USER IF EXISTS $(DB_USERNAME); \
Expand All @@ -41,7 +41,7 @@ server-up: server-down
"

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
15 changes: 8 additions & 7 deletions adapter/cockroachdb/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"database/sql/driver"
"time"

"github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqlbuilder"
)

Expand Down Expand Up @@ -90,12 +91,12 @@ func DecodeJSONB(dst interface{}, src interface{}) error {
//
// Example:
//
// type MyCustomStruct struct {
// ID int64 `db:"id" json:"id"`
// Name string `db:"name" json:"name"`
// ...
// cockroachdb.JSONBConverter
// }
// type MyCustomStruct struct {
// ID int64 `db:"id" json:"id"`
// Name string `db:"name" json:"name"`
// ...
// cockroachdb.JSONBConverter
// }
type JSONBConverter struct {
}

Expand Down Expand Up @@ -144,7 +145,7 @@ func (t *timeWrapper) Scan(src interface{}) error {
}

func (d *database) ConvertValueContext(ctx context.Context, in interface{}) interface{} {
tz, _ := ctx.Value("timezone").(*time.Location)
tz, _ := ctx.Value(db.ContextKey("timezone")).(*time.Location)

switch v := in.(type) {
case *time.Time:
Expand Down
5 changes: 4 additions & 1 deletion adapter/cockroachdb/database_pgx.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !pq
// +build !pq

// Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
Expand Down Expand Up @@ -26,7 +27,9 @@ package cockroachdb
import (
"context"
"database/sql"

_ "github.com/jackc/pgx/v4/stdlib"
"github.com/upper/db/v4"
"github.com/upper/db/v4/internal/sqladapter"
"time"
)
Expand All @@ -38,7 +41,7 @@ func (*database) OpenDSN(sess sqladapter.Session, dsn string) (*sql.DB, error) {
}
if tz := connURL.Options["timezone"]; tz != "" {
loc, _ := time.LoadLocation(tz)
ctx := context.WithValue(sess.Context(), "timezone", loc)
ctx := context.WithValue(sess.Context(), db.ContextKey("timezone"), loc)
sess.SetContext(ctx)
}
return sql.Open("pgx", dsn)
Expand Down
3 changes: 0 additions & 3 deletions adapter/cockroachdb/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
version: '3'

services:

server:
image: cockroachdb/cockroach:${COCKROACHDB_VERSION:-v2}
environment:
Expand Down
6 changes: 3 additions & 3 deletions adapter/mongo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL ?= bash

MONGO_VERSION ?= 4
MONGO_SUPPORTED ?= $(MONGO_VERSION) 3
PROJECT ?= upper_mongo_$(MONGO_VERSION)
PROJECT ?= $(subst .,_,"upper_mongo_$(MONGO_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 27017
Expand Down Expand Up @@ -31,11 +31,11 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 10

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
80 changes: 22 additions & 58 deletions adapter/mongo/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@ package mongo

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConnectionURL(t *testing.T) {

c := ConnectionURL{}

// Default connection string is only the protocol.
if c.String() != "" {
t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
}
assert.Equal(t, "", c.String(), "Expecting default connectiong string to be empty")

// Adding a database name.
c.Database = "myfilename"

if c.String() != "mongodb://myfilename" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://myfilename", c.String())

// Adding an option.
c.Options = map[string]string{
Expand All @@ -54,31 +51,22 @@ func TestConnectionURL(t *testing.T) {
// Setting host.
c.Host = "localhost"

if c.String() != "mongodb://user:pass@localhost/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost/myfilename?cache=foobar&mode=ro", c.String())

// Setting host and port.
c.Host = "localhost:27017"

if c.String() != "mongodb://user:pass@localhost:27017/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost:27017/myfilename?cache=foobar&mode=ro", c.String())

// Setting cluster.
c.Host = "localhost,1.2.3.4,example.org:1234"

if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro", c.String())

// Setting another database.
c.Database = "another_database"

if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}

assert.Equal(t, "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro", c.String())
}

func TestParseConnectionURL(t *testing.T) {
Expand All @@ -88,48 +76,24 @@ func TestParseConnectionURL(t *testing.T) {

s = "mongodb:///mydatabase"

if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
u, err = ParseURL(s)
require.NoError(t, err)

if u.Database != "mydatabase" {
t.Fatal("Failed to parse database.")
}
assert.Equal(t, "mydatabase", u.Database)

s = "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro"

if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
u, err = ParseURL(s)
require.NoError(t, err)

if u.Database != "another_database" {
t.Fatal("Failed to get database.")
}

if u.Options["cache"] != "foobar" {
t.Fatal("Expecting option.")
}

if u.Options["mode"] != "ro" {
t.Fatal("Expecting option.")
}

if u.User != "user" {
t.Fatal("Expecting user.")
}

if u.Password != "pass" {
t.Fatal("Expecting password.")
}

if u.Host != "localhost,1.2.3.4,example.org:1234" {
t.Fatal("Expecting host.")
}
assert.Equal(t, "another_database", u.Database)
assert.Equal(t, "foobar", u.Options["cache"])
assert.Equal(t, "ro", u.Options["mode"])
assert.Equal(t, "user", u.User)
assert.Equal(t, "pass", u.Password)
assert.Equal(t, "localhost,1.2.3.4,example.org:1234", u.Host)

s = "http://example.org"

if _, err = ParseURL(s); err == nil {
t.Fatal("Expecting error.")
}

_, err = ParseURL(s)
require.Error(t, err)
}
6 changes: 3 additions & 3 deletions adapter/mssql/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL ?= bash

MSSQL_VERSION ?= 2022-latest
MSSQL_SUPPORTED ?= $(MSSQL_VERSION) 2019-latest
PROJECT ?= upper_mssql_$(MSSQL_VERSION)
PROJECT ?= $(subst .,_,"upper_mssql_$(MSSQL_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 1433
Expand Down Expand Up @@ -31,11 +31,11 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 20

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
50 changes: 14 additions & 36 deletions adapter/mssql/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@ package mssql

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConnectionURL(t *testing.T) {

c := ConnectionURL{}

// Zero value equals to an empty string.
if c.String() != "" {
t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
}
assert.Equal(t, "", c.String(), "Expecting default connectiong string to be empty")

// Adding a database name.
c.Database = "mydbname"

if c.String() != "sqlserver://127.0.0.1?database=mydbname" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "sqlserver://127.0.0.1?database=mydbname", c.String())

// Adding an option.
c.Options = map[string]string{
Expand All @@ -48,9 +45,7 @@ func TestConnectionURL(t *testing.T) {
"instance": "instance1",
}

if c.String() != "sqlserver://127.0.0.1/instance1?connection+timeout=30&database=mydbname&param1=value1" {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, "sqlserver://127.0.0.1/instance1?connection+timeout=30&database=mydbname&param1=value1", c.String())

// Setting default options
c.Options = nil
Expand All @@ -59,16 +54,11 @@ func TestConnectionURL(t *testing.T) {
c.User = "user"
c.Password = "pass"

if c.String() != `sqlserver://user:[email protected]?database=mydbname` {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, `sqlserver://user:[email protected]?database=mydbname`, c.String())

// Setting host.
c.Host = "1.2.3.4:1433"

if c.String() != `sqlserver://user:[email protected]:1433?database=mydbname` {
t.Fatal(`Test failed, got:`, c.String())
}
assert.Equal(t, `sqlserver://user:[email protected]:1433?database=mydbname`, c.String())
}

func TestParseConnectionURL(t *testing.T) {
Expand All @@ -78,23 +68,11 @@ func TestParseConnectionURL(t *testing.T) {

s = "sqlserver://user:[email protected]:1433?connection+timeout=30&database=mydbname&param1=value1"

if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}

if u.User != "user" {
t.Fatal("Expecting username.")
}

if u.Password != "pass" {
t.Fatal("Expecting password.")
}

if u.Host != "127.0.0.1:1433" {
t.Fatal("Expecting host.")
}
u, err = ParseURL(s)
require.NoError(t, err)

if u.Database != "mydbname" {
t.Fatal("Expecting database.")
}
assert.Equal(t, "user", u.User)
assert.Equal(t, "pass", u.Password)
assert.Equal(t, "127.0.0.1:1433", u.Host)
assert.Equal(t, "mydbname", u.Database)
}
8 changes: 4 additions & 4 deletions adapter/mysql/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SHELL ?= bash

MYSQL_VERSION ?= 8.1
MYSQL_VERSION ?= 8.4
MYSQL_SUPPORTED ?= $(MYSQL_VERSION) 5.7
PROJECT ?= upper_mysql_$(MYSQL_VERSION)
PROJECT ?= $(subst .,_,"upper_mysql_$(MYSQL_VERSION)")

DB_HOST ?= 127.0.0.1
DB_PORT ?= 3306
Expand Down Expand Up @@ -31,11 +31,11 @@ test-no-race:
go test -v -failfast $(TEST_FLAGS)

server-up: server-down
docker-compose -p $(PROJECT) up -d && \
docker compose -p $(PROJECT) up -d && \
sleep 15

server-down:
docker-compose -p $(PROJECT) down
docker compose -p $(PROJECT) down

test-extended:
parallel $(PARALLEL_FLAGS) \
Expand Down
Loading

0 comments on commit 1bb9dd8

Please sign in to comment.