Skip to content

Commit

Permalink
Merge pull request #67 from RevealBi/remove-duplicates-during-jointab…
Browse files Browse the repository at this point in the history
…les-validation

Update JoinTable logic
  • Loading branch information
brianlagunas authored Oct 9, 2024
2 parents c2be041 + 9bc2438 commit f655089
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 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_PreventsDuplicates_OnMultipleValidations()
{
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
22 changes: 21 additions & 1 deletion src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void DataSourceItem_DataSource_SetValue()
}

[Fact]
public void DataSourceItem_Join_AddsJoinTable()
public void DataSourceItem_Join_AddsJoinTableUsingJoinConditions()
{
// Arrange
var dataSourceItem = new DataSourceItem();
Expand All @@ -111,6 +111,26 @@ public void DataSourceItem_Join_AddsJoinTable()
Assert.Equal("Alias.[right]", dataSourceItem.JoinTables[0].JoinConditions[0].RightFieldName);
}

[Fact]
public void DataSourceItem_Join_AddsJoinTableUsingLeftAndRightFieldNames()
{
// Arrange
var dataSourceItem = new DataSourceItem();
var joinConditions = new List<JoinCondition> { new JoinCondition("left", "right") };
var dataSourceItemToJoin = new DataSourceItem();

// Act
dataSourceItem.Join("Alias", "left", "right", dataSourceItemToJoin);

// Assert
Assert.Single(dataSourceItem.JoinTables);
Assert.Equal("Alias", dataSourceItem.JoinTables[0].Alias);
Assert.Equal(dataSourceItemToJoin, dataSourceItem.JoinTables[0].DataDefinition.DataSourceItem);
Assert.Single(dataSourceItem.JoinTables[0].JoinConditions);
Assert.Equal("[left]", dataSourceItem.JoinTables[0].JoinConditions[0].LeftFieldName);
Assert.Equal("Alias.[right]", dataSourceItem.JoinTables[0].JoinConditions[0].RightFieldName);
}

[Theory]
[InlineData("FieldName", "[FieldName]")]
[InlineData("[FieldName]", "[FieldName]")]
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.DataSourceItem.JoinTables.Count > 0)
{
tdd.JoinTables = tdd.DataSourceItem.JoinTables.Clone();
}
}
}

Expand Down

0 comments on commit f655089

Please sign in to comment.