Ejemplo n.º 1
0
    public void Null_Or_Whitespace_Throws_JsonException(string input)
    {
        // Arrange
        var handler = new JsonTypeHandler <Test>();

        // Act
        var action = void() => handler.Parse(input);

        // Assert
        Assert.Throws <JsonException>(action);
    }
Ejemplo n.º 2
0
    public void Null_Sets_Null_Value(Test input)
    {
        // Arrange
        var handler   = new JsonTypeHandler <Test>();
        var parameter = Substitute.For <IDbDataParameter>();

        // Act
        handler.SetValue(parameter, input);

        // Assert
        Assert.IsType <DBNull>(parameter.Value);
    }
Ejemplo n.º 3
0
    public void InvalidJson_Throws_JsonException()
    {
        // Arrange
        var handler = new JsonTypeHandler <Test>();
        var input   = Rnd.Str;

        // Act
        var action = void() => handler.Parse(input);

        // Assert
        Assert.Throws <JsonException>(action);
    }
Ejemplo n.º 4
0
    public void ValidJson_ReturnsObject()
    {
        // Arrange
        var handler  = new JsonTypeHandler <Test>();
        var v0       = Rnd.Str;
        var v1       = Rnd.Int;
        var input    = $"{{\"foo\":\"{v0}\",\"bar\":{v1},\"ignore\":\"this\"}}";
        var expected = new Test {
            Foo = v0, Bar = v1
        };

        // Act
        var result = handler.Parse(input);

        // Assert
        Assert.Equal(expected, result);
    }
Ejemplo n.º 5
0
    public void Object_Sets_Value_As_Json()
    {
        // Arrange
        var handler   = new JsonTypeHandler <Test>();
        var parameter = Substitute.For <IDbDataParameter>();
        var v0        = Rnd.Str;
        var v1        = Rnd.Int;
        var input     = new Test {
            Foo = v0, Bar = v1
        };
        var expected = $"{{\"foo\":\"{v0}\",\"bar\":{v1},\"empty\":null}}";

        // Act
        handler.SetValue(parameter, input);

        // Assert
        Assert.Equal(expected, parameter.Value);
    }
        public void Build()
        {
            var alias = "NamedTypeHandlerCacheTest";

            JsonTypeHandler expectedJson      = new JsonTypeHandler();
            XmlTypeHandler  expectedXml       = new XmlTypeHandler();
            var             namedTypeHandlers = new Dictionary <string, ITypeHandler>
            {
                { "Json", expectedJson },
                { "Xml", expectedXml }
            };

            NamedTypeHandlerCache.Build(alias, namedTypeHandlers);
            var actualJson = NamedTypeHandlerCache.GetTypeHandlerField(alias, "Json").GetValue(null);

            Assert.Equal(expectedJson, actualJson);

            var actualXml = NamedTypeHandlerCache.GetTypeHandlerField(alias, "Xml").GetValue(null);

            Assert.Equal(expectedXml, actualXml);
        }
Ejemplo n.º 7
0
        private static async Task Main(string[] args)
        {
            using var connection = new SqlConnection(ConnectionString);
            JsonTypeHandler <IList <Review> > .Configure();

            ArrayTypeHandler.Configure();

            var posts = await connection.QueryAsync <Post>("SELECT Id, Title, Content, Date, Authors, Reviews FROM Posts");

            var posts2 = await connection.QueryAsync <Post>("SELECT * FROM Posts WHERE Authors LIKE @authors", new { Authors = $"%Marco%" });

            var post = new Post
            {
                Id      = Guid.NewGuid(),
                Title   = "TinyHelpers3",
                Content = "New Description",
                Authors = new string[] { "Andrea", "Calogero" },
                Date    = DateTime.UtcNow
            };

            await connection.ExecuteAsync("INSERT INTO Posts(Id, Title, Content, Date, Authors, Reviews) VALUES(@Id, @Title, @Content, @Date, @Authors, @Reviews)",
                                          post);
        }