Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Jan 6, 2025
1 parent 4cb13f8 commit 884dab6
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 94 deletions.
36 changes: 29 additions & 7 deletions chunker/json_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ import (
"encoding/json"
"fmt"
"math"
"os"
"testing"
"time"

"github.com/golang/glog"
"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/dgraphtest"
"github.com/dgraph-io/dgraph/v24/testutil"
"github.com/dgraph-io/dgraph/v24/tok"
"github.com/dgraph-io/dgraph/v24/types"
"github.com/dgraph-io/dgraph/v24/x"
)

var (
dg *dgraphapi.GrpcClient
)

func makeNquad(sub, pred string, val *api.Value) *api.NQuad {
Expand Down Expand Up @@ -96,17 +104,12 @@ func FastParse(b []byte, op int) ([]*api.NQuad, error) {

func (exp *Experiment) verify() {
// insert the data into dgraph
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
if err != nil {
exp.t.Fatalf("Error while getting a dgraph client: %v", err)
}

ctx := context.Background()
require.NoError(exp.t, dg.Login(ctx, dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
require.NoError(exp.t, dg.Alter(ctx, &api.Operation{DropAll: true}), "drop all failed")
require.NoError(exp.t, dg.Alter(ctx, &api.Operation{Schema: exp.schema}),
"schema change failed")

_, err = dg.NewTxn().Mutate(ctx,
_, err := dg.NewTxn().Mutate(ctx,
&api.Mutation{Set: exp.nqs, CommitNow: true})
require.NoError(exp.t, err, "mutation failed")

Expand Down Expand Up @@ -1512,3 +1515,22 @@ func TestNquadsJsonValidVector(t *testing.T) {
exp.nqs = fastNQ
exp.verify()
}

func TestMain(m *testing.M) {
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
c, err := dgraphtest.NewLocalCluster(conf)

x.Panic(err)
x.Panic(c.Start())
var cleanup func()
dg, cleanup, err = c.Client()
x.Panic(err)
defer cleanup()
code := m.Run()
if code != 0 {
c.Cleanup(true)
} else {
c.Cleanup(false)
}
os.Exit(code)
}
80 changes: 41 additions & 39 deletions contrib/integration/testtxn/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ import (

"github.com/dgraph-io/dgo/v240"
"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/testutil"
"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/dgraphtest"
"github.com/dgraph-io/dgraph/v24/x"
)

type state struct {
dg *dgo.Dgraph
dg *dgraphapi.GrpcClient
}

var s state

func TestMain(m *testing.M) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
x.Panic(testutil.AssignUids(200))
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
c, err := dgraphtest.NewLocalCluster(conf)

x.Panic(err)
x.Panic(c.Start())
dg, cleanup, err := c.Client()
x.Panic(err)
defer cleanup()

x.Panic(dg.Login(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
x.Panic(c.AssignUids(dg.Dgraph, 200))
s.dg = dg
_ = m.Run()
}
Expand Down Expand Up @@ -724,25 +733,23 @@ query countAnswers($num: int) {
)

func TestCountIndexConcurrentTxns(t *testing.T) {
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
require.NoError(t, err)
testutil.DropAll(t, dg)
alterSchema(t, dg, "answer: [uid] @count .")
require.NoError(t, s.dg.DropAll())
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))

// Expected edge count of 0x100: 1
txn0 := dg.NewTxn()
txn0 := s.dg.NewTxn()
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
_, err = txn0.Mutate(ctxb, &mu)
_, err := txn0.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn0.Commit(ctxb))

// The following two mutations are in separate interleaved txns.
txn1 := dg.NewTxn()
txn1 := s.dg.NewTxn()
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x2> .")}
_, err = txn1.Mutate(ctxb, &mu)
require.NoError(t, err)

txn2 := dg.NewTxn()
txn2 := s.dg.NewTxn()
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x3> .")}
_, err = txn2.Mutate(ctxb, &mu)
require.NoError(t, err)
Expand All @@ -752,21 +759,21 @@ func TestCountIndexConcurrentTxns(t *testing.T) {
"the txn2 should be aborted due to concurrent update on the count index of <0x01>")

// retry the mutation
txn3 := dg.NewTxn()
txn3 := s.dg.NewTxn()
_, err = txn3.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn3.Commit(ctxb))

// Verify count queries
txn := dg.NewReadOnlyTxn()
txn := s.dg.NewReadOnlyTxn()
vars := map[string]string{"$num": "1"}
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
js := string(resp.GetJson())
require.JSONEq(t,
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
js)
txn = dg.NewReadOnlyTxn()
txn = s.dg.NewReadOnlyTxn()
vars = map[string]string{"$num": "2"}
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
Expand All @@ -777,43 +784,41 @@ func TestCountIndexConcurrentTxns(t *testing.T) {
}

func TestCountIndexSerialTxns(t *testing.T) {
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
require.NoError(t, err)
testutil.DropAll(t, dg)
alterSchema(t, dg, "answer: [uid] @count .")
require.NoError(t, s.dg.DropAll())
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))

// Expected Edge count of 0x100: 1
txn0 := dg.NewTxn()
txn0 := s.dg.NewTxn()
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
_, err = txn0.Mutate(ctxb, &mu)
_, err := txn0.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn0.Commit(ctxb))

// Expected edge count of 0x1: 2
// This should NOT appear in the query result
// The following two mutations are in serial txns.
txn1 := dg.NewTxn()
txn1 := s.dg.NewTxn()
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x2> .")}
_, err = txn1.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn1.Commit(ctxb))

txn2 := dg.NewTxn()
txn2 := s.dg.NewTxn()
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x3> .")}
_, err = txn2.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn2.Commit(ctxb))

