Пример #1
0
        public void To_Should_Parse_Nullable_Values()
        {
            var nodes = new[] { GraphNodeGraphSON2Tests.GetGraphNode("null"), GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"gz:Obj\", \"@value\": null}") };

            foreach (var node in nodes)
            {
                Assert.Null(node.To <short?>());
                Assert.Null(node.To <int?>());
                Assert.Null(node.To <long?>());
                Assert.Null(node.To <decimal?>());
                Assert.Null(node.To <float?>());
                Assert.Null(node.To <double?>());
                Assert.Null(node.To <Guid?>());
                Assert.Null(node.To <TimeUuid?>());
                Assert.Null(node.To <BigInteger?>());
                Assert.Null(node.To <Duration?>());
                Assert.Null(node.To <DateTimeOffset?>());
            }
            Assert.AreEqual(1, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Int16\", \"@value\": 1}").To <short?>());
            Assert.AreEqual(1, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Int32\", \"@value\": 1}").To <int?>());
            Assert.AreEqual(1L, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Int64\", \"@value\": 1}").To <long?>());
            Assert.AreEqual(1M, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:BigDecimal\", \"@value\": 1}").To <decimal?>());
            Assert.AreEqual(1F, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Float\", \"@value\": 1}").To <float?>());
            Assert.AreEqual(1D, GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Double\", \"@value\": 1}").To <double?>());
            Assert.AreEqual(Guid.Parse("2cc83ef0-5da4-11e7-8c51-2578d2fa5d3a"), GraphNodeGraphSON2Tests.GetGraphNode(
                                "{\"@type\": \"g:Int16\", \"@value\": \"2cc83ef0-5da4-11e7-8c51-2578d2fa5d3a\"}").To <Guid?>());
            Assert.AreEqual((TimeUuid)Guid.Parse("2cc83ef0-5da4-11e7-8c51-2578d2fa5d3a"), GraphNodeGraphSON2Tests.GetGraphNode(
                                "{\"@type\": \"g:UUID\", \"@value\": \"2cc83ef0-5da4-11e7-8c51-2578d2fa5d3a\"}").To <TimeUuid?>());
            Assert.AreEqual(BigInteger.Parse("1"),
                            GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Int16\", \"@value\": 1}").To <BigInteger?>());
            Assert.AreEqual(Duration.Parse("12h"),
                            GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Duration\", \"@value\": \"12h\"}").To <Duration?>());
            Assert.AreEqual(DateTimeOffset.Parse("1970-01-01 00:00:01Z"),
                            GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"gx:Instant\", \"@value\": 1000}").To <DateTimeOffset?>());
        }
Пример #2
0
        public void To_Should_Parse_Scalar_Values <T>(string json, T value)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(node.To <T>(), value);
            Assert.True(node.IsScalar);
        }
Пример #3
0
        public void HasProperty_Should_Check_For_JSON_Objects()
        {
            const string json = "{\"prop1\": {\"@type\": \"g:Double\", \"@value\": 789}}";
            var          node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.True(node.HasProperty("prop1"));
            Assert.False(node.HasProperty("propZ"));
        }
Пример #4
0
        public void Get_T_Should_Navigate_Properties_Of_Json_Objects()
        {
            const string json = "{\"prop1\": {\"@type\": \"g:Double\", \"@value\": 789}," +
                                "  \"prop2\": true}";
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(789D, node.Get <double>("prop1"));
            Assert.AreEqual(true, node.Get <bool>("prop2"));
        }
Пример #5
0
        public void To_Should_Parse_Null_Vertex_Edge_Or_Path()
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode("null");

            Assert.Null(node.To <Vertex>());
            Assert.Null(node.To <Edge>());
            Assert.Null(node.To <Path>());
            Assert.Null(node.To <IVertex>());
            Assert.Null(node.To <IEdge>());
            Assert.Null(node.To <IPath>());
        }
Пример #6
0
        public void Dynamic_Should_Navigate_Properties_Of_GraphSON2_Objects()
        {
            dynamic node = GraphNodeGraphSON2Tests.GetGraphNode(
                "{\"@type\": \"g:MyObject\", \"@value\": {" +
                "\"id\":{\"@type\":\"g:Int32\",\"@value\":150}," +
                "\"label\":\"topic\"" +
                "}}");
            int id = node.id;

            Assert.AreEqual(150, id);
            Assert.AreEqual("topic", node.label.ToString());
        }
Пример #7
0
        public void HasProperty_Should_Check_For_GraphSON2_Objects()
        {
            const string json = "{\"@type\": \"gex:ObjectTree\", \"@value\": " +
                                "  {" +
                                "    \"prop1\": {\"@type\": \"g:Int32\", \"@value\": 789}," +
                                "    \"prop2\": \"prop2 value\"" +
                                "  }" +
                                "}";
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.True(node.HasProperty("prop1"));
            Assert.False(node.HasProperty("propZ"));
        }
Пример #8
0
        public void To_Should_Throw_For_Structs_With_Null()
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode("null");

            Assert.Throws <InvalidOperationException>(() => node.To <short>());
            Assert.Throws <InvalidOperationException>(() => node.To <int>());
            Assert.Throws <InvalidOperationException>(() => node.To <long>());
            Assert.Throws <InvalidOperationException>(() => node.To <decimal>());
            Assert.Throws <InvalidOperationException>(() => node.To <float>());
            Assert.Throws <InvalidOperationException>(() => node.To <double>());
            Assert.Throws <InvalidOperationException>(() => node.To <Guid>());
            Assert.Throws <InvalidOperationException>(() => node.To <TimeUuid>());
            Assert.Throws <InvalidOperationException>(() => node.To <BigInteger>());
            Assert.Throws <InvalidOperationException>(() => node.To <Duration>());
        }
