Browse Source

Make ServiceDB an interface called `Storer`

Implement a nop `Storer` called `NopStorage` which tests can make use of.
This was awful to write.
kegan/db-interface
Kegan Dougal 8 years ago
parent
commit
38ead2c174
  1. 6
      src/github.com/matrix-org/go-neb/database/db.go
  2. 140
      src/github.com/matrix-org/go-neb/database/interface.go
  3. 12
      src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go

6
src/github.com/matrix-org/go-neb/database/db.go

@ -17,15 +17,15 @@ type ServiceDB struct {
// A single global instance of the service DB.
// XXX: I can't think of any way of doing this without one without creating
// cyclical dependencies somewhere -- Kegan
var globalServiceDB *ServiceDB
var globalServiceDB Storer
// SetServiceDB sets the global service DB instance.
func SetServiceDB(db *ServiceDB) {
func SetServiceDB(db Storer) {
globalServiceDB = db
}
// GetServiceDB gets the global service DB instance.
func GetServiceDB() *ServiceDB {
func GetServiceDB() Storer {
return globalServiceDB
}

140
src/github.com/matrix-org/go-neb/database/interface.go

@ -0,0 +1,140 @@
package database
import (
"github.com/matrix-org/go-neb/api"
"github.com/matrix-org/go-neb/types"
)
// Storer is the interface which needs to be conformed to in order to persist Go-NEB data
type Storer interface {
StoreMatrixClientConfig(config api.ClientConfig) (oldConfig api.ClientConfig, err error)
LoadMatrixClientConfigs() (configs []api.ClientConfig, err error)
LoadMatrixClientConfig(userID string) (config api.ClientConfig, err error)
UpdateNextBatch(userID, nextBatch string) (err error)
LoadNextBatch(userID string) (nextBatch string, err error)
LoadService(serviceID string) (service types.Service, err error)
DeleteService(serviceID string) (err error)
LoadServicesForUser(serviceUserID string) (services []types.Service, err error)
LoadServicesByType(serviceType string) (services []types.Service, err error)
StoreService(service types.Service) (oldService types.Service, err error)
LoadAuthRealm(realmID string) (realm types.AuthRealm, err error)
LoadAuthRealmsByType(realmType string) (realms []types.AuthRealm, err error)
StoreAuthRealm(realm types.AuthRealm) (old types.AuthRealm, err error)
StoreAuthSession(session types.AuthSession) (old types.AuthSession, err error)
LoadAuthSessionByUser(realmID, userID string) (session types.AuthSession, err error)
LoadAuthSessionByID(realmID, sessionID string) (session types.AuthSession, err error)
RemoveAuthSession(realmID, userID string) error
LoadBotOptions(userID, roomID string) (opts types.BotOptions, err error)
StoreBotOptions(opts types.BotOptions) (oldOpts types.BotOptions, err error)
InsertFromConfig(cfg *api.ConfigFile) error
}
// NopStorage nops every store API call. This is intended to be embedded into derived structs
// in tests
type NopStorage struct{}
// StoreMatrixClientConfig NOP
func (s *NopStorage) StoreMatrixClientConfig(config api.ClientConfig) (oldConfig api.ClientConfig, err error) {
return api.ClientConfig{}, nil
}
// LoadMatrixClientConfigs NOP
func (s *NopStorage) LoadMatrixClientConfigs() (configs []api.ClientConfig, err error) {
return
}
// LoadMatrixClientConfig NOP
func (s *NopStorage) LoadMatrixClientConfig(userID string) (config api.ClientConfig, err error) {
return
}
// UpdateNextBatch NOP
func (s *NopStorage) UpdateNextBatch(userID, nextBatch string) (err error) {
return
}
// LoadNextBatch NOP
func (s *NopStorage) LoadNextBatch(userID string) (nextBatch string, err error) {
return
}
// LoadService NOP
func (s *NopStorage) LoadService(serviceID string) (service types.Service, err error) {
return
}
// DeleteService NOP
func (s *NopStorage) DeleteService(serviceID string) (err error) {
return
}
// LoadServicesForUser NOP
func (s *NopStorage) LoadServicesForUser(serviceUserID string) (services []types.Service, err error) {
return
}
// LoadServicesByType NOP
func (s *NopStorage) LoadServicesByType(serviceType string) (services []types.Service, err error) {
return
}
// StoreService NOP
func (s *NopStorage) StoreService(service types.Service) (oldService types.Service, err error) {
return
}
// LoadAuthRealm NOP
func (s *NopStorage) LoadAuthRealm(realmID string) (realm types.AuthRealm, err error) {
return
}
// LoadAuthRealmsByType NOP
func (s *NopStorage) LoadAuthRealmsByType(realmType string) (realms []types.AuthRealm, err error) {
return
}
// StoreAuthRealm NOP
func (s *NopStorage) StoreAuthRealm(realm types.AuthRealm) (old types.AuthRealm, err error) {
return
}
// StoreAuthSession NOP
func (s *NopStorage) StoreAuthSession(session types.AuthSession) (old types.AuthSession, err error) {
return
}
// LoadAuthSessionByUser NOP
func (s *NopStorage) LoadAuthSessionByUser(realmID, userID string) (session types.AuthSession, err error) {
return
}
// LoadAuthSessionByID NOP
func (s *NopStorage) LoadAuthSessionByID(realmID, sessionID string) (session types.AuthSession, err error) {
return
}
// RemoveAuthSession NOP
func (s *NopStorage) RemoveAuthSession(realmID, userID string) error {
return nil
}
// LoadBotOptions NOP
func (s *NopStorage) LoadBotOptions(userID, roomID string) (opts types.BotOptions, err error) {
return
}
// StoreBotOptions NOP
func (s *NopStorage) StoreBotOptions(opts types.BotOptions) (oldOpts types.BotOptions, err error) {
return
}
// InsertFromConfig NOP
func (s *NopStorage) InsertFromConfig(cfg *api.ConfigFile) error {
return nil
}

12
src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go

@ -7,7 +7,6 @@ import (
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/types"
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"net/http"
"net/url"
@ -45,16 +44,7 @@ func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
}
func TestHTMLEntities(t *testing.T) {
// FIXME: Make ServiceDB an interface so we don't need to do this and import sqlite3!
// We are NOT interested in db operations, but need them because OnPoll will
// call StoreService.
db, err := database.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal("Failed to create in-memory db: ", err)
return
}
database.SetServiceDB(db)
database.SetServiceDB(&database.NopStorage{})
feedURL := "https://thehappymaskshop.hyrule"
// Replace the cachingClient with a mock so we can intercept RSS requests
rssTrans := struct{ MockTransport }{}

Loading…
Cancel
Save