From 375cc4df26e9d7ecdabc3febeedd196c78ec9c95 Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Tue, 21 May 2024 20:45:09 -0600 Subject: [PATCH 1/2] refactored data source objects --- .../Data/ExcelFileDataSourceItemFixture.cs | 62 +++++++++++- .../Data/RestDataSourceItemFixture.cs | 94 +++++++++++++++++++ src/Reveal.Sdk.Dom/Data/DataSourceItem.cs | 30 +++--- .../ExcelFileDataSourceItem.cs | 23 +++-- .../DataSourceItems/RestDataSourceItem.cs | 18 +--- .../Data/DataSources/CsvDataSource.cs | 13 +++ .../Data/DataSources/ExcelDataSource.cs | 13 +++ .../Data/DataSources/JsonDataSource.cs | 13 +++ .../Data/DataSources/LocalFileDataSource.cs | 13 +++ 9 files changed, 241 insertions(+), 38 deletions(-) create mode 100644 src/Reveal.Sdk.Dom/Data/DataSources/CsvDataSource.cs create mode 100644 src/Reveal.Sdk.Dom/Data/DataSources/ExcelDataSource.cs create mode 100644 src/Reveal.Sdk.Dom/Data/DataSources/JsonDataSource.cs create mode 100644 src/Reveal.Sdk.Dom/Data/DataSources/LocalFileDataSource.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs index 40fb7e88..724ca696 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs @@ -1,5 +1,8 @@ using Reveal.Sdk.Dom.Core.Constants; +using Reveal.Sdk.Dom.Core.Utilities; using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Visualizations; +using System.Collections.Generic; using Xunit; namespace Reveal.Sdk.Dom.Tests.Data @@ -19,7 +22,7 @@ public void Constructor_WithTitleAndDataSource_ShouldSetProperties() // Assert Assert.Equal(title, excelFile.Title); Assert.Equal(title, excelFile.ResourceItem.Title); - Assert.Equal(dataSource, excelFile.DataSource); + Assert.IsType(excelFile.DataSource); Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id); Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider); Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider); @@ -40,6 +43,7 @@ public void Constructor_WithTitle_ShouldSetProperties() Assert.Equal(title, excelFile.Title); Assert.Equal(title, excelFile.ResourceItem.Title); Assert.NotNull(excelFile.DataSource); + Assert.IsType(excelFile.DataSource); Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id); Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider); Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider); @@ -61,6 +65,7 @@ public void Constructor_WithTitleAndPath_ShouldSetProperties() Assert.Equal(title, excelFile.Title); Assert.Equal(title, excelFile.ResourceItem.Title); Assert.NotNull(excelFile.DataSource); + Assert.IsType(excelFile.DataSource); Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id); Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider); Assert.Equal($"local:{path}", excelFile.Path); @@ -84,6 +89,7 @@ public void Constructor_WithTitlePathAndSheet_ShouldSetProperties() Assert.Equal(title, excelFile.Title); Assert.Equal(title, excelFile.ResourceItem.Title); Assert.NotNull(excelFile.DataSource); + Assert.IsType(excelFile.DataSource); Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id); Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider); Assert.Equal($"local:{path}", excelFile.Path); @@ -120,5 +126,59 @@ public void Sheet_GetAndSet_ShouldSetSheet() // Assert Assert.Equal(sheet, excelFile.Sheet); } + + [Fact] + public void Constructor_WithTitle_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new ExcelFileDataSourceItem("Test").SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.Excel, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.MicrosoftExcel, document.DataSources[0].Provider); + } + + [Fact] + public void Constructor_WithTitleAndPath_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new ExcelFileDataSourceItem("Test", "Path").SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.Excel, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.MicrosoftExcel, document.DataSources[0].Provider); + } + + [Fact] + public void Constructor_WithTitleAndPathAndSheet_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new ExcelFileDataSourceItem("Test", "Path", "Sheet").SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.Excel, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.MicrosoftExcel, document.DataSources[0].Provider); + } } } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs index cd3da84e..bffe553d 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs @@ -1,10 +1,12 @@ using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Core.Extensions; +using Reveal.Sdk.Dom.Core.Utilities; using Reveal.Sdk.Dom.Data; using Reveal.Sdk.Dom.Visualizations; using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using Xunit; namespace Reveal.Sdk.Dom.Tests.Data @@ -311,5 +313,97 @@ public void DataSource_No_Title_Or_Subtitle_Should_Use_ResourceItem_Title_Or_Sub Assert.Equal(dataSourceItem.ResourceItemDataSource.Title, dataSourceItem.ResourceItem.Title); Assert.Equal(dataSourceItem.ResourceItemDataSource.Subtitle, dataSourceItem.ResourceItem.Subtitle); } + + [Fact] + public void Constructor_WithTitle_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new RestDataSourceItem("Test").SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.JSON, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.JSON, document.DataSources[0].Provider); + } + + [Fact] + public void Constructor_WithTitleAndUri_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new RestDataSourceItem("TITLE", "URI").SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.JSON, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.JSON, document.DataSources[0].Provider); + } + + [Fact] + public void Constructor_WithNullDataSource_Should_Add_TwoDataSourcese() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new RestDataSourceItem("Test", "URI", null).SetFields(new List() { null }); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.JSON, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.JSON, document.DataSources[0].Provider); + } + + [Fact] + public void UseExcel_Should_Add_TwoDataSources() + { + // Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new RestDataSourceItem("Test").SetFields(new List() { null }); + dataSourceItem.UseExcel(); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.Excel, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.MicrosoftExcel, document.DataSources[0].Provider); + } + + [Fact] + public void UseCsv_Should_Add_TwoDataSources() + { + //Arrange + var document = new RdashDocument("Test"); + var dataSourceItem = new RestDataSourceItem("Test").SetFields(new List() { null }); + dataSourceItem.UseCsv(); + + document.Visualizations.Add(new GridVisualization(dataSourceItem)); + + // Ensure data sources are added to the data sources collection + RdashDocumentValidator.FixDocument(document); + + // Assert + Assert.Equal(2, document.DataSources.Count); + Assert.Equal(DataSourceIds.CSV, document.DataSources[0].Id); + Assert.Equal(DataSourceProvider.CSV, document.DataSources[0].Provider); + } } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs index faa51d11..cf64de7b 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItem.cs @@ -64,15 +64,17 @@ public string Subtitle internal DataSourceItem ResourceItem { get; set; } private List _fields = new List(); + private DataSource _dataSource; + [JsonIgnore] - public List Fields - { - get => _fields; + public List Fields + { + get => _fields; set - { + { _fields = value; OnFieldsPropertyChanged(_fields); - } + } } /// @@ -80,7 +82,15 @@ public List Fields /// If this is null, then the DataSourceItem was manually added to the document and the DataSourceId property should be used to find the data source. /// [JsonIgnore] - internal DataSource DataSource { get; set; } + internal DataSource DataSource + { + get => _dataSource; + set + { + _dataSource = value; + DataSourceId = _dataSource?.Id; + } + } /// /// The data source for the ResourceItem. This is set internally by a data source builder and is only used during the RdashDocumentValidator process. @@ -128,12 +138,6 @@ protected virtual void InitializeDataSourceItem(string title) Title = title; } - protected virtual void OnFieldsPropertyChanged(List fields) { } - - protected virtual void UpdateDataSourceId(string id) - { - DataSource.Id = id; - DataSourceId = id; - } + protected virtual void OnFieldsPropertyChanged(List fields) { } } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs index 0441cd86..790aeed3 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Core.Extensions; namespace Reveal.Sdk.Dom.Data @@ -9,7 +8,7 @@ namespace Reveal.Sdk.Dom.Data public class ExcelFileDataSourceItem : DataSourceItem { public ExcelFileDataSourceItem(string title) : - this(title, new DataSource()) + this(title, new ExcelDataSource()) { } public ExcelFileDataSourceItem(string title, string path) : @@ -17,13 +16,13 @@ public ExcelFileDataSourceItem(string title, string path) : { } public ExcelFileDataSourceItem(string title, string path, string sheet) : - this(title, new DataSource()) + this(title, new ExcelDataSource()) { Path = path; Sheet = sheet; } - public ExcelFileDataSourceItem(string title, DataSource dataSource) : + internal ExcelFileDataSourceItem(string title, DataSource dataSource) : base(title, dataSource) { InitializeResourceItem(title); @@ -43,16 +42,21 @@ public string Sheet set { Properties.SetItem("Sheet", value); } } - protected override void InitializeDataSource(DataSource dataSource, string title) + protected override DataSource CreateDataSourceInstance(DataSource dataSource) { - base.InitializeDataSource(dataSource, title); - UpdateDataSourceId(DataSourceIds.Excel); - DataSource.Provider = DataSourceProvider.MicrosoftExcel; + if (dataSource is ExcelDataSource) + return dataSource; + + return new ExcelDataSource() + { + Title = dataSource.Title, + Subtitle = dataSource.Subtitle, + }; } private void InitializeResourceItem(string title) { - ResourceItemDataSource = new DataSource { Provider = DataSourceProvider.LocalFile, Id = DataSourceIds.LOCALFILE }; + ResourceItemDataSource = new LocalFileDataSource(); ResourceItem = new DataSourceItem { DataSource = ResourceItemDataSource, @@ -63,6 +67,5 @@ private void InitializeResourceItem(string title) ResourceItemDataSource = ResourceItemDataSource; ResourceItem = ResourceItem; } - } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs index 25b1e908..028cedee 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Visualizations; using System.Collections.Generic; @@ -26,7 +25,7 @@ public RestDataSourceItem(string title, string uri, DataSource dataSource) : } public RestDataSourceItem(string title, DataSource dataSource) : - base(title, null) + base(title, new JsonDataSource()) { _dataSource = dataSource ?? new DataSource(); InitializeResourceItem(title); @@ -71,17 +70,15 @@ public void AddHeader(HeaderType headerType, string value) public void UseCsv() { ClearJsonConfig(); - UpdateDataSourceId(DataSourceIds.CSV); - DataSource.Provider = DataSourceProvider.CSV; + DataSource = new CsvDataSource(); ResourceItemDataSource.Properties.SetItem("Result-Type", ".csv"); } public void UseExcel(string sheet = null, ExcelFileType fileType = ExcelFileType.Xlsx) { ClearJsonConfig(); - UpdateDataSourceId(DataSourceIds.Excel); - DataSource.Provider = DataSourceProvider.MicrosoftExcel; - + DataSource = new ExcelDataSource(); + var fileExt = fileType == ExcelFileType.Xlsx ? ".xlsx" : ".xls"; ResourceItemDataSource.Properties.SetItem("Result-Type", fileExt); @@ -89,13 +86,6 @@ public void UseExcel(string sheet = null, ExcelFileType fileType = ExcelFileType Properties.SetItem("Sheet", sheet); } - protected override void InitializeDataSource(DataSource dataSource, string title) - { - base.InitializeDataSource(dataSource, title); - UpdateDataSourceId(DataSourceIds.JSON); - DataSource.Provider = DataSourceProvider.JSON; - } - protected void InitializeResourceItem(string title) { ResourceItemDataSource = new DataSource { Provider = DataSourceProvider.REST }; diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/CsvDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/CsvDataSource.cs new file mode 100644 index 00000000..6f91dd58 --- /dev/null +++ b/src/Reveal.Sdk.Dom/Data/DataSources/CsvDataSource.cs @@ -0,0 +1,13 @@ +using Reveal.Sdk.Dom.Core.Constants; + +namespace Reveal.Sdk.Dom.Data +{ + internal class CsvDataSource : DataSource + { + public CsvDataSource() + { + Id = DataSourceIds.CSV; + Provider = DataSourceProvider.CSV; + } + } +} diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/ExcelDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/ExcelDataSource.cs new file mode 100644 index 00000000..0eca608c --- /dev/null +++ b/src/Reveal.Sdk.Dom/Data/DataSources/ExcelDataSource.cs @@ -0,0 +1,13 @@ +using Reveal.Sdk.Dom.Core.Constants; + +namespace Reveal.Sdk.Dom.Data +{ + internal class ExcelDataSource : DataSource + { + public ExcelDataSource() + { + Id = DataSourceIds.Excel; + Provider = DataSourceProvider.MicrosoftExcel; + } + } +} diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/JsonDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/JsonDataSource.cs new file mode 100644 index 00000000..259670f0 --- /dev/null +++ b/src/Reveal.Sdk.Dom/Data/DataSources/JsonDataSource.cs @@ -0,0 +1,13 @@ +using Reveal.Sdk.Dom.Core.Constants; + +namespace Reveal.Sdk.Dom.Data +{ + internal class JsonDataSource : DataSource + { + public JsonDataSource() + { + Id = DataSourceIds.JSON; + Provider = DataSourceProvider.JSON; + } + } +} diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/LocalFileDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/LocalFileDataSource.cs new file mode 100644 index 00000000..883eb030 --- /dev/null +++ b/src/Reveal.Sdk.Dom/Data/DataSources/LocalFileDataSource.cs @@ -0,0 +1,13 @@ +using Reveal.Sdk.Dom.Core.Constants; + +namespace Reveal.Sdk.Dom.Data +{ + internal class LocalFileDataSource : DataSource + { + public LocalFileDataSource() + { + Id = DataSourceIds.LOCALFILE; + Provider = DataSourceProvider.LocalFile; + } + } +} From 5b16a47c915fa791957c69d062fe147e3deb0f3c Mon Sep 17 00:00:00 2001 From: Brian Lagunas <835562+brianlagunas@users.noreply.github.com> Date: Tue, 21 May 2024 20:48:53 -0600 Subject: [PATCH 2/2] removed title/subtitle setting from excel file DSI --- .../Data/DataSourceItems/ExcelFileDataSourceItem.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs index 790aeed3..7dbc528f 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs @@ -47,11 +47,7 @@ protected override DataSource CreateDataSourceInstance(DataSource dataSource) if (dataSource is ExcelDataSource) return dataSource; - return new ExcelDataSource() - { - Title = dataSource.Title, - Subtitle = dataSource.Subtitle, - }; + return new ExcelDataSource(); } private void InitializeResourceItem(string title)