-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Test coverage for disable triggers in TableRestorerInsertFormat
- Loading branch information
Showing
3 changed files
with
199 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
internal/db/postgres/restorers/table_insert_format_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package restorers | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
|
||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/greenmaskio/greenmask/internal/db/postgres/pgrestore" | ||
"github.com/greenmaskio/greenmask/internal/db/postgres/toc" | ||
"github.com/greenmaskio/greenmask/internal/domains" | ||
"github.com/greenmaskio/greenmask/internal/utils/testutils" | ||
"github.com/greenmaskio/greenmask/pkg/toolkit" | ||
) | ||
|
||
func (s *restoresSuite) Test_TableRestorerInsertFormat_check_triggers_errors() { | ||
s.Run("check triggers causes error by default", func() { | ||
ctx := context.Background() | ||
schemaName := "public" | ||
tableName := "orders" | ||
fileName := "test_table" | ||
copyStmt := "COPY orders (id, user_id, order_amount, raise_error) FROM stdin;" | ||
entry := &toc.Entry{ | ||
Namespace: &schemaName, | ||
Tag: &tableName, | ||
FileName: &fileName, | ||
CopyStmt: ©Stmt, | ||
} | ||
data := "6\t1\t100.50\tTest exception\n" | ||
buf := new(bytes.Buffer) | ||
gzData := gzip.NewWriter(buf) | ||
_, err := gzData.Write([]byte(data)) | ||
s.Require().NoError(err) | ||
err = gzData.Flush() | ||
s.Require().NoError(err) | ||
err = gzData.Close() | ||
s.Require().NoError(err) | ||
objSrc := &readCloserMock{Buffer: buf} | ||
|
||
st := new(testutils.StorageMock) | ||
st.On("GetObject", ctx, mock.Anything).Return(objSrc, nil) | ||
opt := &pgrestore.DataSectionSettings{ | ||
ExitOnError: true, | ||
} | ||
t := &toolkit.Table{ | ||
Schema: schemaName, | ||
Name: tableName, | ||
Columns: []*toolkit.Column{ | ||
{ | ||
Name: "id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "user_id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "order_amount", | ||
TypeName: "numeric", | ||
}, | ||
{ | ||
Name: "raise_error", | ||
TypeName: "text", | ||
}, | ||
}, | ||
} | ||
|
||
tr := NewTableRestorerInsertFormat(entry, t, st, opt, new(domains.DataRestorationErrorExclusions)) | ||
|
||
conn, err := s.GetConnectionWithUser(ctx, s.nonSuperUser, s.nonSuperUserPassword) | ||
err = tr.Execute(ctx, conn) | ||
s.Require().ErrorContains(err, "Test exception (SQLSTATE P0001)") | ||
}) | ||
|
||
s.Run("disable triggers", func() { | ||
ctx := context.Background() | ||
schemaName := "public" | ||
tableName := "orders" | ||
fileName := "test_table" | ||
copyStmt := "COPY orders (id, user_id, order_amount, raise_error) FROM stdin;" | ||
entry := &toc.Entry{ | ||
Namespace: &schemaName, | ||
Tag: &tableName, | ||
FileName: &fileName, | ||
CopyStmt: ©Stmt, | ||
} | ||
data := "7\t1\t100.50\tTest exception\n" | ||
buf := new(bytes.Buffer) | ||
gzData := gzip.NewWriter(buf) | ||
_, err := gzData.Write([]byte(data)) | ||
s.Require().NoError(err) | ||
err = gzData.Flush() | ||
s.Require().NoError(err) | ||
err = gzData.Close() | ||
s.Require().NoError(err) | ||
objSrc := &readCloserMock{Buffer: buf} | ||
|
||
st := new(testutils.StorageMock) | ||
st.On("GetObject", ctx, mock.Anything).Return(objSrc, nil) | ||
opt := &pgrestore.DataSectionSettings{ | ||
ExitOnError: true, | ||
DisableTriggers: true, | ||
SuperUser: s.GetSuperUser(), | ||
} | ||
t := &toolkit.Table{ | ||
Schema: schemaName, | ||
Name: tableName, | ||
Columns: []*toolkit.Column{ | ||
{ | ||
Name: "id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "user_id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "order_amount", | ||
TypeName: "numeric", | ||
}, | ||
{ | ||
Name: "raise_error", | ||
TypeName: "text", | ||
}, | ||
}, | ||
} | ||
|
||
tr := NewTableRestorerInsertFormat(entry, t, st, opt, new(domains.DataRestorationErrorExclusions)) | ||
|
||
conn, err := s.GetConnectionWithUser(ctx, s.nonSuperUser, s.nonSuperUserPassword) | ||
err = tr.Execute(ctx, conn) | ||
s.Require().NoError(err) | ||
}) | ||
|
||
s.Run("session_replication_role is replica", func() { | ||
ctx := context.Background() | ||
schemaName := "public" | ||
tableName := "orders" | ||
fileName := "test_table" | ||
copyStmt := "COPY orders (id, user_id, order_amount, raise_error) FROM stdin;" | ||
entry := &toc.Entry{ | ||
Namespace: &schemaName, | ||
Tag: &tableName, | ||
FileName: &fileName, | ||
CopyStmt: ©Stmt, | ||
} | ||
data := "8\t1\t100.50\tTest exception\n" | ||
buf := new(bytes.Buffer) | ||
gzData := gzip.NewWriter(buf) | ||
_, err := gzData.Write([]byte(data)) | ||
s.Require().NoError(err) | ||
err = gzData.Flush() | ||
s.Require().NoError(err) | ||
err = gzData.Close() | ||
s.Require().NoError(err) | ||
objSrc := &readCloserMock{Buffer: buf} | ||
|
||
st := new(testutils.StorageMock) | ||
st.On("GetObject", ctx, mock.Anything).Return(objSrc, nil) | ||
opt := &pgrestore.DataSectionSettings{ | ||
ExitOnError: true, | ||
UseSessionReplicationRoleReplica: true, | ||
SuperUser: s.GetSuperUser(), | ||
} | ||
t := &toolkit.Table{ | ||
Schema: schemaName, | ||
Name: tableName, | ||
Columns: []*toolkit.Column{ | ||
{ | ||
Name: "id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "user_id", | ||
TypeName: "int4", | ||
}, | ||
{ | ||
Name: "order_amount", | ||
TypeName: "numeric", | ||
}, | ||
{ | ||
Name: "raise_error", | ||
TypeName: "text", | ||
}, | ||
}, | ||
} | ||
|
||
tr := NewTableRestorerInsertFormat(entry, t, st, opt, new(domains.DataRestorationErrorExclusions)) | ||
|
||
conn, err := s.GetConnectionWithUser(ctx, s.nonSuperUser, s.nonSuperUserPassword) | ||
err = tr.Execute(ctx, conn) | ||
s.Require().NoError(err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters