diff --git a/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs b/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs index 01df3700..e9829d16 100644 --- a/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Xunit; namespace Reveal.Sdk.Dom.Tests @@ -287,5 +288,93 @@ public void ImportThrows_WhenVisualizationIsNull() Assert.Throws(() => document.Import(new RdashDocument(), (IVisualization)null)); } + + private RdashDocument TestDashboardForJoins() + { + var dashboard = new RdashDocument(); + + var dsiFactory = new DataSourceItemFactory(); + + var productDataSourceItem = dsiFactory.Create(DataSourceType.REST, "http://localhost:8080/api", "Sales") + .SetFields(new List() + { + new TextField("Product"), + new NumberField("Revenue") + }); + + var productDetailsDataSourceItem = dsiFactory.Create(DataSourceType.REST, "http://localhost:8080/api", "ProductDetails") + .SetFields(new List() + { + new TextField("Product"), + new TextField("Product2"), + new TextField("Category") + }); + + productDataSourceItem.Join("A", "Product", "Product", productDetailsDataSourceItem); + + var visualization = new GridVisualization("JoinTest",productDataSourceItem); + + ((ITabularColumns)visualization).Columns.Add(new TabularColumn("Product")); + ((ITabularColumns)visualization).Columns.Add(new TabularColumn("Revenue")); + ((ITabularColumns)visualization).Columns.Add(new TabularColumn("Category")); + + dashboard.Visualizations.Add(visualization); + + return dashboard; + } + + [Fact] + public void ToJsonString_DontDuplicateJoins() + { + var dashboard = TestDashboardForJoins(); + + //sanity check + var json = dashboard.ToJsonString(); + var jsonDocument = JObject.Parse(json); + + var joins = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[*]"); + + Assert.Equal(1, joins.Count()); + + + //second ToJsonString should still return a single join + json = dashboard.ToJsonString(); + jsonDocument = JObject.Parse(json); + + joins = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[*]"); + + Assert.Equal(1, joins.Count()); + } + + [Fact] + public void ToJsonString_ReplaceUpdatedJoin() + { + var dashboard = TestDashboardForJoins(); + + //sanity check + var json = dashboard.ToJsonString(); + var jsonDocument = JObject.Parse(json); + + var firstJoinRightFieldName = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[0].JoinConditions[0].RightFieldName").First().ToString(); + var joins = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[*]"); + + Assert.Equal(1, joins.Count()); + Assert.Equal("A.[Product]", firstJoinRightFieldName); + + //retrieve the right DSI to not create it again + var productDetailsDataSourceItem = dashboard.Visualizations[0].DataDefinition.AsTabular().DataSourceItem.JoinTables[0].DataDefinition.DataSourceItem; + + dashboard.Visualizations[0].DataDefinition.AsTabular().DataSourceItem.Join("A","Product","Product2",productDetailsDataSourceItem); + + //second ToJsonString should have the updated value + json = dashboard.ToJsonString(); + jsonDocument = JObject.Parse(json); + + firstJoinRightFieldName = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[0].JoinConditions[0].RightFieldName").First().ToString(); + joins = jsonDocument.SelectTokens("$.Widgets[0].DataSpec.AdditionalTables[*]"); + + Assert.Equal(1, joins.Count()); + Assert.Equal("A.[Product2]", firstJoinRightFieldName); + } } } diff --git a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs index 99dd10d1..d9bc7010 100644 --- a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs +++ b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs @@ -52,7 +52,12 @@ static void FixJoinedTables(TabularDataDefinition tdd) if (tdd.DataSourceItem.JoinTables != null) { tdd.JoinTables.AddRange(tdd.DataSourceItem.JoinTables.Clone()); + + //remove any duplicates, keeping the newest versions + //the Alias should be unique for each different join + tdd.JoinTables = tdd.JoinTables.GroupBy(jt => jt.Alias).Select(g => g.Last()).ToList(); } + } static void FixDataSources(RdashDocument document, DataSourceItem dataSourceItem, Dictionary dataSources)