MCPcopy Create free account
hub / github.com/blevesearch/bleve / TestMappingForTextMarshaler

Function TestMappingForTextMarshaler

mapping/mapping_test.go:1010–1065  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1008}
1009
1010func TestMappingForTextMarshaler(t *testing.T) {
1011 tm := struct {
1012 Marshalable *textMarshalable
1013 }{
1014 Marshalable: &textMarshalable{
1015 body: "text",
1016 Extra: "stuff",
1017 },
1018 }
1019
1020 // first verify that when using a mapping that doesn't explicitly
1021 // map the struct field as text, then we traverse inside the struct
1022 // and do our best
1023 m := NewIndexMapping()
1024 doc := document.NewDocument("x")
1025 err := m.MapDocument(doc, tm)
1026 if err != nil {
1027 t.Fatal(err)
1028 }
1029
1030 if len(doc.Fields) != 1 {
1031 t.Fatalf("expected 1 field, got: %d", len(doc.Fields))
1032 }
1033 if doc.Fields[0].Name() != "Marshalable.Extra" {
1034 t.Errorf("expected field to be named 'Marshalable.Extra', got: '%s'", doc.Fields[0].Name())
1035 }
1036 if string(doc.Fields[0].Value()) != tm.Marshalable.Extra {
1037 t.Errorf("expected field value to be '%s', got: '%s'", tm.Marshalable.Extra, string(doc.Fields[0].Value()))
1038 }
1039
1040 // now verify that when a mapping explicitly
1041 m = NewIndexMapping()
1042 txt := NewTextFieldMapping()
1043 m.DefaultMapping.AddFieldMappingsAt("Marshalable", txt)
1044 doc = document.NewDocument("x")
1045 err = m.MapDocument(doc, tm)
1046 if err != nil {
1047 t.Fatal(err)
1048 }
1049
1050 if len(doc.Fields) != 1 {
1051 t.Fatalf("expected 1 field, got: %d", len(doc.Fields))
1052
1053 }
1054 if doc.Fields[0].Name() != "Marshalable" {
1055 t.Errorf("expected field to be named 'Marshalable', got: '%s'", doc.Fields[0].Name())
1056 }
1057 want, err := tm.Marshalable.MarshalText()
1058 if err != nil {
1059 t.Fatal(err)
1060 }
1061 if string(doc.Fields[0].Value()) != string(want) {
1062 t.Errorf("expected field value to be '%s', got: '%s'", string(want), string(doc.Fields[0].Value()))
1063 }
1064
1065}
1066
1067func TestMappingForNilTextMarshaler(t *testing.T) {

Callers

nothing calls this directly

Calls 9

MapDocumentMethod · 0.95
NewDocumentFunction · 0.92
AddFieldMappingsAtMethod · 0.80
MarshalTextMethod · 0.80
NewIndexMappingFunction · 0.70
NewTextFieldMappingFunction · 0.70
FatalfMethod · 0.65
NameMethod · 0.65
ValueMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…