github.com/weaviate/weaviate@v1.24.6/entities/schema/crossref/crossref_source_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 crossref
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/go-openapi/strfmt"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  )
    22  
    23  func Test_Source_ParsingFromString(t *testing.T) {
    24  	t.Run("from a local object ref that is well-formed", func(t *testing.T) {
    25  		uri := "weaviate://localhost/MyClassName/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp"
    26  		ref, err := ParseSource(uri)
    27  
    28  		require.Nil(t, err, "should not error")
    29  
    30  		t.Run("is a local ref", func(t *testing.T) {
    31  			assert.Equal(t, ref.Local, true)
    32  		})
    33  
    34  		t.Run("peerName points to localhost", func(t *testing.T) {
    35  			assert.Equal(t, ref.PeerName, "localhost")
    36  		})
    37  
    38  		t.Run("id points correctly", func(t *testing.T) {
    39  			assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d"))
    40  		})
    41  
    42  		t.Run("the class name is correct", func(t *testing.T) {
    43  			assert.Equal(t, ref.Class, schema.ClassName("MyClassName"))
    44  		})
    45  
    46  		t.Run("the property name is correct", func(t *testing.T) {
    47  			assert.Equal(t, ref.Property, schema.PropertyName("myRefProp"))
    48  		})
    49  
    50  		t.Run("assembling a new source and comparing if the match", func(t *testing.T) {
    51  			alt := NewSource("MyClassName", "myRefProp",
    52  				"c2cd3f91-0160-477e-869a-8da8829e0a4d")
    53  			assert.Equal(t, ref, alt)
    54  		})
    55  	})
    56  
    57  	t.Run("from a local action ref that is well-formed", func(t *testing.T) {
    58  		uri := "weaviate://localhost/MyActionClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp"
    59  		ref, err := ParseSource(uri)
    60  
    61  		require.Nil(t, err, "should not error")
    62  
    63  		t.Run("is a local ref", func(t *testing.T) {
    64  			assert.Equal(t, ref.Local, true)
    65  		})
    66  
    67  		t.Run("peerName points to localhost", func(t *testing.T) {
    68  			assert.Equal(t, ref.PeerName, "localhost")
    69  		})
    70  
    71  		t.Run("id points correctly", func(t *testing.T) {
    72  			assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d"))
    73  		})
    74  
    75  		t.Run("the class name is correct", func(t *testing.T) {
    76  			assert.Equal(t, ref.Class, schema.ClassName("MyActionClass"))
    77  		})
    78  
    79  		t.Run("the property name is correct", func(t *testing.T) {
    80  			assert.Equal(t, ref.Property, schema.PropertyName("myRefProp"))
    81  		})
    82  
    83  		t.Run("assembling a new source and comparing if the match", func(t *testing.T) {
    84  			alt := NewSource("MyActionClass", "myRefProp",
    85  				"c2cd3f91-0160-477e-869a-8da8829e0a4d")
    86  			assert.Equal(t, ref, alt)
    87  		})
    88  	})
    89  
    90  	t.Run("from a network action ref that is well-formed", func(t *testing.T) {
    91  		uri := "weaviate://another-weaviate/SomeActionClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp"
    92  		ref, err := ParseSource(uri)
    93  
    94  		require.Nil(t, err, "should not error")
    95  
    96  		t.Run("is a local ref", func(t *testing.T) {
    97  			assert.Equal(t, ref.Local, false)
    98  		})
    99  
   100  		t.Run("peerName points to localhost", func(t *testing.T) {
   101  			assert.Equal(t, ref.PeerName, "another-weaviate")
   102  		})
   103  
   104  		t.Run("id points correctly", func(t *testing.T) {
   105  			assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d"))
   106  		})
   107  
   108  		t.Run("the class name is correct", func(t *testing.T) {
   109  			assert.Equal(t, ref.Class, schema.ClassName("SomeActionClass"))
   110  		})
   111  
   112  		t.Run("the property name is correct", func(t *testing.T) {
   113  			assert.Equal(t, ref.Property, schema.PropertyName("myRefProp"))
   114  		})
   115  	})
   116  
   117  	t.Run("with formatting errors", func(t *testing.T) {
   118  		type testCaseError struct {
   119  			name string
   120  			uri  string
   121  		}
   122  
   123  		tests := []testCaseError{
   124  			{
   125  				name: "with an invalid URL",
   126  				uri:  "i:am:not:a:url",
   127  			},
   128  			{
   129  				name: "with too few path segments",
   130  				uri:  "weaviate://localhost/SomeClass",
   131  			},
   132  			{
   133  				name: "with too many path segments",
   134  				uri:  "weaviate://localhost/SomeClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp/somethingElse",
   135  			},
   136  			{
   137  				name: "without a property",
   138  				uri:  "weaviate://localhost/SomeClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/",
   139  			},
   140  			{
   141  				name: "with an invalid uuid",
   142  				uri:  "weaviate://localhost/SomeClass/c2cd3f91-iSneakedInHere-477e-869a-8da8829e0a4d",
   143  			},
   144  			{
   145  				name: "with an invalid kind", // was /humans/SomeClass
   146  				uri:  "weaviate://localhost/SomeClass/c2cd3f91-0160-477e-869a-8da8829e0a4d",
   147  			},
   148  			{
   149  				name: "with a lowercased class name",
   150  				uri:  "weaviate://localhost/someClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp",
   151  			},
   152  		}
   153  
   154  		for _, test := range tests {
   155  			t.Run(test.name, func(t *testing.T) {
   156  				_, err := ParseSource(test.uri)
   157  				assert.NotNil(t, err, test.name)
   158  			})
   159  		}
   160  	})
   161  }
   162  
   163  func Test_Source_GenerateString(t *testing.T) {
   164  	uri := "weaviate://localhost/MyClass/c2cd3f91-0160-477e-869a-8da8829e0a4d/myRefProp"
   165  	ref, err := ParseSource(uri)
   166  
   167  	require.Nil(t, err, "should not error")
   168  	assert.Equal(t, uri, ref.String(), "should be the same as the input string")
   169  }