Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed Sep 5, 2024
1 parent 65dfa1f commit 334df14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,51 @@ public void Validate_Adds_Fields()
var document = new RdashDocument();
document.Visualizations.Add(new GridVisualization(dataSourceItem));

Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields);

RdashDocumentValidator.Validate(document);

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

[Fact]
public void Validate_ThrowsException_When_Fields_Are_Null()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource());
dataSourceItem.Fields = null;

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

var exception = Assert.Throws<Exception>(() => RdashDocumentValidator.Validate(document));

Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message);
}

[Fact]
public void Validate_ThrowsException_When_Fields_Are_Empty()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List<IField>());

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

var exception = Assert.Throws<Exception>(() => RdashDocumentValidator.Validate(document));

Assert.Equal("Fields for DataSourceItem Test is null or empty.", exception.Message);
}

[Fact]
public void Validate_PreventsDuplicate_Fields()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List<IField>
{
new TextField("Test"),
new TextField("Test"),
new TextField("Test")
});

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

Assert.Empty(document.Visualizations[0].DataDefinition.AsTabular().Fields);

RdashDocumentValidator.Validate(document);

Assert.Single(document.Visualizations[0].DataDefinition.AsTabular().Fields);
Expand Down
26 changes: 7 additions & 19 deletions src/Reveal.Sdk.Dom/Core/Utilities/RdashDocumentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,15 @@ static void FixVisualizations(RdashDocument document)

private static void FixFields(TabularDataDefinition tdd)
{
if (tdd.DataSourceItem.Fields?.Count != 0)
{
// Create a HashSet to track added field names
HashSet<string> fieldNames = new HashSet<string>(tdd.Fields.Select(f => f.FieldName));

foreach (var field in tdd.DataSourceItem.Fields.Clone())
{
if (field == null)
throw new Exception($"Field for DataSourceItem {tdd.DataSourceItem.Title} is null.");
if (tdd.Fields?.Count == 0)
throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null or empty.");

//prevent adding duplicate fields
if (!fieldNames.Contains(field.FieldName))
{
tdd.Fields.Add(field);
fieldNames.Add(field.FieldName);
}
}
// Check if there are duplicates
var hasDuplicates = tdd.Fields.GroupBy(f => f.FieldName).Any(g => g.Count() > 1);
if (hasDuplicates)
{
tdd.Fields = tdd.Fields.GroupBy(f => f.FieldName).Select(g => g.First()).ToList();
}

if (tdd.Fields?.Count == 0)
throw new Exception($"Fields for DataSourceItem {tdd.DataSourceItem.Title} is null.");
}

static void FixJoinedTables(TabularDataDefinition tdd)
Expand Down

0 comments on commit 334df14

Please sign in to comment.