From 2214026432251dd39c4578e01e76522d96d6508a Mon Sep 17 00:00:00 2001 From: Rahul Rumalla Date: Sat, 6 Oct 2018 16:37:21 +0200 Subject: [PATCH] Removing unused code and improving coverage. --- papergres.go | 97 +++++++---------------------------------------- papergres_test.go | 85 ++++++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 122 deletions(-) diff --git a/papergres.go b/papergres.go index b1c4c81..b1e4dd7 100644 --- a/papergres.go +++ b/papergres.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "log" "net" "net/url" "reflect" @@ -114,10 +115,6 @@ func logDebug(args ...interface{}) { Log.Debug(args) } -func logDebugf(format string, args ...interface{}) { - Log.Debug(format, args) -} - // Schema holds the schema to query along with the Database type Schema struct { Name string @@ -172,10 +169,6 @@ func (conn Connection) NewDatabase() *Database { } } -const ( - databaseURLRegex = "(?i)^postgres://(?:([^:@]+):([^@]*)@)?([^@/:]+):(\\d+)/(.*)$" -) - // NewConnection creates and returns the Connection object to the postgres server func NewConnection(databaseURL string, appName string) Connection { u, err := url.Parse(databaseURL) @@ -211,14 +204,14 @@ func NewConnection(databaseURL string, appName string) Connection { } // NewDomain creates a new Domain -func (db *Database) NewDomain(name, pkg string, schema ...string) *Domain { - return &Domain{ - db: db, - schema: schema, - pkg: pkg, - name: name, - } -} +// func (db *Database) NewDomain(name, pkg string, schema ...string) *Domain { +// return &Domain{ +// db: db, +// schema: schema, +// pkg: pkg, +// name: name, +// } +// } // NewResult returns an empty Result func NewResult() *Result { @@ -229,49 +222,6 @@ func NewResult() *Result { return result } -// Database returns back the Database -func (d *Domain) Database() *Database { - return d.db -} - -// Schema returns domain schemas -func (d *Domain) Schema() []string { - return d.schema -} - -// Package returns the domain package -func (d *Domain) Package() string { - return d.pkg -} - -// Name returns the domain name -func (d *Domain) Name() string { - return d.name -} - -// Namespace is the fully qualified name of a Domain -func (d *Domain) Namespace() string { - s := fmt.Sprintf("%s.%s.", d.name, d.pkg) - for _, schema := range d.schema { - s += schema + "_" - } - s = strings.TrimRight(s, "_") - return s -} - -func (d *Domain) String() string { - return fmt.Sprintf(` - Name... %s - Package %s - Schema - %s - Database %s`, - d.Name(), - d.Package(), - strings.Join(d.Schema(), "\n\t"), - prettifyConnString(d.db.ConnectionString())) -} - func (q *Query) String() string { return fmt.Sprintf(` Query: @@ -354,16 +304,12 @@ func Reset() { // Shutdown performs a graceful shutdown of all DBs func Shutdown() { for _, db := range openDBs { - db.Close() + if err := db.Close(); err != nil { + log.Fatalf("Error shutting down DB: %s", err.Error()) + } } } -// SetConnection will set the connection to the passed in value -func (db *Database) SetConnection(c Connection) { - db.conn = &c - db.connString = "" -} - // CreateDatabase creates a default database // Good for use during testing and local dev func (db *Database) CreateDatabase() *Result { @@ -385,15 +331,11 @@ CREATE DATABASE %s conn := db.Connection() conn.Database = "" - db.SetConnection(conn) + db.conn = &conn + db.connString = "" return db.Query(sql).ExecNonQuery() } -// Ping tests the database connection -func (d *Domain) Ping() error { - return d.Database().Ping() -} - // Ping tests the database connection func (db *Database) Ping() error { return open(db.ConnectionString()).Ping() @@ -404,17 +346,6 @@ func (db *Database) Stats() sql.DBStats { return open(db.ConnectionString()).Stats() } -// IsLocal determines if a database host URL is local -func IsLocal(hostURL string, localURLs ...string) bool { - for _, u := range localURLs { - if strings.Contains(strings.ToLower(hostURL), u) { - return true - } - } - return strings.Contains(strings.ToLower(hostURL), "localhost") || - strings.Contains(strings.ToLower(hostURL), "127.0.0.1") -} - // Schema allows for certain operations that require a specific schema func (db *Database) Schema(name string) *Schema { return &Schema{ diff --git a/papergres_test.go b/papergres_test.go index c628e00..754bdf0 100644 --- a/papergres_test.go +++ b/papergres_test.go @@ -32,6 +32,52 @@ type Character struct { CreatedBy string `db:"created_by"` } +func TestCanPing(t *testing.T) { + conn := NewConnection(testDbURL, "papergres-tests") + err := conn.NewDatabase().Ping() + assert.Nil(t, err) +} + +func TestCanCutFirstIndex(t *testing.T) { + tt := []struct { + name string + input string + separator string + expectedLeft string + expectedRight string + }{ + { + "case 1", + "a.b", + ".", + "a", + "b", + }, + { + "case 2", + "a.b.c.d", + ".", + "a", + "b.c.d", + }, + { + "case 3", + "abcd", + ".", + "abcd", + "", + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + left, right := cutFirstIndex(tc.input, tc.separator) + assert.Equal(t, tc.expectedLeft, left) + assert.Equal(t, tc.expectedRight, right) + }) + } +} + func TestSetupTeardown(t *testing.T) { Log = &testLogger{} err := setup() @@ -286,45 +332,6 @@ func TestCanUpdate(t *testing.T) { assert.Equal(t, "The New Martian", martian.Title, "Update failed!") } -type testDatabase struct { - *Domain -} - -// func schema() *Schema { -// return defdb().Schema("paper") -// } - -// func defdb() *Database { -// return db(conn()).Database() -// } - -// func db(c Connection) *testDatabase { -// return &testDatabase{ -// domain(c), -// } -// } - -// func (db *testDatabase) GetDomain() *Domain { -// return db.Domain -// } - -// func domain(c Connection) *Domain { -// return c.NewDatabase().NewDomain("papergres", "papergres", "paper") -// } - -// func conn() Connection { -// return Connection{ -// Database: "papergres", -// User: "postgres", -// Password: "postgres", -// Host: "localhost", -// Port: "5432", -// AppName: "papergres", -// Timeout: 0, -// SSLMode: "disable", -// } -// } - type testLogger struct{} func (t *testLogger) Info(args ...interface{}) {