Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
#90: configurable database
Browse files Browse the repository at this point in the history
  • Loading branch information
Lior Tamari committed Sep 7, 2017
1 parent bb59782 commit 92bae3c
Show file tree
Hide file tree
Showing 15 changed files with 1,325 additions and 101 deletions.
127 changes: 127 additions & 0 deletions database/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* Copyright 2017 IBM Corp.
*
* 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 database

import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"fmt"
"github.com/IBM/ubiquity/utils/logs"
"errors"
)

var globalConnectionFactory ConnectionFactory = nil

func initConnectionFactory(connectionFactory ConnectionFactory) func() {
if globalConnectionFactory != nil {
panic("globalConnectionFactory already initialized")
}
globalConnectionFactory = connectionFactory
return func() { globalConnectionFactory = nil }
}

func InitPostgres(hostname string) func() {
return initConnectionFactory(&postgresFactory{host: hostname})
}

func InitSqlite(filepath string) func() {
return initConnectionFactory(&sqliteFactory{path: filepath})
}

func InitTestError() func() {
return initConnectionFactory(&testErrorFactory{})
}

type ConnectionFactory interface {
newConnection() (*gorm.DB, error)
}

type postgresFactory struct {
host string
}

type sqliteFactory struct {
path string
}

type testErrorFactory struct {
}

func (f *postgresFactory) newConnection() (*gorm.DB, error) {
return gorm.Open("postgres", fmt.Sprintf("host=%s user=postgres dbname=postgres sslmode=disable", f.host))
}

func (f *sqliteFactory) newConnection() (*gorm.DB, error) {
return gorm.Open("sqlite3", f.path)
}

func (f *testErrorFactory) newConnection() (*gorm.DB, error) {
return nil, errors.New("testErrorFactory")
}

type Connection struct {
factory ConnectionFactory
logger logs.Logger
db *gorm.DB
}

func NewConnection() Connection {
return Connection{logger: logs.GetLogger(), factory: globalConnectionFactory}
}

func (c *Connection) Open() (error) {
defer c.logger.Trace(logs.DEBUG)()
var err error

// sanity
if c.db != nil {
return c.logger.ErrorRet(errors.New("Connection already open"), "failed")
}

// open db connection
if c.db, err = c.factory.newConnection(); err != nil {
return c.logger.ErrorRet(err, "failed")
}

return nil
}

func (c *Connection) Close() (error) {
defer c.logger.Trace(logs.DEBUG)()
var err error

// sanity
if c.db == nil {
return c.logger.ErrorRet(errors.New("Connection already closed"), "failed")
}

// close db connection
err = c.db.Close()
c.db = nil
if err != nil {
return c.logger.ErrorRet(err, "failed")
}

return nil
}

func (c *Connection) GetDb() (*gorm.DB) {
defer c.logger.Trace(logs.DEBUG)()

return c.db
}
64 changes: 64 additions & 0 deletions database/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2017 IBM Corp.
*
* 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 database_test


import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/IBM/ubiquity/database"
)


var _ = Describe("Connection", func() {
var (
err error
)
BeforeEach(func() {
})

Context(".Open and Close", func() {
It("open and close success", func() {
dbConnection := database.NewConnection()
Expect(dbConnection.GetDb()).To(BeNil())
err = dbConnection.Open()
Expect(err).To(Not(HaveOccurred()))
Expect(dbConnection.GetDb()).To(Not(BeNil()))
err = dbConnection.Close()
Expect(err).To(Not(HaveOccurred()))
Expect(dbConnection.GetDb()).To(BeNil())
})
It("open fail", func() {
dbConnection := database.NewConnection()
Expect(dbConnection.GetDb()).To(BeNil())
err = dbConnection.Open()
Expect(err).To(Not(HaveOccurred()))
Expect(dbConnection.GetDb()).To(Not(BeNil()))
err = dbConnection.Open()
Expect(err).To(HaveOccurred())
err = dbConnection.Close()
Expect(err).To(Not(HaveOccurred()))
Expect(dbConnection.GetDb()).To(BeNil())
})
It("close fail", func() {
dbConnection := database.NewConnection()
Expect(dbConnection.GetDb()).To(BeNil())
err = dbConnection.Close()
Expect(err).To(HaveOccurred())
})
})
})
40 changes: 40 additions & 0 deletions database/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2017 IBM Corp.
*
* 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 database_test


import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"

"github.com/IBM/ubiquity/utils/logs"
"github.com/IBM/ubiquity/database"
"os"
)

const (
dbPath = "/tmp/database_test"
)

func TestDb(t *testing.T) {
RegisterFailHandler(Fail)
defer logs.InitStdoutLogger(logs.DEBUG)()
os.Remove(dbPath)
defer database.InitSqlite(dbPath)()
RunSpecs(t, "database Test Suite")
}
Loading

0 comments on commit 92bae3c

Please sign in to comment.