Skip to content

Commit

Permalink
Moved JoinTables copy to the Visualization.UpdateDataSourceItem
Browse files Browse the repository at this point in the history
Mirrored fields logic to remove JoinTable duplicates
Removed Validate_Adds_JoinTables test as Validate doesn't add the JoinTables anymore, replaced them with two new validator tests
  • Loading branch information
srodriguezinfragistics committed Oct 7, 2024
1 parent c2be041 commit 60e6b35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,46 @@ public void Validate_PreventsDuplicate_Fields()
}

[Fact]
public void Validate_Adds_JoinTables()
public void Validate_PreventsDuplicate_JoinTables()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List<IField> { new TextField() });
var joinConditions = new List<JoinCondition> { new JoinCondition("left", "right") };
var dataSourceItemToJoin = new DataSourceItem().SetFields(new List<IField> { new TextField() });

dataSourceItem.Join("Alias", joinConditions, dataSourceItemToJoin);
dataSourceItem.Join("Alias", joinConditions, dataSourceItemToJoin);

var document = new RdashDocument();
document.Visualizations.Add(new GridVisualization(dataSourceItem));

Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().JoinTables);
Assert.Equal(2,document.Visualizations[0].DataDefinition.AsTabular().JoinTables.Count);

RdashDocumentValidator.Validate(document);

Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().JoinTables);
}

[Fact]
public void Validate_JoinTables_JoinsAreNotDuplicated()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List<IField> { new TextField() });
var joinConditions = new List<JoinCondition> { new JoinCondition("left", "right") };
var dataSourceItemToJoin = new DataSourceItem().SetFields(new List<IField> { new TextField() });

dataSourceItem.Join("Alias", joinConditions, dataSourceItemToJoin);

var document = new RdashDocument();
document.Visualizations.Add(new GridVisualization(dataSourceItem));

Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().JoinTables);

RdashDocumentValidator.Validate(document);

Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().JoinTables);

RdashDocumentValidator.Validate(document);

//should still be single after another validate call
Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().JoinTables);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ private static void FixFields(TabularDataDefinition tdd)

static void FixJoinedTables(TabularDataDefinition tdd)
{
if (tdd.DataSourceItem.JoinTables != null)
// Check if there are duplicates
var hasDuplicates = tdd.JoinTables.GroupBy(jt => jt.Alias).Any(g => g.Count() > 1);
if (hasDuplicates)
{
tdd.JoinTables.AddRange(tdd.DataSourceItem.JoinTables.Clone());
tdd.JoinTables = tdd.JoinTables.GroupBy(jt => jt.Alias).Select(g => g.First()).ToList();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Reveal.Sdk.Dom/Visualizations/Visualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public void UpdateDataSourceItem(DataSourceItem dataSourceItem)
if (DataDefinition is TabularDataDefinition tdd)
{
tdd.Fields = dataSourceItem.Fields.Clone();

if(tdd.DataSourceItem.JoinTables != null)
{
tdd.JoinTables.AddRange(tdd.DataSourceItem.JoinTables.Clone());
}
}
}

Expand Down

0 comments on commit 60e6b35

Please sign in to comment.