@@ -22,25 +22,28 @@ const (
2222 testCredentialsUsername = "username"
2323)
2424
25- type mariaDBClientMocked struct {
25+ type mockSettings struct {
2626 getInstanceFails bool
2727 getInstanceResp * mariadb.Instance
2828 getCredentialsFails bool
2929 getCredentialsResp * mariadb.CredentialsResponse
3030}
3131
32- func (m * mariaDBClientMocked ) GetInstanceExecute (_ context.Context , _ , _ string ) (* mariadb.Instance , error ) {
33- if m .getInstanceFails {
34- return nil , fmt .Errorf ("could not get instance" )
35- }
36- return m .getInstanceResp , nil
37- }
38-
39- func (m * mariaDBClientMocked ) GetCredentialsExecute (_ context.Context , _ , _ , _ string ) (* mariadb.CredentialsResponse , error ) {
40- if m .getCredentialsFails {
41- return nil , fmt .Errorf ("could not get user" )
32+ func newAPIMock (m mockSettings ) mariadb.DefaultAPI {
33+ return & mariadb.DefaultAPIServiceMock {
34+ GetInstanceExecuteMock : utils .Ptr (func (_ mariadb.ApiGetInstanceRequest ) (* mariadb.Instance , error ) {
35+ if m .getInstanceFails {
36+ return nil , fmt .Errorf ("could not get instance" )
37+ }
38+ return m .getInstanceResp , nil
39+ }),
40+ GetCredentialsExecuteMock : utils .Ptr (func (_ mariadb.ApiGetCredentialsRequest ) (* mariadb.CredentialsResponse , error ) {
41+ if m .getCredentialsFails {
42+ return nil , fmt .Errorf ("could not get user" )
43+ }
44+ return m .getCredentialsResp , nil
45+ }),
4246 }
43- return m .getCredentialsResp , nil
4447}
4548
4649func TestGetInstanceName (t * testing.T ) {
@@ -54,7 +57,7 @@ func TestGetInstanceName(t *testing.T) {
5457 {
5558 description : "base" ,
5659 getInstanceResp : & mariadb.Instance {
57- Name : utils . Ptr ( testInstanceName ) ,
60+ Name : testInstanceName ,
5861 },
5962 isValid : true ,
6063 expectedOutput : testInstanceName ,
@@ -68,12 +71,12 @@ func TestGetInstanceName(t *testing.T) {
6871
6972 for _ , tt := range tests {
7073 t .Run (tt .description , func (t * testing.T ) {
71- client := & mariaDBClientMocked {
74+ settings := mockSettings {
7275 getInstanceFails : tt .getInstanceFails ,
7376 getInstanceResp : tt .getInstanceResp ,
7477 }
7578
76- output , err := GetInstanceName (context .Background (), client , testProjectId , testInstanceId )
79+ output , err := GetInstanceName (context .Background (), newAPIMock ( settings ) , testProjectId , testInstanceId )
7780
7881 if tt .isValid && err != nil {
7982 t .Errorf ("failed on valid input" )
@@ -103,8 +106,8 @@ func TestGetCredentialsUsername(t *testing.T) {
103106 description : "base" ,
104107 getCredentialsResp : & mariadb.CredentialsResponse {
105108 Raw : & mariadb.RawCredentials {
106- Credentials : & mariadb.Credentials {
107- Username : utils . Ptr ( testCredentialsUsername ) ,
109+ Credentials : mariadb.Credentials {
110+ Username : testCredentialsUsername ,
108111 },
109112 },
110113 },
@@ -120,12 +123,12 @@ func TestGetCredentialsUsername(t *testing.T) {
120123
121124 for _ , tt := range tests {
122125 t .Run (tt .description , func (t * testing.T ) {
123- client := & mariaDBClientMocked {
126+ settings := mockSettings {
124127 getCredentialsFails : tt .getCredentialsFails ,
125128 getCredentialsResp : tt .getCredentialsResp ,
126129 }
127130
128- output , err := GetCredentialsUsername (context .Background (), client , testProjectId , testInstanceId , testCredentialsId )
131+ output , err := GetCredentialsUsername (context .Background (), newAPIMock ( settings ) , testProjectId , testInstanceId , testCredentialsId )
129132
130133 if tt .isValid && err != nil {
131134 t .Errorf ("failed on valid input" )
0 commit comments