-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from hasura/m-bilal/fix-subfield-extraction
make `internal.ExtractTypes` return subFieldMap
- Loading branch information
Showing
13 changed files
with
1,232 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ var tests = []test{ | |
{ | ||
name: "books", | ||
}, | ||
{ | ||
name: "books_2", | ||
}, | ||
} | ||
|
||
func TestSchema(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var fieldsAndSubfieldsTests = []struct { | ||
name string | ||
fieldMapStr string | ||
wantLegacyFieldAndSubfields []string | ||
wantType string | ||
wantSubFieldsMap map[string]string | ||
}{ | ||
{ | ||
name: "nested_field", | ||
fieldMapStr: `{ | ||
"type": "text", | ||
"fields": { | ||
"raw": { | ||
"type": "keyword" | ||
} | ||
} | ||
}`, | ||
wantLegacyFieldAndSubfields: []string{"text", "keyword"}, | ||
wantType: "text", | ||
wantSubFieldsMap: map[string]string{"keyword": "raw"}, | ||
}, | ||
{ | ||
name: "nested_fields", | ||
fieldMapStr: `{ | ||
"type": "text", | ||
"fields": { | ||
"raw": { | ||
"type": "keyword" | ||
}, | ||
"raw_int": { | ||
"type": "integer" | ||
}, | ||
"raw_double": { | ||
"type": "double" | ||
}, | ||
"simple_boolean": { | ||
"type": "boolean" | ||
}, | ||
"ip": { | ||
"type": "ip" | ||
}, | ||
"version": { | ||
"type": "version" | ||
} | ||
} | ||
}`, | ||
wantLegacyFieldAndSubfields: []string{ | ||
"text", | ||
"boolean", | ||
"integer", | ||
"double", | ||
"ip", | ||
"keyword", | ||
"version", | ||
}, | ||
wantType: "text", | ||
wantSubFieldsMap: map[string]string{ | ||
"keyword": "raw", | ||
"integer": "raw_int", | ||
"double": "raw_double", | ||
"boolean": "simple_boolean", | ||
"ip": "ip", | ||
"version": "version", | ||
}, | ||
}, | ||
{ | ||
name: "no_nested_field", | ||
fieldMapStr: `{ | ||
"type": "keyword" | ||
}`, | ||
wantLegacyFieldAndSubfields: []string{"keyword"}, | ||
wantType: "keyword", | ||
wantSubFieldsMap: map[string]string{}, | ||
}, | ||
{ | ||
name: "nested_field_type_same_as_field_type", | ||
fieldMapStr: `{ | ||
"type": "text", | ||
"fields": { | ||
"english": { | ||
"type": "text", | ||
"analyzer": "english" | ||
} | ||
} | ||
}`, | ||
wantLegacyFieldAndSubfields: []string{"text"}, | ||
wantType: "text", | ||
wantSubFieldsMap: map[string]string{}, | ||
}, | ||
{ | ||
name: "duplicate_nested_field", | ||
fieldMapStr: `{ | ||
"type": "text", | ||
"fields": { | ||
"raw": { | ||
"type": "keyword" | ||
}, | ||
"raw": { | ||
"type": "keyword" | ||
} | ||
} | ||
}`, | ||
wantLegacyFieldAndSubfields: []string{"text", "keyword"}, | ||
wantType: "text", | ||
wantSubFieldsMap: map[string]string{"keyword": "raw"}, | ||
}, | ||
} | ||
|
||
func TestExtractTypes(t *testing.T) { | ||
for _, tt := range fieldsAndSubfieldsTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var fieldMap map[string]interface{} | ||
err := json.Unmarshal([]byte(tt.fieldMapStr), &fieldMap) | ||
assert.NoError(t, err, "Error unmarshalling JSON") | ||
|
||
fieldAndSubfields, fieldType, subFieldsMap := ExtractTypes(fieldMap) | ||
|
||
assert.Equal(t, tt.wantLegacyFieldAndSubfields, fieldAndSubfields) | ||
assert.Equal(t, tt.wantType, fieldType) | ||
assert.Equal(t, tt.wantSubFieldsMap, subFieldsMap) | ||
}) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
testdata/unit_tests/fields_tests/books_2/configuration.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"indices": { | ||
"my_book_index": { | ||
"mappings": { | ||
"properties": { | ||
"author": { | ||
"type": "keyword" | ||
}, | ||
"description": { | ||
"type": "text" | ||
}, | ||
"genre": { | ||
"type": "keyword" | ||
}, | ||
"pages": { | ||
"type": "integer" | ||
}, | ||
"published_date": { | ||
"format": "yyyy-MM-dd", | ||
"type": "date" | ||
}, | ||
"rating": { | ||
"type": "float" | ||
}, | ||
"title": { | ||
"fields": { | ||
"keywordSubField": { | ||
"type": "keyword" | ||
} | ||
}, | ||
"type": "text" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"queries": {} | ||
} |
Oops, something went wrong.