common

package
v25.0.0-split-vector3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 40 Imported by: 0

README

This package contains test functions which are called by other packages. The intention of this package is to contain all the end to end tests which can be run with different configuration options like schema, encoding, http method etc.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Alpha1HTTP = testutil.ContainerAddr("alpha1", 8080)
	Alpha1gRPC = testutil.ContainerAddr("alpha1", 9080)

	GraphqlURL      = "http://" + Alpha1HTTP + "/graphql"
	GraphqlAdminURL = "http://" + Alpha1HTTP + "/admin"
)

Functions

func AssertSchemaUpdateCounterIncrement

func AssertSchemaUpdateCounterIncrement(t *testing.T, authority string, oldCounter uint64, header http.Header)

AssertSchemaUpdateCounterIncrement asserts that the schemaUpdateCounter is greater than the oldCounter, indicating that the GraphQL schema has been updated. If it can't make the assertion with enough retries, it fails the test.

func AssertUpdateGQLSchemaFailure

func AssertUpdateGQLSchemaFailure(t *testing.T, authority, schema string, headers http.Header,
	expectedErrors []string)

AssertUpdateGQLSchemaFailure tries to update the GraphQL schema and asserts that the update failed with all of the given errors.

func BootstrapAuthData

func BootstrapAuthData() ([]byte, []byte)

func BootstrapServer

func BootstrapServer(schema, data []byte)

func CheckGraphQLStarted

func CheckGraphQLStarted(url string) error

func CreateNamespace

func CreateNamespace(t *testing.T, headers http.Header, whichAlpha string) uint64

func CreateNamespaces

func CreateNamespaces(t *testing.T, headers http.Header, whichAlpha string, count int) []uint64

func DeleteGqlType

func DeleteGqlType(
	t *testing.T,
	typeName string,
	filter map[string]interface{},
	expectedNumUids int,
	expectedErrors x.GqlErrorList)

func DeleteNamespace

func DeleteNamespace(t *testing.T, id uint64, header http.Header, whichAlpha string)

func DgraphDirectiveWithSpecialCharacters

func DgraphDirectiveWithSpecialCharacters(t *testing.T)

func GetJWT

func GetJWT(t *testing.T, user, role interface{}, metaInfo *testutil.AuthMeta) http.Header

func GetJWTForInterfaceAuth

func GetJWTForInterfaceAuth(t *testing.T, user, role string, ans bool, metaInfo *testutil.AuthMeta) http.Header

func GetJWTWithNullUser

func GetJWTWithNullUser(t *testing.T, role interface{}, metaInfo *testutil.AuthMeta) http.Header

func GetXidFilter

func GetXidFilter(xidKey string, xidVals []interface{}) map[string]interface{}

func JSONEqGraphQL

func JSONEqGraphQL(t *testing.T, expected, actual string)

JSONEqGraphQL compares two JSON strings obtained from a /graphql response. To avoid issues, don't use space for indentation in expected input.

The comparison requirements for JSON reported by /graphql are following:

  • The key order matters in object comparison, i.e. {"hello": "world", "foo": "bar"} is not same as: {"foo": "bar", "hello": "world"}
  • A key missing in an object is not same as that key present with value null, i.e. {"hello": "world"} is not same as: {"hello": "world", "foo": null}
  • Integers that are out of the [-(2^53)+1, (2^53)-1] precision range supported by JSON RFC, should still be encoded with full precision. i.e., the number 9007199254740993 ( = 2^53 + 1) should not get encoded as 9007199254740992 ( = 2^53). This happens in Go's standard JSON parser due to IEEE754 precision loss for floating point numbers.

The above requirements are not satisfied by the standard require.JSONEq or testutil.CompareJSON methods. In order to satisfy all these requirements, this implementation just requires that the input strings be equal after removing `\r`, `\n`, `\t` whitespace characters from the inputs. TODO:

Find a better way to do this such that order isn't mandated in list comparison.
So that it is actually usable at places it is not used at present.

func ListNamespaces

