Example #1
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(new RequestBuilder().
                                                      WithMutations(new MutationBuilder {
                SetJson = json
            }));

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

            await txn1.Discard();

            // Can we see it
            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}\"}}]}}");
        }
Example #2
0
        private async Task UnrelatedTransactionsDoNotConflict(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();
            var txn2 = client.NewTransaction();

            var personTxn1 = MintAPerson("Alfred Name");
            var personTxn2 = MintAPerson("Fank Person");

            // Name has term and exact indexes, so these shouldn't clash
            var transactionResultTxn1 = await txn1.Mutate(new RequestBuilder().
                                                          WithMutations(new MutationBuilder {
                SetJson = JsonConvert.SerializeObject(personTxn1)
            }));

            AssertResultIsSuccess(transactionResultTxn1);
            personTxn1.Uid = transactionResultTxn1.Value.Uids[personTxn1.Uid.Substring(2)];

            var transactionResultTxn2 = await txn2.Mutate(new RequestBuilder().
                                                          WithMutations(new MutationBuilder {
                SetJson = JsonConvert.SerializeObject(personTxn2)
            }));

            AssertResultIsSuccess(transactionResultTxn2);
            personTxn2.Uid = transactionResultTxn2.Value.Uids[personTxn2.Uid.Substring(2)];

            // Can't see the other result
            var queryByName = await txn2.QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn1.Name }
            });

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

            // Cause the mutates can't clash these should be ok
            AssertResultIsSuccess(await txn1.Commit());
            AssertResultIsSuccess(await txn2.Commit());

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

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value.Json, personTxn1);

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

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value.Json, personTxn2);
        }
Example #3
0
        private async Task ConflictingTransactionsDontBothSucceed(IDgraphClient client)
        {
            var txn1 = client.NewTransaction();
            var txn2 = client.NewTransaction();

            var personTxn1 = MintAPerson("Bill Person");
            var personTxn2 = MintAPerson("Jane Person");

            var transactionResultTxn1 = await txn1.Mutate(new RequestBuilder().
                                                          WithMutations(new MutationBuilder {
                SetJson = JsonConvert.SerializeObject(personTxn1)
            }));

            AssertResultIsSuccess(transactionResultTxn1);
            personTxn1.Uid = transactionResultTxn1.Value.Uids[personTxn1.Uid.Substring(2)];

            var transactionResultTxn2 = await txn2.Mutate(new RequestBuilder().
                                                          WithMutations(new MutationBuilder {
                SetJson = JsonConvert.SerializeObject(personTxn2)
            }));

            AssertResultIsSuccess(transactionResultTxn2);

            // Name has term and exact indexes, so these clash on 'Person' term
            AssertResultIsSuccess(await txn1.Commit());
            var txn2Result = await txn2.Commit();

            txn2Result.IsFailed.Should().BeTrue();
            // Not 100% sure about this quirk.  Pretty sure this is how is how
            // dgo does it, but I wonder that it should be in an error state?
            txn2.TransactionState.Should().Be(TransactionState.Committed);

            // txn1 is there
            var queryByName = await client.NewReadOnlyTransaction().QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn1.Name }
            });

            AssertResultIsSuccess(queryByName);
            FriendQueries.AssertStringIsPerson(queryByName.Value.Json, personTxn1);

            // txn2 had no effect
            queryByName = await client.NewReadOnlyTransaction().QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", personTxn2.Name }
            });

            AssertResultIsSuccess(queryByName);
            queryByName.Value.Json.Should().Be("{\"q\":[]}");
        }
Example #4
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(setJson : json);

                AssertResultIsSuccess(result, "Mutation failed");

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

                var transactionResult = await transaction.Commit();

                AssertResultIsSuccess(transactionResult);
            }

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

            AssertResultIsSuccess(queryPerson, "Query failed");

            FriendQueries.AssertStringIsPerson(queryPerson.Value.Json, Person3);
        }
Example #5
0
        private async Task ErrorsResultInFailedQuery(IDgraphClient client)
        {
            // maformed
            var q1result = await client.NewReadOnlyTransaction().Query(
                "schema(pred: [name, friends, dob, scores]) { type ");

            q1result.IsSuccess.Should().BeFalse();
        }
Example #6
0
        private async Task InitialSchemaIsAsExpected(IDgraphClient client)
        {
            var response = await client.NewReadOnlyTransaction().Query("schema {}");

            AssertResultIsSuccess(response);

            var schema = JsonConvert.DeserializeObject <DgraphSchema>(response.Value.Json);

            this.Assent(schema.ToString(), AssentConfiguration);
        }
Example #7
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);
        }
Example #8
0
        private async Task SchemaQueryWithRestrictions(IDgraphClient client)
        {
            var response = await client.NewReadOnlyTransaction().Query(
                "schema(pred: [name, friends, dob, scores]) { type }");

            AssertResultIsSuccess(response);

            var schema = JsonConvert.DeserializeObject <DgraphSchema>(response.Value.Json);

            this.Assent(schema.ToString(), AssentConfiguration);
        }
Example #9
0
        private async Task QueryWithVars(IDgraphClient client)
        {
            var queryPerson = await client.NewReadOnlyTransaction().QueryWithVars(
                FriendQueries.QueryByName,
                new Dictionary <string, string> {
                { "$name", Person3.Name }
            });

            AssertResultIsSuccess(queryPerson, "Query failed");

            FriendQueries.AssertStringIsPerson(queryPerson.Value.Json, Person3);
        }
Example #10
0
        private async Task QueryAllThreePeople(IDgraphClient client)
        {
            var people = new List <Person> {
                Person1, Person2, Person3
            };

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

                AssertResultIsSuccess(queryPerson, "Query failed");

                // the query result is json like { q: [ ...Person... ] }
                FriendQueries.AssertStringIsPerson(queryPerson.Value.Json, person);
            }
        }
Example #11
0
        private async Task AlterSchemaAgainAsExpected(IDgraphClient client)
        {
            var alterSchemaResult = await client.Alter(
                new Operation { Schema = ReadEmbeddedFile("altered.schema") });

            AssertResultIsSuccess(alterSchemaResult);

            Thread.Sleep(TimeSpan.FromSeconds(5));

            var response = await client.NewReadOnlyTransaction().Query("schema {}");

            AssertResultIsSuccess(response);

            var schema = JsonConvert.DeserializeObject <DgraphSchema>(response.Value.Json);

            this.Assent(schema.ToString(), AssentConfiguration);
        }
Example #12
0
        private async Task AlterSchemAsExpected(IDgraphClient client)
        {
            var alterSchemaResult = await client.Alter(
                new Operation { Schema = ReadEmbeddedFile("test.schema") });

            AssertResultIsSuccess(alterSchemaResult);

            // After an Alter, Dgraph computes indexes in the background.
            // So a first schema query after Alter might return a schema
            // without indexes.  We could poll and backoff and show that
            // ... but we aren't testing that here, just that the schema
            // updates.
            Thread.Sleep(TimeSpan.FromSeconds(5));

            var response = await client.NewReadOnlyTransaction().Query("schema {}");

            AssertResultIsSuccess(response);

            var schema = JsonConvert.DeserializeObject <DgraphSchema>(response.Value.Json);

            this.Assent(schema.ToString(), AssentConfiguration);
        }