Skip to content

Commit

Permalink
Merge pull request #407 from RevealBi/visualization/linear-gauge
Browse files Browse the repository at this point in the history
Create unit tests LinearGaugeVisualization
  • Loading branch information
hainv-ohio authored Jan 23, 2025
2 parents 169de82 + ec91ed0 commit 16ef852
Showing 1 changed file with 232 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Visualizations;

public class LinearGaugeVisualizationFixture
{
[Fact]
public void Constructor_FieldsHaveDefaultValues_WhenInstanceIsCreated()
{
// Act
var visualization = new LinearGaugeVisualization();

// Assert
Assert.Null(visualization.Title);
Assert.Null(visualization.DataDefinition);
Assert.Equal(ChartType.LinearGauge, visualization.ChartType);
}

[Fact]
public void Constructor_SetsDataSourceItem_WhenOnlyDataSourceItemProvided()
{
// Arrange
var dataSourceItem = new DataSourceItem();

// Act
var visualization = new LinearGaugeVisualization(dataSourceItem);

// Assert
Assert.Equal(dataSourceItem, visualization.DataDefinition.DataSourceItem);
Assert.Equal(ChartType.LinearGauge, visualization.ChartType);
}

[Fact]
public void Constructor_SetsTitleAndDataSourceItem_WhenBothArgumentsAreProvided()
{
// Arrange
var title = "Test Linear Gauge";
var dataSourceItem = new DataSourceItem();

// Act
var visualization = new LinearGaugeVisualization(title, dataSourceItem);

// Assert
Assert.Equal(title, visualization.Title);
Assert.Equal(dataSourceItem, visualization.DataDefinition.DataSourceItem);
Assert.Equal(ChartType.LinearGauge, visualization.ChartType);
}

[Fact]
public void ToJsonString_GeneratesCorrectJson_WhenSerialized()
{
// Arrange
var expectedJson =
"""
[ {
"Id" : "6f72810e-cc44-4a80-90ad-4b9c9162d44b",
"Title" : "Linear Gauge",
"IsTitleVisible" : true,
"ColumnSpan" : 0,
"RowSpan" : 0,
"VisualizationSettings" : {
"_type" : "GaugeVisualizationSettingsType",
"ViewType" : "Linear",
"GaugeBands" : [ {
"_type" : "GaugeBandType",
"Type" : "NumberValue",
"Color" : "Green",
"Value" : 10000.0
}, {
"_type" : "GaugeBandType",
"Type" : "NumberValue",
"Color" : "Yellow",
"Value" : 5000.0
}, {
"_type" : "GaugeBandType",
"Type" : "NumberValue",
"Color" : "Red"
} ],
"VisualizationType" : "GAUGE"
},
"DataSpec" : {
"_type" : "TabularDataSpecType",
"IsTransposed" : false,
"Fields" : [ {
"FieldName" : "Date",
"FieldLabel" : "Date",
"UserCaption" : "Date",
"IsCalculated" : false,
"Properties" : { },
"Sorting" : "None",
"FieldType" : "Date"
}, {
"FieldName" : "Spend",
"FieldLabel" : "Spend",
"UserCaption" : "Spend",
"IsCalculated" : false,
"Properties" : { },
"Sorting" : "None",
"FieldType" : "Number"
} ],
"TransposedFields" : [ ],
"QuickFilters" : [ ],
"AdditionalTables" : [ ],
"ServiceAdditionalTables" : [ ],
"DataSourceItem" : {
"_type" : "DataSourceItemType",
"Id" : "080cc17d-4a0a-4837-aa3f-ef2571ea443a",
"Title" : "Sample Data",
"Subtitle" : "Excel Data Source Item",
"DataSourceId" : "__JSON",
"HasTabularData" : true,
"HasAsset" : false,
"Properties" : { },
"Parameters" : {
"config" : {
"iterationDepth" : 0,
"columnsConfig" : [ {
"key" : "Date",
"type" : 2
}, {
"key" : "Spend",
"type" : 1
} ]
}
},
"ResourceItem" : {
"_type" : "DataSourceItemType",
"Id" : "d593dd79-7161-4929-afc9-c26393f5b650",
"Title" : "Marketing Sheet",
"Subtitle" : "Excel Data Source Item",
"DataSourceId" : "33077d1e-19c5-44fe-b981-6765af3156a6",
"HasTabularData" : true,
"HasAsset" : false,
"Properties" : {
"Url" : "http://dl.infragistics.com/reportplus/reveal/samples/Samples.xlsx"
},
"Parameters" : { }
}
},
"Expiration" : 1440,
"Bindings" : {
"Bindings" : [ ]
}
},
"VisualizationDataSpec" : {
"_type" : "LinearGaugeVisualizationDataSpecType",
"Value" : [ {
"_type" : "MeasureColumnSpecType",
"SummarizationField" : {
"_type" : "SummarizationValueFieldType",
"FieldLabel" : "Spend",
"UserCaption" : "Spend",
"IsHidden" : false,
"AggregationType" : "Sum",
"Sorting" : "None",
"IsCalculated" : false,
"FieldName" : "Spend"
}
} ],
"Target" : [ ],
"FormatVersion" : 0,
"AdHocExpandedElements" : [ ],
"Rows" : [ {
"_type" : "DimensionColumnSpecType",
"SummarizationField" : {
"_type" : "SummarizationDateFieldType",
"DateAggregationType" : "Month",
"DrillDownElements" : [ ],
"ExpandedItems" : [ ],
"FieldName" : "Date"
}
} ]
}
} ]
""";

var document = new RdashDocument("Dashboard");

var dataSourceItem = new RestDataSourceItem("Sample Data")
{
Id = "080cc17d-4a0a-4837-aa3f-ef2571ea443a",
Subtitle = "Excel Data Source Item",
Url = "http://dl.infragistics.com/reportplus/reveal/samples/Samples.xlsx",
IsAnonymous = true,
ResourceItem = new DataSourceItem
{
Id = "d593dd79-7161-4929-afc9-c26393f5b650",
DataSourceId = "33077d1e-19c5-44fe-b981-6765af3156a6",
Title = "Marketing Sheet",
Subtitle = "Excel Data Source Item",
HasTabularData = true,
HasAsset = false,
Properties = new Dictionary<string, object>
{
{ "Url", "http://dl.infragistics.com/reportplus/reveal/samples/Samples.xlsx" }
}
},
Fields = new List<IField>
{
new DateField("Date"),
new NumberField("Spend"),
}
};

document.Visualizations.Add(new LinearGaugeVisualization("Linear Gauge", dataSourceItem)
{
Id = "6f72810e-cc44-4a80-90ad-4b9c9162d44b",
IsTitleVisible = true,
}
.SetLabel(new DateDataField("Date") { AggregationType = DateAggregationType.Month }).SetValue("Spend")
.ConfigureSettings(settings =>
{
settings.ValueComparisonType = ValueComparisonType.Number;
settings.UpperBand.Value = 10000;
settings.MiddleBand.Value = 5000;
}));;

// Act
var json = document.ToJsonString();
var actualJson = JObject.Parse(json)["Widgets"];
var actualNormalized = JsonConvert.SerializeObject(actualJson, Formatting.Indented);
var expectedNormalized = JArray.Parse(expectedJson).ToString(Formatting.Indented);

// Assert
Assert.Equal(expectedNormalized.Trim(), actualNormalized.Trim());
}
}

0 comments on commit 16ef852

Please sign in to comment.