Skip to content

Commit

Permalink
Issue #20: add backend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 28, 2023
1 parent b66cce1 commit 1d64540
Showing 1 changed file with 313 additions and 0 deletions.
313 changes: 313 additions & 0 deletions pkg/quickwit/timestamp_infos_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}

0 comments on commit 1d64540

Please sign in to comment.