Skip to content

Commit

Permalink
Merge pull request #58 from RevealBi/localfile
Browse files Browse the repository at this point in the history
added local excel file data source item
  • Loading branch information
brianlagunas authored May 22, 2024
2 parents 3d1fe33 + 12b86b1 commit 2c788d7
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 17 deletions.
Binary file added e2e/Sandbox/Data/Samples.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions e2e/Sandbox/Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<None Update="Dashboards\Sales.rdash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Samples.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand All @@ -57,4 +60,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>

</Project>
124 changes: 124 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/ExcelFileDataSourceItemFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Data;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data
{
public class ExcelFileDataSourceItemFixture
{
[Fact]
public void Constructor_WithTitleAndDataSource_ShouldSetProperties()
{
// Arrange
string title = "Test Excel File";
DataSource dataSource = new DataSource();

// Act
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem(title, dataSource);

// Assert
Assert.Equal(title, excelFile.Title);
Assert.Equal(title, excelFile.ResourceItem.Title);
Assert.Equal(dataSource, excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItemDataSource.Id);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItem.DataSourceId);
}

[Fact]
public void Constructor_WithTitle_ShouldSetProperties()
{
// Arrange
string title = "Test Excel File";

// Act
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem(title);

// Assert
Assert.Equal(title, excelFile.Title);
Assert.Equal(title, excelFile.ResourceItem.Title);
Assert.NotNull(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItemDataSource.Id);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItem.DataSourceId);
}

[Fact]
public void Constructor_WithTitleAndPath_ShouldSetProperties()
{
// Arrange
string title = "Test Excel File";
string path = "test.xlsx";

// Act
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem(title, path);

// Assert
Assert.Equal(title, excelFile.Title);
Assert.Equal(title, excelFile.ResourceItem.Title);
Assert.NotNull(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal($"local:{path}", excelFile.Path);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItemDataSource.Id);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItem.DataSourceId);
}

[Fact]
public void Constructor_WithTitlePathAndSheet_ShouldSetProperties()
{
// Arrange
string title = "Test Excel File";
string path = "test.xlsx";
string sheet = "Sheet1";

// Act
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem(title, path, sheet);

// Assert
Assert.Equal(title, excelFile.Title);
Assert.Equal(title, excelFile.ResourceItem.Title);
Assert.NotNull(excelFile.DataSource);
Assert.Equal(DataSourceIds.Excel, excelFile.DataSource.Id);
Assert.Equal(DataSourceProvider.MicrosoftExcel, excelFile.DataSource.Provider);
Assert.Equal($"local:{path}", excelFile.Path);
Assert.Equal(sheet, excelFile.Sheet);
Assert.Equal(DataSourceProvider.LocalFile, excelFile.ResourceItemDataSource.Provider);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItemDataSource.Id);
Assert.Equal(DataSourceIds.LOCALFILE, excelFile.ResourceItem.DataSourceId);
}

[Fact]
public void Path_GetAndSet_ShouldSetLocalPath()
{
// Arrange
string path = "test.xlsx";
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem("Test Excel File");

// Act
excelFile.Path = path;

// Assert
Assert.Equal($"local:{path}", excelFile.Path);
}

[Fact]
public void Sheet_GetAndSet_ShouldSetSheet()
{
// Arrange
string sheet = "Sheet1";
ExcelFileDataSourceItem excelFile = new ExcelFileDataSourceItem("Test Excel File");

// Act
excelFile.Sheet = sheet;

// Assert
Assert.Equal(sheet, excelFile.Sheet);
}
}
}
1 change: 1 addition & 0 deletions src/Reveal.Sdk.Dom/Core/Constants/DataSourceIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ internal class DataSourceIds
public const string CSV = "__CSV";
public const string Excel = "__EXCEL";
public const string JSON = "__JSON";
public const string LOCALFILE = "__LOCAL";
}
}
18 changes: 2 additions & 16 deletions src/Reveal.Sdk.Dom/Data/DataSourceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DataSourceItem : SchemaType
{
public DataSourceItem(string title, DataSource dataSource) : this()
{
Initialize(dataSource, title);
Initialize(dataSource ?? new DataSource(), title);
}

public DataSourceItem()
Expand Down Expand Up @@ -91,7 +91,7 @@ public List<IField> Fields

private void Initialize(DataSource dataSource, string title)
{
DataSource = CreateDataSourceInstance(dataSource ?? new DataSource());
DataSource = CreateDataSourceInstance(dataSource);
InitializeDataSource(DataSource, title);
InitializeDataSourceItem(title);
}
Expand Down Expand Up @@ -128,20 +128,6 @@ protected virtual void InitializeDataSourceItem(string title)
Title = title;
}

