From 0bf6683872db3bd1568937e3b329dac18b40b279 Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Thu, 16 May 2024 10:32:51 -0600 Subject: [PATCH] refactored parameter order for DSI factory and the DSI classes --- e2e/Sandbox/Factories/CustomDashboard.cs | 35 --------- .../Factories/RestDataSourceDashboards.cs | 6 +- e2e/Sandbox/Helpers/DataSourceFactory.cs | 8 +- .../RdashDocumentValidatorFixture.cs | 6 +- .../Data/DataSourceItemFactoryFixture.cs | 73 +++++++++++++++++++ .../Data/RestDataSourceItemFixture.cs | 4 +- .../RdashDocumentFixture.cs | 4 +- src/Reveal.Sdk.Dom/Data/DataSourceItem.cs | 2 +- .../Data/DataSourceItemFactory.cs | 31 +++++--- .../Data/Interfaces/IDataSourceItemFactory.cs | 13 ++++ .../Data/MicrosoftSqlServerDataSourceItem.cs | 6 +- src/Reveal.Sdk.Dom/Data/RestDataSourceItem.cs | 6 +- 12 files changed, 127 insertions(+), 67 deletions(-) create mode 100644 src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFactoryFixture.cs create mode 100644 src/Reveal.Sdk.Dom/Data/Interfaces/IDataSourceItemFactory.cs diff --git a/e2e/Sandbox/Factories/CustomDashboard.cs b/e2e/Sandbox/Factories/CustomDashboard.cs index 7cb31e7e..d1429d66 100644 --- a/e2e/Sandbox/Factories/CustomDashboard.cs +++ b/e2e/Sandbox/Factories/CustomDashboard.cs @@ -2,7 +2,6 @@ using Reveal.Sdk.Dom.Data; using Reveal.Sdk.Dom.Visualizations; using Sandbox.Helpers; -using DataSourceFactory = Sandbox.Helpers.DataSourceFactory; namespace Sandbox.Factories { @@ -12,15 +11,6 @@ internal static RdashDocument CreateDashboard() { var factory = new DataSourceItemFactory(); - //var excelDataSourceItem = factory.Create(DataSourceType.REST, "Marketing Sheet") - // .SetSubtitle("Excel Data Source Item") - // .SetFields(DataSourceFactory.GetMarketingDataSourceFields()) - // .As() - // .Uri("http://dl.infragistics.com/reportplus/reveal/samples/Samples.xlsx") - // .IsAnonymous(true) - // .UseExcel("Marketing") - // .Build(); - var excelDataSourceItem = new RestDataSourceItem("Marketing Sheet") { Subtitle = "Excel Data Source Item", @@ -30,15 +20,6 @@ internal static RdashDocument CreateDashboard() }; excelDataSourceItem.UseExcel("Marketing"); - //var csvDataSourceItem = factory.Create(DataSourceType.REST, "Illinois School Info") - // .SetSubtitle("CSV Data Source Item") - // .SetFields(DataSourceFactory.GetCsvDataSourceFields()) - // .As() - // .Uri("https://query.data.world/s/y32gtgblzpemyyvtig47dz7tedgkto") - // .IsAnonymous(true) - // .UseCsv() - // .Build(); - var csvDataSourceItem = new RestDataSourceItem("Illinois School Info") { Subtitle = "CSV Data Source Item", @@ -48,14 +29,6 @@ internal static RdashDocument CreateDashboard() }; csvDataSourceItem.UseCsv(); - //var financialDataSourceItem = factory.Create(DataSourceType.REST, "OHLC") - // .SetSubtitle("Financial Data Source Item") - // .SetFields(DataSourceFactory.GetOHLCDataSourceFields()) - // .As() - // .Uri("https://excel2json.io/api/share/8bb2cd78-1b87-4142-00a2-08da188ec9ab") - // .IsAnonymous(true) - // .Build(); - var financialDataSourceItem = new RestDataSourceItem("OHLC") { Subtitle = "Financial Data Source Item", @@ -64,14 +37,6 @@ internal static RdashDocument CreateDashboard() Fields = DataSourceFactory.GetOHLCDataSourceFields(), }; - //var revenueDataSourceItem = factory.Create(DataSourceType.REST, "Revenue") - // .SetSubtitle("Choropleth Data Source Item") - // .SetFields(DataSourceFactory.GetRevenueDataSourceFields()) - // .As() - // .Uri("https://excel2json.io/api/share/818e7b9a-f463-4565-435d-08da496bf5f2") - // .IsAnonymous(true) - // .Build(); - var revenueDataSourceItem = new RestDataSourceItem("Revenue") { Subtitle = "Choropleth Data Source Item", diff --git a/e2e/Sandbox/Factories/RestDataSourceDashboards.cs b/e2e/Sandbox/Factories/RestDataSourceDashboards.cs index 032b9bf4..c31cb791 100644 --- a/e2e/Sandbox/Factories/RestDataSourceDashboards.cs +++ b/e2e/Sandbox/Factories/RestDataSourceDashboards.cs @@ -12,7 +12,7 @@ internal static RdashDocument CreateDashboard() var document = new RdashDocument("My Dashboard"); //json - default - var jsonDataSourceItem = new RestDataSourceItem(new DataSource { Title = "JSON DS", Subtitle = "JSON DS Subtitle" },"Sales by Category") + var jsonDataSourceItem = new RestDataSourceItem("Sales by Category", new DataSource { Title = "JSON DS", Subtitle = "JSON DS Subtitle" }) { Id = "TESTING", Subtitle = "JSON Data Source Item", @@ -25,7 +25,7 @@ internal static RdashDocument CreateDashboard() .SetLabel("CategoryName").SetValue("ProductSales")); //excel - var excelDataSourceItem = new RestDataSourceItem(new DataSource { Title = "Excel DS", Subtitle = "Excel DS Subtitle" }, "Marketing") + RestDataSourceItem excelDataSourceItem = new RestDataSourceItem("Marketing", new DataSource { Title = "Excel DS", Subtitle = "Excel DS Subtitle" }) { Subtitle = "Excel Data Source Item", Uri = "http://dl.infragistics.com/reportplus/reveal/samples/Samples.xlsx", @@ -38,7 +38,7 @@ internal static RdashDocument CreateDashboard() .SetLabel("Territory").SetValue("Conversions")); //csv - var csvDataSourceItem = new RestDataSourceItem(new DataSource { Title = "CSV DS", Subtitle = "CSV DS Subtitle" }, "Illinois School Info") + var csvDataSourceItem = new RestDataSourceItem("Illinois School Info", new DataSource { Title = "CSV DS", Subtitle = "CSV DS Subtitle" }) { Subtitle = "CSV Data Source Item", Uri = "https://query.data.world/s/y32gtgblzpemyyvtig47dz7tedgkto", diff --git a/e2e/Sandbox/Helpers/DataSourceFactory.cs b/e2e/Sandbox/Helpers/DataSourceFactory.cs index a3afc97d..55d22199 100644 --- a/e2e/Sandbox/Helpers/DataSourceFactory.cs +++ b/e2e/Sandbox/Helpers/DataSourceFactory.cs @@ -18,7 +18,7 @@ internal class DataSourceFactory internal static DataSourceItem GetMarketingDataSourceItem() { - var excelDataSourceItem = new RestDataSourceItem(_excelDataSource, "Marketing") + var excelDataSourceItem = new RestDataSourceItem("Marketing", _excelDataSource) { Subtitle = "Excel Data Source Item", Uri = _restExcelUri, @@ -32,7 +32,7 @@ internal static DataSourceItem GetMarketingDataSourceItem() internal static DataSourceItem GetHealthcareDataSourceItem() { - var excelDataSourceItem = new RestDataSourceItem(_excelDataSource, "Healthcare") + var excelDataSourceItem = new RestDataSourceItem("Healthcare", _excelDataSource) { Subtitle = "Excel Data Source Item", Uri = _restExcelUri, @@ -46,7 +46,7 @@ internal static DataSourceItem GetHealthcareDataSourceItem() internal static DataSourceItem GetManufacturingDataSourceItem() { - var excelDataSourceItem = new RestDataSourceItem(_excelDataSource, "Manufacturing") + var excelDataSourceItem = new RestDataSourceItem("Manufacturing", _excelDataSource) { Subtitle = "Excel Data Source Item", Uri = _restExcelUri, @@ -60,7 +60,7 @@ internal static DataSourceItem GetManufacturingDataSourceItem() internal static DataSourceItem GetSalesDataSourceItem() { - var excelDataSourceItem = new RestDataSourceItem(_excelDataSource, "Sales") + var excelDataSourceItem = new RestDataSourceItem("Sales", _excelDataSource) { Subtitle = "Excel Data Source Item", Uri = _restExcelUri, diff --git a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs index b5cfa58c..acf4713a 100644 --- a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs @@ -12,7 +12,7 @@ public class RdashDocumentValidatorFixture [Fact] public void DataSources_AddedToRdashDocument() { - var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "").SetFields(new List() { null }); + var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "", "").SetFields(new List() { null }); var document = new RdashDocument(); document.Visualizations.Add(new KpiTimeVisualization(dataSourceItem)); @@ -27,7 +27,7 @@ public void DataSources_AddedToRdashDocument() [Fact] public void DataSources_FromVisualizationsAreNotDuplicated() { - var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "").SetFields(new List() { null }); + var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "", "").SetFields(new List() { null }); var document = new RdashDocument(); document.Visualizations.Add(new KpiTimeVisualization(dataSourceItem)); @@ -47,7 +47,7 @@ public void DataSources_FromVisualizationsAreNotDuplicated() [Fact] public void DataSources_FromVisualizations_AndDataSources_AreNotDuplicated() { - var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "").SetFields(new List() { null }); + var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "", "").SetFields(new List() { null }); var dataSource = dataSourceItem.DataSource; var document = new RdashDocument(); diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFactoryFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFactoryFixture.cs new file mode 100644 index 00000000..3741fe1c --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFactoryFixture.cs @@ -0,0 +1,73 @@ +using Reveal.Sdk.Dom.Data; +using System; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Data +{ + public class DataSourceItemFactoryFixture + { + [Fact] + public void Create_ShouldReturnDataSourceItem_WithDefaultDataSource() + { + // Arrange + var factory = new DataSourceItemFactory(); + var type = DataSourceType.MicrosoftSqlServer; + var id = "1"; + var title = "Test Title"; + + // Act + var result = factory.Create(type, id, title); + + // Assert + Assert.NotNull(result); + Assert.IsType(result); + Assert.Equal(id, result.Id); + Assert.Equal(title, result.Title); + Assert.Null(result.Subtitle); + Assert.NotNull(result.DataSource); + Assert.IsType(result.DataSource); + } + + [Fact] + public void Create_ShouldReturnDataSourceItem_WithCustomDataSource() + { + // Arrange + var factory = new DataSourceItemFactory(); + var type = DataSourceType.MicrosoftSqlServer; + var id = "2"; + var title = "Test Title"; + var subtitle = "Test Subtitle"; + var dataSource = new DataSource() + { + Id = "CustomId", + Title = "Custom Title", + Subtitle = "Custom Subtitle", + }; + + // Act + var result = factory.Create(type, id, title, subtitle, dataSource); + + // Assert + Assert.NotNull(result); + Assert.IsType(result); + Assert.Equal(id, result.Id); + Assert.Equal(title, result.Title); + Assert.Equal(subtitle, result.Subtitle); + Assert.NotNull(result.DataSource); + Assert.Same(dataSource, result.DataSource); + } + + [Fact] + public void Create_ShouldThrowNotImplementedException_WhenUnknownType() + { + // Arrange + var factory = new DataSourceItemFactory(); + var type = (DataSourceType)99; + var id = "3"; + var title = "Test Title"; + + // Act & Assert + Assert.Throws(() => factory.Create(type, id, title)); + } + } +} diff --git a/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs index e846c897..cd3da84e 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs @@ -289,7 +289,7 @@ public void DataSource_Properties_Should_Set_ResourceItemDataSource_Properties() { // Arrange var dataSource = new DataSource() { Id = "DS-ID", Title = "DS-TITLE", Subtitle = "DS-SUBTITLE" }; - var dataSourceItem = new RestDataSourceItem(dataSource, "Test"); + var dataSourceItem = new RestDataSourceItem("Test", dataSource); // Assert Assert.Equal(dataSource.Id, dataSourceItem.ResourceItemDataSource.Id); @@ -305,7 +305,7 @@ public void DataSource_No_Title_Or_Subtitle_Should_Use_ResourceItem_Title_Or_Sub { // Arrange var dataSource = new DataSource(); - var dataSourceItem = new RestDataSourceItem(dataSource, "Test"); + var dataSourceItem = new RestDataSourceItem("Test", dataSource); // Assert Assert.Equal(dataSourceItem.ResourceItemDataSource.Title, dataSourceItem.ResourceItem.Title); diff --git a/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs b/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs index 72406825..8ccef5e1 100644 --- a/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs @@ -53,7 +53,7 @@ public void RdashDocument_TitleConstructor_ShouldSetTitle() public void RdashDocument_Import_ShouldImportVisualizations() { // Arrange - var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "").SetFields(new List() { null }); + var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "", "").SetFields(new List() { null }); var sourceDocument = new RdashDocument(); sourceDocument.Visualizations.Add(new KpiTimeVisualization(dataSourceItem)); @@ -79,7 +79,7 @@ public void RdashDocument_Import_ShouldImportVisualizations() public void RdashDocument_Import_ShouldImportSingleVisualization() { // Arrange - var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "").SetFields(new List() { null }); + var dataSourceItem = new DataSourceItemFactory().Create(DataSourceType.REST, "", "").SetFields(new List() { null }); var sourceDocument = new RdashDocument(); sourceDocument.Visualizations.Add(new KpiTimeVisualization(dataSourceItem)); diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs index 335b0c30..f2f62abc 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs @@ -9,7 +9,7 @@ namespace Reveal.Sdk.Dom.Data { public class DataSourceItem : SchemaType { - public DataSourceItem(DataSource dataSource, string title) : this() + public DataSourceItem(string title, DataSource dataSource) : this() { InitializeDataSource(dataSource, title); InitializeDataSourceItem(title); diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItemFactory.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItemFactory.cs index 9f12c134..f57fa80e 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItemFactory.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItemFactory.cs @@ -2,28 +2,35 @@ namespace Reveal.Sdk.Dom.Data { - public interface IDataSourceItemFactory + public class DataSourceItemFactory : IDataSourceItemFactory { - DataSourceItem Create(DataSourceType type, string title); + public DataSourceItem Create(DataSourceType type, string id, string title) + { + return Create(type, id, title, new DataSource()); + } - DataSourceItem Create(DataSourceType type, DataSource dataSource, string title); - } + public DataSourceItem Create(DataSourceType type, string id, string title, DataSource dataSource) + { + return Create(type, id, title, null, dataSource); + } - public class DataSourceItemFactory : IDataSourceItemFactory - { - public DataSourceItem Create(DataSourceType type, string title) + public DataSourceItem Create(DataSourceType type, string id, string title, string subtitle) { - return Create(type, new DataSource(), title); + return Create(type, id, title, subtitle, new DataSource()); } - public DataSourceItem Create(DataSourceType type, DataSource dataSource, string title) + public DataSourceItem Create(DataSourceType type, string id, string title, string subtitle, DataSource dataSource) { - return type switch + DataSourceItem item = type switch { - DataSourceType.MicrosoftSqlServer => new MicrosoftSqlServerDataSourceItem(dataSource, title), - DataSourceType.REST => new RestDataSourceItem(dataSource, title), + DataSourceType.MicrosoftSqlServer => new MicrosoftSqlServerDataSourceItem(title, dataSource), + DataSourceType.REST => new RestDataSourceItem(title, dataSource), _ => throw new NotImplementedException($"No builder implemented for provider: {type}") }; + + item.Id = id; + item.Subtitle = subtitle; + return item; } } } diff --git a/src/Reveal.Sdk.Dom/Data/Interfaces/IDataSourceItemFactory.cs b/src/Reveal.Sdk.Dom/Data/Interfaces/IDataSourceItemFactory.cs new file mode 100644 index 00000000..f94a46dc --- /dev/null +++ b/src/Reveal.Sdk.Dom/Data/Interfaces/IDataSourceItemFactory.cs @@ -0,0 +1,13 @@ +namespace Reveal.Sdk.Dom.Data +{ + public interface IDataSourceItemFactory + { + DataSourceItem Create(DataSourceType type, string id, string title); + + DataSourceItem Create(DataSourceType type, string id, string title, string subtitle); + + DataSourceItem Create(DataSourceType type, string id, string title, DataSource dataSource); + + DataSourceItem Create(DataSourceType type, string id, string title, string subtitle, DataSource dataSource); + } +} diff --git a/src/Reveal.Sdk.Dom/Data/MicrosoftSqlServerDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/MicrosoftSqlServerDataSourceItem.cs index aa414bbd..4a58e40e 100644 --- a/src/Reveal.Sdk.Dom/Data/MicrosoftSqlServerDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/MicrosoftSqlServerDataSourceItem.cs @@ -5,11 +5,11 @@ namespace Reveal.Sdk.Dom.Data public class MicrosoftSqlServerDataSourceItem : DataSourceItem { public MicrosoftSqlServerDataSourceItem(string title) : - this(new DataSource(), title) + base(title, new DataSource()) { } - public MicrosoftSqlServerDataSourceItem(DataSource dataSource, string title) : - base(dataSource, title) + public MicrosoftSqlServerDataSourceItem(string title, DataSource dataSource) : + base(title, dataSource) { } public string Database diff --git a/src/Reveal.Sdk.Dom/Data/RestDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/RestDataSourceItem.cs index 4f7a8e30..6783715c 100644 --- a/src/Reveal.Sdk.Dom/Data/RestDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/RestDataSourceItem.cs @@ -10,10 +10,12 @@ public class RestDataSourceItem : DataSourceItem { DataSource _dataSource; - public RestDataSourceItem(string title) : this(new DataSource(), title) + public RestDataSourceItem(string title) : + this(title, new DataSource()) { } - public RestDataSourceItem(DataSource dataSource, string title) : base(null, title) + public RestDataSourceItem(string title, DataSource dataSource) : + base(title, null) { _dataSource = dataSource ?? new DataSource(); InitializeResourceItem(DataSourceProvider.REST, title);