Exemplo n.º 1
0
        [Test] public void TestFieldConvertion()
        {
            var setRef = new Dictionary <string, Value> {
                { "@ref", "databases" }
            };

            var obj = ObjectV.With(
                "string", "a string",
                "bool", true,
                "double", 3.14,
                "long", 1234,
                "ref", BuiltIn.DATABASES,
                "setref", new SetRefV(setRef));

            Assert.AreEqual("a string",
                            obj.Get(Field.At("string").To <string>()));

            Assert.AreEqual(true,
                            obj.Get(Field.At("bool").To <bool>()));

            Assert.AreEqual(3.14,
                            obj.Get(Field.At("double").To <double>()));

            Assert.AreEqual(1234L,
                            obj.Get(Field.At("long").To <long>()));

            Assert.AreEqual(BuiltIn.DATABASES,
                            obj.Get(Field.At("ref").To <RefV>()));

            Assert.AreEqual(new SetRefV(setRef),
                            obj.Get(Field.At("setref").To <SetRefV>()));
        }
Exemplo n.º 2
0
        public void TestUserClassConstructor()
        {
            var person = Decode <Product>(ObjectV.With("Description", "Laptop", "Price", 999.9));

            Assert.AreEqual("Laptop", person.Description);
            Assert.AreEqual(999.9, person.Price);
        }
Exemplo n.º 3
0
        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) }
            })
                );
        }
Exemplo n.º 4
0
        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 }
            })
                );
        }
Exemplo n.º 5
0
        [Test] public void TestObject()
        {
            AssertJsonEqual(ObjectV.With("foo", ObjectV.With("bar", ArrayV.Of(BooleanV.True, NullV.Instance))),
                            "{\"foo\":{\"bar\":[true,null]}}");

            AssertJsonEqual(ObjectV.Empty, "{}");
        }
Exemplo n.º 6
0
 public void TestDocuments()
 {
     AssertJsonEqual(
         ObjectV.With("documents", ObjectV.With("collection", "foo")),
         "{\"documents\":{\"collection\":\"foo\"}}"
         );
 }
Exemplo n.º 7
0
 [Test] public void TestImplicitExpr()
 {
     Assert.AreEqual(LongV.Of(10), (Expr)10);
     Assert.AreEqual(LongV.Of(10), (Expr)10L);
     Assert.AreEqual(BooleanV.True, (Expr)true);
     Assert.AreEqual(BooleanV.False, (Expr)false);
     Assert.AreEqual(DoubleV.Of(3.14), (Expr)3.14);
     Assert.AreEqual(StringV.Of("a string"), (Expr)"a string");
     Assert.AreEqual(StringV.Of("create"), (Expr)ActionType.Create);
     Assert.AreEqual(StringV.Of("delete"), (Expr)ActionType.Delete);
     Assert.AreEqual(StringV.Of("second"), (Expr)TimeUnit.Second);
     Assert.AreEqual(StringV.Of("millisecond"), (Expr)TimeUnit.Millisecond);
     Assert.AreEqual(StringV.Of("microsecond"), (Expr)TimeUnit.Microsecond);
     Assert.AreEqual(StringV.Of("nanosecond"), (Expr)TimeUnit.Nanosecond);
     Assert.AreEqual(DateV.Of("2000-01-01"), (Expr) new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc));
     Assert.AreEqual(DateV.Of("2000-01-01"), (Expr) new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero));
     Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Expr) new DateTime(2000, 1, 1, 1, 1, 1, 123, DateTimeKind.Utc));
     Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Expr) new DateTimeOffset(2000, 1, 1, 1, 1, 1, 123, TimeSpan.Zero));
     Assert.AreEqual(NullV.Instance, (Expr)(string)null);
     Assert.AreEqual(
         ObjectV.With("name", "foo", "count", 42),
         (Expr) new Dictionary <string, Expr>()
     {
         { "name", "foo" }, { "count", 42 }
     });
 }
Exemplo n.º 8
0
        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
                );
        }