func ListNamespaces(t *testing.T, jwtToken string, headers http.Header, whichAlpha string) []uint64

func PopulateGraphQLData

func PopulateGraphQLData(client *dgo.Dgraph, data []byte) error

func RequireNoGQLErrors

func RequireNoGQLErrors(t *testing.T, resp *GraphQLResponse)

func RunAll

func RunAll(t *testing.T)

RunAll runs all the test functions in this package as sub tests.

func RunGQLRequest

func RunGQLRequest(req *http.Request) ([]byte, error)

RunGQLRequest runs a HTTP GraphQL request and returns the data or any errors.

func SafelyDropAll

func SafelyDropAll(t *testing.T)

SafelyDropAll can be used in tests for doing DROP_ALL when ACL is disabled. This should be used after at least one schema update operation has succeeded. Once the control returns from it, one can be sure that the DROP_ALL has reached the GraphQL layer and the existing schema has been updated to an empty schema.

func SafelyDropAllWithGroot

func SafelyDropAllWithGroot(t *testing.T)

SafelyDropAllWithGroot can be used in tests for doing DROP_ALL when ACL is enabled. This should be used after at least one schema update operation has succeeded. Once the control returns from it, one can be sure that the DROP_ALL has reached the GraphQL layer and the existing schema has been updated to an empty schema.

func SchemaTest

func SchemaTest(t *testing.T, expectedDgraphSchema string)

Types

type ErrorCase

type ErrorCase struct {
	Name         string
	GQLRequest   string
	GQLVariables string
	Errors       x.GqlErrorList
}

type GqlSchema

type GqlSchema struct {
	Id              string
	Schema          string
	GeneratedSchema string
}

func AssertGetGQLSchema

func AssertGetGQLSchema(t *testing.T, authority string, header http.Header) *GqlSchema

AssertGetGQLSchema queries the current GraphQL schema using getGQLSchema query and asserts that the query doesn't give any errors. It returns a *GqlSchema received in response to the query.

func AssertGetGQLSchemaRequireId

func AssertGetGQLSchemaRequireId(t *testing.T, authority string, header http.Header) *GqlSchema

In addition to AssertGetGQLSchema, it also asserts that the response returned from the getGQLSchema query isn't nil and the Id in the response is actually a uid.

func AssertUpdateGQLSchemaSuccess

func AssertUpdateGQLSchemaSuccess(t *testing.T, authority, schema string,
	headers http.Header) *GqlSchema

AssertUpdateGQLSchemaSuccess updates the GraphQL schema, asserts that the update succeeded and the returned response is correct. It returns a *GqlSchema it received in the response.

func SafelyUpdateGQLSchema

func SafelyUpdateGQLSchema(t *testing.T, authority, schema string, headers http.Header) *GqlSchema

SafelyUpdateGQLSchema can be safely used in tests to update the GraphQL schema. Once the control returns from it, one can be sure that the newly applied schema is the one being served by the GraphQL layer, and hence it is safe to make any queries as per the new schema. Note that if the schema being provided is same as the current schema in the GraphQL layer, then this function will fail the test with a fatal error.

func SafelyUpdateGQLSchemaOnAlpha1

func SafelyUpdateGQLSchemaOnAlpha1(t *testing.T, schema string) *GqlSchema

SafelyUpdateGQLSchemaOnAlpha1 is SafelyUpdateGQLSchema for alpha1 test container.

type GraphQLParams

type GraphQLParams struct {
	Query         string                    `json:"query"`
	OperationName string                    `json:"operationName"`
	Variables     map[string]interface{}    `json:"variables"`
	Extensions    *schema.RequestExtensions `json:"extensions,omitempty"`

	Headers http.Header
	// contains filtered or unexported fields
}

func (*GraphQLParams) CreateGQLPost

func (params *GraphQLParams) CreateGQLPost(url string) (*http.Request, error)

func (*GraphQLParams) Execute

func (params *GraphQLParams) Execute(t require.TestingT, req *http.Request) *GraphQLResponse

Execute takes a HTTP request from either ExecuteAsPost or ExecuteAsGet and executes the request

