Skip to content

Commit

Permalink
Merge pull request #100 from RevealBi/develop
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
brianlagunas authored Dec 3, 2024
2 parents 6ab6e69 + 13fda76 commit b00a88b
Show file tree
Hide file tree
Showing 56 changed files with 2,572 additions and 195 deletions.
78 changes: 63 additions & 15 deletions src/Reveal.Sdk.Dom.Tests/Data/DataSourceFixture.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Data;
using System.Reflection;
using Xunit;

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

// Act
var dataSource = new DataSource();

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

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

// Act
var dataSource = new DataSource();

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

[Fact]
public void Constructor_GeneratesUniqueNotEmptyId_WithoutArguments()
{
// Arrange

// Act
var dataSource1 = new DataSource();
var dataSource2 = new DataSource();

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

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

// Act
dataSource1.Id = null;
dataSource2.Id = null;

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

[Fact]
public void DataSource_SetId_NullValue_GeneratesUniqueId()
public void GetId_ReturnSameId_WhenSetNotNullId()
{
// Arrange
var dataSource = new DataSource();
var expectedId = "test-id";

// Act
dataSource.Id = null;
dataSource.Id = expectedId;

// Assert
Assert.NotNull(dataSource.Id);
Assert.Equal(expectedId, dataSource.Id);
}

[Fact]
public void DataSource_DefaultRefreshRate_Should_SetAndGetValue()
public void GetDefaultRefreshRate_ReturnSameValue_WithSetValue()
{
// Arrange
var dataSource = new DataSource();
Expand All @@ -69,23 +93,24 @@ public void DataSource_DefaultRefreshRate_Should_SetAndGetValue()

// Assert
Assert.Equal(expectedValue, actualValue);
Assert.Equal(expectedValue, dataSource.Properties.GetValue<string>("DefaultRefreshRate"));
}

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

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

// Assert
Assert.False(result);
}

[Fact]
public void DataSource_Equals_SameId_ReturnsTrue()
public void CheckEqual_ReturnTrue_WhenIdsAreTheSame()
{
// Arrange
var dataSource1 = new DataSource { Id = "same-id" };
Expand All @@ -99,17 +124,40 @@ public void DataSource_Equals_SameId_ReturnsTrue()
}

