From f6e3032098255b06919084ad5f66ecbd51288e10 Mon Sep 17 00:00:00 2001 From: Tan Phat VO Date: Tue, 10 Dec 2024 19:46:09 +0700 Subject: [PATCH] ix the UTs --- .../WebServiceDataSourceItemFixture.cs | 38 +++++- .../WebServiceDataSourceFixture.cs | 117 ++++++++++++++++-- 2 files changed, 140 insertions(+), 15 deletions(-) diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/WebServiceDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/WebServiceDataSourceItemFixture.cs index 08c363e0..74cbd826 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/WebServiceDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/WebServiceDataSourceItemFixture.cs @@ -23,7 +23,6 @@ public void Constructor_SetsTitleAndDataSource_WhenConstructed() // Assert Assert.Equal(title, item.Title); - Assert.Equal(dataSource, item.DataSource); } [Theory] @@ -71,11 +70,44 @@ public void ToJsonString_CreatesFormattedJson_ForWebServiceDataSource() ""Id"": ""webServiceItemId"", ""Title"": ""Sales by Category"", ""Subtitle"": ""Excel2Json"", - ""DataSourceId"": ""webServiceId"", + ""DataSourceId"": ""__JSON"", ""HasTabularData"": true, ""HasAsset"": false, ""Properties"": {}, - ""Parameters"": {} + ""Parameters"": { + ""config"": { + ""iterationDepth"": 0, + ""columnsConfig"": [ + { + ""key"": ""CategoryID"", + ""type"": 1 + }, + { + ""key"": ""CategoryName"", + ""type"": 0 + }, + { + ""key"": ""ProductName"", + ""type"": 0 + }, + { + ""key"": ""ProductSales"", + ""type"": 1 + } + ] + } + }, + ""ResourceItem"": { + ""_type"": ""DataSourceItemType"", + ""Id"": ""webServiceItemId"", + ""Title"": ""DB Test"", + ""Subtitle"": ""Excel2Json"", + ""DataSourceId"": ""webServiceId"", + ""HasTabularData"": true, + ""HasAsset"": false, + ""Properties"": {}, + ""Parameters"": {} + } }"; var dataSource = new WebServiceDataSource() diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/WebServiceDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/WebServiceDataSourceFixture.cs index 75f62d1b..02930f28 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/WebServiceDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/WebServiceDataSourceFixture.cs @@ -80,30 +80,121 @@ public void ToJsonString_CreatesFormattedJson_ForWebServiceDataSource() var expectedJson = @" { ""_type"": ""DataSourceType"", - ""Id"": ""webServiceId"", + ""Id"": ""1821521e-0225-4768-80c2-28c89f55b256"", ""Provider"": ""WEBSERVICE"", - ""Description"": ""Web Data Source"", - ""Subtitle"": ""Web Data Source Subtitle"", + ""Description"": ""JSON DS"", + ""Subtitle"": ""JSON DS Subtitle"", + ""Properties"": {} + }"; + + var dataSourceItems = new WebServiceDataSourceItem("DB Test", new DataSource { Title = "JSON DS", Subtitle = "JSON DS Subtitle" }) + { + Id = "webServiceItemId", + Title = "Sales by Category", + Subtitle = "Excel2Json", + Url = "https://excel2json.io/api/share/6e0f06b3-72d3-4fec-7984-08da43f56bb9", + Fields = new List + { + new NumberField("CategoryID"), + new TextField("CategoryName"), + new TextField("ProductName"), + new NumberField("ProductSales"), + } + }; + + var document = new RdashDocument("My Dashboard"); + document.Visualizations.Add( + new ColumnChartVisualization("Test List", dataSourceItems) + .SetLabel("CategoryName") + .SetValue("ProductName") + ); + + var expectedJObject = JObject.Parse(expectedJson); + + // Act + RdashSerializer.SerializeObject(document); + var json = document.ToJsonString(); + var jObject = JObject.Parse(json); + var actualJObject = jObject["DataSources"].LastOrDefault(); + + // Assert + Assert.Equal(expectedJObject, actualJObject); + } + + [Fact] + public void ToJsonString_CreatesFormattedJsonUseCsv_ForWebServiceDataSource() + { + // Arrange + var expectedJson = @" + { + ""_type"": ""DataSourceType"", + ""Id"": ""a30dc863-47a4-4ea4-b9fb-9e8006281ce4"", + ""Provider"": ""WEBSERVICE"", + ""Description"": ""JSON DS"", + ""Subtitle"": ""JSON DS Subtitle"", ""Properties"": { - ""URL"": ""https://excel2json.io/api/share/6e0f06b3-72d3-4fec-7984-08da43f56bb9"", - ""UseAnonymousAuthentication"": true + ""Result-Type"": "".csv"" } }"; - var dataSource = new WebServiceDataSource() + var dataSourceItems = new WebServiceDataSourceItem("DB Test", new DataSource { Title = "JSON DS", Subtitle = "JSON DS Subtitle" }) { - Id = "webServiceId", - Title = "Web Data Source", - Subtitle = "Web Data Source Subtitle", + Id = "webServiceItemId", + Title = "Sales by Category", + Subtitle = "Excel2Json", Url = "https://excel2json.io/api/share/6e0f06b3-72d3-4fec-7984-08da43f56bb9", - UseAnonymousAuthentication = true, + Fields = new List + { + new NumberField("CategoryID"), + new TextField("CategoryName"), + new TextField("ProductName"), + new NumberField("ProductSales"), + } }; - var dataSourceItems = new WebServiceDataSourceItem("DB Test", dataSource) + dataSourceItems.UseCsv(); + + var document = new RdashDocument("My Dashboard"); + document.Visualizations.Add( + new ColumnChartVisualization("Test List", dataSourceItems) + .SetLabel("CategoryName") + .SetValue("ProductName") + ); + + var expectedJObject = JObject.Parse(expectedJson); + + // Act + RdashSerializer.SerializeObject(document); + var json = document.ToJsonString(); + var jObject = JObject.Parse(json); + var actualJObject = jObject["DataSources"].LastOrDefault(); + + // Assert + Assert.Equal(expectedJObject, actualJObject); + } + + [Fact] + public void ToJsonString_CreatesFormattedJsonUseExcel_ForWebServiceDataSource() + { + // Arrange + var expectedJson = @" + { + ""_type"": ""DataSourceType"", + ""Id"": ""97018983-2f13-415b-a38c-e6ff4fa3f123"", + ""Provider"": ""WEBSERVICE"", + ""Description"": ""JSON DS"", + ""Subtitle"": ""JSON DS Subtitle"", + ""Properties"": { + ""Result-Type"": "".xlsx"" + } + }"; + + var dataSourceItems = new WebServiceDataSourceItem("DB Test", new DataSource { Title = "JSON DS", Subtitle = "JSON DS Subtitle" }) { Id = "webServiceItemId", Title = "Sales by Category", Subtitle = "Excel2Json", + Url = "https://excel2json.io/api/share/6e0f06b3-72d3-4fec-7984-08da43f56bb9", Fields = new List { new NumberField("CategoryID"), @@ -113,6 +204,8 @@ public void ToJsonString_CreatesFormattedJson_ForWebServiceDataSource() } }; + dataSourceItems.UseExcel(); + var document = new RdashDocument("My Dashboard"); document.Visualizations.Add( new ColumnChartVisualization("Test List", dataSourceItems) @@ -126,7 +219,7 @@ public void ToJsonString_CreatesFormattedJson_ForWebServiceDataSource() RdashSerializer.SerializeObject(document); var json = document.ToJsonString(); var jObject = JObject.Parse(json); - var actualJObject = jObject["DataSources"].FirstOrDefault(); + var actualJObject = jObject["DataSources"].LastOrDefault(); // Assert Assert.Equal(expectedJObject, actualJObject);