Skip to content

Commit 37df33a

Browse files
committed
Merge remote-tracking branch 'origin/changjian-wang/improve_tests_for_cu_ga_sdk_tsp_client' into yslin/cu_ga_sdk_tsp_client
2 parents f6f4290 + 60d491c commit 37df33a

15 files changed

+2803
-235
lines changed

sdk/contentunderstanding/Azure.AI.ContentUnderstanding/tests/samples/Sample01_AnalyzeBinary.cs

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ public async Task AnalyzeBinaryAsync()
4545

4646
#region Assertion:ContentUnderstandingAnalyzeBinaryAsync
4747
Assert.IsTrue(File.Exists(filePath), $"Sample file not found at {filePath}");
48+
Assert.IsTrue(fileBytes.Length > 0, "File should not be empty");
49+
Assert.IsNotNull(binaryData, "Binary data should not be null");
4850
Assert.IsNotNull(operation, "Analysis operation should not be null");
51+
Assert.IsTrue(operation.HasCompleted, "Operation should be completed");
4952
Assert.IsNotNull(operation.GetRawResponse(), "Analysis operation should have a raw response");
50-
TestContext.WriteLine("✅ Analysis operation properties verified");
53+
Assert.IsTrue(operation.GetRawResponse().Status >= 200 && operation.GetRawResponse().Status < 300,
54+
$"Response status should be successful, but was {operation.GetRawResponse().Status}");
55+
Console.WriteLine("✅ Analysis operation properties verified");
5156
Assert.IsNotNull(result, "Analysis result should not be null");
57+
Assert.IsNotNull(result.Contents, "Result contents should not be null");
58+
Console.WriteLine($"✅ Analysis result contains {result.Contents?.Count ?? 0} content(s)");
5259
#endregion
5360

5461
#region Snippet:ContentUnderstandingExtractMarkdown
@@ -75,11 +82,16 @@ public async Task AnalyzeBinaryAsync()
7582
#region Assertion:ContentUnderstandingExtractMarkdown
7683
Assert.IsNotNull(result.Contents, "Result should contain contents");
7784
Assert.IsTrue(result.Contents!.Count > 0, "Result should have at least one content");
85+
Assert.AreEqual(1, result.Contents.Count, "PDF file should have exactly one content element");
7886
Assert.IsNotNull(content, "Content should not be null");
87+
Assert.IsInstanceOf<MediaContent>(content, "Content should be of type MediaContent");
7988
if (content is MediaContent mediaContent)
8089
{
8190
Assert.IsNotNull(mediaContent.Markdown, "Markdown content should not be null");
8291
Assert.IsTrue(mediaContent.Markdown.Length > 0, "Markdown content should not be empty");
92+
Assert.IsFalse(string.IsNullOrWhiteSpace(mediaContent.Markdown),
93+
"Markdown content should not be just whitespace");
94+
Console.WriteLine($"✅ Markdown content extracted successfully ({mediaContent.Markdown.Length} characters)");
8395
}
8496
#endregion
8597

@@ -118,31 +130,103 @@ public async Task AnalyzeBinaryAsync()
118130
#endregion
119131

