Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored data source objects #59

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ExcelDataSource>(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Expand All @@ -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<ExcelDataSource>(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Expand All @@ -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<ExcelDataSource>(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal($"local:{path}", excelFile.Path);
Expand All @@ -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<ExcelDataSource>(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal($"local:{path}", excelFile.Path);
Expand Down Expand Up @@ -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<IField>() { 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<IField>() { 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<IField>() { 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);
}
}
}
94 changes: 94 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/RestDataSourceItemFixture.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<IField>() { 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<IField>() { 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<IField>() { 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<IField>() { 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<IField>() { 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);
}
}
}
30 changes: 17 additions & 13 deletions src/Reveal.Sdk.Dom/Data/DataSourceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,33 @@ public string Subtitle
internal DataSourceItem ResourceItem { get; set; }

private List<IField> _fields = new List<IField>();
private DataSource _dataSource;

[JsonIgnore]
public List<IField> Fields
{
get => _fields;
public List<IField> Fields
{
get => _fields;
set
{
{
_fields = value;
OnFieldsPropertyChanged(_fields);
}
}
}

/// <summary>
/// The data source for the current DataSourceItem. This is set internally by a data source builder and is only used during the RdashDocumentValidator process.
/// 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.
/// </summary>
[JsonIgnore]
internal DataSource DataSource { get; set; }
internal DataSource DataSource
{
get => _dataSource;
set
{
_dataSource = value;
DataSourceId = _dataSource?.Id;
}
}

/// <summary>
/// The data source for the ResourceItem. This is set internally by a data source builder and is only used during the RdashDocumentValidator process.
Expand Down Expand Up @@ -128,12 +138,6 @@ protected virtual void InitializeDataSourceItem(string title)
Title = title;
}

protected virtual void OnFieldsPropertyChanged(List<IField> fields) { }

protected virtual void UpdateDataSourceId(string id)
{
DataSource.Id = id;
DataSourceId = id;
}
protected virtual void OnFieldsPropertyChanged(List<IField> fields) { }
}
}
19 changes: 9 additions & 10 deletions src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;

namespace Reveal.Sdk.Dom.Data
Expand All @@ -9,21 +8,21 @@ 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) :
this(title, path, null)
{ }

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);
Expand All @@ -43,16 +42,17 @@ 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();
}

private void InitializeResourceItem(string title)
{
ResourceItemDataSource = new DataSource { Provider = DataSourceProvider.LocalFile, Id = DataSourceIds.LOCALFILE };
ResourceItemDataSource = new LocalFileDataSource();
ResourceItem = new DataSourceItem
{
DataSource = ResourceItemDataSource,
Expand All @@ -63,6 +63,5 @@ private void InitializeResourceItem(string title)
ResourceItemDataSource = ResourceItemDataSource;
ResourceItem = ResourceItem;
}

}
}
18 changes: 4 additions & 14 deletions src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -71,31 +70,22 @@ 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);

if (sheet != null)
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 };
Expand Down
13 changes: 13 additions & 0 deletions src/Reveal.Sdk.Dom/Data/DataSources/CsvDataSource.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Loading
Loading