MCPcopy
hub / github.com/dgraph-io/dgraph / TestConditionalUpsertExample0

Function TestConditionalUpsertExample0

dgraph/cmd/alpha/upsert_test.go:913–987  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

911}
912
913func TestConditionalUpsertExample0(t *testing.T) {
914 require.NoError(t, dropAll())
915 require.NoError(t, alterSchema(`email: string @index(exact) .`))
916
917 // Mutation with wrong name
918 m1 := `
919upsert {
920 query {
921 q(func: eq(email, "email@company.io")) {
922 v as uid
923 }
924 }
925
926 mutation @if(eq(len(v), 0)) {
927 set {
928 uid(v) <name> "Wrong" .
929 uid(v) <email> "email@company.io" .
930 }
931 }
932}`
933 mr, err := mutationWithTs(mutationInp{body: m1, typ: "application/rdf", commitNow: true})
934 require.NoError(t, err)
935 require.True(t, len(mr.keys) == 0)
936 require.Equal(t, []string{"email", "name"}, splitPreds(mr.preds))
937 result := QueryResult{}
938 require.NoError(t, json.Unmarshal(mr.data, &result))
939 require.Equal(t, 0, len(result.Queries["q"]))
940
941 // Trying again, should be a NOOP
942 mr, err = mutationWithTs(mutationInp{body: m1, typ: "application/rdf", commitNow: true})
943 require.NoError(t, err)
944 result = QueryResult{}
945 require.NoError(t, json.Unmarshal(mr.data, &result))
946
947 // query should return the wrong name
948 q1 := `
949{
950 q(func: has(email)) {
951 uid
952 name
953 email
954 }
955}`
956 res, _, err := queryWithTs(queryInp{body: q1, typ: "application/dql"})
957 require.NoError(t, err)
958 require.Contains(t, res, "Wrong")
959
960 // mutation with correct name
961 m2 := `
962upsert {
963 query {
964 q(func: eq(email, "email@company.io")) {
965 v as uid
966 }
967 }
968
969 mutation @if(eq(len(v), 1)) {
970 set {

Callers

nothing calls this directly

Calls 5

dropAllFunction · 0.85
alterSchemaFunction · 0.85
mutationWithTsFunction · 0.85
splitPredsFunction · 0.85
queryWithTsFunction · 0.85

Tested by

no test coverage detected