protected virtual void InitializeResourceItem(DataSourceProvider resourceItemDataSourceProvider, string title)
{
ResourceItemDataSource = new DataSource { Provider = resourceItemDataSourceProvider };
ResourceItem = new DataSourceItem
{
DataSource = ResourceItemDataSource,
DataSourceId = ResourceItemDataSource.Id,
Title = title
};

ResourceItemDataSource = ResourceItemDataSource;
ResourceItem = ResourceItem;
}

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

protected virtual void UpdateDataSourceId(string id)
Expand Down
68 changes: 68 additions & 0 deletions src/Reveal.Sdk.Dom/Data/DataSourceItems/ExcelFileDataSourceItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Newtonsoft.Json;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;

namespace Reveal.Sdk.Dom.Data
{
//todo: we may need a base class named LocalFileDataSourceItem which has shared properties between different file types
//todo: need to add more Excel specific properties
public class ExcelFileDataSourceItem : DataSourceItem
{
public ExcelFileDataSourceItem(string title) :
this(title, new DataSource())
{ }

public ExcelFileDataSourceItem(string title, string path) :
this(title, path, null)
{ }

public ExcelFileDataSourceItem(string title, string path, string sheet) :
this(title, new DataSource())
{
Path = path;
Sheet = sheet;
}

public ExcelFileDataSourceItem(string title, DataSource dataSource) :
base(title, dataSource)
{
InitializeResourceItem(title);
}

[JsonIgnore]
public string Path
{
get { return ResourceItem.Properties.GetValue<string>("URI"); }
set { ResourceItem.Properties.SetItem("URI", $"local:{value}"); }
}

[JsonIgnore]
public string Sheet
{
get { return Properties.GetValue<string>("Sheet"); }
set { Properties.SetItem("Sheet", value); }
}

protected override void InitializeDataSource(DataSource dataSource, string title)
{
base.InitializeDataSource(dataSource, title);
UpdateDataSourceId(DataSourceIds.Excel);
DataSource.Provider = DataSourceProvider.MicrosoftExcel;
}

private void InitializeResourceItem(string title)
{
ResourceItemDataSource = new DataSource { Provider = DataSourceProvider.LocalFile, Id = DataSourceIds.LOCALFILE };
ResourceItem = new DataSourceItem
{
DataSource = ResourceItemDataSource,
DataSourceId = ResourceItemDataSource.Id,
Title = title
};

ResourceItemDataSource = ResourceItemDataSource;
ResourceItem = ResourceItem;
}

}
}
16 changes: 15 additions & 1 deletion src/Reveal.Sdk.Dom/Data/DataSourceItems/RestDataSourceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public RestDataSourceItem(string title, DataSource dataSource) :
base(title, null)
{
_dataSource = dataSource ?? new DataSource();
InitializeResourceItem(DataSourceProvider.REST, title);
InitializeResourceItem(title);
UpdateResourceItemDataSource(_dataSource);
}

Expand Down Expand Up @@ -96,6 +96,20 @@ protected override void InitializeDataSource(DataSource dataSource, string title
DataSource.Provider = DataSourceProvider.JSON;
}

protected void InitializeResourceItem(string title)
{
ResourceItemDataSource = new DataSource { Provider = DataSourceProvider.REST };
ResourceItem = new DataSourceItem
{
DataSource = ResourceItemDataSource,
DataSourceId = ResourceItemDataSource.Id,
Title = title
};

ResourceItemDataSource = ResourceItemDataSource;
ResourceItem = ResourceItem;
}

protected override void OnFieldsPropertyChanged(List<IField> fields)
{
Parameters.Add("config", BuildConfig(fields));
Expand Down

0 comments on commit 2c788d7

Please sign in to comment.