// Verify query
txn := dg.NewReadOnlyTxn()
txn := s.dg.NewReadOnlyTxn()
vars := map[string]string{"$num": "1"}
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
js := string(resp.GetJson())
require.JSONEq(t,
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
js)
txn = dg.NewReadOnlyTxn()
txn = s.dg.NewReadOnlyTxn()
vars = map[string]string{"$num": "2"}
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
Expand All @@ -824,22 +829,20 @@ func TestCountIndexSerialTxns(t *testing.T) {
}

func TestCountIndexSameTxn(t *testing.T) {
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
require.NoError(t, err)
testutil.DropAll(t, dg)
alterSchema(t, dg, "answer: [uid] @count .")
require.NoError(t, s.dg.DropAll())
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))

// Expected Edge count of 0x100: 1
txn0 := dg.NewTxn()
txn0 := s.dg.NewTxn()
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
_, err = txn0.Mutate(ctxb, &mu)
_, err := txn0.Mutate(ctxb, &mu)
require.NoError(t, err)
require.NoError(t, txn0.Commit(ctxb))

// Expected edge count of 0x1: 2
// This should NOT appear in the query result
// The following two mutations are in the same txn.
txn1 := dg.NewTxn()
txn1 := s.dg.NewTxn()
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x2> .")}
_, err = txn1.Mutate(ctxb, &mu)
require.NoError(t, err)
Expand All @@ -849,15 +852,15 @@ func TestCountIndexSameTxn(t *testing.T) {
require.NoError(t, txn1.Commit(ctxb))

// Verify query
txn := dg.NewReadOnlyTxn()
txn := s.dg.NewReadOnlyTxn()
vars := map[string]string{"$num": "1"}
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
js := string(resp.GetJson())
require.JSONEq(t,
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
js)
txn = dg.NewReadOnlyTxn()
txn = s.dg.NewReadOnlyTxn()
vars = map[string]string{"$num": "2"}
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
require.NoError(t, err)
Expand All @@ -868,9 +871,8 @@ func TestCountIndexSameTxn(t *testing.T) {
}

func TestConcurrentQueryMutate(t *testing.T) {
testutil.DropAll(t, s.dg)
alterSchema(t, s.dg, "name: string .")

require.NoError(t, s.dg.DropAll())
require.NoError(t, s.dg.SetupSchema("name: string ."))
txn := s.dg.NewTxn()
defer func() { require.NoError(t, txn.Discard(context.Background())) }()

Expand Down Expand Up @@ -904,8 +906,8 @@ func TestConcurrentQueryMutate(t *testing.T) {
}

func TestTxnDiscardBeforeCommit(t *testing.T) {
testutil.DropAll(t, s.dg)
alterSchema(t, s.dg, "name: string .")
require.NoError(t, s.dg.DropAll())
require.NoError(t, s.dg.SetupSchema("name: string ."))

txn := s.dg.NewTxn()
mu := &api.Mutation{
Expand Down
16 changes: 8 additions & 8 deletions dgraph/cmd/alpha/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/protos/pb"
"github.com/dgraph-io/dgraph/v24/query"
"github.com/dgraph-io/dgraph/v24/testutil"
"github.com/dgraph-io/dgraph/v24/x"
)

Expand Down Expand Up @@ -846,13 +846,13 @@ func setDrainingMode(t *testing.T, enable bool, accessJwt string) {
}
}
}`
params := &testutil.GraphQLParams{
params := dgraphapi.GraphQLParams{
Query: drainingRequest,
Variables: map[string]interface{}{"enable": enable},
}
resp := testutil.MakeGQLRequestWithAccessJwt(t, params, accessJwt)
resp.RequireNoGraphQLErrors(t)
require.JSONEq(t, `{"draining":{"response":{"code":"Success"}}}`, string(resp.Data))
resp, err := hc.RunGraphqlQuery(params, true)
require.NoError(t, err)
require.JSONEq(t, `{"draining":{"response":{"code":"Success"}}}`, string(resp))
}

func TestDrainingMode(t *testing.T) {
Expand Down Expand Up @@ -894,12 +894,12 @@ func TestDrainingMode(t *testing.T) {

}

token := testutil.GrootHttpLogin(addr + "/admin")
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, 0))

setDrainingMode(t, true, token.AccessJwt)
setDrainingMode(t, true, hc.AccessJwt)
runRequests(true)

setDrainingMode(t, false, token.AccessJwt)
setDrainingMode(t, false, hc.AccessJwt)
runRequests(false)
}

Expand Down
Loading

0 comments on commit 884dab6

Please sign in to comment.