예제 #1
0
        async Task SetUpAsync()
        {
            Func <string, string, string> Env = (name, @default) =>
                                                Environment.GetEnvironmentVariable(name) ?? @default;

            var cfg = await Config.GetConfig();

            var domain   = Env("FAUNA_DOMAIN", cfg.Domain);
            var scheme   = Env("FAUNA_SCHEME", cfg.Scheme);
            var port     = Env("FAUNA_PORT", cfg.Port);
            var secret   = Env("FAUNA_ROOT_KEY", cfg.Secret);
            var endpoint = $"{scheme}://{domain}:{port}";

            rootClient = new FaunaClient(secret: secret, endpoint: endpoint);

            const string dbName = "faunadb-csharp-test";

            DbRef = new DatabaseV(dbName);

            try {
                await rootClient.Query(Delete(DbRef));
            } catch (BadRequest) {}

            await rootClient.Query(CreateDatabase(Obj("name", dbName)));

            clientKey = await rootClient.Query(CreateKey(Obj("database", DbRef, "role", "server")));

            client = rootClient.NewSessionClient(clientKey.Get(SECRET_FIELD));
        }
예제 #2
0
        [Test] public async Task TestHandleConstraintViolations()
        {
            RefV classRef = await RandomClass();

            await client.Query(
                CreateIndex(Obj(
                                "name", RandomStartingWith("class_index_"),
                                "source", classRef,
                                "terms", Arr(Obj("field", Arr("data", "uniqueField"))),
                                "unique", true)));

            AsyncTestDelegate create = async() =>
            {
                await client.Query(
                    Create(classRef,
                           Obj("data", Obj("uniqueField", "same value"))));
            };

            Assert.DoesNotThrowAsync(create);

            var ex = Assert.ThrowsAsync <BadRequest>(create);

            Assert.AreEqual("instance not unique: Instance is not unique.", ex.Message);

            AssertErrors(ex, code: "instance not unique", description: "Instance is not unique.");

            AssertPosition(ex, positions: Is.Empty);
        }
예제 #3
0
        [Test] public async Task TestNestedRef()
        {
            var client1 = await CreateNewDatabase(adminClient, "parent-database");

            await CreateNewDatabase(client1, "child-database");

            var key = await client1.Query(CreateKey(Obj("database", Database("child-database"), "role", "server")));

            var client2 = client1.NewSessionClient(secret: key.Get(SECRET_FIELD));

            await client2.Query(CreateClass(Obj("name", "a_class")));

            var nestedClassRef = new ClassV(
                id: "a_class",
                database: new DatabaseV(
                    id: "child-database",
                    database: new DatabaseV("parent-database")));

            var client3 = client2.NewSessionClient(secret: clientKey.Get(SECRET_FIELD));

            Assert.AreEqual(BooleanV.True, await client3.Query(Exists(nestedClassRef)));

            var _ref = new RefV(
                id: "classes",
                database: new DatabaseV("child-database", new DatabaseV("parent-database")));

            var ret = await client3.Query(Paginate(_ref));

            Assert.That(ret.Get(REF_LIST),
                        Is.EquivalentTo(new List <RefV> {
                nestedClassRef
            }));
        }
예제 #4
0
        [Test] public async Task TestEvalDoExpression()
        {
            RefV @ref = await RandomClass();

            Value res = await client.Query(
                Do(Create(@ref, Obj("data", Obj("name", "Magic Missile"))),
                   Get(@ref))
                );

            Assert.AreEqual(@ref, res.Get(REF_FIELD));
        }
예제 #5
0
        async Task SetUpAsync()
        {
            adminKey = await rootClient.Query(CreateKey(Obj("database", DbRef, "role", "admin")));

            adminClient = rootClient.NewSessionClient(adminKey.Get(SECRET_FIELD));

            await client.Query(CreateClass(Obj("name", "spells")));

            await client.Query(CreateClass(Obj("name", "characters")));

            await client.Query(CreateClass(Obj("name", "spellbooks")));

            await client.Query(CreateIndex(Obj(
                                               "name", "all_spells",
                                               "source", Class("spells")
                                               )));

            await client.Query(CreateIndex(Obj(
                                               "name", "spells_by_element",
                                               "source", Class("spells"),
                                               "terms", Arr(Obj("field", Arr("data", "element")))
                                               )));

            await client.Query(CreateIndex(Obj(
                                               "name", "elements_of_spells",
                                               "source", Class("spells"),
                                               "values", Arr(Obj("field", Arr("data", "element")))
                                               )));

            await client.Query(CreateIndex(Obj(
                                               "name", "spellbooks_by_owner",
                                               "source", Class("spellbooks"),
                                               "terms", Arr(Obj("field", Arr("data", "owner")))
                                               )));

            await client.Query(CreateIndex(Obj(
                                               "name", "spells_by_spellbook",
                                               "source", Class("spells"),
                                               "terms", Arr(Obj("field", Arr("data", "spellbook")))
                                               )));

            magicMissile = GetRef(await client.Query(
                                      Create(Class("spells"),
                                             Obj("data",
                                                 Obj(
                                                     "name", "Magic Missile",
                                                     "element", "arcane",
                                                     "cost", 10)))
                                      ));

            fireball = GetRef(await client.Query(
                                  Create(Class("spells"),
                                         Obj("data",
                                             Obj(
                                                 "name", "Fireball",
                                                 "element", "fire",
                                                 "cost", 10)))
                                  ));

            faerieFire = GetRef(await client.Query(
                                    Create(Class("spells"),
                                           Obj("data",
                                               Obj(
                                                   "name", "Faerie Fire",
                                                   "cost", 10,
                                                   "element", Arr(
                                                       "arcane",
                                                       "nature"
                                                       ))))
                                    ));

            summon = GetRef(await client.Query(
                                Create(Class("spells"),
                                       Obj("data",
                                           Obj(
                                               "name", "Summon Animal Companion",
                                               "element", "nature",
                                               "cost", 10)))
                                ));

            thor = GetRef(await client.Query(
                              Create(Class("characters"),
                                     Obj("data", Obj("name", "Thor")))
                              ));

            var thorsSpellbook = GetRef(await client.Query(
                                            Create(Class("spellbooks"),
                                                   Obj("data",
                                                       Obj("owner", thor)))
                                            ));

            thorSpell1 = GetRef(await client.Query(
                                    Create(Class("spells"),
                                           Obj("data",
                                               Obj("spellbook", thorsSpellbook)))
                                    ));

            thorSpell2 = GetRef(await client.Query(
                                    Create(Class("spells"),
                                           Obj("data",
                                               Obj("spellbook", thorsSpellbook)))
                                    ));
        }