From 60e6b35e58f918733e444171d2673de1c8d212a5 Mon Sep 17 00:00:00 2001 From: Sebastian Rodriguez Date: Mon, 7 Oct 2024 17:22:44 -0300 Subject: [PATCH] Moved JoinTables copy to the Visualization.UpdateDataSourceItem 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 --- .../RdashDocumentValidatorFixture.cs | 29 +++++++++++++++++-- .../Core/Utilities/RdashDocumentValidator.cs | 6 ++-- .../Visualizations/Visualization.cs | 5 ++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs index ea46dce8..7d1f6e2b 100644 --- a/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Core/Utilities/RdashDocumentValidatorFixture.cs @@ -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 { new TextField() }); var joinConditions = new List { new JoinCondition("left", "right") }; var dataSourceItemToJoin = new DataSourceItem().SetFields(new List { 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 { new TextField() }); + var joinConditions = new List { new JoinCondition("left", "right") }; + var dataSourceItemToJoin = new DataSourceItem().SetFields(new List { 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); } diff --git a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs index 99dd10d1..78366f5f 100644 --- a/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs +++ b/src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs @@ -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(); } } diff --git a/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs b/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs index 8c4241b2..154332d6 100644 --- a/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs +++ b/src/Reveal.Sdk.Dom/Visualizations/Visualization.cs @@ -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()); + } } }