File tree 3 files changed +25
-12
lines changed
3 files changed +25
-12
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ func init() {
23
23
// Initialize gets the datasources from persistence.json
24
24
func Initialize () {
25
25
var persistence config.Persistence
26
- persistence = config .GetPersistenceConfig ()
26
+ persistence = config .GetPersistenceConfig ("persistence.json" )
27
27
28
28
for _ , datasource := range persistence .Datasources {
29
29
driver := new (manager.GoedbSQLDriver )
Original file line number Diff line number Diff line change @@ -2,9 +2,7 @@ package config
2
2
3
3
import (
4
4
"encoding/json"
5
- "fmt"
6
5
"io/ioutil"
7
- "os"
8
6
)
9
7
10
8
// Persistence represents the collection of datasources defined by the developer in persistence.json
@@ -21,15 +19,13 @@ type Datasource struct {
21
19
}
22
20
23
21
// GetPersistenceConfig generates the persistence struct from persistence.json
24
- func GetPersistenceConfig () Persistence {
25
- raw , err := ioutil .ReadFile ("./persistence.json" )
26
- if err != nil {
27
- fmt .Println (err .Error ())
28
- os .Exit (1 )
29
- }
30
-
22
+ func GetPersistenceConfig (persistenceConfigFile string ) Persistence {
31
23
var persistence Persistence
32
-
33
- json .Unmarshal (raw , & persistence )
24
+ raw , err := ioutil .ReadFile (persistenceConfigFile )
25
+ if err == nil {
26
+ json .Unmarshal (raw , & persistence )
27
+ }else {
28
+ persistence .Datasources = make ([]Datasource , 0 )
29
+ }
34
30
return persistence
35
31
}
Original file line number Diff line number Diff line change
1
+ package tests
2
+
3
+ import (
4
+ "testing"
5
+ "github.com/plopezm/goedb/config"
6
+ "github.com/stretchr/testify/assert"
7
+ )
8
+
9
+ func TestGetPersistenceConfig (t * testing.T ){
10
+ persistence := config .GetPersistenceConfig ("persistence.json" )
11
+ assert .Equal (t ,2 , len (persistence .Datasources ))
12
+ }
13
+
14
+ func TestGetPersistenceConfigNotFound (t * testing.T ){
15
+ persistence := config .GetPersistenceConfig ("Notfoundfile.json" )
16
+ assert .Equal (t ,0 , len (persistence .Datasources ))
17
+ }
You can’t perform that action at this time.
0 commit comments