github.com/weaviate/weaviate@v1.24.6/usecases/schema/ref_finder_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 schema
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/weaviate/weaviate/entities/filters"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  )
    22  
    23  func TestRefFinder(t *testing.T) {
    24  	t.Run("on an empty schema", func(t *testing.T) {
    25  		s := schema.Schema{
    26  			Objects: &models.Schema{
    27  				Classes: nil,
    28  			},
    29  		}
    30  
    31  		getter := &fakeSchemaGetterForRefFinder{s}
    32  		res := NewRefFinder(getter, 3).Find("Car")
    33  
    34  		assert.Len(t, res, 0)
    35  	})
    36  
    37  	t.Run("on an schema containing only the target class and unrelated classes", func(t *testing.T) {
    38  		s := schema.Schema{
    39  			Objects: &models.Schema{
    40  				Classes: []*models.Class{
    41  					{
    42  						Class: "Car",
    43  						Properties: []*models.Property{
    44  							{
    45  								Name:     "model",
    46  								DataType: schema.DataTypeText.PropString(),
    47  							},
    48  						},
    49  					},
    50  					{
    51  						Class: "Tree",
    52  						Properties: []*models.Property{
    53  							{
    54  								Name:     "kind",
    55  								DataType: schema.DataTypeText.PropString(),
    56  							},
    57  						},
    58  					},
    59  				},
    60  			},
    61  		}
    62  
    63  		getter := &fakeSchemaGetterForRefFinder{s}
    64  		res := NewRefFinder(getter, 3).Find("Car")
    65  
    66  		assert.Len(t, res, 0)
    67  	})
    68  
    69  	t.Run("on a schema containing a single level ref to the target", func(t *testing.T) {
    70  		s := schema.Schema{
    71  			Objects: &models.Schema{
    72  				Classes: []*models.Class{
    73  					{
    74  						Class: "Car",
    75  						Properties: []*models.Property{
    76  							{
    77  								Name:     "model",
    78  								DataType: schema.DataTypeText.PropString(),
    79  							},
    80  						},
    81  					},
    82  					{
    83  						Class: "Tree",
    84  						Properties: []*models.Property{
    85  							{
    86  								Name:     "kind",
    87  								DataType: schema.DataTypeText.PropString(),
    88  							},
    89  						},
    90  					},
    91  					{
    92  						Class: "Drive",
    93  						Properties: []*models.Property{
    94  							{
    95  								Name:     "destination",
    96  								DataType: schema.DataTypeText.PropString(),
    97  							},
    98  						},
    99  					},
   100  					{
   101  						Class: "Drive",
   102  						Properties: []*models.Property{
   103  							{
   104  								Name:     "destination",
   105  								DataType: schema.DataTypeText.PropString(),
   106  							},
   107  							{
   108  								Name:     "vehicle",
   109  								DataType: []string{"Car"},
   110  							},
   111  						},
   112  					},
   113  				},
   114  			},
   115  		}
   116  
   117  		getter := &fakeSchemaGetterForRefFinder{s}
   118  		res := NewRefFinder(getter, 3).Find("Car")
   119  
   120  		assert.Equal(t, []filters.Path{
   121  			{
   122  				Class:    "Drive",
   123  				Property: "vehicle",
   124  				Child: &filters.Path{
   125  					Class:    "Car",
   126  					Property: "id",
   127  				},
   128  			},
   129  		}, res)
   130  	})
   131  
   132  	t.Run("on a schema containing a single level and a multi level ref to the target", func(t *testing.T) {
   133  		s := schema.Schema{
   134  			Objects: &models.Schema{
   135  				Classes: []*models.Class{
   136  					{
   137  						Class: "Dog",
   138  						Properties: []*models.Property{
   139  							{
   140  								Name:     "name",
   141  								DataType: schema.DataTypeText.PropString(),
   142  							},
   143  							{
   144  								Name:     "hasOwner",
   145  								DataType: []string{"Person"},
   146  							},
   147  						},
   148  					},
   149  					{
   150  						Class: "Car",
   151  						Properties: []*models.Property{
   152  							{
   153  								Name:     "model",
   154  								DataType: schema.DataTypeText.PropString(),
   155  							},
   156  						},
   157  					},
   158  					{
   159  						Class: "Person",
   160  						Properties: []*models.Property{
   161  							{
   162  								Name:     "name",
   163  								DataType: schema.DataTypeText.PropString(),
   164  							},
   165  							{
   166  								Name:     "travels",
   167  								DataType: []string{"Drive"},
   168  							},
   169  							{
   170  								Name:     "hasPets",
   171  								DataType: []string{"Dog"},
   172  							},
   173  						},
   174  					},
   175  					{
   176  						Class: "Drive",
   177  						Properties: []*models.Property{
   178  							{
   179  								Name:     "destination",
   180  								DataType: schema.DataTypeText.PropString(),
   181  							},
   182  							{
   183  								Name:     "vehicle",
   184  								DataType: []string{"Car"},
   185  							},
   186  						},
   187  					},
   188  				},
   189  			},
   190  		}
   191  
   192  		getter := &fakeSchemaGetterForRefFinder{s}
   193  		res := NewRefFinder(getter, 3).Find("Car")
   194  
   195  		assert.Equal(t, []filters.Path{
   196  			{
   197  				Class:    "Drive",
   198  				Property: "vehicle",
   199  				Child: &filters.Path{
   200  					Class:    "Car",
   201  					Property: "id",
   202  				},
   203  			},
   204  			{
   205  				Class:    "Person",
   206  				Property: "travels",
   207  				Child: &filters.Path{
   208  					Class:    "Drive",
   209  					Property: "vehicle",
   210  					Child: &filters.Path{
   211  						Class:    "Car",
   212  						Property: "id",
   213  					},
   214  				},
   215  			},
   216  			{
   217  				Class:    "Dog",
   218  				Property: "hasOwner",
   219  				Child: &filters.Path{
   220  					Class:    "Person",
   221  					Property: "travels",
   222  					Child: &filters.Path{
   223  						Class:    "Drive",
   224  						Property: "vehicle",
   225  						Child: &filters.Path{
   226  							Class:    "Car",
   227  							Property: "id",
   228  						},
   229  					},
   230  				},
   231  			},
   232  		}, res)
   233  	})
   234  }
   235  
   236  type fakeSchemaGetterForRefFinder struct {
   237  	schema schema.Schema
   238  }
   239  
   240  func (f *fakeSchemaGetterForRefFinder) GetSchemaSkipAuth() schema.Schema {
   241  	return f.schema
   242  }