-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain_test.go
111 lines (93 loc) · 4.61 KB
/
main_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
package main
import (
"os"
"sort"
"testing"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/internal/events"
"github.com/RedHatInsights/sources-api-go/internal/testutils/database"
"github.com/RedHatInsights/sources-api-go/internal/testutils/fixtures"
"github.com/RedHatInsights/sources-api-go/internal/testutils/mocks"
"github.com/RedHatInsights/sources-api-go/internal/testutils/parser"
l "github.com/RedHatInsights/sources-api-go/logger"
"github.com/RedHatInsights/sources-api-go/middleware"
"github.com/RedHatInsights/sources-api-go/service"
"github.com/labstack/echo/v4"
)
var (
mockSourceDao dao.SourceDao
mockApplicationTypeDao dao.ApplicationTypeDao
mockEndpointDao dao.EndpointDao
mockSourceTypeDao dao.SourceTypeDao
mockApplicationDao dao.ApplicationDao
mockMetaDataDao dao.MetaDataDao
mockRhcConnectionDao dao.RhcConnectionDao
mockApplicationAuthenticationDao dao.ApplicationAuthenticationDao
mockAuthenticationDao dao.AuthenticationDao
)
func TestMain(t *testing.M) {
l.InitLogger(conf)
flags := parser.ParseFlags()
if flags.CreateDb {
database.CreateTestDB()
} else if flags.Integration {
database.ConnectAndMigrateDB("public")
getSecretDao = getSecretDaoWithTenant
getSourceDao = getSourceDaoWithTenant
getApplicationDao = getApplicationDaoWithTenant
getEndpointDao = getEndpointDaoWithTenant
getApplicationTypeDao = getApplicationTypeDaoWithTenant
getSourceTypeDao = getSourceTypeDaoWithoutTenant
getMetaDataDao = getMetaDataDaoWithoutTenant
getRhcConnectionDao = getDefaultRhcConnectionDao
getApplicationAuthenticationDao = getApplicationAuthenticationDaoWithTenant
getAuthenticationDao = getAuthenticationDaoWithTenant
dao.Vault = &mocks.MockVault{}
service.Producer = func() events.Sender { return events.EventStreamProducer{Sender: &mocks.MockSender{}} }
database.CreateFixtures("public")
err := dao.PopulateStaticTypeCache()
if err != nil {
panic("failed to populate static type cache")
}
} else {
mockSourceDao = &mocks.MockSourceDao{Sources: fixtures.TestSourceData, RelatedSources: fixtures.TestSourceData}
mockApplicationDao = &mocks.MockApplicationDao{Applications: fixtures.TestApplicationData}
mockEndpointDao = &mocks.MockEndpointDao{Endpoints: fixtures.TestEndpointData}
mockSourceTypeDao = &mocks.MockSourceTypeDao{SourceTypes: fixtures.TestSourceTypeData}
mockApplicationTypeDao = &mocks.MockApplicationTypeDao{ApplicationTypes: fixtures.TestApplicationTypeData}
mockMetaDataDao = &mocks.MockMetaDataDao{MetaDatas: fixtures.TestMetaDataData}
mockRhcConnectionDao = &mocks.MockRhcConnectionDao{RhcConnections: fixtures.TestRhcConnectionData, RelatedRhcConnections: fixtures.TestRhcConnectionData}
mockApplicationAuthenticationDao = &mocks.MockApplicationAuthenticationDao{ApplicationAuthentications: fixtures.TestApplicationAuthenticationData}
mockAuthenticationDao = &mocks.MockAuthenticationDao{Authentications: fixtures.TestAuthenticationData}
getSourceDao = func(c echo.Context) (dao.SourceDao, error) { return mockSourceDao, nil }
getApplicationDao = func(c echo.Context) (dao.ApplicationDao, error) { return mockApplicationDao, nil }
getEndpointDao = func(c echo.Context) (dao.EndpointDao, error) { return mockEndpointDao, nil }
getSourceTypeDao = func(c echo.Context) (dao.SourceTypeDao, error) { return mockSourceTypeDao, nil }
getApplicationTypeDao = func(c echo.Context) (dao.ApplicationTypeDao, error) { return mockApplicationTypeDao, nil }
getMetaDataDao = func(c echo.Context) (dao.MetaDataDao, error) { return mockMetaDataDao, nil }
getRhcConnectionDao = func(c echo.Context) (dao.RhcConnectionDao, error) { return mockRhcConnectionDao, nil }
getApplicationAuthenticationDao = func(c echo.Context) (dao.ApplicationAuthenticationDao, error) {
return mockApplicationAuthenticationDao, nil
}
getAuthenticationDao = func(c echo.Context) (dao.AuthenticationDao, error) { return mockAuthenticationDao, nil }
err := dao.PopulateMockStaticTypeCache()
if err != nil {
panic("failed to populate mock static type cache")
}
}
code := t.Run()
if flags.Integration {
database.DropSchema("public")
}
os.Exit(code)
}
func SortByStringValueOnKey(field string, data []interface{}) {
sort.SliceStable(data, func(i, j int) bool {
dataI := data[i].(map[string]interface{})
dataJ := data[j].(map[string]interface{})
return dataI[field].(string) < dataJ[field].(string)
})
}
func ErrorHandlingContext(handler echo.HandlerFunc) func(echo.Context) error {
return middleware.HandleErrors(handler)
}