public async Task TestCreateAComplexInstance() { Value instance = await client.Query( Create(await RandomClass(), Obj("data", Obj("testField", Obj( "array", Arr(1, "2", 3.4, Obj("name", "JR")), "bool", true, "num", 1234, "string", "sup", "float", 1.234) )))); Value testField = instance.Get(DATA).At("testField"); Assert.AreEqual("sup", testField.At("string").To <string>().Value); Assert.AreEqual(1234L, testField.At("num").To <long>().Value); Assert.AreEqual(true, testField.At("bool").To <bool>().Value); Assert.AreEqual(None(), testField.At("bool").To <string>().ToOption); Assert.AreEqual(None(), testField.At("credentials").To <Value>().ToOption); Assert.AreEqual(None(), testField.At("credentials", "password").To <string>().ToOption); Value array = testField.At("array"); Assert.AreEqual(4, array.To <Value[]>().Value.Length); Assert.AreEqual(1L, array.At(0).To <long>().Value); Assert.AreEqual("2", array.At(1).To <string>().Value); Assert.AreEqual(3.4, array.At(2).To <double>().Value); Assert.AreEqual("JR", array.At(3).At("name").To <string>().Value); Assert.AreEqual(None(), array.At(4).To <Value>().ToOption); }
[Test] public async Task TestEchoAnObjectBack() { Value res = await client.Query(Obj("name", "Hen Wen", "age", 123)); Assert.AreEqual("Hen Wen", res.At("name").To <string>().Value); Assert.AreEqual(123L, res.At("age").To <long>().Value); res = await client.Query(res); Assert.AreEqual("Hen Wen", res.At("name").To <string>().Value); Assert.AreEqual(123L, res.At("age").To <long>().Value); }
[Test] public async Task TestPaginateOverAnIndex() { Value page1 = await client.Query( Paginate(Match(Index("all_spells")), size : 3)); Assert.AreEqual(3, page1.Get(DATA).To <Value[]>().Value.Length); Assert.NotNull(page1.At("after")); Assert.AreEqual(None(), page1.At("before").To <Value>().ToOption); Value page2 = await client.Query( Paginate(Match(Index("all_spells")), after : page1.At("after"), size : 3)); Assert.AreEqual(3, page2.Get(DATA).To <Value[]>().Value.Length); Assert.AreNotEqual(page1.At("data"), page2.Get(DATA)); Assert.NotNull(page2.At("before")); Assert.AreEqual(None(), page2.At("after").To <Value>().ToOption); }
public async Task TestStreamEventsOnDocumentReferenceWithDocumentFieldByDefault() { Value createdInstance = await adminClient.Query( Create(await RandomCollection(), Obj("credentials", Obj("password", "abcdefg")))); var docRef = createdInstance.At("ref"); var provider = await adminClient.Stream(docRef); var done = new TaskCompletionSource <object>(); List <Value> events = new List <Value>(); var monitor = new StreamingEventMonitor( value => { events.Add(value); if (events.Count == 4) { provider.Complete(); } else { provider.RequestData(); } }, ex => { done.SetException(ex); }, () => { done.SetResult(null); } ); // subscribe to data provider monitor.Subscribe(provider); // push 3 updates await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1")))); await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2")))); await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3")))); // blocking until we receive all the events await done.Task; // clear the subscription monitor.Unsubscribe(); Value startEvent = events[0]; Assert.AreEqual("start", startEvent.At("type").To <string>().Value); Value e1 = events[1]; Assert.AreEqual("version", e1.At("type").To <string>().Value); Assert.AreEqual("testValue1", e1.At("event", "document", "data", "testField").To <string>().Value); Value e2 = events[2]; Assert.AreEqual("version", e1.At("type").To <string>().Value); Assert.AreEqual("testValue2", e2.At("event", "document", "data", "testField").To <string>().Value); Value e3 = events[3]; Assert.AreEqual("version", e1.At("type").To <String>().Value); Assert.AreEqual("testValue3", e3.At("event", "document", "data", "testField").To <string>().Value); }
public async Task TestStreamHandlesLossOfAuthorization() { await adminClient.Query( CreateCollection(Obj("name", "streamed-things-auth")) ); Value createdInstance = await adminClient.Query( Create(Collection("streamed-things-auth"), Obj("credentials", Obj("password", "abcdefg")))); var docRef = createdInstance.At("ref"); // new key + client Value newKey = await adminClient.Query(CreateKey(Obj("role", "server-readonly"))); FaunaClient streamingClient = adminClient.NewSessionClient(newKey.At("secret").To <string>().Value); var provider = await streamingClient.Stream(docRef); var done = new TaskCompletionSource <object>(); List <Value> events = new List <Value>(); var monitor = new StreamingEventMonitor( async value => { if (events.Count == 0) { try { // update doc on `start` event await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterStart")))); // delete key await adminClient.Query(Delete(newKey.At("ref").To <RefV>().Value)); // push an update to force auth revalidation. await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterKeyDelete")))); } catch (Exception ex) { done.SetException(ex); } } // capture element events.Add(value); // ask for more elements provider.RequestData(); }, ex => { done.SetException(ex); }, () => { done.SetResult(null); } ); // subscribe to data provider monitor.Subscribe(provider); // wrapping an asynchronous call AsyncTestDelegate res = async() => await done.Task; // blocking until we get an exception var exception = Assert.ThrowsAsync <StreamingException>(res); // clear the subscription monitor.Unsubscribe(); // validating exception message Assert.AreEqual("permission denied: Authorization lost during stream evaluation.", exception.Message); AssertErrors(exception, code: "permission denied", description: "Authorization lost during stream evaluation."); }
public async Task TeststreamEventsOnDocumentReferenceWithOptInFields() { Value createdInstance = await adminClient.Query( Create(await RandomCollection(), Obj("data", Obj("testField", "testValue0")))); var docRef = createdInstance.At("ref"); var fields = new List <EventField> { EventField.ActionField, EventField.DiffField, EventField.DocumentField, EventField.PrevField }; var provider = await adminClient.Stream(docRef, fields); var done = new TaskCompletionSource <object>(); List <Value> events = new List <Value>(); var monitor = new StreamingEventMonitor( value => { events.Add(value); if (events.Count == 4) { provider.Complete(); } else { provider.RequestData(); } }, ex => { done.SetException(ex); }, () => { done.SetResult(null); } ); // subscribe to data provider monitor.Subscribe(provider); // push 3 updates await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1")))); await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2")))); await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3")))); // blocking until we receive all the events await done.Task; // clear the subscription monitor.Unsubscribe(); Value startEvent = events[0]; Assert.AreEqual("start", startEvent.At("type").To <string>().Value); Value e1 = events[1]; Assert.AreEqual("version", e1.At("type").To <string>().Value); Assert.AreEqual("update", e1.At("event", "action").To <string>().Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), ((ObjectV)e1.At("event", "diff", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), ((ObjectV)e1.At("event", "document", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue0")), ((ObjectV)e1.At("event", "prev", "data")).Value); Value e2 = events[2]; Assert.AreEqual("version", e2.At("type").To <string>().Value); Assert.AreEqual("update", e2.At("event", "action").To <string>().Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), ((ObjectV)e2.At("event", "diff", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), ((ObjectV)e2.At("event", "document", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), ((ObjectV)e2.At("event", "prev", "data")).Value); Value e3 = events[3]; Assert.AreEqual("version", e3.At("type").To <string>().Value); Assert.AreEqual("update", e3.At("event", "action").To <string>().Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")), ((ObjectV)e3.At("event", "diff", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")), ((ObjectV)e3.At("event", "document", "data")).Value); Assert.AreEqual( FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), ((ObjectV)e3.At("event", "prev", "data")).Value); }