120132
#region Assertion:ContentUnderstandingAccessDocumentProperties
133+
Assert.IsNotNull(content, "Content should not be null for document properties validation");
134+
121135
if (content is DocumentContent docContent)
122136
{
137+
// Validate MIME type
123138
Assert.IsNotNull(docContent.MimeType, "MIME type should not be null");
139+
Assert.IsFalse(string.IsNullOrWhiteSpace(docContent.MimeType), "MIME type should not be empty");
140+
Assert.AreEqual("application/pdf", docContent.MimeType, "MIME type should be application/pdf");
141+
Console.WriteLine($"✅ MIME type verified: {docContent.MimeType}");
142+
143+
// Validate page numbers
124144
Assert.IsTrue(docContent.StartPageNumber >= 1, "Start page should be >= 1");
125145
Assert.IsTrue(docContent.EndPageNumber >= docContent.StartPageNumber,
126146
"End page should be >= start page");
147+
int totalPages = docContent.EndPageNumber - docContent.StartPageNumber + 1;
148+
Assert.IsTrue(totalPages > 0, "Total pages should be positive");
149+
Console.WriteLine($"✅ Page range verified: {docContent.StartPageNumber} to {docContent.EndPageNumber} ({totalPages} pages)");
127150

151+
// Validate pages collection
128152
if (docContent.Pages != null && docContent.Pages.Count > 0)
129153
{
154+
Assert.IsTrue(docContent.Pages.Count > 0, "Pages collection should not be empty when not null");
155+
Assert.AreEqual(totalPages, docContent.Pages.Count,
156+
"Pages collection count should match calculated total pages");
157+
Console.WriteLine($"✅ Pages collection verified: {docContent.Pages.Count} pages");
158+
159+
// Track page numbers to ensure they're sequential and unique
160+
var pageNumbers = new System.Collections.Generic.HashSet<int>();
161+
130162
foreach (var page in docContent.Pages)
131163
{
164+
Assert.IsNotNull(page, "Page object should not be null");
132165
Assert.IsTrue(page.PageNumber >= 1, "Page number should be >= 1");
133-
Assert.IsTrue(page.Width > 0, "Page width should be > 0");
134-
Assert.IsTrue(page.Height > 0, "Page height should be > 0");
166+
Assert.IsTrue(page.PageNumber >= docContent.StartPageNumber &&
167+
page.PageNumber <= docContent.EndPageNumber,
168+
$"Page number {page.PageNumber} should be within document range [{docContent.StartPageNumber}, {docContent.EndPageNumber}]");
169+
Assert.IsTrue(page.Width > 0, $"Page {page.PageNumber} width should be > 0, but was {page.Width}");
170+
Assert.IsTrue(page.Height > 0, $"Page {page.PageNumber} height should be > 0, but was {page.Height}");
171+
172+
// Ensure page numbers are unique
173+
Assert.IsTrue(pageNumbers.Add(page.PageNumber),
174+
$"Page number {page.PageNumber} appears multiple times");
175+
176+
Console.WriteLine($" ✅ Page {page.PageNumber}: {page.Width} x {page.Height} {docContent.Unit?.ToString() ?? "units"}");
135177
}
136178
}
179+
else
180+
{
181+
Console.WriteLine("⚠️ No pages collection available in document content");
182+
}
137183

184+
// Validate tables collection
138185
if (docContent.Tables != null && docContent.Tables.Count > 0)
139186
{
187+
Assert.IsTrue(docContent.Tables.Count > 0, "Tables collection should not be empty when not null");
188+
Console.WriteLine($"✅ Tables collection verified: {docContent.Tables.Count} tables");
189+
190+
int tableCounter = 1;
140191
foreach (var table in docContent.Tables)
141192
{
142-
Assert.IsTrue(table.RowCount > 0, "Table should have at least 1 row");
143-
Assert.IsTrue(table.ColumnCount > 0, "Table should have at least 1 column");
193+
Assert.IsNotNull(table, $"Table {tableCounter} should not be null");
194+
Assert.IsTrue(table.RowCount > 0, $"Table {tableCounter} should have at least 1 row, but had {table.RowCount}");
195+
Assert.IsTrue(table.ColumnCount > 0, $"Table {tableCounter} should have at least 1 column, but had {table.ColumnCount}");
196+
197+
// Validate table cells if available
198+
if (table.Cells != null)
199+
{
200+
Assert.IsTrue(table.Cells.Count > 0, $"Table {tableCounter} cells collection should not be empty when not null");
201+
202+
foreach (var cell in table.Cells)
203+
{
204+
Assert.IsNotNull(cell, "Table cell should not be null");
205+
Assert.IsTrue(cell.RowIndex >= 0 && cell.RowIndex < table.RowCount,
206+
$"Cell row index {cell.RowIndex} should be within table row count {table.RowCount}");
207+
Assert.IsTrue(cell.ColumnIndex >= 0 && cell.ColumnIndex < table.ColumnCount,
208+
$"Cell column index {cell.ColumnIndex} should be within table column count {table.ColumnCount}");
209+
Assert.IsTrue(cell.RowSpan >= 1, $"Cell row span should be >= 1, but was {cell.RowSpan}");
210+
Assert.IsTrue(cell.ColumnSpan >= 1, $"Cell column span should be >= 1, but was {cell.ColumnSpan}");
211+
}
212+
}
213+
214+
Console.WriteLine($" ✅ Table {tableCounter}: {table.RowCount} rows x {table.ColumnCount} columns" +
215+
(table.Cells != null ? $" ({table.Cells.Count} cells)" : ""));
216+
tableCounter++;
144217
}
145218
}
219+
else
220+
{
221+
Console.WriteLine("ℹ️ No tables found in document content");
222+
}
223+
224+
Console.WriteLine("✅ All document properties validated successfully");
225+
}
226+
else
227+
{
228+
Console.WriteLine("ℹ️ Content is not DocumentContent type, skipping document-specific validations");
229+
Assert.Warn("Expected DocumentContent but got " + content?.GetType().Name);
146230
}
147231
#endregion
148232
}

sdk/contentunderstanding/Azure.AI.ContentUnderstanding/tests/samples/Sample02_AnalyzeUrl.cs

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,32 @@ public async Task AnalyzeUrlAsync()
3838
AnalyzeResult result = operation.Value;
3939
#endregion
4040

