public void TestCollection() { Assert.AreEqual( ArrayV.Of(1, 2, 3), Encode(new int[] { 1, 2, 3 }) ); Assert.AreEqual( ArrayV.Of(1, 2, 3), Encode(new List <int> { 1, 2, 3 }) ); Assert.AreEqual( ArrayV.Of("a string", 3.14, NullV.Instance, 10, true), Encode(new object[] { "a string", 3.14, null, 10, true }) ); Assert.AreEqual( ObjectV.With("field1", "value1", "field2", 10), Encode(new Dictionary <string, object> { { "field1", "value1" }, { "field2", 10 } }) ); }
[Test] public void TestComplex() { var obj = ObjectV.With("foo", ArrayV.Of(1, 2, ObjectV.With("bar", "a string"))); Assert.AreEqual("a string", obj.Get(Field.At("foo").At(Field.At(2)).At(Field.At("bar").To <string>()))); }
[Test] public void TestObject() { AssertJsonEqual(ObjectV.With("foo", ObjectV.With("bar", ArrayV.Of(BooleanV.True, NullV.Instance))), "{\"foo\":{\"bar\":[true,null]}}"); AssertJsonEqual(ObjectV.Empty, "{}"); }
public void TestErrors() { Assert.AreEqual( "The type System.Collections.IList is not generic", Assert.Throws <InvalidOperationException>(() => Decode <IList>(ArrayV.Of("a string"))).Message ); Assert.AreEqual( "The type System.Collections.IDictionary is not generic", Assert.Throws <InvalidOperationException>(() => Decode <IDictionary>(ObjectV.With("key", "vlaue"))).Message ); Assert.AreEqual( "No default constructor or constructor/static method annotated with attribute [FaunaConstructor] found on type `Test.DecoderTest+NoDefaultConstructor`", Assert.Throws <InvalidOperationException>(() => Decode <NoDefaultConstructor>("a string")).Message ); Assert.AreEqual( "More than one static method creator found on type `Test.DecoderTest+MoreThanOneStaticCreator`", Assert.Throws <InvalidOperationException>(() => Decode <MoreThanOneStaticCreator>("a string")).Message ); Assert.AreEqual( "More than one constructor found on type `Test.DecoderTest+MoreThanOneConstructor`", Assert.Throws <InvalidOperationException>(() => Decode <MoreThanOneConstructor>("a string")).Message ); }
public void TestUserClassDefaultConstructor() { var product1 = ObjectV.With("Description", "Laptop", "Price", 999.9); var product2 = ObjectV.With("Description", "Mouse", "Price", 9.9); var order = Decode <Order>(ObjectV.With( "number", "XXXYYY999", "products", ArrayV.Of(product1, product2), "vouchers", ArrayV.Of(111111, 222222), "addresses", ArrayV.Of("744 Montgomery Street Suite 200") )); Assert.AreEqual("XXXYYY999", order.Number); Assert.AreEqual(2, order.Products.Count); Assert.AreEqual("Laptop", order.Products[0].Description); Assert.AreEqual(999.9, order.Products[0].Price); Assert.AreEqual("Mouse", order.Products[1].Description); Assert.AreEqual(9.9, order.Products[1].Price); Assert.IsTrue(order.Vouchers.Contains(111111)); Assert.IsTrue(order.Vouchers.Contains(222222)); Assert.IsTrue(order.Addresses.Contains("744 Montgomery Street Suite 200")); }
[Test] public void TestArray() { AssertJsonEqual(ArrayV.Of(LongV.Of(1), StringV.Of("a string"), DoubleV.Of(3.14), BooleanV.True, NullV.Instance), "[1, \"a string\", 3.14, true, null]"); AssertJsonEqual(ArrayV.Empty, "[]"); }
public void TestUserClass() { Assert.AreEqual( ObjectV.With("name", "John", "birth_date", new DateV("1980-01-01"), "birth_date2", new TimeV("1980-01-01T01:00:00.0Z"), "address", ObjectV.With("number", 123, "street", "Market St"), "avatar_image", new BytesV(1, 2, 3, 4)), Encode(new Person("John", new DateTime(1980, 1, 1), new Address("Market St", 123), Person.DefaultAvatar)) ); // list of person Assert.AreEqual( ArrayV.Of( ObjectV.With("name", "John", "birth_date", new DateV("1980-01-01"), "birth_date2", new TimeV("1980-01-01T01:00:00.0Z"), "address", ObjectV.With("number", 123, "street", "Market St"), "avatar_image", new BytesV(1, 2, 3, 4)), ObjectV.With("name", "Mary", "birth_date", new DateV("1975-01-01"), "birth_date2", new TimeV("1975-01-01T01:00:00.0Z"), "address", ObjectV.With("number", 321, "street", "Mission St"), "avatar_image", new BytesV(1, 2, 3, 4)) ), Encode(new List <Person> { new Person("John", new DateTime(1980, 1, 1), new Address("Market St", 123), Person.DefaultAvatar), new Person("Mary", new DateTime(1975, 1, 1), new Address("Mission St", 321), Person.DefaultAvatar) }) ); // dictionary of string => person Assert.AreEqual( ObjectV.With( "first", ObjectV.With("name", "John", "birth_date", new DateV("1980-01-01"), "birth_date2", new TimeV("1980-01-01T01:00:00.0Z"), "address", ObjectV.With("number", 123, "street", "Market St"), "avatar_image", new BytesV(1, 2, 3, 4)), "second", ObjectV.With("name", "Mary", "birth_date", new DateV("1975-01-01"), "birth_date2", new TimeV("1975-01-01T01:00:00.0Z"), "address", ObjectV.With("number", 321, "street", "Mission St"), "avatar_image", new BytesV(1, 2, 3, 4)) ), Encode(new Dictionary <string, Person> { { "first", new Person("John", new DateTime(1980, 1, 1), new Address("Market St", 123), Person.DefaultAvatar) }, { "second", new Person("Mary", new DateTime(1975, 1, 1), new Address("Mission St", 321), Person.DefaultAvatar) } }) ); }
[Test] public void TestCollectComplex() { var array = ArrayV.Of( ObjectV.With("name", ArrayV.Of("John")), ObjectV.With("name", ArrayV.Of("Bill")) ); Assert.That(array.Collect(Field.At("name").At(Field.At(0)).To <string>()), Is.EquivalentTo(new List <string> { "John", "Bill", })); }
public async Task <IEnumerable <Product> > GetProductsSortedByName() { var client = GetClient(); var values = await client.Query(Map(Paginate( Match(Index(PRODUCTS_SORTED_BY_NAME_PRICEIN_INDEX)) ), Lambda(ArrayV.Of("name", "price", "prodRef"), Get(Var("prodRef"))) )); IResult <Value[]> data = values.At("data").To <Value[]>(); List <Product> products = await GetProducts(data.Value, client); return(products); }
public async Task <IEnumerable <Product> > GetRecentProducts() { var client = GetClient(); var values = await client.Query(Map(Paginate( Match(Index(RECENT_PRODUCTS_INDEX)), size: MAX_RECENT_PRODUCTS ), Lambda(ArrayV.Of("datetime", "prodRef"), Get(Var("prodRef"))) )); IResult <Value[]> data = values.At("data").To <Value[]>(); List <Product> products = await GetProducts(data.Value, client); return(products); }
public void TestUserClassInheritance() { var product = ObjectV.With("Description", "Laptop", "Price", 999.9); var order = Decode <OrderWithCustomer>( ObjectV.With("customer", "John", "number", "XXXYYY999", "products", ArrayV.Of(product)) ); Assert.AreEqual("John", order.Customer); Assert.AreEqual("XXXYYY999", order.Number); Assert.AreEqual(1, order.Products.Count); Assert.AreEqual("Laptop", order.Products[0].Description); Assert.AreEqual(999.9, order.Products[0].Price); }
public void TestCollection() { Assert.AreEqual(new byte[] { 1, 2, 3 }, Decode <byte[]>(new BytesV(1, 2, 3))); Assert.AreEqual(new int[] { 1, 2, 3 }, Decode <int[]>(ArrayV.Of(1, 2, 3))); Assert.AreEqual(new long[] { 1, 2, 3 }, Decode <long[]>(ArrayV.Of(1, 2, 3))); //interfaces Assert.That( Decode <IDictionary <string, Value> >(ObjectV.With("a", "b")), Is.EquivalentTo(new Dictionary <string, Value> { { "a", "b" } }) ); Assert.That( Decode <IList <string> >(ArrayV.Of("a", "b")), Is.EquivalentTo(new List <string> { "a", "b" }) ); Assert.That( Decode <IEnumerable <string> >(ArrayV.Of("a", "b")), Is.EquivalentTo(new List <string> { "a", "b" }) ); //concrete types Assert.That( Decode <Dictionary <string, Value> >(ObjectV.With("a", "b")), Is.EquivalentTo(new Dictionary <string, Value> { { "a", "b" } }) ); Assert.That( Decode <List <string> >(ArrayV.Of("a", "b")), Is.EquivalentTo(new List <string> { "a", "b" }) ); }
[Test] public void TestNestedArray() { var nested = ArrayV.Of(10, ArrayV.Of(1234, ArrayV.Of(4321))); Assert.AreEqual(LongV.Of(10), nested.Get(Field.At(0))); Assert.AreEqual(LongV.Of(1234), nested.Get(Field.At(1, 0))); Assert.AreEqual(LongV.Of(4321), nested.Get(Field.At(1, 1, 0))); Assert.Throws(typeof(InvalidOperationException), () => nested.Get(Field.At(1, 1, 1)), "Cannot find path \"1/1/1\". Array index \"1\" not found"); }
[Test] public void TestArrayIndex() { var array = ArrayV.Of("a string", 10); Assert.AreEqual(StringV.Of("a string"), array.Get(Field.At(0))); Assert.AreEqual(LongV.Of(10), array.Get(Field.At(1))); Assert.AreEqual(None(), array.GetOption(Field.At(1234))); Assert.Throws(typeof(InvalidOperationException), () => array.Get(Field.At(1234)), "Cannot find path \"1234\". Array index \"1234\" not found"); }
[Test] public async Task TestIssueABatchedQueryWithVarargs() { var result0 = await client.Query( Get(thorSpell1), Get(thorSpell2) ); Assert.That(ArrayV.Of(result0).Collect(REF_FIELD), Is.EquivalentTo(new List <RefV> { thorSpell1, thorSpell2 })); var result1 = await client.Query(Add(1, 2), Subtract(1, 2)); Assert.That(result1, Is.EquivalentTo(new List <Value> { 3, -1 })); }
[Test] public void TestCollect() { var array = ArrayV.Of("John", "Bill"); Assert.That(array.Collect(Field.To <string>()), Is.EquivalentTo(new List <string> { "John", "Bill" })); var obj = ObjectV.With("arrayOfNames", array); Assert.That(obj.Get(Field.At("arrayOfNames").Collect(Field.To <string>())), Is.EquivalentTo(new List <string> { "John", "Bill" })); Assert.Throws(typeof(InvalidOperationException), () => obj.Collect(Field.To <string>()), "Cannot convert ObjectV to ArrayV"); }
public void TestFaunaTypes() { Assert.AreEqual( ObjectV.With(new Dictionary <string, Value> { { "stringV", "a string" }, { "longV", 123 }, { "booleanV", true }, { "doubleV", 3.14 }, { "nullV", NullV.Instance }, { "dateV", new DateV("2001-01-01") }, { "timeV", new TimeV("2000-01-01T01:10:30.123Z") }, { "refV", new RefV("collections") }, { "setRefV", new SetRefV(new Dictionary <string, Value>()) }, { "arrayV", ArrayV.Of(1, 2, 3) }, { "objectV", ObjectV.With("a", "b") }, { "bytesV", new BytesV(1, 2, 3, 4) } }), Encode(new FaunaTypes()) ); }
public void TestUserClassDefaultConstructor() { var product1 = ObjectV.With("Description", "Laptop", "Price", 999.9); var product2 = ObjectV.With("Description", "Mouse", "Price", 9.9); var order = Decode <Order>(ObjectV.With("number", "XXXYYY999", "products", ArrayV.Of(product1, product2))); Assert.AreEqual("XXXYYY999", order.Number); Assert.AreEqual(2, order.Products.Count); Assert.AreEqual("Laptop", order.Products[0].Description); Assert.AreEqual(999.9, order.Products[0].Price); Assert.AreEqual("Mouse", order.Products[1].Description); Assert.AreEqual(9.9, order.Products[1].Price); }
public void TestErrors() { Assert.AreEqual( "Cannot convert `StringV(a string)` to System.Int32", Assert.Throws <InvalidOperationException>(() => Decode <int>("a string")).Message ); Assert.AreEqual( "Cannot convert `DoubleV(3.14)` to System.Char", Assert.Throws <InvalidOperationException>(() => Decode <char>(3.14)).Message ); Assert.AreEqual( "Cannot convert `LongV(10)` to System.Collections.Generic.List`1[System.Int32]", Assert.Throws <InvalidOperationException>(() => Decode <List <int> >(10)).Message ); Assert.AreEqual( "Cannot convert `LongV(10)` to System.Collections.Generic.Dictionary`2[System.String,System.Int32]", Assert.Throws <InvalidOperationException>(() => Decode <Dictionary <string, int> >(10)).Message ); Assert.AreEqual( "Cannot convert `LongV(10)` to System.Double", Assert.Throws <InvalidOperationException>(() => Decode <Product>(ObjectV.With("Description", "product", "Price", 10))).Message ); Assert.AreEqual( "Cannot convert `StringV(a string)` to System.Collections.Generic.IList`1[System.Int32]", Assert.Throws <InvalidOperationException>(() => Decode <IList <int> >("a string")).Message ); Assert.AreEqual( "The type System.Collections.IList is not generic", Assert.Throws <InvalidOperationException>(() => Decode <IList>(ArrayV.Of("a string"))).Message ); Assert.AreEqual( "Cannot convert `StringV(a string)` to System.Collections.Generic.IDictionary`2[System.String,System.Int32]", Assert.Throws <InvalidOperationException>(() => Decode <IDictionary <string, int> >("a string")).Message ); Assert.AreEqual( "The type System.Collections.IDictionary is not generic", Assert.Throws <InvalidOperationException>(() => Decode <IDictionary>(ObjectV.With("key", "vlaue"))).Message ); Assert.AreEqual( "Cannot convert `StringV(a string)` to an array of type System.Int32", Assert.Throws <InvalidOperationException>(() => Decode <int[]>("a string")).Message ); Assert.AreEqual( "No default constructor or constructor/static method annotated with attribute [FaunaConstructor] found on type `Test.DecoderTest+NoDefaultConstructor`", Assert.Throws <InvalidOperationException>(() => Decode <NoDefaultConstructor>("a string")).Message ); Assert.AreEqual( "More than one static method creator found on type `Test.DecoderTest+MoreThanOneStaticCreator`", Assert.Throws <InvalidOperationException>(() => Decode <MoreThanOneStaticCreator>("a string")).Message ); Assert.AreEqual( "More than one constructor found on type `Test.DecoderTest+MoreThanOneConstructor`", Assert.Throws <InvalidOperationException>(() => Decode <MoreThanOneConstructor>("a string")).Message ); }