|
| 1 | +package csvexport_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spring-media/curation-pkgs-public/pkg/csvexport" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +var dynamoMockRespV2 = []map[string]interface{}{ |
| 14 | + { |
| 15 | + "ArticleLastUpdated": "2022-04-27T08:36:48.386Z", |
| 16 | + "Block": "Meldungen1", |
| 17 | + "Categories": []interface{}{ |
| 18 | + map[string]interface{}{ |
| 19 | + "name": "money_business1", |
| 20 | + "score": 0.958231, |
| 21 | + }, |
| 22 | + }, |
| 23 | + "EntriesAveragedFromHome": map[string]interface{}{ |
| 24 | + "15": 1.333333, |
| 25 | + "180": 2.861111, |
| 26 | + "30": 3.166667, |
| 27 | + "45": 4.888889, |
| 28 | + "5": 5.000000, |
| 29 | + }, |
| 30 | + "IsPremium": false, |
| 31 | + "IsSponsored": false, |
| 32 | + "Meta": map[string]interface{}{ |
| 33 | + "Department": "wirtschaft", |
| 34 | + "FromInvestigativ": false, |
| 35 | + "FromNewsteam": true, |
| 36 | + "HasVideo": false, |
| 37 | + }, |
| 38 | + "PerformanceLastUpdated": "2022-04-27T13:10:30Z", |
| 39 | + }, |
| 40 | + { |
| 41 | + "ArticleLastUpdated": "2022-04-28T08:36:48.386Z", |
| 42 | + "Block": "Meldungen2", |
| 43 | + "Categories": []interface{}{ |
| 44 | + map[string]interface{}{ |
| 45 | + "name": "money_business2", |
| 46 | + "score": 0.99, |
| 47 | + }, |
| 48 | + }, |
| 49 | + "EntriesAveragedFromHome": map[string]interface{}{ |
| 50 | + "15": 6.333333, |
| 51 | + "180": 7.861111, |
| 52 | + "30": 8.166667, |
| 53 | + "45": 9.888889, |
| 54 | + "5": 10.000000, |
| 55 | + }, |
| 56 | + "IsPremium": true, |
| 57 | + "IsSponsored": true, |
| 58 | + "Meta": map[string]interface{}{ |
| 59 | + "Department": "wirtschaft2", |
| 60 | + "FromInvestigativ": true, |
| 61 | + "FromNewsteam": false, |
| 62 | + "HasVideo": true, |
| 63 | + }, |
| 64 | + "PerformanceLastUpdated": "2022-04-28T13:10:30Z", |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +type mockScanV2 struct { |
| 69 | + resp []map[string]interface{} |
| 70 | +} |
| 71 | + |
| 72 | +func (d mockScanV2) Scan(ctx context.Context, opt csvexport.ScanOption, startKey map[string]types.AttributeValue) ([]map[string]interface{}, map[string]types.AttributeValue, error) { |
| 73 | + return d.resp, map[string]types.AttributeValue{}, nil |
| 74 | +} |
| 75 | + |
| 76 | +func TestDynamoToCSVV2(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + option := csvexport.ScanOption{} |
| 79 | + |
| 80 | + db := mockScanV2{resp: dynamoMockRespV2} |
| 81 | + b, err := csvexport.DynamoToCSVV2(db, context.Background(), option) |
| 82 | + |
| 83 | + assert.Nil(t, err) |
| 84 | + |
| 85 | + expectedCSV := `ArticleLastUpdated,Block,Categories,EntriesAveragedFromHome,IsPremium,IsSponsored,Meta,PerformanceLastUpdated |
| 86 | +2022-04-27T08:36:48.386Z,Meldungen1,"[{""name"":""money_business1"",""score"":0.958231}]","{""15"":1.333333,""180"":2.861111,""30"":3.166667,""45"":4.888889,""5"":5}",false,false,"{""Department"":""wirtschaft"",""FromInvestigativ"":false,""FromNewsteam"":true,""HasVideo"":false}",2022-04-27T13:10:30Z |
| 87 | +2022-04-28T08:36:48.386Z,Meldungen2,"[{""name"":""money_business2"",""score"":0.99}]","{""15"":6.333333,""180"":7.861111,""30"":8.166667,""45"":9.888889,""5"":10}",true,true,"{""Department"":""wirtschaft2"",""FromInvestigativ"":true,""FromNewsteam"":false,""HasVideo"":true}",2022-04-28T13:10:30Z |
| 88 | +` |
| 89 | + assert.Equal(t, expectedCSV, string(b)) |
| 90 | +} |
| 91 | + |
| 92 | +func TestDynamoToCSVWithColsV2(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + db := mockScanV2{resp: dynamoMockRespV2} |
| 95 | + cols := csvexport.Columns{ |
| 96 | + csvexport.Column{Name: "ArticleLastUpdated"}, |
| 97 | + csvexport.Column{Name: "Block", OverwriteValue: true, OverwriteWithValue: ""}, |
| 98 | + csvexport.Column{Name: "Categories", OverwriteValue: true, OverwriteWithValue: nil}, |
| 99 | + csvexport.Column{Name: "EntriesAveragedFromHome", OverwriteValue: true, OverwriteWithValue: 3}, |
| 100 | + csvexport.Column{Name: "IsPremium", OverwriteValue: true, OverwriteWithValue: 1.33}, |
| 101 | + csvexport.Column{Name: "IsSponsored", OverwriteValue: true, OverwriteWithValue: false}, |
| 102 | + csvexport.Column{Name: "PerformanceLastUpdated"}, |
| 103 | + } |
| 104 | + b, err := csvexport.DynamoToCSVV2(db, context.Background(), csvexport.ScanOption{}, csvexport.WithColumns(cols)) |
| 105 | + |
| 106 | + assert.Nil(t, err) |
| 107 | + |
| 108 | + expectedCSV := `ArticleLastUpdated,Block,Categories,EntriesAveragedFromHome,IsPremium,IsSponsored,PerformanceLastUpdated |
| 109 | +2022-04-27T08:36:48.386Z,,null,3,1.33,false,2022-04-27T13:10:30Z |
| 110 | +2022-04-28T08:36:48.386Z,,null,3,1.33,false,2022-04-28T13:10:30Z |
| 111 | +` |
| 112 | + assert.Equal(t, expectedCSV, string(b)) |
| 113 | +} |
| 114 | + |
| 115 | +func TestDynamoToCSVWithColsTargetNameV2(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + db := mockScanV2{resp: dynamoMockRespV2} |
| 118 | + cols := csvexport.Columns{ |
| 119 | + csvexport.Column{Name: "ArticleLastUpdated"}, |
| 120 | + csvexport.Column{Name: "PerformanceLastUpdated", TargetName: "PerformanceUpdatedLast"}, |
| 121 | + } |
| 122 | + b, err := csvexport.DynamoToCSVV2(db, context.Background(), csvexport.ScanOption{}, csvexport.WithColumns(cols)) |
| 123 | + |
| 124 | + assert.Nil(t, err) |
| 125 | + |
| 126 | + expectedCSV := `ArticleLastUpdated,PerformanceUpdatedLast |
| 127 | +2022-04-27T08:36:48.386Z,2022-04-27T13:10:30Z |
| 128 | +2022-04-28T08:36:48.386Z,2022-04-28T13:10:30Z |
| 129 | +` |
| 130 | + assert.Equal(t, expectedCSV, string(b)) |
| 131 | +} |
| 132 | + |
| 133 | +func TestDynamoToCSVWithColsValueFuncV2(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + db := mockScanV2{resp: dynamoMockRespV2} |
| 136 | + |
| 137 | + valueFn := func(val interface{}) (string, error) { |
| 138 | + v, ok := val.(string) |
| 139 | + if !ok { |
| 140 | + return "", fmt.Errorf("failed to cast to string") |
| 141 | + } |
| 142 | + |
| 143 | + if v == "Meldungen1" { |
| 144 | + return "Meldungen123", nil |
| 145 | + } |
| 146 | + |
| 147 | + return v, nil |
| 148 | + } |
| 149 | + |
| 150 | + valueFnNew := func(val interface{}) (string, error) { |
| 151 | + v, ok := val.(string) |
| 152 | + if !ok { |
| 153 | + return "", fmt.Errorf("failed to cast to string") |
| 154 | + } |
| 155 | + |
| 156 | + if v == "Meldungen2" { |
| 157 | + return "Meldungen999", nil |
| 158 | + } |
| 159 | + |
| 160 | + return v, nil |
| 161 | + } |
| 162 | + cols := csvexport.Columns{ |
| 163 | + csvexport.Column{Name: "ArticleLastUpdated"}, |
| 164 | + csvexport.Column{Name: "Block", ValueFunc: valueFn}, |
| 165 | + csvexport.Column{Name: "NewBlock", ValueFunc: valueFnNew, ValueFuncCol: "Block"}, |
| 166 | + } |
| 167 | + b, err := csvexport.DynamoToCSVV2(db, context.Background(), csvexport.ScanOption{}, csvexport.WithColumns(cols)) |
| 168 | + |
| 169 | + assert.Nil(t, err) |
| 170 | + |
| 171 | + expectedCSV := `ArticleLastUpdated,Block,NewBlock |
| 172 | +2022-04-27T08:36:48.386Z,Meldungen123,Meldungen1 |
| 173 | +2022-04-28T08:36:48.386Z,Meldungen2,Meldungen999 |
| 174 | +` |
| 175 | + assert.Equal(t, expectedCSV, string(b)) |
| 176 | +} |
0 commit comments