github.com/weaviate/weaviate@v1.24.6/usecases/traverser/traverser_get_params_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 traverser
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  	"github.com/weaviate/weaviate/entities/search"
    20  )
    21  
    22  func TestGetParams(t *testing.T) {
    23  	t.Run("without any select properties", func(t *testing.T) {
    24  		sp := search.SelectProperties{}
    25  		assert.Equal(t, false, sp.HasRefs(), "indicates no refs are present")
    26  	})
    27  
    28  	t.Run("with only primitive select properties", func(t *testing.T) {
    29  		sp := search.SelectProperties{
    30  			search.SelectProperty{
    31  				IsPrimitive: true,
    32  				Name:        "Foo",
    33  			},
    34  			search.SelectProperty{
    35  				IsPrimitive: true,
    36  				Name:        "Bar",
    37  			},
    38  		}
    39  
    40  		assert.Equal(t, false, sp.HasRefs(), "indicates no refs are present")
    41  
    42  		resolve, err := sp.ShouldResolve([]string{"inCountry", "Country"})
    43  		require.Nil(t, err)
    44  		assert.Equal(t, false, resolve)
    45  	})
    46  
    47  	t.Run("with a ref prop", func(t *testing.T) {
    48  		sp := search.SelectProperties{
    49  			search.SelectProperty{
    50  				IsPrimitive: true,
    51  				Name:        "name",
    52  			},
    53  			search.SelectProperty{
    54  				IsPrimitive: false,
    55  				Name:        "inCity",
    56  				Refs: []search.SelectClass{
    57  					{
    58  						ClassName: "City",
    59  						RefProperties: search.SelectProperties{
    60  							search.SelectProperty{
    61  								Name:        "name",
    62  								IsPrimitive: true,
    63  							},
    64  							search.SelectProperty{
    65  								Name:        "inCountry",
    66  								IsPrimitive: false,
    67  								Refs: []search.SelectClass{
    68  									{
    69  										ClassName: "Country",
    70  										RefProperties: search.SelectProperties{
    71  											search.SelectProperty{
    72  												Name:        "name",
    73  												IsPrimitive: true,
    74  											},
    75  										},
    76  									},
    77  								},
    78  							},
    79  						},
    80  					},
    81  				},
    82  			},
    83  		}
    84  
    85  		t.Run("checking for refs", func(t *testing.T) {
    86  			assert.Equal(t, true, sp.HasRefs(), "indicates refs are present")
    87  		})
    88  
    89  		t.Run("checking valid single level ref", func(t *testing.T) {
    90  			resolve, err := sp.ShouldResolve([]string{"inCity", "City"})
    91  			require.Nil(t, err)
    92  			assert.Equal(t, true, resolve)
    93  		})
    94  
    95  		t.Run("checking invalid single level ref", func(t *testing.T) {
    96  			resolve, err := sp.ShouldResolve([]string{"inCity", "Town"})
    97  			require.Nil(t, err)
    98  			assert.Equal(t, false, resolve)
    99  		})
   100  
   101  		t.Run("checking valid nested ref", func(t *testing.T) {
   102  			resolve, err := sp.ShouldResolve([]string{"inCity", "City", "inCountry", "Country"})
   103  			require.Nil(t, err)
   104  			assert.Equal(t, true, resolve)
   105  		})
   106  
   107  		t.Run("checking invalid nested level refs", func(t *testing.T) {
   108  			resolve, err := sp.ShouldResolve([]string{"inCity", "Town", "inCountry", "Country"})
   109  			require.Nil(t, err)
   110  			assert.Equal(t, false, resolve)
   111  
   112  			resolve, err = sp.ShouldResolve([]string{"inCity", "City", "inCountry", "Land"})
   113  			require.Nil(t, err)
   114  			assert.Equal(t, false, resolve)
   115  		})
   116  
   117  		t.Run("selecting a specific prop", func(t *testing.T) {
   118  			prop := sp.FindProperty("inCity")
   119  			assert.Equal(t, prop, &sp[1])
   120  		})
   121  	})
   122  }