Exemplo n.º 9
0
        public void TestCastNumbers()
        {
            //to long
            Assert.AreEqual(10L, Decode <long>(10L));
            Assert.AreEqual(10L, Decode <long>(10d));
            Assert.AreEqual(10L, Decode <long>(10));
            Assert.AreEqual(10L, Decode <long>("10"));

            //to double
            Assert.AreEqual(10d, Decode <double>(10L));
            Assert.AreEqual(10d, Decode <double>(10d));
            Assert.AreEqual(10d, Decode <double>(10));
            Assert.AreEqual(10d, Decode <double>("10"));

            //to int
            Assert.AreEqual(10, Decode <int>(10L));
            Assert.AreEqual(10, Decode <int>(10d));
            Assert.AreEqual(10, Decode <int>(10));
            Assert.AreEqual(10, Decode <int>("10"));

            //to short
            Assert.AreEqual((short)10, Decode <short>(10L));
            Assert.AreEqual((short)10, Decode <short>(10d));
            Assert.AreEqual((short)10, Decode <short>(10));
            Assert.AreEqual((short)10, Decode <short>("10"));

            var time = DateTime.Now;

            Assert.AreEqual(
                new Product("MacBook", 10, time, time),
                Decode <Product>(ObjectV.With("Description", "MacBook", "Price", 10, "Created", time, "LastUpdated", time))
                );
        }
Exemplo n.º 10
0
        [Test] public void TestObjectLiteral()
        {
            AssertJsonEqual(ObjectV.With("@name", StringV.Of("Test")),
                            "{\"@obj\":{\"@name\":\"Test\"}}");

            AssertJsonEqual(ObjectV.Empty, "{\"@obj\":{}}");
        }
Exemplo n.º 11
0
        [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>())));
        }
Exemplo n.º 12
0
        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"));
        }
Exemplo n.º 13
0
 public void TestUserStruct()
 {
     Assert.AreEqual(
         ObjectV.With("x", 10, "y", 20, "width", 10, "height", 10),
         Encode(new Rect(10, 20, 20, 30))
         );
 }
Exemplo n.º 14
0
        public void TestUserClassConstructor()
        {
            var person = Decode <Product>(ObjectV.With("Description", "Laptop", "Price", 999.9, "Created", DateV.Of("2000-01-01"), "LastUpdated", TimeV.Of("2001-01-02T11:00:00.123Z")));

            Assert.AreEqual("Laptop", person.Description);
            Assert.AreEqual(999.9, person.Price);
            Assert.AreEqual(new DateTime(2000, 1, 1), person.Created);
            Assert.AreEqual(new DateTimeOffset(2001, 1, 2, 11, 0, 0, 123, TimeSpan.Zero), person.LastUpdated);
        }
Exemplo n.º 15
0
        public void TestDateTimeOverride()
        {
            DateTime testDateTime = new DateTime(2000, 1, 1, 1, 1, 1, 1);

            Assert.AreEqual(
                ObjectV.With("timev", new TimeV(testDateTime), "datev", new DateV(testDateTime.Date)),
                Encode(new DateTimeOverride(testDateTime, testDateTime))
                );
        }
Exemplo n.º 16
0
        public void TestIgnoreConstructorParameter()
        {
            var obj = Decode <IgnoreConstructorParameter>(ObjectV.With(
                                                              "property1", "property1",
                                                              "property2", "should not be assigned to property2"
                                                              ));

            Assert.AreEqual("property1", obj.Property1);
            Assert.AreEqual("property2", obj.Property2);
        }
Exemplo n.º 17
0
        public void TestFillMissingPropertiesOnMethodCreator()
        {
            var obj = Decode <MissingPropertiesOnMethodCreator>(
                ObjectV.With("Field1", "field1", "a_missing_field", "field2", "a_missing_property", "field3")
                );

            Assert.AreEqual("field1", obj.Field1);
            Assert.AreEqual("field2", obj.Field2);
            Assert.AreEqual("field3", obj.Field3);
        }
Exemplo n.º 18
0
        public void TestStringOverride()
        {
            Uri  testUri  = new Uri("https://fauna.com");
            Guid testGuid = Guid.NewGuid();

            Assert.AreEqual(
                ObjectV.With("uri", new StringV(testUri.ToString()), "guid", new StringV(testGuid.ToString())),
                Encode(new StringOverride(testUri, testGuid))
                );
        }
Exemplo n.º 19
0
        private FaunaException ConstructStreamingException(ObjectV value)
        {
            var code        = value.Get(CODE);
            var description = value.Get(DESCRIPTION);
            var queryError  = new QueryError(null, code, description, null);
            var response    = new QueryErrorResponse(500, new List <QueryError> {
                queryError
            });

            return(new StreamingException(response));
        }
Exemplo n.º 20
0
        [Test] public void TestNestedObject()
        {
            var nested = ObjectV.With("foo", ObjectV.With("bar", "a string"));

            Assert.AreEqual(StringV.Of("a string"),
                            nested.Get(Field.At("foo", "bar")));

            Assert.Throws(typeof(InvalidOperationException),
                          () => nested.Get(Field.At("foo", "nonexistent")),
                          "Cannot find path \"foo/nonexistent\". Object key \"nonexistent\" not found");
        }
