Skip to content

Commit

Permalink
feat: test on a non-primary database
Browse files Browse the repository at this point in the history
 - Create function NewTestDatabase
 - Remove function old newTestDatabase
 - Change tests
  • Loading branch information
Nikolay committed Apr 8, 2020
1 parent 92e5dd9 commit decd7f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestUpdateUsers(t *testing.T) {
t.Fatal(err)
}

database := redis.NewDatabase(logger, conf.Redis.GetSettings(), redis.Cli)
database := redis.NewTestDatabase(logger)
conf.Cleanup.Whitelist = []string{"Nikolay", ""}

users := []string{"Aleksey", "Arkadiy", "Emil"}
Expand Down
11 changes: 11 additions & 0 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const (
Cli DBSource = "Cli"
)

// Test data for configuration
const (
db = 1
testSource DBSource = "test"
)

// DbConnector contains redis pool
type DbConnector struct {
pool *redis.Pool
Expand Down Expand Up @@ -108,6 +114,11 @@ func NewDatabase(logger moira.Logger, config Config, source DBSource) *DbConnect
return connector
}

// NewTestDatabase use it only for tests
func NewTestDatabase(logger moira.Logger) *DbConnector {
return NewDatabase(logger, Config{Port: "6379", Host: "0.0.0.0", DB: db}, testSource)
}

// AllowStale returns a database instance, that prioritizes connections to slave nodes
// Should only be used for read accesses when data actuality is not needed
func (connector *DbConnector) AllowStale() moira.Database {
Expand Down
4 changes: 2 additions & 2 deletions database/redis/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

var config = Config{Port: "6379", Host: "0.0.0.0"}
var emptyConfig = Config{}
var testSource = DBSource("test")

// use it only for tests
// use it only for tests in redis package
func newTestDatabase(logger moira.Logger, config Config) *DbConnector {
config.DB = db
return NewDatabase(logger, config, testSource)
}

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var event = moira.NotificationEvent{
func TestNotifier(t *testing.T) {
mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
database := redis.NewDatabase(logger, redis.Config{Port: "6379", Host: "localhost"}, redis.Notifier)
database := redis.NewTestDatabase(logger)
metricsSourceProvider := metricSource.CreateMetricSourceProvider(local.Create(database), nil)
database.SaveContact(&contact)
database.SaveSubscription(&subscription)
Expand Down

0 comments on commit decd7f8

Please sign in to comment.