[Fact]
public void DataSource_GetHashCode_ReturnsConsistentValue()
public void GetHashCode_ReturnsConsistentValue_ForTheSameValues()
{
// Arrange
var dataSource1 = new DataSource() { Id = "id", Title = "Title", Subtitle = "Sub Title", Provider = DataSourceProvider.REST, SchemaTypeName = SchemaTypeNames.AssetVisualizationDataSpecType};
var dataSource2 = new DataSource() { Id = "id", Title = "Title", Subtitle = "Sub Title", Provider = DataSourceProvider.REST, SchemaTypeName = SchemaTypeNames.AssetVisualizationDataSpecType };

// Act
var hashCode1 = dataSource1.GetHashCode();
var hashCode2 = dataSource2.GetHashCode();

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

[Theory]
[InlineData("SchemaTypeName", SchemaTypeNames.AssetVisualizationDataSpecType)]
[InlineData("Id", "new-id")]
[InlineData("Title", "new-title")]
[InlineData("Subtitle", "new-subtitle")]
[InlineData("Provider", DataSourceProvider.MicrosoftExcel)]
public void GetHashCode_ReturnsDifferentValue_WhenSetDifferentProperties(string propertyName, object propertyValue)
{
// Arrange
var dataSource = new DataSource();

// Act
var hashCode1 = dataSource.GetHashCode();
dataSource.GetType()
.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance).SetValue(dataSource, propertyValue, null);
var hashCode2 = dataSource.GetHashCode();


// Assert
Assert.Equal(hashCode1, hashCode2);
Assert.NotEqual(hashCode1, hashCode2);
}
}
}
126 changes: 126 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/DataSourceItemFactoryFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using Reveal.Sdk.Dom.Data;
using System;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data
{
public class DataSourceItemFactoryFixture
{
[Fact]
public void Create_ReturnDataSourceItem_WithDefaultDataSource()
{
// Arrange
var factory = new DataSourceItemFactory();
var type = DataSourceType.MicrosoftSqlServer;
var id = "1";
var title = "Test Title";

// Act
var result = factory.Create(type, id, title);

// Assert
Assert.NotNull(result);
Assert.IsType<MicrosoftSqlServerDataSourceItem>(result);
Assert.Equal(id, result.Id);
Assert.Equal(title, result.Title);
Assert.Null(result.Subtitle);
Assert.NotNull(result.DataSource);
Assert.IsType<MicrosoftSqlServerDataSource>(result.DataSource);
}

[Fact]
public void Create_ReturnDataSourceItem_WithCustomDataSource()
{
// Arrange
var factory = new DataSourceItemFactory();
var type = DataSourceType.MicrosoftSqlServer;
var id = "1";
var title = "Test Title";
var dataSource = new DataSource();

// Act
var result = factory.Create(type, id, title, dataSource);

// Assert
Assert.NotNull(result);
Assert.IsType<MicrosoftSqlServerDataSourceItem>(result);
Assert.Equal(id, result.Id);
Assert.Equal(title, result.Title);
Assert.Null(result.Subtitle);
Assert.NotNull(result.DataSource);
Assert.NotSame(dataSource, result.DataSource);
Assert.IsType<MicrosoftSqlServerDataSource>(result.DataSource);
}

[Fact]
public void Create_ReturnDataSourceItem_WithCustomSubtitle()
{
// Arrange
var factory = new DataSourceItemFactory();
var type = DataSourceType.MicrosoftSqlServer;
var id = "1";
var title = "Test Title";
var subTitle = "Test Subtitle";

// Act
var result = factory.Create(type, id, title, subTitle);

// Assert
Assert.NotNull(result);
Assert.IsType<MicrosoftSqlServerDataSourceItem>(result);
Assert.Equal(id, result.Id);
Assert.Equal(title, result.Title);
Assert.Equal(subTitle, result.Subtitle);
Assert.NotNull(result.DataSource);
Assert.IsType<MicrosoftSqlServerDataSource>(result.DataSource);
}

[Fact]
public void Create_ReturnDataSourceItem_WithCustomDataSourceAndSubTitle()
{
// Arrange
var factory = new DataSourceItemFactory();
var type = DataSourceType.MicrosoftSqlServer;
var id = "2";
var title = "Test Title";
var subtitle = "Test Subtitle";
var dataSource = new DataSource()
{
Id = "CustomId",
Title = "Custom Title",
Subtitle = "Custom Subtitle",
};

// Act
var result = factory.Create(type, id, title, subtitle, dataSource);

// Assert
Assert.NotNull(result);
Assert.IsType<MicrosoftSqlServerDataSourceItem>(result);
Assert.Equal(id, result.Id);
Assert.Equal(title, result.Title);
Assert.Equal(subtitle, result.Subtitle);
Assert.NotNull(result.DataSource);
Assert.NotSame(dataSource, result.DataSource);
Assert.IsType<MicrosoftSqlServerDataSource>(result.DataSource);
}

[Fact]
public void Create_ThrowNotImplementedException_WhenUnknownType()
{
// Arrange
var factory = new DataSourceItemFactory();
var type = (DataSourceType)99;
var id = "3";
var title = "Test Title";
var subtitle = "Test Subtitle";
var dataSource = new DataSource();

// Act & Assert
Assert.Throws<NotImplementedException>(() => factory.Create(type, id, title));
Assert.Throws<NotImplementedException>(() => factory.Create(type, id, title, dataSource));
Assert.Throws<NotImplementedException>(() => factory.Create(type, id, title, subtitle));
Assert.Throws<NotImplementedException>(() => factory.Create(type, id, title, subtitle, dataSource));
}
}
}
Loading

0 comments on commit b00a88b

Please sign in to comment.