Exemplo n.º 21
0
        [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",
            }));
        }
Exemplo n.º 22
0
        public void TestEncodeRawExpressions()
        {
            var indexCfg = new Dictionary <string, object>()
            {
                { "name", "index_name" },
                { "source", Collection("class_name") }
            };

            Assert.AreEqual(
                ObjectV.With("name", "index_name", "source", ExprV.Of(Collection("class_name"))),
                Encode(indexCfg)
                );
        }
Exemplo n.º 23
0
        [Test] public void TestObjectKey()
        {
            var obj = ObjectV.With("foo", "bar");

            Assert.AreEqual(StringV.Of("bar"),
                            obj.Get(Field.At("foo")));

            Assert.AreEqual(None(),
                            obj.GetOption(Field.At("nonexistent")));

            Assert.Throws(typeof(InvalidOperationException),
                          () => obj.Get(Field.At("nonexistent")),
                          "Cannot find path \"nonexistent\". Object key \"nonexistent\" not found");
        }
Exemplo n.º 24
0
        public void TestNullableFields()
        {
            var structWithNullableFields = Decode <StructWithNullableFields>(ObjectV.With(
                                                                                 "anInteger", LongV.Of(10),
                                                                                 "aDouble", DoubleV.Of(3.14),
                                                                                 "aStruct", ObjectV.With("aByte", LongV.Of(10))
                                                                                 ));

            Assert.AreEqual(10, structWithNullableFields.anInteger);
            Assert.AreEqual(3.14, structWithNullableFields.aDouble);
            Assert.IsNotNull(structWithNullableFields.aStruct);
            Assert.AreEqual(10, structWithNullableFields.aStruct?.aByte);
            Assert.IsNull(structWithNullableFields.aStruct?.aShort);
        }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
0
        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"
            })
                );
        }
Exemplo n.º 27
0
        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);
        }
Exemplo n.º 28
0
        public void TestNullToValueTypes()
        {
            Assert.AreEqual(0, Decode <long>(NullV.Instance));
            Assert.AreEqual(0, Decode <int>(NullV.Instance));
            Assert.AreEqual(0, Decode <short>(NullV.Instance));
            Assert.AreEqual(0, Decode <sbyte>(NullV.Instance));

            Assert.AreEqual(0, Decode <ulong>(NullV.Instance));
            Assert.AreEqual(0, Decode <uint>(NullV.Instance));
            Assert.AreEqual(0, Decode <ushort>(NullV.Instance));
            Assert.AreEqual(0, Decode <byte>(NullV.Instance));
            Assert.AreEqual(0, Decode <char>(NullV.Instance));

            Assert.AreEqual((float)0, Decode <float>(NullV.Instance));
            Assert.AreEqual((double)0, Decode <double>(NullV.Instance));
            Assert.AreEqual((decimal)0, Decode <decimal>(NullV.Instance));

            Assert.AreEqual(default(Point), Decode <Point>(NullV.Instance));
            Assert.AreEqual(default(Point), Decode <Point>(ObjectV.Empty));
            Assert.AreEqual(default(Point), Decode <Point>(ObjectV.With("x", NullV.Instance, "y", NullV.Instance)));

            Assert.AreEqual(default(Rect), Decode <Rect>(NullV.Instance));
            Assert.AreEqual(default(Rect), Decode <Rect>(ObjectV.Empty));
            Assert.AreEqual(default(Rect), Decode <Rect>(ObjectV.With("UpperLeft", NullV.Instance, "BottonRight", NullV.Instance)));

            //

            Assert.AreEqual(0, Decode <long>(null));
            Assert.AreEqual(0, Decode <int>(null));
            Assert.AreEqual(0, Decode <short>(null));
            Assert.AreEqual(0, Decode <sbyte>(null));

            Assert.AreEqual(0, Decode <ulong>(null));
            Assert.AreEqual(0, Decode <uint>(null));
            Assert.AreEqual(0, Decode <ushort>(null));
            Assert.AreEqual(0, Decode <byte>(null));
            Assert.AreEqual(0, Decode <char>(null));

            Assert.AreEqual((float)0, Decode <float>(null));
            Assert.AreEqual((double)0, Decode <double>(null));
            Assert.AreEqual((decimal)0, Decode <decimal>(null));

            Assert.AreEqual(default(Point), Decode <Point>(null));
            Assert.AreEqual(default(Rect), Decode <Rect>(null));
        }
Exemplo n.º 29
0
        [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");
        }
Exemplo n.º 30
0
 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())
         );
 }