-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamingAnalyticsResultTests.cs
More file actions
190 lines (154 loc) · 6.15 KB
/
StreamingAnalyticsResultTests.cs
File metadata and controls
190 lines (154 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Text.Json;
using Couchbase.AnalyticsClient.Internal.Results;
using Couchbase.Core.Json;
using Moq;
using Xunit;
using Xunit.Abstractions;
namespace Couchbase.AnalyticsClient.UnitTests.Internal;
public class StreamingAnalyticsResultTests
{
private readonly ITestOutputHelper _output;
public StreamingAnalyticsResultTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task StreamingAnalyticsResult_DeserializesCorrectly()
{
var json = File.ReadAllBytes("JsonDocuments/analyticsResponse.json");
var stream = new MemoryStream(json);
var analyticsResult = new StreamingAnalyticsResult(stream, new StjJsonDeserializer(), new Mock<IDisposable>().Object);
await analyticsResult.InitializeAsync(CancellationToken.None);
var airlines = await analyticsResult.ToListAsync(CancellationToken.None);
Assert.NotNull(airlines);
Assert.NotEmpty(airlines);
}
public class Root
{
public Airline? airline { get; set; }
}
public class Airline
{
public int id { get; set; }
public string? type { get; set; }
public string? name { get; set; }
public string? iata { get; set; }
public string? icao { get; set; }
public string? callsign { get; set; }
public string? country { get; set; }
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
[Fact]
public void Constructor_InitializesCorrectly()
{
// Arrange
var mockStream = new MemoryStream();
var mockDisposable = new Mock<IDeserializer>();
// Act
var result = new StreamingAnalyticsResult(mockStream, mockDisposable.Object);
// Assert
Assert.NotNull(result);
}
[Fact]
public async Task InitializeAsync_CompletesSuccessfully()
{
// Arrange
var mockStream = new MemoryStream();
var mockSerializer = new Mock<IDeserializer>();
mockSerializer.Setup(x => x.CreateJsonStreamReader(It.IsAny<Stream>(),
It.IsAny<CancellationToken>()))
.Returns(new Mock<IJsonStreamReader>().Object);
var result = new StreamingAnalyticsResult(mockStream, mockSerializer.Object);
// Act
await result.InitializeAsync();
// Assert
Assert.True(true); // No exception should be thrown
}
[Fact]
public async Task EnumerateRows_SecondEnumeration_ThrowsInvalidOperationException()
{
// Arrange
var json = File.ReadAllBytes("JsonDocuments/analyticsResponse.json");
var stream = new MemoryStream(json);
var result = new StreamingAnalyticsResult(stream, new StjJsonDeserializer());
await result.InitializeAsync(CancellationToken.None);
// Act — first enumeration should succeed
var rows = await result.ToListAsync(CancellationToken.None);
Assert.NotEmpty(rows);
// Assert — second enumeration should throw
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await foreach (var _ in result) { }
});
}
[Fact]
public async Task DisposeAsync_DisposesOwnedResources()
{
// Arrange
var stream = new MemoryStream();
var ownedResource = new Mock<IDisposable>();
var result = new StreamingAnalyticsResult(stream, new StjJsonDeserializer(), ownedResource.Object);
// Act
await result.DisposeAsync();
// Assert
ownedResource.Verify(r => r.Dispose(), Times.Once);
Assert.False(stream.CanRead); // Stream should be disposed
}
[Fact]
public async Task DisposeAsync_CalledTwice_DisposesOnlyOnce()
{
// Arrange
var stream = new MemoryStream();
var ownedResource = new Mock<IDisposable>();
var result = new StreamingAnalyticsResult(stream, new StjJsonDeserializer(), ownedResource.Object);
// Act
await result.DisposeAsync();
await result.DisposeAsync();
// Assert
ownedResource.Verify(r => r.Dispose(), Times.Once);
}
[Fact]
public async Task StreamingAnalyticsResult_DdlResponse_NoResultsArray_InitializesSuccessfully()
{
// Arrange - DDL responses (CREATE DATABASE, DROP SCOPE, etc.) don't have a "results" array
var json = File.ReadAllBytes("JsonDocuments/ddlResponse.json");
var stream = new MemoryStream(json);
var analyticsResult = new StreamingAnalyticsResult(stream, new StjJsonDeserializer());
// Act
await analyticsResult.InitializeAsync(CancellationToken.None);
// Assert - should initialize without throwing
Assert.NotNull(analyticsResult.MetaData);
Assert.Equal("8c090478-269b-4051-b660-c2a5ae2c4aaa", analyticsResult.MetaData.RequestId);
}
[Fact]
public async Task StreamingAnalyticsResult_DdlResponse_EnumerationReturnsEmpty()
{
// Arrange - DDL responses have no results to enumerate
var json = File.ReadAllBytes("JsonDocuments/ddlResponse.json");
var stream = new MemoryStream(json);
var analyticsResult = new StreamingAnalyticsResult(stream, new StjJsonDeserializer());
await analyticsResult.InitializeAsync(CancellationToken.None);
// Act
var rows = await analyticsResult.ToListAsync(CancellationToken.None);
// Assert - should return empty list, not throw
Assert.NotNull(rows);
Assert.Empty(rows);
}
[Fact]
public async Task StreamingAnalyticsResult_DdlResponse_MetricsAvailable()
{
// Arrange
var json = File.ReadAllBytes("JsonDocuments/ddlResponse.json");
var stream = new MemoryStream(json);
var analyticsResult = new StreamingAnalyticsResult(stream, new StjJsonDeserializer());
await analyticsResult.InitializeAsync(CancellationToken.None);
// Act - enumerate to completion (empty)
await analyticsResult.ToListAsync(CancellationToken.None);
// Assert - metrics should still be available
Assert.NotNull(analyticsResult.MetaData.Metrics);
Assert.Equal(0, analyticsResult.MetaData.Metrics.ResultCount);
}
}