Skip to content

Commit ee0e906

Browse files
committed
♻️ move repository test to infrastructure/repository
1 parent a1e90f4 commit ee0e906

File tree

9 files changed

+235
-218
lines changed

9 files changed

+235
-218
lines changed

integration_tests/repository/contest_test.go infrastructure/repository/contest_test.go

+20-22
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/traPtitech/traPortfolio/domain"
1111
"github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e"
12-
"github.com/traPtitech/traPortfolio/integration_tests/testutils"
1312

14-
irepository "github.com/traPtitech/traPortfolio/infrastructure/repository"
1513
urepository "github.com/traPtitech/traPortfolio/usecases/repository"
1614
"github.com/traPtitech/traPortfolio/util/mockdata"
1715
"github.com/traPtitech/traPortfolio/util/random"
@@ -20,8 +18,8 @@ import (
2018
func TestContestRepository_GetContests(t *testing.T) {
2119
t.Parallel()
2220

23-
db := testutils.SetupGormDB(t)
24-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
21+
db := SetupTestGormDB(t)
22+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
2523
contest1 := mustMakeContest(t, repo, nil)
2624
contest2 := mustMakeContest(t, repo, nil)
2725
contests, err := repo.GetContests(context.Background())
@@ -35,8 +33,8 @@ func TestContestRepository_GetContests(t *testing.T) {
3533
func TestContestRepository_GetContest(t *testing.T) {
3634
t.Parallel()
3735

38-
db := testutils.SetupGormDB(t)
39-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
36+
db := SetupTestGormDB(t)
37+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
4038
contest1 := mustMakeContest(t, repo, nil)
4139
contest2 := mustMakeContest(t, repo, nil)
4240

@@ -52,8 +50,8 @@ func TestContestRepository_GetContest(t *testing.T) {
5250
func TestContestRepository_UpdateContest(t *testing.T) {
5351
t.Parallel()
5452

55-
db := testutils.SetupGormDB(t)
56-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
53+
db := SetupTestGormDB(t)
54+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
5755
tests := []struct {
5856
name string
5957
ctx context.Context
@@ -109,8 +107,8 @@ func TestContestRepository_UpdateContest(t *testing.T) {
109107
func TestContestRepository_DeleteContest(t *testing.T) {
110108
t.Parallel()
111109

112-
db := testutils.SetupGormDB(t)
113-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
110+
db := SetupTestGormDB(t)
111+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
114112
contest1 := mustMakeContest(t, repo, nil)
115113
contest2 := mustMakeContest(t, repo, nil)
116114

@@ -137,8 +135,8 @@ func TestContestRepository_DeleteContest(t *testing.T) {
137135
func TestContestRepository_GetContestTeams(t *testing.T) {
138136
t.Parallel()
139137

140-
db := testutils.SetupGormDB(t)
141-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
138+
db := SetupTestGormDB(t)
139+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
142140

143141
contest1 := mustMakeContest(t, repo, nil)
144142
contest2 := mustMakeContest(t, repo, nil)
@@ -169,8 +167,8 @@ func TestContestRepository_GetContestTeams(t *testing.T) {
169167
func TestContestRepository_GetContestTeam(t *testing.T) {
170168
t.Parallel()
171169

172-
db := testutils.SetupGormDB(t)
173-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
170+
db := SetupTestGormDB(t)
171+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
174172

175173
contest1 := mustMakeContest(t, repo, nil)
176174
contest2 := mustMakeContest(t, repo, nil)
@@ -202,8 +200,8 @@ func TestContestRepository_GetContestTeam(t *testing.T) {
202200
func TestContestRepository_UpdateContestTeam(t *testing.T) {
203201
t.Parallel()
204202

205-
db := testutils.SetupGormDB(t)
206-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
203+
db := SetupTestGormDB(t)
204+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
207205

208206
tests := []struct {
209207
name string
@@ -251,8 +249,8 @@ func TestContestRepository_UpdateContestTeam(t *testing.T) {
251249
func TestContestRepository_DeleteContestTeam(t *testing.T) {
252250
t.Parallel()
253251

254-
db := testutils.SetupGormDB(t)
255-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
252+
db := SetupTestGormDB(t)
253+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
256254

257255
contest1 := mustMakeContest(t, repo, nil)
258256
contest2 := mustMakeContest(t, repo, nil)
@@ -297,10 +295,10 @@ func TestContestRepository_DeleteContestTeam(t *testing.T) {
297295
func TestContestRepository_GetContestTeamMembers(t *testing.T) {
298296
t.Parallel()
299297

300-
db := testutils.SetupGormDB(t)
298+
db := SetupTestGormDB(t)
301299
err := mockdata.InsertSampleDataToDB(db)
302300
assert.NoError(t, err)
303-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
301+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
304302

305303
contest1 := mustMakeContest(t, repo, nil)
306304
contest2 := mustMakeContest(t, repo, nil)
@@ -357,10 +355,10 @@ func TestContestRepository_GetContestTeamMembers(t *testing.T) {
357355
func TestContestRepository_EditContestTeamMembers(t *testing.T) {
358356
t.Parallel()
359357

360-
db := testutils.SetupGormDB(t)
358+
db := SetupTestGormDB(t)
361359
err := mockdata.InsertSampleDataToDB(db)
362360
assert.NoError(t, err)
363-
repo := irepository.NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
361+
repo := NewContestRepository(db, mock_external_e2e.NewMockPortalAPI())
364362

365363
contest1 := mustMakeContest(t, repo, nil)
366364
contest2 := mustMakeContest(t, repo, nil)

integration_tests/repository/event_test.go infrastructure/repository/event_test.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/traPtitech/traPortfolio/domain"
1111
"github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e"
12-
irepository "github.com/traPtitech/traPortfolio/infrastructure/repository"
13-
"github.com/traPtitech/traPortfolio/integration_tests/testutils"
1412
urepository "github.com/traPtitech/traPortfolio/usecases/repository"
1513
"github.com/traPtitech/traPortfolio/util/mockdata"
1614
"github.com/traPtitech/traPortfolio/util/optional"
@@ -20,8 +18,8 @@ import (
2018
func TestEventRepository_GetEvents(t *testing.T) {
2119
t.Parallel()
2220

23-
db := testutils.SetupGormDB(t)
24-
repo := irepository.NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
21+
db := SetupTestGormDB(t)
22+
repo := NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
2523

2624
expected := make([]*domain.Event, 0)
2725
for _, e := range mockdata.MockKnoqEvents {
@@ -42,8 +40,8 @@ func TestEventRepository_GetEvents(t *testing.T) {
4240
func TestEventRepository_GetEvent(t *testing.T) {
4341
t.Parallel()
4442

45-
db := testutils.SetupGormDB(t)
46-
repo := irepository.NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
43+
db := SetupTestGormDB(t)
44+
repo := NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
4745

4846
levels := createRandomEventLevels(t, repo)
4947
selected := mockdata.MockKnoqEvents[rand.IntN(len(mockdata.MockKnoqEvents)-1)]
@@ -100,8 +98,8 @@ func TestEventRepository_GetEvent(t *testing.T) {
10098
func TestEventRepository_UpdateEventLevel(t *testing.T) {
10199
t.Parallel()
102100

103-
db := testutils.SetupGormDB(t)
104-
repo := irepository.NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
101+
db := SetupTestGormDB(t)
102+
repo := NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
105103

106104
levels := createRandomEventLevels(t, repo)
107105

@@ -140,8 +138,8 @@ func TestEventRepository_UpdateEventLevel(t *testing.T) {
140138
func TestEventRepository_GetUserEvents(t *testing.T) {
141139
t.Parallel()
142140

143-
db := testutils.SetupGormDB(t)
144-
repo := irepository.NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
141+
db := SetupTestGormDB(t)
142+
repo := NewEventRepository(db, mock_external_e2e.NewMockKnoqAPI())
145143

146144
expected := make([]*domain.Event, 0)
147145
for _, e := range mockdata.MockKnoqEvents {

integration_tests/repository/main_test.go infrastructure/repository/main_test.go

+40-14
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,71 @@ package repository
22

33
import (
44
"context"
5+
"database/sql"
6+
"fmt"
57
"io"
68
"log"
79
"math/rand/v2"
10+
"strings"
811
"testing"
912

1013
"github.com/go-sql-driver/mysql"
1114
"github.com/gofrs/uuid"
1215
"github.com/stretchr/testify/assert"
1316
"github.com/traPtitech/traPortfolio/domain"
14-
"github.com/traPtitech/traPortfolio/integration_tests/testutils"
1517
"github.com/traPtitech/traPortfolio/usecases/repository"
1618
"github.com/traPtitech/traPortfolio/util/config"
1719
"github.com/traPtitech/traPortfolio/util/optional"
1820
"github.com/traPtitech/traPortfolio/util/random"
21+
"github.com/traPtitech/traPortfolio/util/testutils"
22+
"gorm.io/gorm"
23+
)
24+
25+
var (
26+
testDB *sql.DB
27+
testSQLConfig config.SQLConfig
1928
)
2029

2130
func TestMain(m *testing.M) {
22-
c, err := config.Load(config.LoadOpts{SkipReadFromFiles: true})
31+
// disable mysql driver logging
32+
_ = mysql.SetLogger(mysql.Logger(log.New(io.Discard, "", 0)))
33+
34+
user, pass, host := "root", "password", "localhost"
35+
db, port, closeFunc, err := testutils.RunMySQLContainerOnDocker(user, pass, host)
2336
if err != nil {
2437
panic(err)
2538
}
2639

27-
// disable mysql driver logging
28-
_ = mysql.SetLogger(mysql.Logger(log.New(io.Discard, "", 0)))
29-
db, closeFunc, err := testutils.RunMySQLContainerOnDocker(c.DB)
30-
if err != nil {
40+
testDB = db
41+
testSQLConfig = config.SQLConfig{
42+
User: user,
43+
Pass: pass,
44+
Host: host,
45+
Name: "",
46+
Port: port,
47+
}
48+
49+
m.Run()
50+
51+
if err := closeFunc(); err != nil {
3152
panic(err)
3253
}
54+
}
3355

34-
defer func() {
35-
if err := closeFunc(); err != nil {
36-
panic(err)
37-
}
38-
}()
56+
func SetupTestGormDB(t *testing.T) *gorm.DB {
57+
t.Helper()
3958

40-
testutils.Config = c
41-
testutils.DB = db
59+
dbName := fmt.Sprintf("portfolio_test_%s", strings.ToLower(t.Name()))
60+
_, err := testDB.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", dbName))
61+
assert.NoError(t, err)
4262

43-
m.Run()
63+
sqlConfig := testSQLConfig
64+
sqlConfig.Name = dbName
65+
66+
gormDB, err := NewGormDB(sqlConfig)
67+
assert.NoError(t, err)
68+
69+
return gormDB
4470
}
4571

4672
func mustMakeContest(t *testing.T, repo repository.ContestRepository, args *repository.CreateContestArgs) *domain.ContestDetail {

integration_tests/repository/project_test.go infrastructure/repository/project_test.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/traPtitech/traPortfolio/domain"
1313
"github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e"
14-
irepository "github.com/traPtitech/traPortfolio/infrastructure/repository"
15-
"github.com/traPtitech/traPortfolio/integration_tests/testutils"
1614
"github.com/traPtitech/traPortfolio/util/mockdata"
1715
"github.com/traPtitech/traPortfolio/util/optional"
1816
"github.com/traPtitech/traPortfolio/util/random"
@@ -21,8 +19,8 @@ import (
2119
func TestProjectRepository_GetProjects(t *testing.T) {
2220
t.Parallel()
2321

24-
db := testutils.SetupGormDB(t)
25-
repo := irepository.NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
22+
db := SetupTestGormDB(t)
23+
repo := NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
2624

2725
projectNum := 4
2826
var projects []*domain.Project
@@ -39,8 +37,8 @@ func TestProjectRepository_GetProjects(t *testing.T) {
3937
func TestProjectRepository_GetProject(t *testing.T) {
4038
t.Parallel()
4139

42-
db := testutils.SetupGormDB(t)
43-
repo := irepository.NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
40+
db := SetupTestGormDB(t)
41+
repo := NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
4442

4543
projectNum := 4
4644
var projects []*domain.ProjectDetail
@@ -68,8 +66,8 @@ func TestProjectRepository_GetProject(t *testing.T) {
6866
func TestProjectRepository_UpdateProject(t *testing.T) {
6967
t.Parallel()
7068

71-
db := testutils.SetupGormDB(t)
72-
repo := irepository.NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
69+
db := SetupTestGormDB(t)
70+
repo := NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
7371

7472
tests := []struct {
7573
name string
@@ -132,10 +130,10 @@ func TestProjectRepository_UpdateProject(t *testing.T) {
132130
func TestProjectRepository_GetProjectMembers(t *testing.T) {
133131
t.Parallel()
134132

135-
db := testutils.SetupGormDB(t)
133+
db := SetupTestGormDB(t)
136134
err := mockdata.InsertSampleDataToDB(db)
137135
assert.NoError(t, err)
138-
repo := irepository.NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
136+
repo := NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
139137

140138
project1 := mustMakeProjectDetail(t, repo, nil)
141139
project2 := mustMakeProjectDetail(t, repo, nil)
@@ -227,10 +225,10 @@ func TestProjectRepository_GetProjectMembers(t *testing.T) {
227225
func TestProjectRepository_EditProjectMembers(t *testing.T) {
228226
t.Parallel()
229227

230-
db := testutils.SetupGormDB(t)
228+
db := SetupTestGormDB(t)
231229
err := mockdata.InsertSampleDataToDB(db)
232230
assert.NoError(t, err)
233-
repo := irepository.NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
231+
repo := NewProjectRepository(db, mock_external_e2e.NewMockPortalAPI())
234232

235233
project1 := mustMakeProjectDetail(t, repo, nil)
236234
user1 := domain.NewUser(

0 commit comments

Comments
 (0)