Пример #9
0
        public void Get_T_Should_Navigate_Properties_Of_GraphSON2_Objects()
        {
            const string json = "{\"@type\": \"gex:ObjectTree\", \"@value\": " +
                                "  {" +
                                "    \"prop1\": {\"@type\": \"g:Int32\", \"@value\": 789}," +
                                "    \"prop2\": \"prop2 value\"" +
                                "  }" +
                                "}";
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(789, node.Get <int>("prop1"));
            Assert.AreEqual("prop2 value", node.Get <string>("prop2"));
            var prop = node.Get <IGraphNode>("prop1");

            Assert.AreEqual(789, prop.To <int>());
            Assert.AreEqual("prop2 value", node.Get <IGraphNode>("prop2").ToString());
        }
Пример #10
0
        public void Implicit_Conversion_Operators_Test()
        {
            var intNode = GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Int16\", \"@value\": 123}");

            Assert.AreEqual(123, (int)intNode);
            Assert.AreEqual(123L, (long)intNode);
            Assert.AreEqual((short)123, (short)intNode);
            Assert.AreEqual(123, (int)intNode);
            string stringValue = GraphNodeGraphSON2Tests.GetGraphNode("\"something\"");

            Assert.AreEqual("something", stringValue);
            bool boolValue = GraphNodeGraphSON2Tests.GetGraphNode("true");

            Assert.True(boolValue);
            var floatNode = GraphNodeGraphSON2Tests.GetGraphNode("{\"@type\": \"g:Float\", \"@value\": 123.1}");

            Assert.AreEqual(123.1f, (float)floatNode);
            Assert.AreEqual(123.1D, (double)floatNode);
        }
Пример #11
0
        public void To_Should_Parse_Vertex_Values()
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(
                "{\"@type\": \"g:Vertex\", \"@value\": {" +
                "\"id\":{\"@type\": \"g:Int32\",\"@value\": 1368843392}," +
                "\"label\":\"user\"," +
                "\"properties\":{" +
                "\"name\":[{\"@type\": \"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":0},\"value\":\"jorge\"}}]," +
                "\"age\":[{\"@type\": \"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":1},\"value\":{\"@type\":\"g:Int32\",\"@value\":35}}}]}" +
                "}}");
            var vertex = node.To <Vertex>();

            Assert.AreEqual("user", vertex.Label);
            Assert.AreEqual("jorge", vertex.Properties["name"].ToArray().First().Get <string>("value"));
            Assert.AreEqual(35, vertex.Properties["age"].ToArray().First().Get <int>("value"));
            var iVertex = node.To <IVertex>();

            Assert.AreEqual("user", iVertex.Label);
            Assert.AreEqual("jorge", iVertex.GetProperty("name").Value.ToString());
            Assert.AreEqual(35, iVertex.GetProperty("age").Value.To <int>());
            Assert.Null(iVertex.GetProperty("nonExistent"));
        }
Пример #12
0
 public void To_Should_Parse_LineString_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, LineString.Parse);
 }
Пример #13
0
 public void To_Should_Parse_InetAddress_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, IPAddress.Parse);
 }
Пример #14
0
        /// <summary>
        /// Asserts that To{T}() method returns the expected instance.
        /// </summary>
        /// <param name="json">The node json</param>
        /// <param name="stringValue">The string representation of the expected value</param>
        /// <param name="parser">The parser used for the expected value</param>
        private static void TestToTypeParsing <T>(string json, string stringValue, Func <string, T> parser)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(node.To <T>(), parser(stringValue));
        }
Пример #15
0
 public void To_Should_Parse_Decimal_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, str => decimal.Parse(str, CultureInfo.InvariantCulture));
 }
Пример #16
0
        public void To_Should_Parse_LocalTime_Values(string json, string stringValue)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(LocalTime.Parse(stringValue), node.To <LocalTime>());
        }
Пример #17
0
        public void To_Should_Parse_DateTimeOffset_Values(string json, string stringValue)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(DateTimeOffset.Parse(stringValue), node.To <DateTimeOffset>());
        }
Пример #18
0
        public void To_Should_Parse_Blob_Values(string json, string stringValue)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            CollectionAssert.AreEqual(Encoding.UTF8.GetBytes(stringValue), node.To <byte[]>());
        }
Пример #19
0
 public void To_Should_Parse_Uuid_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, Guid.Parse);
 }
Пример #20
0
 public void To_Should_Parse_TimeUuid_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, s => (TimeUuid)Guid.Parse(s));
 }
Пример #21
0
 public void To_Should_Parse_Polygon_Values(string json, string stringValue)
 {
     GraphNodeGraphSON2Tests.TestToTypeParsing(json, stringValue, Polygon.Parse);
 }
Пример #22
0
        public void ToString_Should_Return_The_String_Representation_Of_Scalars(string json, object value)
        {
            var node = GraphNodeGraphSON2Tests.GetGraphNode(json);

            Assert.AreEqual(node.ToString(), value.ToString());
        }