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

Update JoinTable logic #67

Merged
merged 3 commits into from
Oct 9, 2024
Merged
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
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
Loading