|
| 1 | +// Copyright Quesma, licensed under the Elastic License 2.0. |
| 2 | +// SPDX-License-Identifier: Elastic-2.0 |
| 3 | + |
| 4 | +package testcases |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "sort" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +type CommonTableAndRegularTable struct { |
| 16 | + IntegrationTestcaseBase |
| 17 | +} |
| 18 | + |
| 19 | +func NewCommonTableAndRegularTable() *CommonTableAndRegularTable { |
| 20 | + return &CommonTableAndRegularTable{ |
| 21 | + IntegrationTestcaseBase: IntegrationTestcaseBase{ |
| 22 | + ConfigTemplate: "quesma-common-table-and-regular-table.yml.template", |
| 23 | + }, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func (a *CommonTableAndRegularTable) SetupContainers(ctx context.Context) error { |
| 28 | + containers, err := setupAllContainersWithCh(ctx, a.ConfigTemplate) |
| 29 | + a.Containers = containers |
| 30 | + return err |
| 31 | +} |
| 32 | + |
| 33 | +func (a *CommonTableAndRegularTable) RunTests(ctx context.Context, t *testing.T) error { |
| 34 | + |
| 35 | + t.Run("all mappings", func(t *testing.T) { a.mappingsAll(ctx, t) }) |
| 36 | + |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func (a *CommonTableAndRegularTable) mappingsAll(ctx context.Context, t *testing.T) { |
| 41 | + |
| 42 | + fetchStarFieldCaps := func() map[string]any { |
| 43 | + |
| 44 | + resp, body := a.RequestToQuesma(ctx, t, "GET", "/*/_field_caps", nil) |
| 45 | + if resp.StatusCode != 200 { |
| 46 | + t.Fatalf("Failed to fetch mappings: %s", body) |
| 47 | + } |
| 48 | + |
| 49 | + var fieldCaps map[string]any |
| 50 | + if err := json.Unmarshal(body, &fieldCaps); err != nil { |
| 51 | + t.Fatalf("Failed to unmarshal mappings: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + return fieldCaps |
| 55 | + } |
| 56 | + |
| 57 | + fetchFieldCaps := func(index string) { |
| 58 | + |
| 59 | + resp, body := a.RequestToQuesma(ctx, t, "GET", fmt.Sprintf("/%s/_field_caps", index), nil) |
| 60 | + if resp.StatusCode != 200 { |
| 61 | + t.Fatalf("Failed to fetch mappings: %s", body) |
| 62 | + } |
| 63 | + |
| 64 | + var fieldCaps map[string]any |
| 65 | + if err := json.Unmarshal(body, &fieldCaps); err != nil { |
| 66 | + t.Fatalf("Failed to unmarshal mappings: %v", err) |
| 67 | + } |
| 68 | + if len(fieldCaps) == 0 { |
| 69 | + t.Fatalf("Expected field caps for index %s, got empty response", index) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + checkFieldCaps := func(fieldCaps map[string]any, expectedIndexes []string) { |
| 74 | + |
| 75 | + indicesAny, ok := fieldCaps["indices"].([]any) |
| 76 | + if !ok { |
| 77 | + t.Fatalf("Expected 'indices' to be a slice of strings, got: %T", fieldCaps["indices"]) |
| 78 | + } |
| 79 | + |
| 80 | + indices := make([]string, len(indicesAny)) |
| 81 | + for i, index := range indicesAny { |
| 82 | + indexStr, ok := index.(string) |
| 83 | + if !ok { |
| 84 | + t.Fatalf("Expected index to be a string, got: %T", index) |
| 85 | + } |
| 86 | + indices[i] = indexStr |
| 87 | + } |
| 88 | + |
| 89 | + assert.Equal(t, len(expectedIndexes), len(indices)) |
| 90 | + |
| 91 | + sort.Strings(indices) |
| 92 | + sort.Strings(expectedIndexes) |
| 93 | + |
| 94 | + for i, index := range expectedIndexes { |
| 95 | + assert.Equal(t, index, indices[i], fmt.Sprintf("Index %s should exist in field caps", index)) |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + fetchStarMapping := func() map[string]any { |
| 101 | + |
| 102 | + resp, body := a.RequestToQuesma(ctx, t, "GET", "/*/_mapping", nil) |
| 103 | + if resp.StatusCode != 200 { |
| 104 | + t.Fatalf("Failed to fetch mappings: %s", body) |
| 105 | + } |
| 106 | + |
| 107 | + var mappings map[string]any |
| 108 | + if err := json.Unmarshal(body, &mappings); err != nil { |
| 109 | + t.Fatalf("Failed to unmarshal mappings: %v", err) |
| 110 | + } |
| 111 | + return mappings |
| 112 | + } |
| 113 | + |
| 114 | + fetchMappings := func(index string) { |
| 115 | + resp, body := a.RequestToQuesma(ctx, t, "GET", fmt.Sprintf("/%s/_mapping", index), nil) |
| 116 | + if resp.StatusCode != 200 { |
| 117 | + t.Fatalf("Failed to fetch mappings: %s", body) |
| 118 | + } |
| 119 | + |
| 120 | + var mappings map[string]any |
| 121 | + if err := json.Unmarshal(body, &mappings); err != nil { |
| 122 | + t.Fatalf("Failed to unmarshal mappings: %v", err) |
| 123 | + } |
| 124 | + if len(mappings) == 0 { |
| 125 | + t.Fatalf("Expected mappings for index %s, got empty response", index) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + checkMappings := func(mappings map[string]any, expectedIndexes []string) { |
| 130 | + |
| 131 | + assert.Equal(t, len(expectedIndexes), len(mappings)) |
| 132 | + |
| 133 | + for _, index := range expectedIndexes { |
| 134 | + _, exists := mappings[index] |
| 135 | + assert.True(t, exists, fmt.Sprintf("Index %s should exist in mappings", index)) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + expectedIndexes := []string{"first", "second", "third"} // explicitly defined indexes in the config |
| 140 | + |
| 141 | + mappings := fetchStarMapping() |
| 142 | + checkMappings(mappings, expectedIndexes) |
| 143 | + |
| 144 | + fieldCaps := fetchStarFieldCaps() |
| 145 | + checkFieldCaps(fieldCaps, expectedIndexes) |
| 146 | + |
| 147 | + for _, index := range expectedIndexes { |
| 148 | + fetchFieldCaps(index) |
| 149 | + fetchMappings(index) |
| 150 | + } |
| 151 | + |
| 152 | + // add a new index (common table) |
| 153 | + |
| 154 | + a.RequestToQuesma(ctx, t, "POST", "/go_to_common_table/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`)) |
| 155 | + |
| 156 | + expectedIndexes = append(expectedIndexes, "go_to_common_table") |
| 157 | + mappings = fetchStarMapping() |
| 158 | + checkMappings(mappings, expectedIndexes) |
| 159 | + |
| 160 | + fieldCaps = fetchStarFieldCaps() |
| 161 | + checkFieldCaps(fieldCaps, expectedIndexes) |
| 162 | + |
| 163 | + for _, index := range expectedIndexes { |
| 164 | + fetchFieldCaps(index) |
| 165 | + fetchMappings(index) |
| 166 | + } |
| 167 | +} |
0 commit comments