github.com/weaviate/weaviate@v1.24.6/entities/filters/path_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package filters 13 14 import ( 15 "testing" 16 17 "github.com/stretchr/testify/assert" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func Test_ParsePath(t *testing.T) { 22 t.Run("with a primitive prop", func(t *testing.T) { 23 rootClass := "City" 24 segments := []interface{}{"population"} 25 expectedPath := &Path{ 26 Class: "City", 27 Property: "population", 28 } 29 30 path, err := ParsePath(segments, rootClass) 31 32 require.Nil(t, err, "should not error") 33 assert.Equal(t, expectedPath, path, "should parse the path correctly") 34 }) 35 36 t.Run("with len prop", func(t *testing.T) { 37 rootClass := "City" 38 segments := []interface{}{"len(population)"} 39 expectedPath := &Path{ 40 Class: "City", 41 Property: "len(population)", 42 } 43 44 path, err := ParsePath(segments, rootClass) 45 46 require.Nil(t, err, "should not error") 47 assert.Equal(t, expectedPath, path, "should parse the path correctly") 48 }) 49 50 t.Run("with nested refs", func(t *testing.T) { 51 rootClass := "City" 52 segments := []interface{}{"inCountry", "Country", "inContinent", "Continent", "onPlanet", "Planet", "name"} 53 expectedPath := &Path{ 54 Class: "City", 55 Property: "inCountry", 56 Child: &Path{ 57 Class: "Country", 58 Property: "inContinent", 59 Child: &Path{ 60 Class: "Continent", 61 Property: "onPlanet", 62 Child: &Path{ 63 Class: "Planet", 64 Property: "name", 65 }, 66 }, 67 }, 68 } 69 70 path, err := ParsePath(segments, rootClass) 71 72 require.Nil(t, err, "should not error") 73 assert.Equal(t, expectedPath, path, "should parse the path correctly") 74 75 // Extract innermost path element 76 innerMost := path.GetInnerMost() 77 assert.Equal(t, innerMost, &Path{Class: "Planet", Property: "name"}) 78 79 // Print Slice 80 }) 81 82 t.Run("with non-valid prop", func(t *testing.T) { 83 rootClass := "City" 84 segments := []interface{}{"populatS356()ion"} 85 _, err := ParsePath(segments, rootClass) 86 require.NotNil(t, err, "should error") 87 }) 88 89 t.Run("with non-valid len prop", func(t *testing.T) { 90 rootClass := "City" 91 segments := []interface{}{"len(populatS356()ion)"} 92 _, err := ParsePath(segments, rootClass) 93 require.NotNil(t, err, "should error") 94 }) 95 } 96 97 func Test_SlicePath(t *testing.T) { 98 t.Run("with a primitive prop", func(t *testing.T) { 99 path := &Path{ 100 Class: "City", 101 Property: "population", 102 } 103 expectedSegments := []interface{}{"population"} 104 105 segments := path.SliceInterface() 106 107 assert.Equal(t, expectedSegments, segments, "should slice the path correctly") 108 }) 109 110 t.Run("with nested refs", func(t *testing.T) { 111 path := &Path{ 112 Class: "City", 113 Property: "inCountry", 114 Child: &Path{ 115 Class: "Country", 116 Property: "inContinent", 117 Child: &Path{ 118 Class: "Continent", 119 Property: "onPlanet", 120 Child: &Path{ 121 Class: "Planet", 122 Property: "name", 123 }, 124 }, 125 }, 126 } 127 128 t.Run("as []interface{}", func(t *testing.T) { 129 expectedSegments := []interface{}{"inCountry", "Country", "inContinent", "Continent", "onPlanet", "Planet", "name"} 130 segments := path.SliceInterface() 131 assert.Equal(t, expectedSegments, segments, "should slice the path correctly") 132 }) 133 134 t.Run("as []string titleized", func(t *testing.T) { 135 expectedSegments := []string{"inCountry", "Country", "inContinent", "Continent", "onPlanet", "Planet", "name"} 136 segments := path.Slice() 137 assert.Equal(t, expectedSegments, segments, "should slice the path correctly") 138 }) 139 140 t.Run("as []string non-titleized", func(t *testing.T) { 141 expectedSegments := []string{"inCountry", "Country", "inContinent", "Continent", "onPlanet", "Planet", "name"} 142 segments := path.SliceNonTitleized() 143 assert.Equal(t, expectedSegments, segments, "should slice the path correctly") 144 }) 145 }) 146 }