github.com/weaviate/weaviate@v1.24.6/entities/schema/accessors_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  	"errors"
    16  	"testing"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/weaviate/weaviate/entities/models"
    20  )
    21  
    22  func Test_Accessors(t *testing.T) {
    23  	car := &models.Class{
    24  		Class: "Car",
    25  		Properties: []*models.Property{
    26  			{Name: "modelName", DataType: DataTypeText.PropString(), Tokenization: models.PropertyTokenizationWhitespace},
    27  			{Name: "manufacturerName", DataType: DataTypeText.PropString(), Tokenization: models.PropertyTokenizationWhitespace},
    28  			{Name: "horsepower", DataType: []string{"int"}},
    29  		},
    30  	}
    31  
    32  	train := &models.Class{
    33  		Class: "Train",
    34  		Properties: []*models.Property{
    35  			{Name: "capacity", DataType: []string{"int"}},
    36  			{Name: "trainCompany", DataType: DataTypeText.PropString(), Tokenization: models.PropertyTokenizationWhitespace},
    37  		},
    38  	}
    39  
    40  	action := &models.Class{
    41  		Class:      "SomeAction",
    42  		Properties: []*models.Property{},
    43  	}
    44  
    45  	sch := Empty()
    46  	sch.Objects.Classes = []*models.Class{car, train, action}
    47  
    48  	t.Run("GetClass by kind and name", func(t *testing.T) {
    49  		class := sch.GetClass("Car")
    50  		assert.Equal(t, car, class)
    51  
    52  		class = sch.GetClass("Invalid")
    53  		assert.Equal(t, (*models.Class)(nil), class)
    54  	})
    55  
    56  	t.Run("FindClass by name (without providing the kind)", func(t *testing.T) {
    57  		class := sch.FindClassByName("Car")
    58  		assert.Equal(t, car, class)
    59  
    60  		class = sch.FindClassByName("SomeAction")
    61  		assert.Equal(t, action, class)
    62  
    63  		class = sch.FindClassByName("Invalid")
    64  		assert.Equal(t, (*models.Class)(nil), class)
    65  	})
    66  
    67  	t.Run("GetPropsOfType", func(t *testing.T) {
    68  		props := sch.GetPropsOfType(DataTypeText.String())
    69  
    70  		expectedProps := []ClassAndProperty{
    71  			{
    72  				ClassName:    "Car",
    73  				PropertyName: "modelName",
    74  			},
    75  			{
    76  				ClassName:    "Car",
    77  				PropertyName: "manufacturerName",
    78  			},
    79  			{
    80  				ClassName:    "Train",
    81  				PropertyName: "trainCompany",
    82  			},
    83  		}
    84  
    85  		assert.ElementsMatch(t, expectedProps, props)
    86  	})
    87  
    88  	t.Run("GetProperty by kind, classname, name", func(t *testing.T) {
    89  		prop, err := sch.GetProperty("Car", "modelName")
    90  		assert.Nil(t, err)
    91  
    92  		expectedProp := &models.Property{
    93  			Name:         "modelName",
    94  			DataType:     DataTypeText.PropString(),
    95  			Tokenization: models.PropertyTokenizationWhitespace,
    96  		}
    97  
    98  		assert.Equal(t, expectedProp, prop)
    99  	})
   100  
   101  	t.Run("GetProperty for invalid class", func(t *testing.T) {
   102  		_, err := sch.GetProperty("WrongClass", "modelName")
   103  		assert.Equal(t, errors.New("no such class with name 'WrongClass' found in the schema. Check your schema files for which classes are available"), err)
   104  	})
   105  
   106  	t.Run("GetProperty for invalid prop", func(t *testing.T) {
   107  		_, err := sch.GetProperty("Car", "wrongProperty")
   108  		assert.Equal(t, errors.New("no such prop with name 'wrongProperty' found in class 'Car' in the schema. Check your schema files for which properties in this class are available"), err)
   109  	})
   110  }