Skip to content

Commit

Permalink
small updates and added additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed May 24, 2024
1 parent d4ee5d6 commit a9254a2
Show file tree
Hide file tree
Showing 24 changed files with 560 additions and 30 deletions.
1 change: 1 addition & 0 deletions e2e/Sandbox/Reveal/DataSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Reveal.Sdk.Data;
using System.Threading.Tasks;
using Reveal.Sdk.Data.Excel;
using Reveal.Sdk.Data.Microsoft.AnalysisServices;

namespace Sandbox.RevealSDK
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void DataSources_AddedToRdashDocument()

Assert.Empty(document.DataSources);

document.Validate();
RdashDocumentValidator.Validate(document);

Assert.True(document.DataSources.Skip(1).Any());
}
Expand All @@ -38,7 +38,7 @@ public void DataSources_FromVisualizationsAreNotDuplicated()

Assert.Empty(document.DataSources);

document.Validate();
RdashDocumentValidator.Validate(document);

var jsonDataSources = document.DataSources.Where(x => x.Provider == DataSourceProvider.JSON);

Expand All @@ -61,13 +61,82 @@ public void DataSources_FromVisualizations_AndDataSources_AreNotDuplicated()

Assert.Single(document.DataSources);

document.Validate();
RdashDocumentValidator.Validate(document);

var jsonDataSources = document.DataSources.Where(x => x.Provider == DataSourceProvider.JSON);

Assert.Single(jsonDataSources);
}

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

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_PreventsDuplicate_Fields()
{
var dataSourceItem = new DataSourceItem("Test", new DataSource()).SetFields(new List<IField>
{
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);
}

[Fact]
public void Validate_Adds_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);

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

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

RdashDocumentValidator.Validate(document);

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

[Fact]
public void Validate_ThrowsException_WhenDataSourceItem_WithDataSourceId_CantFindDataSource()
{
// Arrange
var dataSourceItem = new DataSourceItem() { DataSourceId = "TEST", Fields = new List<IField>() { new TextField() } };
var viz = new GridVisualization("TEST", dataSourceItem)
{
Title = "Test Visualization",
};
var document = new RdashDocument("Test");
document.Visualizations.Add(viz);

// Act & Assert
Assert.Throws<Exception>(() => RdashDocumentValidator.Validate(document));
}

