Skip to content

Commit 33f7c42

Browse files
Refactor test code (#9250)
1 parent ac15f6d commit 33f7c42

File tree

7 files changed

+293
-96
lines changed

7 files changed

+293
-96
lines changed

chunker/json_parser_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@ import (
1414
"encoding/json"
1515
"fmt"
1616
"math"
17+
"os"
1718
"testing"
1819
"time"
1920

2021
"github.com/golang/glog"
2122
"github.com/stretchr/testify/require"
2223

2324
"github.com/dgraph-io/dgo/v240/protos/api"
25+
"github.com/hypermodeinc/dgraph/v24/dgraphapi"
26+
"github.com/hypermodeinc/dgraph/v24/dgraphtest"
2427
"github.com/hypermodeinc/dgraph/v24/testutil"
2528
"github.com/hypermodeinc/dgraph/v24/tok"
2629
"github.com/hypermodeinc/dgraph/v24/types"
30+
"github.com/hypermodeinc/dgraph/v24/x"
31+
)
32+
33+
var (
34+
dg *dgraphapi.GrpcClient
2735
)
2836

2937
func makeNquad(sub, pred string, val *api.Value) *api.NQuad {
@@ -85,17 +93,12 @@ func FastParse(b []byte, op int) ([]*api.NQuad, error) {
8593

8694
func (exp *Experiment) verify() {
8795
// insert the data into dgraph
88-
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
89-
if err != nil {
90-
exp.t.Fatalf("Error while getting a dgraph client: %v", err)
91-
}
92-
9396
ctx := context.Background()
97+
require.NoError(exp.t, dg.Login(ctx, dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
9498
require.NoError(exp.t, dg.Alter(ctx, &api.Operation{DropAll: true}), "drop all failed")
9599
require.NoError(exp.t, dg.Alter(ctx, &api.Operation{Schema: exp.schema}),
96100
"schema change failed")
97-
98-
_, err = dg.NewTxn().Mutate(ctx,
101+
_, err := dg.NewTxn().Mutate(ctx,
99102
&api.Mutation{Set: exp.nqs, CommitNow: true})
100103
require.NoError(exp.t, err, "mutation failed")
101104

@@ -1501,3 +1504,22 @@ func TestNquadsJsonValidVector(t *testing.T) {
15011504
exp.nqs = fastNQ
15021505
exp.verify()
15031506
}
1507+
1508+
func TestMain(m *testing.M) {
1509+
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
1510+
c, err := dgraphtest.NewLocalCluster(conf)
1511+
1512+
x.Panic(err)
1513+
x.Panic(c.Start())
1514+
var cleanup func()
1515+
dg, cleanup, err = c.Client()
1516+
x.Panic(err)
1517+
defer cleanup()
1518+
code := m.Run()
1519+
if code != 0 {
1520+
c.Cleanup(true)
1521+
} else {
1522+
c.Cleanup(false)
1523+
}
1524+
os.Exit(code)
1525+
}

contrib/integration/testtxn/main_test.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,30 @@ import (
2424

2525
"github.com/dgraph-io/dgo/v240"
2626
"github.com/dgraph-io/dgo/v240/protos/api"
27-
"github.com/hypermodeinc/dgraph/v24/testutil"
27+
"github.com/hypermodeinc/dgraph/v24/dgraphapi"
28+
"github.com/hypermodeinc/dgraph/v24/dgraphtest"
2829
"github.com/hypermodeinc/dgraph/v24/x"
2930
)
3031

3132
type state struct {
32-
dg *dgo.Dgraph
33+
dg *dgraphapi.GrpcClient
3334
}
3435

3536
var s state
3637

3738
func TestMain(m *testing.M) {
3839
log.SetFlags(log.LstdFlags | log.Lshortfile)
39-
x.Panic(testutil.AssignUids(200))
40-
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
40+
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
41+
c, err := dgraphtest.NewLocalCluster(conf)
42+
43+
x.Panic(err)
44+
x.Panic(c.Start())
45+
dg, cleanup, err := c.Client()
4146
x.Panic(err)
47+
defer cleanup()
48+
49+
x.Panic(dg.Login(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword))
50+
x.Panic(c.AssignUids(dg.Dgraph, 200))
4251
s.dg = dg
4352
_ = m.Run()
4453
}
@@ -713,25 +722,23 @@ query countAnswers($num: int) {
713722
)
714723

715724
func TestCountIndexConcurrentTxns(t *testing.T) {
716-
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
717-
require.NoError(t, err)
718-
testutil.DropAll(t, dg)
719-
alterSchema(t, dg, "answer: [uid] @count .")
725+
require.NoError(t, s.dg.DropAll())
726+
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))
720727

721728
// Expected edge count of 0x100: 1
722-
txn0 := dg.NewTxn()
729+
txn0 := s.dg.NewTxn()
723730
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
724-
_, err = txn0.Mutate(ctxb, &mu)
731+
_, err := txn0.Mutate(ctxb, &mu)
725732
require.NoError(t, err)
726733
require.NoError(t, txn0.Commit(ctxb))
727734

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

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

743750
// retry the mutation
744-
txn3 := dg.NewTxn()
751+
txn3 := s.dg.NewTxn()
745752
_, err = txn3.Mutate(ctxb, &mu)
746753
require.NoError(t, err)
747754
require.NoError(t, txn3.Commit(ctxb))
748755

749756
// Verify count queries
750-
txn := dg.NewReadOnlyTxn()
757+
txn := s.dg.NewReadOnlyTxn()
751758
vars := map[string]string{"$num": "1"}
752759
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
753760
require.NoError(t, err)
754761
js := string(resp.GetJson())
755762
require.JSONEq(t,
756763
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
757764
js)
758-
txn = dg.NewReadOnlyTxn()
765+
txn = s.dg.NewReadOnlyTxn()
759766
vars = map[string]string{"$num": "2"}
760767
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
761768
require.NoError(t, err)
@@ -766,43 +773,41 @@ func TestCountIndexConcurrentTxns(t *testing.T) {
766773
}
767774

768775
func TestCountIndexSerialTxns(t *testing.T) {
769-
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
770-
require.NoError(t, err)
771-
testutil.DropAll(t, dg)
772-
alterSchema(t, dg, "answer: [uid] @count .")
776+
require.NoError(t, s.dg.DropAll())
777+
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))
773778

774779
// Expected Edge count of 0x100: 1
775-
txn0 := dg.NewTxn()
780+
txn0 := s.dg.NewTxn()
776781
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
777-
_, err = txn0.Mutate(ctxb, &mu)
782+
_, err := txn0.Mutate(ctxb, &mu)
778783
require.NoError(t, err)
779784
require.NoError(t, txn0.Commit(ctxb))
780785

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

790-
txn2 := dg.NewTxn()
795+
txn2 := s.dg.NewTxn()
791796
mu = api.Mutation{SetNquads: []byte("<0x1> <answer> <0x3> .")}
792797
_, err = txn2.Mutate(ctxb, &mu)
793798
require.NoError(t, err)
794799
require.NoError(t, txn2.Commit(ctxb))
795800

796801
// Verify query
797-
txn := dg.NewReadOnlyTxn()
802+
txn := s.dg.NewReadOnlyTxn()
798803
vars := map[string]string{"$num": "1"}
799804
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
800805
require.NoError(t, err)
801806
js := string(resp.GetJson())
802807
require.JSONEq(t,
803808
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
804809
js)
805-
txn = dg.NewReadOnlyTxn()
810+
txn = s.dg.NewReadOnlyTxn()
806811
vars = map[string]string{"$num": "2"}
807812
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
808813
require.NoError(t, err)
@@ -813,22 +818,20 @@ func TestCountIndexSerialTxns(t *testing.T) {
813818
}
814819

815820
func TestCountIndexSameTxn(t *testing.T) {
816-
dg, err := testutil.DgraphClientWithGroot(testutil.SockAddr)
817-
require.NoError(t, err)
818-
testutil.DropAll(t, dg)
819-
alterSchema(t, dg, "answer: [uid] @count .")
821+
require.NoError(t, s.dg.DropAll())
822+
require.NoError(t, s.dg.SetupSchema("answer: [uid] @count ."))
820823

821824
// Expected Edge count of 0x100: 1
822-
txn0 := dg.NewTxn()
825+
txn0 := s.dg.NewTxn()
823826
mu := api.Mutation{SetNquads: []byte("<0x100> <answer> <0x200> .")}
824-
_, err = txn0.Mutate(ctxb, &mu)
827+
_, err := txn0.Mutate(ctxb, &mu)
825828
require.NoError(t, err)
826829
require.NoError(t, txn0.Commit(ctxb))
827830

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

840843
// Verify query
841-
txn := dg.NewReadOnlyTxn()
844+
txn := s.dg.NewReadOnlyTxn()
842845
vars := map[string]string{"$num": "1"}
843846
resp, err := txn.QueryWithVars(ctxb, countQuery, vars)
844847
require.NoError(t, err)
845848
js := string(resp.GetJson())
846849
require.JSONEq(t,
847850
`{"me": [{"count(answer)": 1, "uid": "0x100"}]}`,
848851
js)
849-
txn = dg.NewReadOnlyTxn()
852+
txn = s.dg.NewReadOnlyTxn()
850853
vars = map[string]string{"$num": "2"}
851854
resp, err = txn.QueryWithVars(ctxb, countQuery, vars)
852855
require.NoError(t, err)
@@ -857,9 +860,8 @@ func TestCountIndexSameTxn(t *testing.T) {
857860
}
858861

859862
func TestConcurrentQueryMutate(t *testing.T) {
860-
testutil.DropAll(t, s.dg)
861-
alterSchema(t, s.dg, "name: string .")
862-
863+
require.NoError(t, s.dg.DropAll())
864+
require.NoError(t, s.dg.SetupSchema("name: string ."))
863865
txn := s.dg.NewTxn()
864866
defer func() { require.NoError(t, txn.Discard(context.Background())) }()
865867

@@ -893,8 +895,8 @@ func TestConcurrentQueryMutate(t *testing.T) {
893895
}
894896

895897
func TestTxnDiscardBeforeCommit(t *testing.T) {
896-
testutil.DropAll(t, s.dg)
897-
alterSchema(t, s.dg, "name: string .")
898+
require.NoError(t, s.dg.DropAll())
899+
require.NoError(t, s.dg.SetupSchema("name: string ."))
898900

899901
txn := s.dg.NewTxn()
900902
mu := &api.Mutation{

dgraph/cmd/alpha/http_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
"github.com/pkg/errors"
2525
"github.com/stretchr/testify/require"
2626

27+
"github.com/hypermodeinc/dgraph/v24/dgraphapi"
2728
"github.com/hypermodeinc/dgraph/v24/protos/pb"
2829
"github.com/hypermodeinc/dgraph/v24/query"
29-
"github.com/hypermodeinc/dgraph/v24/testutil"
3030
"github.com/hypermodeinc/dgraph/v24/x"
3131
)
3232

@@ -835,13 +835,13 @@ func setDrainingMode(t *testing.T, enable bool, accessJwt string) {
835835
}
836836
}
837837
}`
838-
params := &testutil.GraphQLParams{
838+
params := dgraphapi.GraphQLParams{
839839
Query: drainingRequest,
840840
Variables: map[string]interface{}{"enable": enable},
841841
}
842-
resp := testutil.MakeGQLRequestWithAccessJwt(t, params, accessJwt)
843-
resp.RequireNoGraphQLErrors(t)
844-
require.JSONEq(t, `{"draining":{"response":{"code":"Success"}}}`, string(resp.Data))
842+
resp, err := hc.RunGraphqlQuery(params, true)
843+
require.NoError(t, err)
844+
require.JSONEq(t, `{"draining":{"response":{"code":"Success"}}}`, string(resp))
845845
}
846846

847847
func TestDrainingMode(t *testing.T) {
@@ -883,12 +883,12 @@ func TestDrainingMode(t *testing.T) {
883883

884884
}
885885

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

888-
setDrainingMode(t, true, token.AccessJwt)
888+
setDrainingMode(t, true, hc.AccessJwt)
889889
runRequests(true)
890890

891-
setDrainingMode(t, false, token.AccessJwt)
891+
setDrainingMode(t, false, hc.AccessJwt)
892892
runRequests(false)
893893
}
894894

0 commit comments

Comments
 (0)