Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(dql): Split mutation test #9038

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 0 additions & 136 deletions worker/mutation_test.go → worker/mutation_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,99 +19,16 @@
package worker

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)

func TestConvertEdgeType(t *testing.T) {
var testEdges = []struct {
input *pb.DirectedEdge
to types.TypeID
expectErr bool
output *pb.DirectedEdge
}{
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
},
to: types.StringID,
expectErr: false,
output: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
ValueType: 9,
},
},
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.NamespaceAttr(0xf2, "name"),
Op: pb.DirectedEdge_DEL,
},
to: types.StringID,
expectErr: false,
output: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.NamespaceAttr(0xf2, "name"),
Op: pb.DirectedEdge_DEL,
ValueType: 9,
},
},
{
input: &pb.DirectedEdge{
ValueId: 123,
Attr: x.GalaxyAttr("name"),
},
to: types.StringID,
expectErr: true,
},
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
},
to: types.UidID,
expectErr: true,
},
}

for _, testEdge := range testEdges {
err := ValidateAndConvert(testEdge.input,
&pb.SchemaUpdate{
ValueType: pb.Posting_ValType(testEdge.to),
})
if testEdge.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.True(t, reflect.DeepEqual(testEdge.input, testEdge.output))
}
}

}

func TestValidateEdgeTypeError(t *testing.T) {
edge := &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
}

err := ValidateAndConvert(edge,
&pb.SchemaUpdate{
ValueType: pb.Posting_ValType(types.DateTimeID),
})
require.Error(t, err)
}

func TestPopulateMutationMap(t *testing.T) {
edges := []*pb.DirectedEdge{{
Value: []byte("set edge"),
Expand Down Expand Up @@ -214,56 +131,3 @@ func TestCheckSchema(t *testing.T) {
require.NoError(t, checkSchema(result.Preds[0]))
require.NoError(t, checkSchema(result.Preds[1]))
}

func TestTypeSanityCheck(t *testing.T) {
// Empty field name check.
typeDef := &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr(""),
},
},
}
err := typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition must have a name")

// Object type without object name.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
ValueType: pb.Posting_OBJECT,
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field with value type OBJECT must specify the name")

// Field with directive.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
Directive: pb.SchemaUpdate_REVERSE,
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition cannot have a directive")

// Field with tokenizer.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
Tokenizer: []string{"int"},
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition cannot have tokenizers")
}
162 changes: 162 additions & 0 deletions worker/mutation_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright 2016-2023 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package worker

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)

func TestConvertEdgeType(t *testing.T) {
var testEdges = []struct {
input *pb.DirectedEdge
to types.TypeID
expectErr bool
output *pb.DirectedEdge
}{
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
},
to: types.StringID,
expectErr: false,
output: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
ValueType: 9,
},
},
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.NamespaceAttr(0xf2, "name"),
Op: pb.DirectedEdge_DEL,
},
to: types.StringID,
expectErr: false,
output: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.NamespaceAttr(0xf2, "name"),
Op: pb.DirectedEdge_DEL,
ValueType: 9,
},
},
{
input: &pb.DirectedEdge{
ValueId: 123,
Attr: x.GalaxyAttr("name"),
},
to: types.StringID,
expectErr: true,
},
{
input: &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
},
to: types.UidID,
expectErr: true,
},
}

for _, testEdge := range testEdges {
err := ValidateAndConvert(testEdge.input,
&pb.SchemaUpdate{
ValueType: pb.Posting_ValType(testEdge.to),
})
if testEdge.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.True(t, reflect.DeepEqual(testEdge.input, testEdge.output))
}
}

}

func TestValidateEdgeTypeError(t *testing.T) {
edge := &pb.DirectedEdge{
Value: []byte("set edge"),
Attr: x.GalaxyAttr("name"),
}

err := ValidateAndConvert(edge,
&pb.SchemaUpdate{
ValueType: pb.Posting_ValType(types.DateTimeID),
})
require.Error(t, err)
}

func TestTypeSanityCheck(t *testing.T) {
// Empty field name check.
typeDef := &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr(""),
},
},
}
err := typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition must have a name")

// Object type without object name.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
ValueType: pb.Posting_OBJECT,
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field with value type OBJECT must specify the name")

// Field with directive.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
Directive: pb.SchemaUpdate_REVERSE,
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition cannot have a directive")

// Field with tokenizer.
typeDef = &pb.TypeUpdate{
Fields: []*pb.SchemaUpdate{
{
Predicate: x.GalaxyAttr("name"),
Tokenizer: []string{"int"},
},
},
}
err = typeSanityCheck(typeDef)
require.Error(t, err)
require.Contains(t, err.Error(), "Field in type definition cannot have tokenizers")
}
Loading