コード例 #1
0
        public async Task ReadObjectAsync_HasDateTimeOffsetAttribute_PreservesTimeZone(DateParseHandling handling)
        {
            {
                using var stream = new MemoryStream(
                          System.Text.Encoding.UTF8.GetBytes("{\"value\":\"2021-06-21T10:16:56.9714243+10:00\"}"));

                var serializer = CreateDefaultJsonSerializer();
                serializer.DateParseHandling = handling;
                using var reader             = new DefaultJsonStreamReader(stream, serializer);
                Assert.True(await reader.InitializeAsync());

                var value = await reader.ReadObjectAsync <JsonDocument>();

                Assert.Equal(10, value.Value.Offset.Hours);
            }
            {
                // check again, just in case the test was run in the +10 timezone and would have given a false positive.
                using var stream = new MemoryStream(
                          System.Text.Encoding.UTF8.GetBytes("{\"value\":\"2021-06-21T10:16:56.9714243-8:00\"}"));

                var serializer = CreateDefaultJsonSerializer();
                serializer.DateParseHandling = handling;
                using var reader             = new DefaultJsonStreamReader(stream, serializer);
                Assert.True(await reader.InitializeAsync());

                var value = await reader.ReadObjectAsync <JsonDocument>();

                Assert.Equal(-8, value.Value.Offset.Hours);
            }
        }
コード例 #2
0
        public async Task ReadObjectAsync_AfterObject_GetsNextProperty()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Query\query-n1ql-error-response-400.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadObjectsAsync <dynamic>().ToListAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadObjectAsync <MetricsData>();

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Null(result);
        }
コード例 #3
0
        public async Task ReadToNextAttributeAsync_NestedArray_ReadsInto()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Query\query-200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());
            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            Assert.Equal("results", await reader.ReadToNextAttributeAsync());

            // Act

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Equal("results[0].abv", result);
            Assert.Equal(3, reader.Depth);
        }
コード例 #4
0
        public async Task InitializeAsync_CalledTwice_InvalidOperationException()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            // Act/Assert

            await reader.InitializeAsync();

            await Assert.ThrowsAsync <InvalidOperationException>(() => reader.InitializeAsync());
        }
コード例 #5
0
        public async Task ReadObjectAsync_NotOnObject_InvalidOperationException()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Query\query-n1ql-error-response-400.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act/Assert

            await reader.ReadToNextAttributeAsync();

            await Assert.ThrowsAsync <InvalidOperationException>(() => reader.ReadObjectAsync <dynamic>());
        }
コード例 #6
0
        public async Task InitializeAsync_EmptyStream_ReturnsFalse()
        {
            // Arrange

            using var stream = new MemoryStream();

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            // Act

            var result = await reader.InitializeAsync();

            // Assert

            Assert.False(result);
        }
コード例 #7
0
        public async Task InitializeAsync_CalledOnce_ReturnsTrue()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            // Act

            var result = await reader.InitializeAsync();

            // Assert

            Assert.True(result);
        }
コード例 #8
0
        public async Task ReadArrayAsync_NotOnArray_InvalidOperationException()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act/Assert

            await reader.ReadToNextAttributeAsync();

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => reader.ReadObjectsAsync <dynamic>().ToListAsync().AsTask());
        }
コード例 #9
0
        public async Task ReadToNextAttributeAsync_FirstTime_ReturnsFirstAttribute()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Equal("total_rows", result);
        }
コード例 #10
0
        public async Task ValueType_NotOnProperty_ReturnsNull()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            var result = reader.ValueType;

            // Assert

            Assert.Null(result);
        }
コード例 #11
0
        public async Task ReadObjectAsync_OnNumber_ReadsNumber()
        {
            // Arrange

            using var stream = new MemoryStream(
                      System.Text.Encoding.UTF8.GetBytes("{\"value\":0.105}"));

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act/Assert

            await reader.ReadToNextAttributeAsync();

            var value = await reader.ReadObjectAsync <double>();

            Assert.Equal(0.105, value);
        }
