Skip to content

Commit

Permalink
Merge pull request #9 from Paperchain/testcov
Browse files Browse the repository at this point in the history
Removing unused code and improving coverage.
  • Loading branch information
rahulrumalla authored Oct 6, 2018
2 parents ba6d25e + 2214026 commit 1f7d214
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 122 deletions.
97 changes: 14 additions & 83 deletions papergres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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{
Expand Down
85 changes: 46 additions & 39 deletions papergres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{}) {
Expand Down

0 comments on commit 1f7d214

Please sign in to comment.