|
| 1 | +using System.Text.Json; |
| 2 | +using NUnit.Framework; |
| 3 | +using Square; |
| 4 | + |
| 5 | +namespace Square.Test.Integration; |
| 6 | + |
| 7 | +// The Reporting API is a beta, bespoke offering served ONLY from production |
| 8 | +// (connect.squareup.com/reporting) — it is not routed on sandbox (returns 404 there). |
| 9 | +// Validating it live therefore needs a production, reporting-provisioned access token, |
| 10 | +// supplied via TEST_SQUARE_REPORTING (distinct from the sandbox TEST_SQUARE_TOKEN used by |
| 11 | +// the rest of the integration suite). This suite is gated behind TEST_SQUARE_REPORTING and |
| 12 | +// skips by default — keeping CI green. The endpoints are read-only (schema discovery + |
| 13 | +// queries). The polling *logic* is covered without a live account in |
| 14 | +// Unit/ReportingHelperTests.cs. |
| 15 | +// |
| 16 | +// Run it against a real prod account: |
| 17 | +// TEST_SQUARE_REPORTING=<prod-reporting-token> \ |
| 18 | +// dotnet test --filter FullyQualifiedName~Integration.ReportingTests |
| 19 | +// # override the host with TEST_SQUARE_BASE_URL=<url> if reporting moves. |
| 20 | +[TestFixture] |
| 21 | +public class ReportingTests |
| 22 | +{ |
| 23 | + private SquareClient _client = null!; |
| 24 | + |
| 25 | + [SetUp] |
| 26 | + public void SetUp() |
| 27 | + { |
| 28 | + var token = Environment.GetEnvironmentVariable("TEST_SQUARE_REPORTING"); |
| 29 | + if (string.IsNullOrEmpty(token)) |
| 30 | + { |
| 31 | + Assert.Ignore( |
| 32 | + "Set TEST_SQUARE_REPORTING to a production, reporting-provisioned access token to run the reporting integration suite." |
| 33 | + ); |
| 34 | + } |
| 35 | + // Reporting only exists on production; allow overriding the host via TEST_SQUARE_BASE_URL. |
| 36 | + var baseUrl = |
| 37 | + Environment.GetEnvironmentVariable("TEST_SQUARE_BASE_URL") ?? SquareEnvironment.Production; |
| 38 | + TestContext.Out.WriteLine( |
| 39 | + $"[reporting] base URL: {baseUrl} -> {baseUrl}/reporting/v1/{{meta,load}}" |
| 40 | + ); |
| 41 | + _client = new SquareClient(token!, new ClientOptions { BaseUrl = baseUrl }); |
| 42 | + } |
| 43 | + |
| 44 | + // Resolves the first queryable measure from the live schema, e.g. "Orders.count". |
| 45 | + private async Task<string> FirstMeasureNameAsync() |
| 46 | + { |
| 47 | + var metadata = await _client.Reporting.GetMetadataAsync(); |
| 48 | + var measure = metadata.Cubes?.FirstOrDefault()?.Measures.FirstOrDefault()?.Name; |
| 49 | + if (string.IsNullOrEmpty(measure)) |
| 50 | + { |
| 51 | + throw new Exception( |
| 52 | + "No cubes/measures are available on the reporting schema for this account." |
| 53 | + ); |
| 54 | + } |
| 55 | + return measure!; |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public async Task GetMetadataReturnsTheQueryableSchema() |
| 60 | + { |
| 61 | + var metadata = await _client.Reporting.GetMetadataAsync(); |
| 62 | + |
| 63 | + Assert.That(metadata.Cubes, Is.Not.Null); |
| 64 | + Assert.That(metadata.Cubes, Is.Not.Empty); |
| 65 | + |
| 66 | + // Surface the live schema so a developer can see what is queryable. |
| 67 | + var summary = (metadata.Cubes ?? new List<Cube>()) |
| 68 | + .Take(5) |
| 69 | + .Select(cube => new |
| 70 | + { |
| 71 | + cube = cube.Name, |
| 72 | + measures = cube.Measures.Take(5).Select(measure => measure.Name).ToList(), |
| 73 | + }); |
| 74 | + TestContext.Out.WriteLine( |
| 75 | + "Reporting schema (first 5 cubes): " |
| 76 | + + JsonSerializer.Serialize(summary, new JsonSerializerOptions { WriteIndented = true }) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public async Task LoadReturnsResultsOrTheContinueWaitSentinel() |
| 82 | + { |
| 83 | + var measure = await FirstMeasureNameAsync(); |
| 84 | + var response = await _client.Reporting.LoadAsync( |
| 85 | + new LoadRequest { Query = new Query { Measures = new List<string> { measure } } } |
| 86 | + ); |
| 87 | + |
| 88 | + if (response.AdditionalProperties.TryGetValue("error", out var sentinel)) |
| 89 | + { |
| 90 | + // Documented async behavior: a still-processing query comes back as HTTP 200 |
| 91 | + // with { "error": "Continue wait" } instead of results. |
| 92 | + Assert.That(sentinel.GetString(), Is.EqualTo("Continue wait")); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + // Flat LoadResponse: a resolved query carries its payload on `Data`. |
| 97 | + Assert.That(response.Data, Is.Not.Null); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public async Task LoadAndWaitResolvesAQueryEndToEndWithoutSurfacingContinueWait() |
| 103 | + { |
| 104 | + var measure = await FirstMeasureNameAsync(); |
| 105 | + |
| 106 | + var response = await _client.Reporting.LoadAndWaitAsync( |
| 107 | + new LoadRequest { Query = new Query { Measures = new List<string> { measure } } }, |
| 108 | + new LoadAndWaitOptions |
| 109 | + { |
| 110 | + MaxAttempts = 20, |
| 111 | + InitialDelay = TimeSpan.FromSeconds(2), |
| 112 | + MaxDelay = TimeSpan.FromSeconds(20), |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + // The polling helper must never hand back the raw "Continue wait" sentinel. |
| 117 | + Assert.That(response.AdditionalProperties.ContainsKey("error"), Is.False); |
| 118 | + // Flat LoadResponse: the resolved query carries its payload on `Data`. |
| 119 | + Assert.That(response.Data, Is.Not.Null); |
| 120 | + } |
| 121 | +} |
0 commit comments