Пример #1
0
        private async Task AlterAPerson(IDgraphClient client)
        {
            using (var transaction = client.NewTransaction()) {
                Person3.Friends.Add(Person2);

                // This will serialize the whole object.  You might not want to
                // do that, and maybe only add in the bits that have changed
                // instead.
                var json = JsonConvert.SerializeObject(Person3);

                var result = await transaction.Mutate(json);

                AssertResultIsSuccess(result, "Mutation failed");

                // no nodes were allocated
                result.Value.Count.Should().Be(0);

                await transaction.Commit();
            }

            var queryPerson = await client.Query(FriendQueries.QueryByUid(Person3.Uid));

            AssertResultIsSuccess(queryPerson, "Query failed");

            FriendQueries.AssertStringIsPerson(queryPerson.Value, Person3);
        }
Пример #2
0
        private async Task DiscardedTransactionsHaveNoEffect(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();

            var person            = MintAPerson(nameof(DiscardedTransactionsHaveNoEffect));
            var json              = JsonConvert.SerializeObject(person);
            var transactionResult = await txn1.Mutate(json);

            AssertResultIsSuccess(transactionResult);
            person.Uid = transactionResult.Value[person.Uid.Substring(2)];

            await txn1.Discard();

            // Can we see it
            var queryByName = await client.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", person.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Should().Be("{\"q\":[]}");

            // Can we see it in the uid (note that uid queries always return the
            // uid ... we are interested if returns the actual person or not)
            var queryByUid = await client.Query(FriendQueries.QueryByUid(person.Uid));

            AssertResultIsSuccess(queryByUid);
            queryByUid.Value.Should().Be($"{{\"q\":[{{\"uid\":\"{person.Uid}\"}}]}}");
        }
Пример #3
0
        private async Task QueryAllThreePeople(IDgraphClient client)
        {
            var people = new List <Person> {
                Person1, Person2, Person3
            };

            foreach (var person in people)
            {
                var queryPerson = await client.Query(FriendQueries.QueryByUid(person.Uid));

                AssertResultIsSuccess(queryPerson, "Query failed");

                // the query result is json like { q: [ ...Person... ] }
                FriendQueries.AssertStringIsPerson(queryPerson.Value, person);
            }
        }
Пример #4
0
        private async Task NoDirtyReads(IDgraphClient client)
        {
            var txn1   = client.NewTransaction();
            var person = MintAPerson(nameof(NoDirtyReads));
            var json   = JsonConvert.SerializeObject(person);

            var transactionResult = await txn1.Mutate(new RequestBuilder().
                                                      WithMutations(new MutationBuilder {
                SetJson = json
            }));

            AssertResultIsSuccess(transactionResult, "Mutation failed");

            person.Uid = transactionResult.Value.Uids[person.Uid.Substring(2)];

            // Can we see this from another transaction?
            var queryByName = await client.NewReadOnlyTransaction().QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", person.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Json.Should().Be("{\"q\":[]}");

            // Can we see it in the uid (note that uid queries always return the
            // uid ... we are interested if returns the actual person or not)
            var queryByUid = await client.NewReadOnlyTransaction().
                             Query(FriendQueries.QueryByUid(person.Uid));

            AssertResultIsSuccess(queryByUid);
            queryByUid.Value.Json.Should().Be($"{{\"q\":[{{\"uid\":\"{person.Uid}\"}}]}}");

            AssertResultIsSuccess(await txn1.Commit());

            queryByName = await client.NewReadOnlyTransaction().QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", person.Name }
            });

            AssertResultIsSuccess(queryByName);

            FriendQueries.AssertStringIsPerson(queryByName.Value.Json, person);
        }
Пример #5
0
        private async Task DeleteAPerson(IDgraphClient client)
        {
            using (var transaction = client.NewTransaction()) {
                // delete a node by passing JSON like this to delete
                var deleteResult = await transaction.Mutate(
                    deleteJson : $"{{\"uid\": \"{Person1.Uid}\"}}");

                AssertResultIsSuccess(deleteResult, "Delete failed");

                var transactionResult = await transaction.Commit();

                AssertResultIsSuccess(transactionResult);
            }

            // that person should be gone...
            var queryPerson1 = await client.NewReadOnlyTransaction().
                               Query(FriendQueries.QueryByUid(Person1.Uid));

            AssertResultIsSuccess(queryPerson1, "Query failed");

            // no matter what uid you query for, Dgraph always succeeds :-(
            // e.g. on a fresh dgraph with no uids allocated
            // { q(func: uid(0x44444444)) { uid }}
            // will answer
            // "q": [ { "uid": "0x44444444" } ]
            //
            // so the only way to test that the node is deleted,
            // is to test that we got only that back

            queryPerson1.Value.Json.Should().Be($"{{\"q\":[{{\"uid\":\"{Person1.Uid}\"}}]}}");

            // ... but watch out, Dgraph can leave dangling references
            // e.g. there are some edges in our graph that still point to
            // Person 1 - we've just removed all it's outgoing edges.
            var queryPerson3 = await client.NewReadOnlyTransaction().
                               Query(FriendQueries.QueryByUid(Person3.Uid));

            AssertResultIsSuccess(queryPerson3, "Query failed");
            var person3 = JObject.Parse(queryPerson3.Value.Json)["q"][0].ToObject <Person>();

            person3.Friends.Count.Should().Be(2);
        }
Пример #6
0
        private async Task DeleteAPerson(IDgraphClient client)
        {
            using (var transaction = client.NewTransaction()) {
                // delete a node by passing JSON like this to delete
                var deleteResult = await transaction.Delete($"{{\"uid\": \"{Person1.Uid}\"}}");

                AssertResultIsSuccess(deleteResult, "Delete failed");

                await transaction.Commit();
            }

            // that person should be gone...
            var queryPerson1 = await client.Query(FriendQueries.QueryByUid(Person1.Uid));

            AssertResultIsSuccess(queryPerson1, "Query failed");

            // no matter what uid you query for, Dgraph always succeeds :-(
            // e.g. on a fresh dgraph with no uids allocated
            // { q(func: uid(0x44444444)) { uid }}
            // will answer
            // "q": [ { "uid": "0x44444444" } ]
            //
            // so the only way to test that the node is deleted,
            // is to test that we got only that back

            queryPerson1.Value.Should().Be($"{{\"q\":[{{\"uid\":\"{Person1.Uid}\"}}]}}");

            // -----------------------------------------------------------
            // ... but watch out, Dgraph can leave dangling references :-(
            // -----------------------------------------------------------
            var queryPerson3 = await client.Query(FriendQueries.QueryByUid(Person3.Uid));

            AssertResultIsSuccess(queryPerson3, "Query failed");
            var person3 = JObject.Parse(queryPerson3.Value) ["q"][0].ToObject <Person>();

            person3.Friends.Count.Should().Be(2);

            // You'll need something like GraphSchema to handle things like
            // cascading deletes etc. and clean up automatically :-)
        }