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

Write Unit tests for Single Value Visualizations (PieChartVisualization) #237

Merged
merged 8 commits into from
Jan 8, 2025
Merged
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
@@ -0,0 +1,269 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Reveal.Sdk.Dom.Core.Serialization;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Filters;
using Reveal.Sdk.Dom.Visualizations;
using Reveal.Sdk.Dom.Visualizations.Settings;
using Reveal.Sdk.Dom.Visualizations.VisualizationSpecs;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Visualizations;

public class PieChartVisualizationFixture
{
[Fact]
public void Constructor_InitializesDefaultValues_WhenInstanceIsCreated()
{
//Act
var visualization = new PieChartVisualization();

//Assert
Assert.Equal(ChartType.Pie, visualization.ChartType);
Assert.Equal(0, visualization.ColumnSpan);
Assert.Null(visualization.DataDefinition);
Assert.Null(visualization.Description);
Assert.NotNull(visualization.Id);
Assert.True(visualization.IsTitleVisible);
Assert.NotNull(visualization.Labels);
Assert.Empty(visualization.Labels);
Assert.Null(visualization.Linker);
Assert.Equal(0, visualization.RowSpan);
Assert.NotNull(visualization.Settings);
Assert.IsType<PieChartVisualizationSettings>(visualization.Settings);
Assert.Null(visualization.Title);
Assert.NotNull(visualization.Values);
Assert.Empty(visualization.Values);
Assert.NotNull(visualization.VisualizationDataSpec);
Assert.IsType<SingleValueLabelsVisualizationDataSpec>(visualization.VisualizationDataSpec);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void Constructor_InitializesPieChartVisualizationWithDataSourceItem_WhenDataSourceItemIsProvided(
bool hasTabularData)
{
// Arrange
var dataSourceItem = new DataSourceItem { HasTabularData = hasTabularData };

// Act
var pieChartVisualization = new PieChartVisualization(dataSourceItem);

// Assert
Assert.Equal(ChartType.Pie, pieChartVisualization.ChartType);
Assert.Equal(dataSourceItem, pieChartVisualization.DataDefinition.DataSourceItem);
Assert.Null(pieChartVisualization.Title);
Assert.Equal(hasTabularData, pieChartVisualization.DataDefinition.DataSourceItem.HasTabularData);
Assert.IsType(
hasTabularData
? typeof(TabularDataDefinition)
: typeof(XmlaDataDefinition),
pieChartVisualization.DataDefinition);
}

[Theory]
[InlineData("Test Title", null, null)]
[InlineData(null, null, null)]
[InlineData("Test Title with Data Source", true, true)]
[InlineData("Test Title without Tabular Data", false, false)]
public void Constructor_SetsTitleAndDataSourceItem_WhenArgumentsAreProvided(string title, bool? hasTabularData,
bool? expectedHasTabularData)
{
// Arrange
var dataSourceItem = hasTabularData.HasValue
? new DataSourceItem { HasTabularData = hasTabularData.Value }
: null;
// Act
var pieChartVisualization = new PieChartVisualization(title, dataSourceItem);

// Assert
Assert.Equal(title, pieChartVisualization.Title);
Assert.Equal(ChartType.Pie, pieChartVisualization.ChartType);

if (dataSourceItem == null)
{
Assert.Null(pieChartVisualization.DataDefinition);
}
else
{
Assert.NotNull(pieChartVisualization.DataDefinition);
Assert.Equal(expectedHasTabularData, pieChartVisualization.DataDefinition.DataSourceItem.HasTabularData);
Assert.IsType(
hasTabularData.Value
? typeof(TabularDataDefinition)
: typeof(XmlaDataDefinition),
pieChartVisualization.DataDefinition);
}
}

[Fact]
public void ToJsonString_GeneratesCorrectJson_WhenPieChartVisualizationIsSerialized()
{
//Arrange
var expectedJson =
"""
[ {
"Description" : "Pie Chart Visualization",
"Id" : "60344a4a-d0ce-4364-9f8c-acfbb8caa32e",
"Title" : "Pie Chart Visualization",
"IsTitleVisible" : true,
"ColumnSpan" : 0,
"RowSpan" : 0,
"VisualizationSettings" : {
"_type" : "ChartVisualizationSettingsType",
"ShowZeroValuesInLegend" : false,
"ShowLegends" : true,
"LabelDisplayMode" : "Value",
"OthersSliceThreshold" : 3.0,
"ChartType" : "Pie",
"VisualizationType" : "CHART"
},
"DataSpec" : {
"_type" : "TabularDataSpecType",
"IsTransposed" : false,
"Fields" : [ {
"FieldName" : "Date",
"FieldLabel" : "Date",
"UserCaption" : "Date",
"IsCalculated" : false,
"Properties" : { },
"Sorting" : "None",
"FieldType" : "Date"
}, {
"FieldName" : "Conversions",
"FieldLabel" : "Conversions",
"UserCaption" : "Conversions",
"IsCalculated" : false,
"Properties" : { },
"Sorting" : "None",
"FieldType" : "String"
}, {
"FieldName" : "Territory",
"FieldLabel" : "Territory",
"UserCaption" : "Territory",
"IsCalculated" : false,
"Properties" : { },
"Sorting" : "None",
"FieldType" : "String"
} ],
"TransposedFields" : [ ],
"QuickFilters" : [ ],
"AdditionalTables" : [ ],
"ServiceAdditionalTables" : [ ],
"DataSourceItem" : {
"_type" : "DataSourceItemType",
"Id" : "080cc17d-4a0a-4837-aa3f-ef2571ea443a",
"Title" : "Marketing Sheet",
"Subtitle" : "Excel Data Source Item",
"DataSourceId" : "__EXCEL",
"HasTabularData" : true,
"HasAsset" : false,
"Properties" : {
"Sheet" : "Marketing"
},
"Parameters" : { },
"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" : "SingleValueLabelsVisualizationDataSpecType",
"Value" : [ {
"_type" : "MeasureColumnSpecType",
"SummarizationField" : {
"_type" : "SummarizationValueFieldType",
"FieldLabel" : "Conversions",
"UserCaption" : "Conversions",
"IsHidden" : false,
"AggregationType" : "Sum",
"Sorting" : "None",
"IsCalculated" : false,
"FieldName" : "Conversions"
}
} ],
"FormatVersion" : 0,
"AdHocExpandedElements" : [ ],
"Rows" : [ {
"_type" : "DimensionColumnSpecType",
"SummarizationField" : {
"_type" : "SummarizationRegularFieldType",
"DrillDownElements" : [ ],
"ExpandedItems" : [ ],
"FieldName" : "Territory"
}
} ]
}
} ]
""";

var document = new RdashDocument("My Dashboard");

var excelDataSourceItem = new RestDataSourceItem("Marketing Sheet")
{
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 TextField("Conversions"),
new TextField("Territory"),
}
};
excelDataSourceItem.UseExcel("Marketing");

document.Visualizations.Add(new PieChartVisualization("Pie Chart Visualization", excelDataSourceItem)
{
Id = "60344a4a-d0ce-4364-9f8c-acfbb8caa32e",
IsTitleVisible = true,
Description = "Pie Chart Visualization"
}
.SetLabel("Territory").SetValue("Conversions")
.ConfigureSettings(settings =>
{
settings.SliceLabelDisplay = LabelDisplayMode.Value;
}));

document.Filters.Add(new DashboardDateFilter("My Date Filter"));

//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());
}
}
Loading