func (*GraphQLParams) ExecuteAsGet

func (params *GraphQLParams) ExecuteAsGet(t *testing.T, url string) *GraphQLResponse

ExecuteAsGet builds a HTTP GET request from the GraphQL input structure and executes the request to url.

func (*GraphQLParams) ExecuteAsPost

func (params *GraphQLParams) ExecuteAsPost(t require.TestingT, url string) *GraphQLResponse

ExecuteAsPost builds a HTTP POST request from the GraphQL input structure and executes the request to url.

func (*GraphQLParams) ExecuteAsPostApplicationGraphql

func (params *GraphQLParams) ExecuteAsPostApplicationGraphql(t *testing.T, url string) *GraphQLResponse

ExecuteAsPostApplicationGraphql builds an HTTP Post with type application/graphql Note, variables are not allowed

type GraphQLResponse

type GraphQLResponse struct {
	Data       json.RawMessage        `json:"data,omitempty"`
	Errors     x.GqlErrorList         `json:"errors,omitempty"`
	Extensions map[string]interface{} `json:"extensions,omitempty"`
}

GraphQLResponse GraphQL response structure. see https://graphql.github.io/graphql-spec/June2018/#sec-Response

func RetryUpdateGQLSchema

func RetryUpdateGQLSchema(t *testing.T, authority, schema string, headers http.Header) *GraphQLResponse

RetryUpdateGQLSchema tries to update the GraphQL schema and if it receives a retryable error, it keeps retrying until it either receives no error or a non-retryable error. Then it returns the GraphQLResponse it received as a result of calling updateGQLSchema.

func (*GraphQLResponse) RequireNoGQLErrors

func (gqlRes *GraphQLResponse) RequireNoGQLErrors(t *testing.T)

type GraphQLSubscriptionClient

type GraphQLSubscriptionClient struct {
	// contains filtered or unexported fields
}

GraphQLSubscriptionClient uses apollo subscription protocol to subscribe on GraphQL server.

func NewGraphQLSubscription

func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload string) (
	*GraphQLSubscriptionClient, error)

NewGraphQLSubscription returns graphql subscription client.

func (*GraphQLSubscriptionClient) RecvMsg

func (client *GraphQLSubscriptionClient) RecvMsg() ([]byte, error)

RecvMsg recives graphql update from the server.

func (*GraphQLSubscriptionClient) Terminate

func (client *GraphQLSubscriptionClient) Terminate()

Terminate will terminate the subscription.

type ProbeGraphQLResp

type ProbeGraphQLResp struct {
	Healthy             bool `json:"-"`
	Status              string
	SchemaUpdateCounter uint64
}

func RetryProbeGraphQL

func RetryProbeGraphQL(t *testing.T, authority string, header http.Header) *ProbeGraphQLResp

type Todo

type Todo struct {
	Id    string `json:"id,omitempty"`
	Text  string `json:"text,omitempty"`
	Owner string `json:"owner,omitempty"`
}

type Tweets

type Tweets struct {
	Id        string `json:"id,omitempty"`
	Text      string `json:"text,omitempty"`
	Timestamp string `json:"timestamp,omitempty"`
	User      *User  `json:"user,omitempty"`
}

func (*Tweets) DeleteByID

func (twt *Tweets) DeleteByID(t *testing.T, user string, metaInfo *testutil.AuthMeta)

type User

type User struct {
	Username string `json:"username,omitempty"`
	Age      uint64 `json:"age,omitempty"`
	IsPublic bool   `json:"isPublic,omitempty"`
	Disabled bool   `json:"disabled,omitempty"`
	Password string `json:"password,omitempty"`
}

type UserSecret

type UserSecret struct {
	Id      string `json:"id,omitempty"`
	ASecret string `json:"aSecret,omitempty"`
	OwnedBy string `json:"ownedBy,omitempty"`
}

func (*UserSecret) Delete

func (us *UserSecret) Delete(t *testing.T, user, role string, metaInfo *testutil.AuthMeta)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL