Skip to content

Commit

Permalink
rdash unit tests for Rest DS
Browse files Browse the repository at this point in the history
  • Loading branch information
recca5p committed Dec 4, 2024
1 parent ff1bb42 commit f851908
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Reveal.Sdk.Dom.Core.Constants;
using Newtonsoft.Json.Linq;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Core.Serialization;
using Reveal.Sdk.Dom.Core.Utilities;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -404,5 +408,21 @@ public void UseCsv_Should_Add_TwoDataSources()
Assert.Equal(DataSourceIds.CSV, document.DataSources[0].Id);
Assert.Equal(DataSourceProvider.CSV, document.DataSources[0].Provider);
}

[Fact]
public void RDashDocument_HasCorrectDataSourceItem_WhenLoadFromFile()
{
// Arrange
var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestRest.rdash");

// Act
var document = RdashDocument.Load(filePath);
var dataSource = document.DataSources.LastOrDefault();
var dataSourceItem = document.Visualizations.LastOrDefault().DataDefinition.DataSourceItem;

// Assert
Assert.Equal(DataSourceProvider.REST, dataSource.Provider);
Assert.NotNull(dataSourceItem.Properties.GetValue<bool>("ServerAggregation"));
}
}
}
70 changes: 70 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/DataSources/RestDataSourceFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Core.Serialization;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data.DataSources
Expand Down Expand Up @@ -104,5 +109,70 @@ public void Method_SetsAndGetsValue_WithDifferentInputs(string method)
Assert.Equal(method, dataSource.Method);
Assert.Equal(method, dataSource.Properties.GetValue<string>("Method"));
}

[Fact]
public void RDashDocument_HasCorrectDataSource_WhenLoadFromFile()
{
// Arrange
var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestRest.rdash");

// Act
var document = RdashDocument.Load(filePath);
var dataSource = document.DataSources.LastOrDefault();

// Assert
Assert.Equal(DataSourceProvider.REST, dataSource.Provider);
Assert.NotNull(dataSource.Properties.GetValue<string>("Url"));
Assert.NotNull(dataSource.Properties.GetValue<string>("_rpUseAnonymousAuthentication"));
Assert.NotNull(dataSource.Properties.GetValue<string>("_rpUsePreemptiveAuthentication"));
}

[Fact]
public void ToJsonString_CreatesFormattedJson_ForRestDataSource()
{
// Arrange
var expectedJson = @"
{
""_type"": ""DataSourceType"",
""Id"": ""Rest"",
""Provider"": ""REST"",
""Description"": ""Rest DS"",
""Subtitle"": ""Excel2Json"",
""Properties"": {}
}";

var dataSource = new RestDataSource()
{
Id = "Rest",
Title = "Rest DS",
DefaultRefreshRate = "120",
Url = "https://excel2json.io/api/share/6e0f06b3-72d3-4fec-7984-08da43f56bb9",
Subtitle = "Excel2Json"
};

var dataSourceItems = new RestDataSourceItem("DB Test", dataSource)
{
Id = "RestItem",
Title = "Rest DS Item",
Fields = new List<IField>
{
new TextField("_id"),
new TextField("name"),
}
};

var document = new RdashDocument("My Dashboard");
document.Visualizations.Add(new GridVisualization("Test List", dataSourceItems).SetColumns("name"));

// Act
RdashSerializer.SerializeObject(document);
var json = document.ToJsonString();
var jObject = JObject.Parse(json);
var actualJObject = jObject["DataSources"].LastOrDefault();
var expectedJObject = JObject.Parse(expectedJson);

// Assert
Assert.Equal(expectedJObject, actualJObject);
}
}
}
7 changes: 3 additions & 4 deletions src/Reveal.Sdk.Dom.Tests/Reveal.Sdk.Dom.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
<None Update="Dashboards\Sales.rdash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Dashboards\" />
<None Update="Dashboards\TestRest.rdash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit f851908

Please sign in to comment.