diff --git a/e2e/Sandbox/DashboardCreators/AmazonRedshiftDashboard.cs b/e2e/Sandbox/DashboardCreators/AmazonRedshiftDashboard.cs index 57d7ab33..0647a7f0 100644 --- a/e2e/Sandbox/DashboardCreators/AmazonRedshiftDashboard.cs +++ b/e2e/Sandbox/DashboardCreators/AmazonRedshiftDashboard.cs @@ -1,4 +1,6 @@ using Reveal.Sdk.Dom; +using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Visualizations; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +15,31 @@ internal class AmazonRedshiftDashboard : IDashboardCreator public RdashDocument CreateDashboard() { - throw new NotImplementedException(); + var document = new RdashDocument(); + var dataSource = new AmazonRedshiftDataSource() + { + Title = "Redshift DS", + Host = "reveal-redshift.cmeyu4xjvffl.us-east-1.redshift.amazonaws.com", + Database = "reveal" + }; + var dataSourceItem = new AmazonRedshiftDataSourceItem("Redshift DSI", dataSource) + { + Id = "redshiftDSItemId", + Title = "Redshift DS Item", + Table = "employees", + Fields = new List + { + new NumberField("employeeid"), + new TextField("firstname"), + new TextField("lastname"), + new TextField("address"), + } + }; + + document.Visualizations.Add(new GridVisualization("List employees", dataSourceItem) + .SetColumns("employeeid", "firstname", "lastname", "address")); + + return document; } } } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/AmazonRedshiftDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/AmazonRedshiftDataSourceItemFixture.cs index fcb1bb85..f4193e8f 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/AmazonRedshiftDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/AmazonRedshiftDataSourceItemFixture.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json.Linq; +using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Data; using Xunit; @@ -6,16 +8,107 @@ namespace Reveal.Sdk.Dom.Tests.Data.DataSourceItems public class AmazonRedshiftDataSourceItemFixture { [Fact] - public void Constructor_SetsTitleAndDataSource_ValidDataSource() + public void Constructor_CreateRedshiftDSItem_WithTitle() + { + // Arrange + var expectedTitle = "DS Item Title"; + + // Act + var dataSourceItem = new AmazonRedshiftDataSourceItem(expectedTitle); + + // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); + Assert.Equal(expectedTitle, dataSourceItem.Title); + Assert.Equal(expectedTitle, dataSourceItem.DataSource.Title); + Assert.NotNull(dataSourceItem.DataSource); + Assert.IsType(dataSourceItem.DataSource); + } + + [Theory] + [InlineData("DS Title", "DS Title", "DS Item Title", "DS Item Title")] + [InlineData(null, "DS Item Title", "DS Item Title", "DS Item Title")] + public void Constructor_CreateRedshiftDSItem_WithTitleAndDataSource(string dsTitle, string expectedDSTitle, string dsItemTitle, string expectedDSItemTitle) + { + // Arrange + var dataSource = new DataSource() { Title = dsTitle }; + + // Act + var dataSourceItem = new AmazonRedshiftDataSourceItem(dsItemTitle, dataSource); + + // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); + Assert.Equal(expectedDSTitle, dataSourceItem.DataSource.Title); + Assert.Equal(expectedDSItemTitle, dataSourceItem.Title); + Assert.Equal(dataSource.Id, dataSourceItem.DataSource.Id); + Assert.Equal(dataSource.Id, dataSourceItem.DataSourceId); + Assert.NotSame(dataSource, dataSourceItem.DataSource); + Assert.IsType(dataSourceItem.DataSource); + } + + [Theory] + [InlineData("DS Title", "DS Title", "DS Item Title", "DS Item Title")] + [InlineData(null, "DS Item Title", "DS Item Title", "DS Item Title")] + public void Constructor_CreateRedshiftDSItem_WithTitleAndRedshiftDataSource(string dsTitle, string expectedDSTitle, string dsItemTitle, string expectedDSItemTitle) + { + // Arrange + var dataSource = new AmazonRedshiftDataSource() { Title = dsTitle }; + + // Act + var dataSourceItem = new AmazonRedshiftDataSourceItem(dsItemTitle, dataSource); + + // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); + Assert.Equal(expectedDSTitle, dataSourceItem.DataSource.Title); + Assert.Equal(expectedDSItemTitle, dataSourceItem.Title); + Assert.Equal(dataSource.Id, dataSourceItem.DataSource.Id); + Assert.Equal(dataSource.Id, dataSourceItem.DataSourceId); + Assert.Same(dataSource, dataSourceItem.DataSource); + } + + [Fact] + public void ToJsonString_CreatesFormattedJson_NoConditions() { // Arrange - string title = "Test Item"; - var dataSource = new AmazonRedshiftDataSource(); - var item = new AmazonRedshiftDataSourceItem(title, dataSource); + var expectedJson = """ + { + "_type": "DataSourceItemType", + "Id": "redshiftDSItemId", + "Title": "Redshift DSItem", + "SubTitle": "Northwind Employees", + "DataSourceId": "redshiftId", + "HasTabularData": true, + "HasAsset": false, + "Properties": { + "Table": "employees", + "Schema": "public", + "Database": "RedshiftDB" + }, + "Parameters": {} + } + """; + var dataSource = new AmazonRedshiftDataSource() + { + Id = "redshiftId", + }; + var dataSourceItem = new AmazonRedshiftDataSourceItem("Redshift DSItem", dataSource) + { + Id = "redshiftDSItemId", + Title = "Redshift DSItem", + Subtitle = "Northwind Employees", + Database = "RedshiftDB", + Table = "employees", + Schema = "public", + HasTabularData = true, + HasAsset = false, + }; + var expectedJObject = JObject.Parse(expectedJson); + + // Act + var json = dataSourceItem.ToJsonString(); + var actualJObject = JObject.Parse(json); // Assert - Assert.Equal(title, item.Title); - Assert.Equal(dataSource, item.DataSource); + Assert.Equal(expectedJObject, actualJObject); } } } \ No newline at end of file diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/AmazonRedshiftDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/AmazonRedshiftDataSourceFixture.cs index 620cd313..827d6485 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/AmazonRedshiftDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/AmazonRedshiftDataSourceFixture.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json.Linq; using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Data; using Xunit; @@ -7,20 +8,17 @@ namespace Reveal.Sdk.Dom.Tests.Data.DataSources public class AmazonRedshiftDataSourceFixture { [Fact] - public void Constructor_SetsProviderToAmazonRedshift_Always() + public void Constructor_SetsProviderToAmazonRedshift_WithoutParameters() { - // Arrange - var dataSource = new AmazonRedshiftDataSource(); - // Act - var actualProvider = dataSource.Provider; + var dataSource = new AmazonRedshiftDataSource(); // Assert - Assert.Equal(DataSourceProvider.AmazonRedshift, actualProvider); + Assert.Equal(DataSourceProvider.AmazonRedshift, dataSource.Provider); } [Fact] - public void Schema_SetsAndGetsValue_ValidSchema() + public void GetSchema_ReturnSameValue_WithSetValue() { // Arrange var dataSource = new AmazonRedshiftDataSource(); @@ -28,41 +26,49 @@ public void Schema_SetsAndGetsValue_ValidSchema() // Act dataSource.Schema = expectedSchema; - var actualSchema = dataSource.Schema; // Assert - Assert.Equal(expectedSchema, actualSchema); + Assert.Equal(expectedSchema, dataSource.Schema); + Assert.Equal(expectedSchema, dataSource.Properties.GetValue("Schema")); } [Fact] - public void Schema_SetsValue_NullSchema() + public void ToJsonString_CreatesFormattedJson_NoConditions() { // Arrange - var dataSource = new AmazonRedshiftDataSource(); + var expectedJson = """ + { + "_type": "DataSourceType", + "Id": "redshiftId", + "Provider": "AMAZON_REDSHIFT", + "Description": "Redshift DS", + "Properties": { + "Host": "RedshiftHost", + "Database": "RedshiftDB", + "Schema": "RedshiftSchema" + }, + "Settings": { + "DefaultRefreshRate": 180 + } + } + """; + var dataSource = new AmazonRedshiftDataSource() + { + Id = "redshiftId", + Title = "Redshift DS", + Host = "RedshiftHost", + Database = "RedshiftDB", + Schema = "RedshiftSchema", + DefaultRefreshRate = "180" + }; + var expectedJObject = JObject.Parse(expectedJson); // Act - dataSource.Schema = null; - var actualSchema = dataSource.Schema; - var actualPropertySchema = dataSource.Properties.GetValue("Schema"); - - // Assert - Assert.Null(actualSchema); - Assert.Null(actualPropertySchema); - } - - [Fact] - public void Properties_StoresSchemaValueCorrectly_ValidSchema() - { - // Arrange - var dataSource = new AmazonRedshiftDataSource(); - var expectedSchema = "TestSchema"; - - // Act - dataSource.Schema = expectedSchema; - var actualPropertySchema = dataSource.Properties.GetValue("Schema"); + var json = dataSource.ToJsonString(); + var actualJObject = JObject.Parse(json); // Assert - Assert.Equal(expectedSchema, actualPropertySchema); + Assert.Equal(expectedJObject, actualJObject); } } } \ No newline at end of file diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/AmazonRedshiftDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/AmazonRedshiftDataSourceItem.cs index db30d53c..5ff4cb72 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/AmazonRedshiftDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/AmazonRedshiftDataSourceItem.cs @@ -1,10 +1,22 @@ namespace Reveal.Sdk.Dom.Data { - internal class AmazonRedshiftDataSourceItem : FunctionDataSourceItem + public class AmazonRedshiftDataSourceItem : FunctionDataSourceItem { + public AmazonRedshiftDataSourceItem(string title) + : base(title, new AmazonRedshiftDataSource()) + { } + public AmazonRedshiftDataSourceItem(string title, DataSource dataSource) : base(title, dataSource) + { } + + public AmazonRedshiftDataSourceItem(string title, AmazonRedshiftDataSource dataSource) : + base(title, dataSource) + { } + + protected override DataSource CreateDataSourceInstance(DataSource dataSource) { + return Create(dataSource); } } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/AmazonRedshiftDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/AmazonRedshiftDataSource.cs index 07de881e..5ce14347 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSources/AmazonRedshiftDataSource.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSources/AmazonRedshiftDataSource.cs @@ -3,7 +3,7 @@ namespace Reveal.Sdk.Dom.Data { - internal class AmazonRedshiftDataSource : HostDataSource + public class AmazonRedshiftDataSource : HostDataSource { public AmazonRedshiftDataSource() {