|
| 1 | +package elasticsearch |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + |
| 11 | + "github.com/inloco/kafka-elasticsearch-injector/src/kafka/fixtures" |
| 12 | + "github.com/inloco/kafka-elasticsearch-injector/src/logger_builder" |
| 13 | + "github.com/inloco/kafka-elasticsearch-injector/src/models" |
| 14 | +) |
| 15 | + |
| 16 | +var codecLogger = logger_builder.NewLogger("elasticsearch-test") |
| 17 | + |
| 18 | +func TestCodec_EncodeElasticRecords(t *testing.T) { |
| 19 | + codec := &basicCodec{ |
| 20 | + config: Config{}, |
| 21 | + logger: codecLogger, |
| 22 | + } |
| 23 | + record, id, value := fixtures.NewRecord(time.Now()) |
| 24 | + |
| 25 | + elasticRecords, err := codec.EncodeElasticRecords([]*models.Record{record}) |
| 26 | + if assert.NoError(t, err) && assert.Len(t, elasticRecords, 1) { |
| 27 | + elasticRecord := elasticRecords[0] |
| 28 | + assert.Equal(t, fmt.Sprintf("%s-%s", record.Topic, record.FormatTimestamp()), elasticRecord.Index) |
| 29 | + assert.Equal(t, record.Topic, elasticRecord.Type) |
| 30 | + assert.Equal(t, fmt.Sprintf("%d:%d", record.Partition, record.Offset), elasticRecord.ID) |
| 31 | + assert.Equal(t, id, elasticRecord.Json["id"]) |
| 32 | + assert.Equal(t, value, elasticRecord.Json["value"]) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestCodec_EncodeElasticRecords_ColumnsBlacklist(t *testing.T) { |
| 37 | + codec := &basicCodec{ |
| 38 | + config: Config{BlacklistedColumns: []string{"value"}}, |
| 39 | + logger: codecLogger, |
| 40 | + } |
| 41 | + record, _, _ := fixtures.NewRecord(time.Now()) |
| 42 | + |
| 43 | + elasticRecords, err := codec.EncodeElasticRecords([]*models.Record{record}) |
| 44 | + if assert.NoError(t, err) && assert.Len(t, elasticRecords, 1) { |
| 45 | + elasticRecord := elasticRecords[0] |
| 46 | + assert.Contains(t, elasticRecord.Json, "id") |
| 47 | + assert.NotContains(t, elasticRecord.Json, "value") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestCodec_EncodeElasticRecords_IndexColumn(t *testing.T) { |
| 52 | + indexPrefix := "prefix" |
| 53 | + |
| 54 | + codec := &basicCodec{ |
| 55 | + config: Config{Index: indexPrefix, IndexColumn: "id"}, |
| 56 | + logger: codecLogger, |
| 57 | + } |
| 58 | + record, id, _ := fixtures.NewRecord(time.Now()) |
| 59 | + |
| 60 | + elasticRecords, err := codec.EncodeElasticRecords([]*models.Record{record}) |
| 61 | + if assert.NoError(t, err) && assert.Len(t, elasticRecords, 1) { |
| 62 | + elasticRecord := elasticRecords[0] |
| 63 | + assert.Equal(t, fmt.Sprintf("%v-%v", indexPrefix, id), elasticRecord.Index) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestCodec_EncodeElasticRecords_DocIDColumn(t *testing.T) { |
| 68 | + codec := &basicCodec{ |
| 69 | + config: Config{DocIDColumn: "id"}, |
| 70 | + logger: codecLogger, |
| 71 | + } |
| 72 | + record, id, _ := fixtures.NewRecord(time.Now()) |
| 73 | + |
| 74 | + elasticRecords, err := codec.EncodeElasticRecords([]*models.Record{record}) |
| 75 | + if assert.NoError(t, err) && assert.Len(t, elasticRecords, 1) { |
| 76 | + elasticRecord := elasticRecords[0] |
| 77 | + assert.Equal(t, strconv.Itoa(int(id)), elasticRecord.ID) |
| 78 | + } |
| 79 | +} |
0 commit comments