mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-10 15:21:27 +00:00
* Reorganize tests into unit/integration pattern * Make DbSets virtual so they can be overridden by tests * Add MessageControllerTests * Implement DigestMiddlewareTests * Refactor SMTPHelper to follow DI pattern which allows for mocking in unit tests. * Fix MailQueueService service registration and shutdown * Implement tests for Status and StatisticsController and reorganize tests * Start working on UserControllerTests * Start refactoring tests to use In-Memory EF provider * Refactor integration tests to reset the database every time Change default unit testing database credentials * Update credentials to use default root with different passwords * Throw exception when integration db is not available instead of falling back to in-memory * Evaluate DbConnected every time * Remove default DbContext constructor * Setup DbContexts with options builder * Convert remaining Moq DbContexts to InMemory ones * Add more tests and use Assert.IsType for testing status code * Add collection attribute to LighthouseServerTest * Remove unused directives and calculate digest in tests * Fix digest calculation in tests * Add test database call * Clear rooms after each test * Fix CommentControllerTests.cs * Disable test parallelization for gameserver tests * Fix failing tests Fix SlotTests Make CreateUser actually add user to database Fix dbConnected Lazy and change expected status codes Properly Remove fragment from url for digest calculation Fix digest calculation for regular requests [skip ci] Remove unused directive Don't use Database CreateUser function Get rid of userId argument for generating random user Rewrite logic for generating random users Fix integration tests * Implement changes from self-code review * Fix registration tests * Replace MailQueueService usages with IMailService
219 lines
No EOL
7.4 KiB
C#
219 lines
No EOL
7.4 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Xml.Serialization;
|
|
using LBPUnion.ProjectLighthouse.Serialization;
|
|
using LBPUnion.ProjectLighthouse.Types.Serialization;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Xunit;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Tests.Integration.Serialization;
|
|
|
|
public class TestSerializable : ILbpSerializable, IHasCustomRoot
|
|
{
|
|
public virtual string GetRoot() => "xmlRoot";
|
|
}
|
|
|
|
[Trait("Category", "Integration")]
|
|
public class SerializationTests
|
|
{
|
|
private static IServiceProvider GetEmptyServiceProvider() => new DefaultServiceProviderFactory().CreateServiceProvider(new ServiceCollection());
|
|
|
|
[Fact]
|
|
public void ShouldNotSerializeNullObject()
|
|
{
|
|
TestSerializable? serializable = null;
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.True(serialized == "");
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeFullEmptyTag()
|
|
{
|
|
TestSerializable serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.True(serialized == "<xmlRoot></xmlRoot>");
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeWithCustomRoot()
|
|
{
|
|
TestSerializable serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.True(serialized == "<xmlRoot></xmlRoot>");
|
|
}
|
|
|
|
public class OverriddenRoot : TestSerializable
|
|
{
|
|
public override string GetRoot() => "xmlRoot2";
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeWithOverriddenRoot()
|
|
{
|
|
OverriddenRoot serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.True(serialized == "<xmlRoot2></xmlRoot2>");
|
|
}
|
|
|
|
[XmlRoot("xmlRoot3")]
|
|
public class RootAttribute : ILbpSerializable { }
|
|
|
|
[Fact]
|
|
public void ShouldSerializeWithRootAttribute()
|
|
{
|
|
RootAttribute serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.True(serialized == "<xmlRoot3></xmlRoot3>");
|
|
}
|
|
|
|
public class DefaultValueInt : TestSerializable
|
|
{
|
|
[DefaultValue(6)]
|
|
[XmlElement("defaultValueInt")]
|
|
public int DefaultValueTest { get; set; } = 6;
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldNotSerializeDefaultValueInt()
|
|
{
|
|
DefaultValueInt serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot></xmlRoot>");
|
|
}
|
|
|
|
public class NonDefaultValueInt : TestSerializable
|
|
{
|
|
[DefaultValue(6)]
|
|
[XmlElement("nonDefaultValueInt")]
|
|
public int NonDefaultValueTest { get; set; }
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldNotSerializeNonDefaultValueInt()
|
|
{
|
|
NonDefaultValueInt serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot><nonDefaultValueInt>0</nonDefaultValueInt></xmlRoot>");
|
|
}
|
|
|
|
public class DefaultNullableStringTest : TestSerializable
|
|
{
|
|
[DefaultValue(null)]
|
|
[XmlElement("defaultNullableString")]
|
|
public string? DefaultNullableString { get; set; }
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldNotSerializeDefaultNullableString()
|
|
{
|
|
DefaultNullableStringTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot></xmlRoot>");
|
|
}
|
|
|
|
public class NullNullableStringTest : TestSerializable
|
|
{
|
|
[XmlElement("nullableString")]
|
|
public string? NullableString { get; set; }
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldNotSerializeNullNullableString()
|
|
{
|
|
NullNullableStringTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot></xmlRoot>");
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeNonNullNullableString()
|
|
{
|
|
NullNullableStringTest serializable = new()
|
|
{
|
|
NullableString = "notNull",
|
|
};
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot><nullableString>notNull</nullableString></xmlRoot>");
|
|
}
|
|
|
|
public class NonEmptyStringTest : TestSerializable
|
|
{
|
|
[XmlElement("stringTest")]
|
|
public string StringTest { get; set; } = "test";
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeNonNullableString()
|
|
{
|
|
NonEmptyStringTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot><stringTest>test</stringTest></xmlRoot>");
|
|
}
|
|
|
|
[XmlRoot("xmlRoot", Namespace = "test")]
|
|
public class NameSpaceTest : TestSerializable
|
|
{
|
|
[XmlElement("test", Namespace = "test2")]
|
|
public int TestValue { get; set; } = 1;
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldExcludeNamespace()
|
|
{
|
|
NameSpaceTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot><test>1</test></xmlRoot>");
|
|
}
|
|
|
|
public class AttributeTest : TestSerializable
|
|
{
|
|
[XmlAttribute("string")]
|
|
public string StringAttribute { get; set; } = "test";
|
|
[XmlAttribute("int")]
|
|
public int NumberAttribute { get; set; } = 5;
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeAttributes()
|
|
{
|
|
AttributeTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized == "<xmlRoot string=\"test\" int=\"5\"></xmlRoot>");
|
|
}
|
|
|
|
public class NestingTest : TestSerializable
|
|
{
|
|
[XmlElement("nested")]
|
|
public NestedType NestedType { get; set; } = new();
|
|
}
|
|
|
|
public class NestedType : ILbpSerializable
|
|
{
|
|
[XmlElement("attributeTest")]
|
|
public AttributeTest AttributeTest { get; set; } = new();
|
|
[XmlElement("nestedString")]
|
|
public NonEmptyStringTest NonEmptyString { get; set; } = new();
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldSerializeNestedType()
|
|
{
|
|
NestingTest serializable = new();
|
|
string serialized = LighthouseSerializer.Serialize(GetEmptyServiceProvider(), serializable);
|
|
Assert.False(string.IsNullOrWhiteSpace(serialized));
|
|
Assert.True(serialized ==
|
|
"<xmlRoot><nested>" +
|
|
"<attributeTest string=\"test\" int=\"5\"></attributeTest>" +
|
|
"<nestedString><stringTest>test</stringTest></nestedString>" +
|
|
"</nested></xmlRoot>");
|
|
}
|
|
|
|
} |