github.com/weaviate/weaviate@v1.24.6/test/acceptance/graphql_resolvers/local_get_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 test
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"fmt"
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"github.com/weaviate/weaviate/test/helper"
    24  	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
    25  )
    26  
    27  // run by setup_test.go
    28  func gettingObjects(t *testing.T) {
    29  	t.Run("listing cities without references", func(t *testing.T) {
    30  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City { name } } }")
    31  		cities := result.Get("Get", "City").AsSlice()
    32  
    33  		expected := []interface{}{
    34  			map[string]interface{}{"name": "Amsterdam"},
    35  			map[string]interface{}{"name": "Rotterdam"},
    36  			map[string]interface{}{"name": "Berlin"},
    37  			map[string]interface{}{"name": "Dusseldorf"},
    38  			map[string]interface{}{"name": "Missing Island"},
    39  			map[string]interface{}{"name": nil},
    40  		}
    41  
    42  		assert.ElementsMatch(t, expected, cities)
    43  	})
    44  
    45  	t.Run("listing cities with relations", func(t *testing.T) {
    46  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{ Get { City { name, inCountry { ... on Country { name } } } } }")
    47  		cities := result.Get("Get", "City").AsSlice()
    48  
    49  		expected := parseJSONSlice(`[
    50      { "name": "Amsterdam",  "inCountry": [{ "name": "Netherlands" }] },
    51      { "name": "Rotterdam",  "inCountry": [{ "name": "Netherlands" }] },
    52      { "name": "Berlin",     "inCountry": [{ "name": "Germany" }] },
    53      { "name": "Dusseldorf", "inCountry": [{ "name": "Germany" }] },
    54      { "name": "Missing Island", "inCountry": null },
    55      { "name": null, "inCountry": null }
    56    ]`)
    57  
    58  		assert.ElementsMatch(t, expected, cities)
    59  	})
    60  
    61  	t.Run("make sure raw response contains no error key", func(t *testing.T) {
    62  		// This test prevents a regression on gh-1535
    63  
    64  		query := []byte(`{"query":"{ Get { City { name } } }"}`)
    65  		res, err := http.Post(fmt.Sprintf("%s%s", helper.GetWeaviateURL(), "/v1/graphql"),
    66  			"application/json", bytes.NewReader(query))
    67  		require.Nil(t, err)
    68  
    69  		defer res.Body.Close()
    70  		var body map[string]interface{}
    71  		err = json.NewDecoder(res.Body).Decode(&body)
    72  		require.Nil(t, err)
    73  
    74  		_, ok := body["errors"]
    75  		assert.False(t, ok)
    76  
    77  		cities := body["data"].(map[string]interface{})["Get"].(map[string]interface{})["City"].([]interface{})
    78  		assert.Greater(t, len(cities), 0)
    79  	})
    80  
    81  	t.Run("listing cities with limit", func(t *testing.T) {
    82  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City(limit: 2) { name } } }")
    83  		cities := result.Get("Get", "City").AsSlice()
    84  
    85  		expected := []interface{}{
    86  			map[string]interface{}{"name": "Rotterdam"},
    87  			map[string]interface{}{"name": "Dusseldorf"},
    88  		}
    89  
    90  		assert.ElementsMatch(t, expected, cities)
    91  	})
    92  
    93  	t.Run("listing cities with offset and limit", func(t *testing.T) {
    94  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City(offset: 2 limit: 2) { name } } }")
    95  		cities := result.Get("Get", "City").AsSlice()
    96  
    97  		expected := []interface{}{
    98  			map[string]interface{}{"name": "Missing Island"},
    99  			map[string]interface{}{"name": nil},
   100  		}
   101  
   102  		assert.ElementsMatch(t, expected, cities)
   103  	})
   104  
   105  	t.Run("listing cities with offset", func(t *testing.T) {
   106  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City(offset: 2) { name } } }")
   107  		cities := result.Get("Get", "City").AsSlice()
   108  
   109  		expected := []interface{}{
   110  			map[string]interface{}{"name": "Missing Island"},
   111  			map[string]interface{}{"name": nil},
   112  			map[string]interface{}{"name": "Amsterdam"},
   113  			map[string]interface{}{"name": "Berlin"},
   114  		}
   115  
   116  		assert.ElementsMatch(t, expected, cities)
   117  	})
   118  
   119  	t.Run("listing cities with offset and limit beyond results size", func(t *testing.T) {
   120  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City(offset: 5 limit: 10) { name } } }")
   121  		cities := result.Get("Get", "City").AsSlice()
   122  
   123  		expected := []interface{}{
   124  			map[string]interface{}{"name": "Berlin"},
   125  		}
   126  
   127  		assert.ElementsMatch(t, expected, cities)
   128  	})
   129  
   130  	t.Run("listing cities with offset beyond results size", func(t *testing.T) {
   131  		result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, "{  Get { City(offset: 6) { name } } }")
   132  		cities := result.Get("Get", "City").AsSlice()
   133  
   134  		expected := []interface{}{}
   135  
   136  		assert.ElementsMatch(t, expected, cities)
   137  	})
   138  }