コード例 #12
0
        public async Task ValueType_OnProperty_ReturnsValue(string filename, Type expectedFirstProperty)
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(filename);

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            await reader.ReadToNextAttributeAsync();

            var result = reader.ValueType;

            // Assert

            Assert.Equal(expectedFirstProperty, result);
        }
コード例 #13
0
        public async Task ReadObjectAsync_OnDateTimeOffset_PreservesTimeZone()
        {
            // Note: this emulates a "SELECT RAW" query returning a DateTimeOffset value, and currently requires
            // that the serializer be set to DateParseHandling.DateTimeOffset. It will fail with DateParseHandling.DateTime.

            {
                using var stream = new MemoryStream(
                          System.Text.Encoding.UTF8.GetBytes("{\"value\":\"2021-06-21T10:16:56.9714243+10:00\"}"));

                var serializer = CreateDefaultJsonSerializer();
                serializer.DateParseHandling = DateParseHandling.DateTimeOffset;
                using var reader             = new DefaultJsonStreamReader(stream, serializer);
                Assert.True(await reader.InitializeAsync());


                await reader.ReadToNextAttributeAsync();

                var value = await reader.ReadObjectAsync <DateTimeOffset>();

                Assert.Equal(10, value.Offset.Hours);
            }
            {
                // check again, just in case the test was run in the +10 timezone and would have given a false positive.
                using var stream = new MemoryStream(
                          System.Text.Encoding.UTF8.GetBytes("{\"value\":\"2021-06-21T10:16:56.9714243-8:00\"}"));

                var serializer = CreateDefaultJsonSerializer();
                serializer.DateParseHandling = DateParseHandling.DateTimeOffset;
                using var reader             = new DefaultJsonStreamReader(stream, serializer);
                Assert.True(await reader.InitializeAsync());


                await reader.ReadToNextAttributeAsync();

                var value = await reader.ReadObjectAsync <DateTimeOffset>();

                Assert.Equal(-8, value.Offset.Hours);
            }
        }
コード例 #14
0
        public async Task ReadToNextAttributeAsync_AfterLast_ReturnsNull()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"404-view-notfound.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Null(result);
        }
コード例 #15
0
        public async Task ReadArrayAsync_ReturnsArray()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Views\200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());

            // Act

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            var result = await reader.ReadObjectsAsync <dynamic>().ToListAsync();

            // Assert

            Assert.Equal(4, result.Count);
            Assert.Equal("21st_amendment_brewery_cafe", (string)result[0]["id"]);
        }
コード例 #16
0
        public async Task ReadToNextAttributeAsync_NestedProperties_GetsPathAndDepth()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Query\query-200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());
            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            Assert.Equal("signature", await reader.ReadToNextAttributeAsync());

            // Act

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Equal("signature.*", result);
            Assert.Equal(2, reader.Depth);
        }
コード例 #17
0
        public async Task ReadToNextAttributeAsync_NestedArray_CorrectPathDuringIteration()
        {
            // Arrange

            using var stream = ResourceHelper.ReadResourceAsStream(@"Documents\Query\query-200-success.json");

            using var reader = new DefaultJsonStreamReader(stream, CreateDefaultJsonSerializer());

            Assert.True(await reader.InitializeAsync());
            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            await reader.ReadToNextAttributeAsync();

            Assert.Equal("results", await reader.ReadToNextAttributeAsync());

            string?x;

            while ((x = await reader.ReadToNextAttributeAsync()) != "results[0].updatedUnixMillis")
            {
                int i = 0;
                // Loop to get to the last attr in the first element
            }

            // Act

            var result = await reader.ReadToNextAttributeAsync();

            // Assert

            Assert.Equal("results[1].abv", result);
            Assert.Equal(3, reader.Depth);
        }