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

Deduplicate join tables on multiple ToJsonString calls #66

Closed
wants to merge 2 commits into from
Closed
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
89 changes: 89 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/RdashDocumentFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;

namespace Reveal.Sdk.Dom.Tests
Expand Down Expand Up @@ -287,5 +288,93 @@ public void ImportThrows_WhenVisualizationIsNull()

Assert.Throws<ArgumentNullException>(() => 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<IField>()
{
new TextField("Product"),
new NumberField("Revenue")
});

var productDetailsDataSourceItem = dsiFactory.Create(DataSourceType.REST, "http://localhost:8080/api", "ProductDetails")
.SetFields(new List<IField>()
{
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);
}
}
}
5 changes: 5 additions & 0 deletions src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, DataSource> dataSources)
Expand Down
Loading