41+
#region Assertion:ContentUnderstandingAnalyzeUrlAsync
42+
Assert.IsNotNull(uriSource, "URI source should not be null");
43+
Assert.IsTrue(uriSource.IsAbsoluteUri, "URI should be absolute");
44+
Assert.IsNotNull(operation, "Analysis operation should not be null");
45+
Assert.IsTrue(operation.HasCompleted, "Operation should be completed");
46+
Assert.IsTrue(operation.HasValue, "Operation should have a value");
47+
Assert.IsNotNull(operation.GetRawResponse(), "Analysis operation should have a raw response");
48+
Assert.IsTrue(operation.GetRawResponse().Status >= 200 && operation.GetRawResponse().Status < 300,
49+
$"Response status should be successful, but was {operation.GetRawResponse().Status}");
50+
Console.WriteLine("✅ Analysis operation properties verified");
51+
Assert.IsNotNull(result, "Analysis result should not be null");
52+
Assert.IsNotNull(result.Contents, "Result contents should not be null");
53+
Console.WriteLine($"✅ Analysis result contains {result.Contents?.Count ?? 0} content(s)");
54+
#endregion
55+
56+
#region Snippet:ContentUnderstandingExtractMarkdownFromUrl
4157
// A PDF file has only one content element even if it contains multiple pages
42-
MediaContent? content = null;
58+
MediaContent? content = null;
4359
if (result.Contents == null || result.Contents.Count == 0)
4460
{
4561
Console.WriteLine("(No content returned from analysis)");
4662
}
4763
else
4864
{
4965
content = result.Contents.First();
50-
if (!string.IsNullOrEmpty(content.Markdown))
66+
if (! string.IsNullOrEmpty(content.Markdown))
5167
{
5268
Console.WriteLine(content.Markdown);
5369
}
@@ -56,22 +72,28 @@ public async Task AnalyzeUrlAsync()
5672
Console.WriteLine("(No markdown content available)");
5773
}
5874
}
75+
#endregion
5976

6077
#region Assertion:ContentUnderstandingExtractMarkdown
6178
Assert.IsNotNull(result.Contents, "Result should contain contents");
6279
Assert.IsTrue(result.Contents!.Count > 0, "Result should have at least one content");
80+
Assert.AreEqual(1, result.Contents.Count, "PDF file should have exactly one content element");
6381
Assert.IsNotNull(content, "Content should not be null");
82+
Assert.IsInstanceOf<MediaContent>(content, "Content should be of type MediaContent");
6483
if (content is MediaContent mediaContent)
6584
{
6685
Assert.IsNotNull(mediaContent.Markdown, "Markdown content should not be null");
6786
Assert.IsTrue(mediaContent.Markdown.Length > 0, "Markdown content should not be empty");
87+
Assert.IsFalse(string.IsNullOrWhiteSpace(mediaContent.Markdown),
88+
"Markdown content should not be just whitespace");
89+
Console.WriteLine($"✅ Markdown content extracted successfully ({mediaContent.Markdown.Length} characters)");
6890
}
6991
#endregion
7092

7193
// Check if this is document content to access document-specific properties
7294
if (content is DocumentContent documentContent)
7395
{
74-
Console.WriteLine($"Document type: {documentContent.MimeType ?? "(unknown)"}");
96+
Console.WriteLine($"Document type: {documentContent.MimeType ?? "(unknown)"}");
7597
Console.WriteLine($"Start page: {documentContent.StartPageNumber}");
7698
Console.WriteLine($"End page: {documentContent.EndPageNumber}");
7799
Console.WriteLine($"Total pages: {documentContent.EndPageNumber - documentContent.StartPageNumber + 1}");
@@ -100,34 +122,104 @@ public async Task AnalyzeUrlAsync()
100122
}
101123
}
102124