[Fact]
public void Validate_ThrowsException_WhenDataSourceItemIsNull()
{
Expand Down Expand Up @@ -109,6 +178,7 @@ public void Validate_ThrowsException_WhenFieldIsNull()
[Fact]
public void Validate_ThrowsException_WhenFieldsAreNullOrEmpty()
{
// Arrange
var dataSourceItem = new DataSourceItem()
{
Fields = new List<IField> { null }
Expand Down
113 changes: 113 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Core/VisualizationCollectionFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Reveal.Sdk.Dom.Core;
using Reveal.Sdk.Dom.Visualizations;
using System.Collections.Generic;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Core
{
public class VisualizationCollectionFixture
{
[Fact]
public void Add_AddsVisualizationToCollection()
{
// Arrange
var collection = new VisualizationCollection();
var visualization = new MockVisualization();

// Act
collection.Add(visualization);

// Assert
Assert.Contains(visualization, collection);
}

[Fact]
public void AddRange_AddsVisualizationsToCollection()
{
// Arrange
var collection = new VisualizationCollection();
var visualizations = new List<IVisualization>
{
new MockVisualization(),
new MockVisualization(),
new MockVisualization()
};

// Act
collection.AddRange(visualizations);

// Assert
Assert.Equal(visualizations.Count, collection.Count);
foreach (var visualization in visualizations)
{
Assert.Contains(visualization, collection);
}
}

[Fact]
public void RemoveById_RemovesVisualizationFromCollection()
{
// Arrange
var collection = new VisualizationCollection();
var visualization1 = new MockVisualization { Id = "1" };
var visualization2 = new MockVisualization { Id = "2" };
var visualization3 = new MockVisualization { Id = "3" };
collection.AddRange(new List<IVisualization> { visualization1, visualization2, visualization3 });

// Act
var result = collection.RemoveById("2");

// Assert
Assert.Equal(1, result);
Assert.DoesNotContain(visualization2, collection);
}

[Fact]
public void RemoveByTitle_RemovesVisualizationsFromCollection()
{
// Arrange
var collection = new VisualizationCollection();
var visualization1 = new MockVisualization { Title = "Title 1" };
var visualization2 = new MockVisualization { Title = "Title 2" };
var visualization3 = new MockVisualization { Title = "Title 3" };
collection.AddRange(new List<IVisualization> { visualization1, visualization2, visualization3 });

// Act
var result = collection.RemoveByTitle("Title 2");

// Assert
Assert.Equal(1, result);
Assert.DoesNotContain(visualization2, collection);
}

[Fact]
public void FindById_ReturnsVisualizationFromCollection()
{
// Arrange
var collection = new VisualizationCollection();
var visualization1 = new MockVisualization { Id = "1" };
var visualization2 = new MockVisualization { Id = "2" };
var visualization3 = new MockVisualization { Id = "3" };
collection.AddRange(new List<IVisualization> { visualization1, visualization2, visualization3 });

// Act
var result = collection.FindById("2");

// Assert
Assert.Equal(visualization2, result);
}

private class MockVisualization : IVisualization, IParentDocument
{
public string Id { get; set; }
public string Title { get; set; }
public ChartType ChartType { get; }
public bool IsTitleVisible { get; set; }
public int ColumnSpan { get; set; }
public int RowSpan { get; set; }
public string Description { get; set; }
public IDataDefinition DataDefinition { get; }
RdashDocument IParentDocument.Document { get; set; }
}
}
}
115 changes: 115 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/DataSourceFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Data;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data
{
public class DataSourceFixture
{
[Fact]
public void DataSource_DefaultConstructor_SetsSchemaTypeName()
{
// Arrange
var dataSource = new DataSource();

// Act

// Assert
Assert.Equal(SchemaTypeNames.DataSourceType, dataSource.SchemaTypeName);
}

[Fact]
public void DataSource_DefaultConstructor_SetsProperties()
{
// Arrange
var dataSource = new DataSource();

// Act

// Assert
Assert.NotNull(dataSource.Properties);
}

[Fact]
public void DataSource_DefaultConstructor_GeneratesUniqueId()
{
// Arrange
var dataSource1 = new DataSource();
var dataSource2 = new DataSource();

// Act

// Assert
Assert.NotEqual(dataSource1.Id, dataSource2.Id);
}

[Fact]
public void DataSource_SetId_NullValue_GeneratesUniqueId()
{
// Arrange
var dataSource = new DataSource();

// Act
dataSource.Id = null;

// Assert
Assert.NotNull(dataSource.Id);
}

[Fact]
public void DataSource_DefaultRefreshRate_Should_SetAndGetValue()
{
// Arrange
var dataSource = new DataSource();
var expectedValue = "10 minutes";

// Act
dataSource.DefaultRefreshRate = expectedValue;
var actualValue = dataSource.DefaultRefreshRate;

// Assert
Assert.Equal(expectedValue, actualValue);
}

[Fact]
public void DataSource_Equals_Null_ReturnsFalse()
{
// Arrange
var dataSource = new DataSource();

// Act
var result = dataSource.Equals(null);

// Assert
Assert.False(result);
}

[Fact]
public void DataSource_Equals_SameId_ReturnsTrue()
{
// Arrange
var dataSource1 = new DataSource { Id = "same-id" };
var dataSource2 = new DataSource { Id = "same-id" };

// Act
var result = dataSource1.Equals(dataSource2);

// Assert
Assert.True(result);
}

[Fact]
public void DataSource_GetHashCode_ReturnsConsistentValue()
{
// Arrange
var dataSource = new DataSource();

// Act
var hashCode1 = dataSource.GetHashCode();
var hashCode2 = dataSource.GetHashCode();

// Assert
Assert.Equal(hashCode1, hashCode2);
}
}
}
Loading

0 comments on commit a9254a2

Please sign in to comment.