From 26c22f5090a85bfda2f6e952538dc8a77a608fa7 Mon Sep 17 00:00:00 2001 From: Idriss Neumann Date: Thu, 28 Dec 2023 12:59:11 +0100 Subject: [PATCH] Issue #20: add backend unit tests --- pkg/quickwit/timestamp_infos_test.go | 313 +++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 pkg/quickwit/timestamp_infos_test.go diff --git a/pkg/quickwit/timestamp_infos_test.go b/pkg/quickwit/timestamp_infos_test.go new file mode 100644 index 0000000..a426d63 --- /dev/null +++ b/pkg/quickwit/timestamp_infos_test.go @@ -0,0 +1,313 @@ +package quickwit + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDecodeTimestampFieldInfos(t *testing.T) { + t.Run("Test decode timestam field infos", func(t *testing.T) { + t.Run("Test decode simple fields", func(t *testing.T) { + // Given + query := []byte(` + { + "version": "0.6", + "index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W", + "index_config": { + "version": "0.6", + "index_id": "myindex", + "index_uri": "s3://quickwit-indexes/myindex", + "doc_mapping": { + "field_mappings": [ + { + "name": "foo", + "type": "text", + "fast": false, + "fieldnorms": false, + "indexed": true, + "record": "basic", + "stored": true, + "tokenizer": "default" + }, + { + "name": "timestamp", + "type": "datetime", + "fast": true, + "fast_precision": "seconds", + "indexed": true, + "input_formats": [ + "rfc3339", + "unix_timestamp" + ], + "output_format": "rfc3339", + "stored": true + } + ], + "tag_fields": [], + "store_source": true, + "index_field_presence": false, + "timestamp_field": "timestamp", + "mode": "dynamic", + "dynamic_mapping": {}, + "partition_key": "foo", + "max_num_partitions": 1, + "tokenizers": [] + }, + "indexing_settings": {}, + "search_settings": { + "default_search_fields": [ + "foo" + ] + }, + "retention": null + }, + "checkpoint": {}, + "create_timestamp": 1701075471, + "sources": [] + } + `) + + // When + timestampFieldName, timestampFieldFormat, err := DecodeTimestampFieldInfos(200, query) + + // Then + require.NoError(t, err) + require.Equal(t, timestampFieldName, "timestamp") + require.Equal(t, timestampFieldFormat, "rfc3339") + }) + + t.Run("Test decode nested fields", func(t *testing.T) { + // Given + query := []byte(` + { + "version": "0.6", + "index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W", + "index_config": { + "version": "0.6", + "index_id": "myindex", + "index_uri": "s3://quickwit-indexes/myindex", + "doc_mapping": { + "field_mappings": [ + { + "name": "foo", + "type": "text", + "fast": false, + "fieldnorms": false, + "indexed": true, + "record": "basic", + "stored": true, + "tokenizer": "default" + }, + { + "name": "sub", + "type": "object", + "field_mappings": [ + { + "fast": true, + "fast_precision": "seconds", + "indexed": true, + "input_formats": [ + "rfc3339", + "unix_timestamp" + ], + "name": "timestamp", + "output_format": "rfc3339", + "stored": true, + "type": "datetime" + } + ] + } + ], + "tag_fields": [], + "store_source": true, + "index_field_presence": false, + "timestamp_field": "sub.timestamp", + "mode": "dynamic", + "dynamic_mapping": {}, + "partition_key": "foo", + "max_num_partitions": 1, + "tokenizers": [] + }, + "indexing_settings": {}, + "search_settings": { + "default_search_fields": [ + "foo" + ] + }, + "retention": null + }, + "checkpoint": {}, + "create_timestamp": 1701075471, + "sources": [] + } + `) + + // When + timestampFieldName, timestampFieldFormat, err := DecodeTimestampFieldInfos(200, query) + + // Then + require.NoError(t, err) + require.Equal(t, timestampFieldName, "sub.timestamp") + require.Equal(t, timestampFieldFormat, "rfc3339") + }) + + t.Run("The timestamp field is not at the expected path", func(t *testing.T) { + // Given + query := []byte(` + { + "version": "0.6", + "index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W", + "index_config": { + "version": "0.6", + "index_id": "myindex", + "index_uri": "s3://quickwit-indexes/myindex", + "doc_mapping": { + "field_mappings": [ + { + "name": "foo", + "type": "text", + "fast": false, + "fieldnorms": false, + "indexed": true, + "record": "basic", + "stored": true, + "tokenizer": "default" + }, + { + "name": "sub", + "type": "object", + "field_mappings": [ + { + "fast": true, + "fast_precision": "seconds", + "indexed": true, + "input_formats": [ + "rfc3339", + "unix_timestamp" + ], + "name": "timestamp", + "output_format": "rfc3339", + "stored": true, + "type": "datetime" + } + ] + } + ], + "tag_fields": [], + "store_source": true, + "index_field_presence": false, + "timestamp_field": "timestamp", + "mode": "dynamic", + "dynamic_mapping": {}, + "partition_key": "foo", + "max_num_partitions": 1, + "tokenizers": [] + }, + "indexing_settings": {}, + "search_settings": { + "default_search_fields": [ + "foo" + ] + }, + "retention": null + }, + "checkpoint": {}, + "create_timestamp": 1701075471, + "sources": [] + } + `) + + // When + _, _, err := DecodeTimestampFieldInfos(200, query) + + // Then + require.Error(t, err) + }) + + t.Run("The timestamp field has not the right type", func(t *testing.T) { + // Given + query := []byte(` + { + "version": "0.6", + "index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W", + "index_config": { + "version": "0.6", + "index_id": "myindex", + "index_uri": "s3://quickwit-indexes/myindex", + "doc_mapping": { + "field_mappings": [ + { + "name": "foo", + "type": "text", + "fast": false, + "fieldnorms": false, + "indexed": true, + "record": "basic", + "stored": true, + "tokenizer": "default" + }, + { + "name": "sub", + "type": "object", + "field_mappings": [ + { + "fast": true, + "fast_precision": "seconds", + "indexed": true, + "input_formats": [ + "rfc3339", + "unix_timestamp" + ], + "name": "timestamp", + "output_format": "rfc3339", + "stored": true, + "type": "whatever" + } + ] + } + ], + "tag_fields": [], + "store_source": true, + "index_field_presence": false, + "timestamp_field": "sub.timestamp", + "mode": "dynamic", + "dynamic_mapping": {}, + "partition_key": "foo", + "max_num_partitions": 1, + "tokenizers": [] + }, + "indexing_settings": {}, + "search_settings": { + "default_search_fields": [ + "foo" + ] + }, + "retention": null + }, + "checkpoint": {}, + "create_timestamp": 1701075471, + "sources": [] + } + `) + + // When + _, _, err := DecodeTimestampFieldInfos(200, query) + + // Then + require.Error(t, err) + }) + }) +} + +func TestNewErrorCreationPayload(t *testing.T) { + t.Run("Test marshall creation payload error", func(t *testing.T) { + // When + err := NewErrorCreationPayload(400, "No valid format") + + // Then + require.Error(t, err) + require.ErrorContains(t, err, "\"message\":\"No valid format\"") + require.ErrorContains(t, err, "\"status\":400") + }) +}