From 48eadbfe8a3dd28848a1cfccddf77cb4f93ce550 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Sep 2024 11:00:29 -0700 Subject: [PATCH] Refactor even more. --- .../table_data_merge_columns_test.go | 107 ++++++++---------- 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/lib/optimization/table_data_merge_columns_test.go b/lib/optimization/table_data_merge_columns_test.go index be4eb8662..3f0fc082b 100644 --- a/lib/optimization/table_data_merge_columns_test.go +++ b/lib/optimization/table_data_merge_columns_test.go @@ -10,21 +10,18 @@ import ( "github.com/stretchr/testify/assert" ) -const strCol = "string" - func TestTableData_UpdateInMemoryColumnsFromDestination(t *testing.T) { + const strCol = "string" + tableDataCols := &columns.Columns{} + tableData := &TableData{inMemoryColumns: tableDataCols} { // Trying to merge an invalid destination column - tableDataCols := &columns.Columns{} - tableData := &TableData{inMemoryColumns: tableDataCols} tableData.AddInMemoryCol(columns.NewColumn("foo", typing.String)) invalidCol := columns.NewColumn("foo", typing.Invalid) assert.ErrorContains(t, tableData.MergeColumnsFromDestination(invalidCol), `column "foo" is invalid`) } { // In-memory column is a string and the destination column is a Date - tableDataCols := &columns.Columns{} - tableData := &TableData{inMemoryColumns: tableDataCols} tableData.AddInMemoryCol(columns.NewColumn("foo", typing.String)) extTime := typing.ETime @@ -40,59 +37,55 @@ func TestTableData_UpdateInMemoryColumnsFromDestination(t *testing.T) { assert.Equal(t, "", updatedColumn.KindDetails.ExtendedTimeDetails.Format) } { - tableDataCols := &columns.Columns{} - tableData := &TableData{inMemoryColumns: tableDataCols} - { - // In-memory column is NUMERIC and destination column is an INTEGER - tableDataCols.AddColumn(columns.NewColumn("numeric_test", typing.EDecimal)) - assert.NoError(t, tableData.MergeColumnsFromDestination(columns.NewColumn("numeric_test", typing.Integer))) - - numericCol, isOk := tableData.inMemoryColumns.GetColumn("numeric_test") - assert.True(t, isOk) - assert.Equal(t, typing.Integer.Kind, numericCol.KindDetails.Kind) - } - { - // Boolean column that has been backfilled - tableDataCols.AddColumn(columns.NewColumn("bool_backfill", typing.Boolean)) - backfilledCol := columns.NewColumn("bool_backfill", typing.Boolean) - backfilledCol.SetBackfilled(true) - - // Backfill was not set - column, isOk := tableData.inMemoryColumns.GetColumn("bool_backfill") - assert.True(t, isOk) - assert.False(t, column.Backfilled()) - - assert.NoError(t, tableData.MergeColumnsFromDestination(backfilledCol)) - // Backfill is set after merge. - column, isOk = tableData.inMemoryColumns.GetColumn("bool_backfill") - assert.True(t, isOk) - assert.True(t, column.Backfilled()) - } - { - // Non-existent columns should not be copied over. - nonExistentTableCols := []string{"dusty", "the", "mini", "aussie"} - var nonExistentCols []columns.Column - for _, nonExistentTableCol := range nonExistentTableCols { - nonExistentCols = append(nonExistentCols, columns.NewColumn(nonExistentTableCol, typing.String)) - } - - assert.NoError(t, tableData.MergeColumnsFromDestination(nonExistentCols...)) - for _, nonExistentTableCol := range nonExistentTableCols { - _, isOk := tableData.inMemoryColumns.GetColumn(nonExistentTableCol) - assert.False(t, isOk, nonExistentTableCol) - } + // In-memory column is NUMERIC and destination column is an INTEGER + tableDataCols.AddColumn(columns.NewColumn("numeric_test", typing.EDecimal)) + assert.NoError(t, tableData.MergeColumnsFromDestination(columns.NewColumn("numeric_test", typing.Integer))) + + numericCol, isOk := tableData.inMemoryColumns.GetColumn("numeric_test") + assert.True(t, isOk) + assert.Equal(t, typing.Integer.Kind, numericCol.KindDetails.Kind) + } + { + // Boolean column that has been backfilled + tableDataCols.AddColumn(columns.NewColumn("bool_backfill", typing.Boolean)) + backfilledCol := columns.NewColumn("bool_backfill", typing.Boolean) + backfilledCol.SetBackfilled(true) + + // Backfill was not set + column, isOk := tableData.inMemoryColumns.GetColumn("bool_backfill") + assert.True(t, isOk) + assert.False(t, column.Backfilled()) + + assert.NoError(t, tableData.MergeColumnsFromDestination(backfilledCol)) + // Backfill is set after merge. + column, isOk = tableData.inMemoryColumns.GetColumn("bool_backfill") + assert.True(t, isOk) + assert.True(t, column.Backfilled()) + } + { + // Non-existent columns should not be copied over. + nonExistentTableCols := []string{"dusty", "the", "mini", "aussie"} + var nonExistentCols []columns.Column + for _, nonExistentTableCol := range nonExistentTableCols { + nonExistentCols = append(nonExistentCols, columns.NewColumn(nonExistentTableCol, typing.String)) } - { - // In-memory column was invalid, but the destination column is valid - tableDataCols.AddColumn(columns.NewColumn("invalid_test", typing.Invalid)) - assert.NoError(t, tableData.MergeColumnsFromDestination(columns.NewColumn("invalid_test", typing.String))) - - invalidCol, isOk := tableData.inMemoryColumns.GetColumn("invalid_test") - assert.True(t, isOk) - assert.Equal(t, typing.String.Kind, invalidCol.KindDetails.Kind) + + assert.NoError(t, tableData.MergeColumnsFromDestination(nonExistentCols...)) + for _, nonExistentTableCol := range nonExistentTableCols { + _, isOk := tableData.inMemoryColumns.GetColumn(nonExistentTableCol) + assert.False(t, isOk, nonExistentTableCol) } + } + { + // In-memory column was invalid, but the destination column is valid + tableDataCols.AddColumn(columns.NewColumn("invalid_test", typing.Invalid)) + assert.NoError(t, tableData.MergeColumnsFromDestination(columns.NewColumn("invalid_test", typing.String))) - tableDataCols.AddColumn(columns.NewColumn("name", typing.String)) + invalidCol, isOk := tableData.inMemoryColumns.GetColumn("invalid_test") + assert.True(t, isOk) + assert.Equal(t, typing.String.Kind, invalidCol.KindDetails.Kind) + } + { // Casting these as STRING so tableColumn via this f(x) will set it correctly. tableDataCols.AddColumn(columns.NewColumn("ext_date", typing.String)) tableDataCols.AddColumn(columns.NewColumn("ext_time", typing.String)) @@ -164,8 +157,6 @@ func TestTableData_UpdateInMemoryColumnsFromDestination(t *testing.T) { } { // String (precision being copied over) - tableDataCols := &columns.Columns{} - tableData := &TableData{inMemoryColumns: tableDataCols} tableDataCols.AddColumn(columns.NewColumn(strCol, typing.String)) assert.NoError(t, tableData.MergeColumnsFromDestination(columns.NewColumn(strCol, typing.KindDetails{