@@ -60,7 +60,7 @@ func TestExecute(t *testing.T) {
6060 })
6161 t .Run ("should return nil when everything is successful" , func (t * testing.T ) {
6262 // arrange
63- client , err := client .NewClient (context .TODO (), client .SetupLogger ("error" ), client .SetupLoader ("APPEND " ))
63+ client , err := client .NewClient (context .TODO (), client .SetupLogger ("error" ), client .SetupLoader ("REPLACE " ))
6464 require .NoError (t , err )
6565 client .OdpsClient = & mockOdpsClient {
6666 partitionResult : func () ([]string , error ) {
@@ -70,6 +70,42 @@ func TestExecute(t *testing.T) {
7070 return nil
7171 },
7272 }
73+ client .Loader = & mockLoader {
74+ getQueryFunc : func (tableID , query string ) string {
75+ return "INSERT OVERWRITE TABLE project_test.table_test SELECT * FROM table;"
76+ },
77+ getPartitionedQueryFunc : func (tableID , query string , partitionNames []string ) string {
78+ assert .True (t , true , "should be called" )
79+ return "INSERT OVERWRITE TABLE project_test.table_test PARTITION(event_date) SELECT * FROM table;"
80+ },
81+ }
82+ require .NoError (t , os .WriteFile ("/tmp/query.sql" , []byte ("SELECT * FROM table;" ), 0644 ))
83+ // act
84+ err = client .Execute (context .TODO (), "project_test.table_test" , "/tmp/query.sql" )
85+ // assert
86+ assert .NoError (t , err )
87+ })
88+ t .Run ("should return nil when everything is successful with enable auto partition" , func (t * testing.T ) {
89+ // arrange
90+ client , err := client .NewClient (context .TODO (), client .SetupLogger ("error" ), client .SetupLoader ("REPLACE" ), client .EnableAutoPartition (true ))
91+ require .NoError (t , err )
92+ client .OdpsClient = & mockOdpsClient {
93+ partitionResult : func () ([]string , error ) {
94+ return []string {"_partition_value" }, nil
95+ },
96+ execSQLResult : func () error {
97+ return nil
98+ },
99+ }
100+ client .Loader = & mockLoader {
101+ getQueryFunc : func (tableID , query string ) string {
102+ return "INSERT OVERWRITE TABLE project_test.table_test SELECT * FROM table;"
103+ },
104+ getPartitionedQueryFunc : func (tableID , query string , partitionNames []string ) string {
105+ assert .False (t , true , "should not be called" )
106+ return "INSERT OVERWRITE TABLE project_test.table_test PARTITION(event_date) SELECT * FROM table;"
107+ },
108+ }
73109 require .NoError (t , os .WriteFile ("/tmp/query.sql" , []byte ("SELECT * FROM table;" ), 0644 ))
74110 // act
75111 err = client .Execute (context .TODO (), "project_test.table_test" , "/tmp/query.sql" )
@@ -90,3 +126,16 @@ func (m *mockOdpsClient) GetPartitionNames(ctx context.Context, tableID string)
90126func (m * mockOdpsClient ) ExecSQL (ctx context.Context , query string ) error {
91127 return m .execSQLResult ()
92128}
129+
130+ type mockLoader struct {
131+ getQueryFunc func (tableID , query string ) string
132+ getPartitionedQueryFunc func (tableID , query string , partitionNames []string ) string
133+ }
134+
135+ func (m * mockLoader ) GetQuery (tableID , query string ) string {
136+ return m .getQueryFunc (tableID , query )
137+ }
138+
139+ func (m * mockLoader ) GetPartitionedQuery (tableID , query string , partitionNames []string ) string {
140+ return m .getPartitionedQueryFunc (tableID , query , partitionNames )
141+ }
0 commit comments