github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/encoding/docs/data_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package docs
    22  
    23  import (
    24  	"bytes"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/m3ninx/doc"
    28  	"github.com/m3db/m3/src/m3ninx/util"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  var tests = []struct {
    34  	name string
    35  	docs []doc.Metadata
    36  }{
    37  	{
    38  		name: "empty document",
    39  		docs: []doc.Metadata{
    40  			{
    41  				Fields: doc.Fields{},
    42  			},
    43  		},
    44  	},
    45  	{
    46  		name: "standard documents",
    47  		docs: []doc.Metadata{
    48  			{
    49  				ID: []byte("831992"),
    50  				Fields: []doc.Field{
    51  					{
    52  						Name:  []byte("fruit"),
    53  						Value: []byte("apple"),
    54  					},
    55  					{
    56  						Name:  []byte("color"),
    57  						Value: []byte("red"),
    58  					},
    59  				},
    60  			},
    61  			{
    62  				ID: []byte("080392"),
    63  				Fields: []doc.Field{
    64  					{
    65  						Name:  []byte("fruit"),
    66  						Value: []byte("banana"),
    67  					},
    68  					{
    69  						Name:  []byte("color"),
    70  						Value: []byte("yellow"),
    71  					},
    72  				},
    73  			},
    74  		},
    75  	},
    76  	{
    77  		name: "node exporter metrics",
    78  		docs: util.MustReadDocs("../../../../../util/testdata/node_exporter.json", 2000),
    79  	},
    80  }
    81  
    82  func TestStoredFieldsData(t *testing.T) {
    83  	w := NewDataWriter(nil)
    84  	for _, test := range tests {
    85  		t.Run(test.name, func(t *testing.T) {
    86  			var (
    87  				buf     = new(bytes.Buffer)
    88  				offsets = make([]int, 0)
    89  				idx     int
    90  			)
    91  			w.Reset(buf)
    92  
    93  			for i := range test.docs {
    94  				n, err := w.Write(test.docs[i])
    95  				require.NoError(t, err)
    96  				offsets = append(offsets, idx)
    97  				idx += n
    98  			}
    99  
   100  			r := NewDataReader(buf.Bytes())
   101  			for i := range test.docs {
   102  				actual, err := r.Read(uint64(offsets[i]))
   103  				require.NoError(t, err)
   104  				require.True(t, actual.Equal(test.docs[i]))
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func TestEncodedDataReader(t *testing.T) {
   111  	w := NewDataWriter(nil)
   112  	for _, test := range tests {
   113  		t.Run(test.name, func(t *testing.T) {
   114  			var (
   115  				buf     = new(bytes.Buffer)
   116  				offsets = make([]int, 0)
   117  				idx     int
   118  			)
   119  			w.Reset(buf)
   120  
   121  			for i := range test.docs {
   122  				n, err := w.Write(test.docs[i])
   123  				require.NoError(t, err)
   124  				offsets = append(offsets, idx)
   125  				idx += n
   126  			}
   127  
   128  			dataReader := NewEncodedDataReader(buf.Bytes())
   129  			docReader := NewEncodedDocumentReader()
   130  			for i := range test.docs {
   131  				encoded, err := dataReader.Read(uint64(offsets[i]))
   132  				require.NoError(t, err)
   133  
   134  				actual, err := docReader.Read(encoded)
   135  				require.NoError(t, err)
   136  				require.True(t, actual.Equal(test.docs[i]))
   137  			}
   138  		})
   139  	}
   140  }