-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_test.go
140 lines (112 loc) · 3.24 KB
/
docker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package postgres
import (
"fmt"
"testing"
contractstesting "github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/mocks/config"
"github.com/goravel/postgres/contracts"
"github.com/stretchr/testify/suite"
)
type DockerTestSuite struct {
suite.Suite
connection string
database string
username string
password string
mockConfig *config.Config
docker *Docker
}
func TestDockerTestSuite(t *testing.T) {
suite.Run(t, new(DockerTestSuite))
}
func (s *DockerTestSuite) SetupTest() {
s.connection = "default"
s.database = "goravel"
s.username = "goravel"
s.password = "Framework!123"
s.mockConfig = config.NewConfig(s.T())
s.docker = NewDocker(NewConfig(s.mockConfig, s.connection), s.database, s.username, s.password)
}
func (s *DockerTestSuite) Test_Build_Config_AddData_Fresh_Shutdown() {
s.Nil(s.docker.Build())
instance, err := s.docker.connect()
s.Nil(err)
s.NotNil(instance)
s.Equal("127.0.0.1", s.docker.Config().Host)
s.Equal(s.database, s.docker.Config().Database)
s.Equal(s.username, s.docker.Config().Username)
s.Equal(s.password, s.docker.Config().Password)
s.True(s.docker.Config().Port > 0)
res := instance.Exec(`
CREATE TABLE users (
id SERIAL PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL
);
`)
s.Nil(res.Error)
res = instance.Exec(`
INSERT INTO users (name) VALUES ('goravel');
`)
s.Nil(res.Error)
s.Equal(int64(1), res.RowsAffected)
var count int64
res = instance.Raw(`
SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public' and table_name = 'users';
`).Scan(&count)
s.Nil(res.Error)
s.Equal(int64(1), count)
s.Nil(s.docker.Fresh())
res = instance.Raw(`
SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public' and table_name = 'users';
`).Scan(&count)
s.Nil(res.Error)
s.Equal(int64(0), count)
databaseDriver, err := s.docker.Database("another")
s.NoError(err)
s.NotNil(databaseDriver)
s.Nil(s.docker.Shutdown())
}
func (s *DockerTestSuite) TestDatabase() {
s.Nil(s.docker.Build())
_, err := s.docker.connect()
s.Nil(err)
docker, err := s.docker.Database("another")
s.Nil(err)
s.NotNil(docker)
dockerImpl := docker.(*Docker)
_, err = dockerImpl.connect()
s.Nil(err)
s.Nil(s.docker.Shutdown())
}
func (s *DockerTestSuite) TestImage() {
image := contractstesting.Image{
Repository: "postgres",
}
s.docker.Image(image)
s.Equal(&image, s.docker.image)
}
func (s *DockerTestSuite) TestReady() {
s.Run("config contains write config", func() {
s.Nil(s.docker.Build())
s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.write", s.connection)).Return([]contracts.Config{
{
Host: "127.0.0.1",
},
}).Once()
s.mockConfig.EXPECT().Add(fmt.Sprintf("database.connections.%s.write", s.connection), []contracts.Config{
{
Host: "127.0.0.1",
Port: s.docker.port,
},
}).Once()
s.Nil(s.docker.Ready())
s.Nil(s.docker.Shutdown())
})
s.Run("config does not contain write config", func() {
s.Nil(s.docker.Build())
s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.write", s.connection)).Return(nil).Once()
s.mockConfig.EXPECT().Add(fmt.Sprintf("database.connections.%s.port", s.connection), s.docker.port).Once()
s.Nil(s.docker.Ready())
s.Nil(s.docker.Shutdown())
})
}