103-
#region Assertion:ContentUnderstandingAccessDocumentProperties
125+
#region Assertion:ContentUnderstandingAccessDocumentPropertiesFromUrl
126+
Assert.IsNotNull(content, "Content should not be null for document properties validation");
127+
104128
if (content is DocumentContent docContent)
105129
{
130+
// Validate MIME type
106131
Assert.IsNotNull(docContent.MimeType, "MIME type should not be null");
132+
Assert.IsFalse(string.IsNullOrWhiteSpace(docContent.MimeType), "MIME type should not be empty");
133+
Assert.AreEqual("application/pdf", docContent.MimeType, "MIME type should be application/pdf");
134+
Console.WriteLine($"✅ MIME type verified: {docContent.MimeType}");
135+
136+
// Validate page numbers
107137
Assert.IsTrue(docContent.StartPageNumber >= 1, "Start page should be >= 1");
108138
Assert.IsTrue(docContent.EndPageNumber >= docContent.StartPageNumber,
109139
"End page should be >= start page");
140+
int totalPages = docContent.EndPageNumber - docContent.StartPageNumber + 1;
141+
Assert.IsTrue(totalPages > 0, "Total pages should be positive");
142+
Console.WriteLine($"✅ Page range verified: {docContent.StartPageNumber} to {docContent.EndPageNumber} ({totalPages} pages)");
110143

144+
// Validate pages collection
111145
if (docContent.Pages != null && docContent.Pages.Count > 0)
112146
{
147+
Assert.IsTrue(docContent.Pages.Count > 0, "Pages collection should not be empty when not null");
148+
Assert.AreEqual(totalPages, docContent.Pages.Count,
149+
"Pages collection count should match calculated total pages");
150+
Console.WriteLine($"✅ Pages collection verified: {docContent.Pages.Count} pages");
151+
152+
// Track page numbers to ensure they're sequential and unique
153+
var pageNumbers = new System.Collections.Generic.HashSet<int>();
154+
113155
foreach (var page in docContent.Pages)
114156
{
157+
Assert.IsNotNull(page, "Page object should not be null");
115158
Assert.IsTrue(page.PageNumber >= 1, "Page number should be >= 1");
116-
Assert.IsTrue(page.Width > 0, "Page width should be > 0");
117-
Assert.IsTrue(page.Height > 0, "Page height should be > 0");
159+
Assert.IsTrue(page.PageNumber >= docContent.StartPageNumber &&
160+
page.PageNumber <= docContent.EndPageNumber,
161+
$"Page number {page.PageNumber} should be within document range [{docContent.StartPageNumber}, {docContent.EndPageNumber}]");
162+
Assert.IsTrue(page.Width > 0, $"Page {page.PageNumber} width should be > 0, but was {page.Width}");
163+
Assert.IsTrue(page.Height > 0, $"Page {page.PageNumber} height should be > 0, but was {page.Height}");
164+
165+
// Ensure page numbers are unique
166+
Assert.IsTrue(pageNumbers.Add(page.PageNumber),
167+
$"Page number {page.PageNumber} appears multiple times");
168+
169+
Console.WriteLine($" ✅ Page {page.PageNumber}: {page.Width} x {page.Height} {docContent.Unit?.ToString() ?? "units"}");
118170
}
119171
}
172+
else
173+
{
174+
Console.WriteLine("⚠️ No pages collection available in document content");
175+
}
120176

177+
// Validate tables collection
121178
if (docContent.Tables != null && docContent.Tables.Count > 0)
122179
{
180+
Assert.IsTrue(docContent.Tables.Count > 0, "Tables collection should not be empty when not null");
181+
Console.WriteLine($"✅ Tables collection verified: {docContent.Tables.Count} tables");
182+
183+
int tableCounter = 1;
123184
foreach (var table in docContent.Tables)
124185
{
125-
Assert.IsTrue(table.RowCount > 0, "Table should have at least 1 row");
126-
Assert.IsTrue(table.ColumnCount > 0, "Table should have at least 1 column");
186+
Assert.IsNotNull(table, $"Table {tableCounter} should not be null");
187+
Assert.IsTrue(table.RowCount > 0, $"Table {tableCounter} should have at least 1 row, but had {table.RowCount}");
188+
Assert.IsTrue(table.ColumnCount > 0, $"Table {tableCounter} should have at least 1 column, but had {table.ColumnCount}");
189+
190+
// Validate table cells if available
191+
if (table.Cells != null)
192+
{
193+
Assert.IsTrue(table.Cells.Count > 0, $"Table {tableCounter} cells collection should not be empty when not null");
194+
195+
foreach (var cell in table.Cells)
196+
{
197+
Assert.IsNotNull(cell, "Table cell should not be null");
198+
Assert.IsTrue(cell.RowIndex >= 0 && cell.RowIndex < table.RowCount,
199+
$"Cell row index {cell.RowIndex} should be within table row count {table.RowCount}");
200+
Assert.IsTrue(cell.ColumnIndex >= 0 && cell.ColumnIndex < table.ColumnCount,
201+
$"Cell column index {cell.ColumnIndex} should be within table column count {table.ColumnCount}");
202+
}
203+
}
204+
205+
Console.WriteLine($" ✅ Table {tableCounter}: {table.RowCount} rows x {table.ColumnCount} columns" +
206+
(table.Cells != null ? $" ({table.Cells.Count} cells)" : ""));
207+
tableCounter++;
127208
}
128209
}
210+
else
211+
{
212+
Console.WriteLine("⚠️ No tables found in document content");
213+
}
214+
215+
Console.WriteLine("✅ All document properties validated successfully");
216+
}
217+
else
218+
{
219+
Console.WriteLine("⚠️ Content is not DocumentContent type, skipping document-specific validations");
220+
Assert.Warn("Expected DocumentContent but got " + content?.GetType().Name);
129221
}
130222
#endregion
131223
}
132224
}
133-
}
225+
}

0 commit comments

Comments
 (0)