Skip to content
Open
Show file tree
Hide file tree
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,144 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Repositories;

namespace Tests.Integration.Repositories;

Copilot AI Apr 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The namespace declaration is inconsistent with other test files in the same project. Other test files use Reservations.Tests.Unit.Validators but this integration test uses Tests.Integration.Repositories. For consistency and to match the project structure, this should be Reservations.Tests.Integration.Repositories.

Suggested change
namespace Tests.Integration.Repositories;
namespace Reservations.Tests.Integration.Repositories;

Copilot uses AI. Check for mistakes.

public class ReservationRepositoryIntegrationTests : IDisposable
{
private readonly SqliteConnection _connection;

public ReservationRepositoryIntegrationTests()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();

CreateSchema();
}

private void CreateSchema()
{
_connection.Execute(@"
CREATE TABLE Reservations (
Id TEXT PRIMARY KEY NOT NULL,
GuestEmail TEXT NOT NULL,
RoomNumber INT NOT NULL,
Start INT NOT NULL,
End INT NOT NULL,
CheckedIn INT NOT NULL,
CheckedOut INT NOT NULL
);
");
}

public void Dispose()
{
_connection.Dispose();
}

private async Task InsertReservation(int roomNumber, DateTime start, DateTime end)
{
await _connection.ExecuteAsync(@"
INSERT INTO Reservations (Id, GuestEmail, RoomNumber, Start, End, CheckedIn, CheckedOut)
VALUES (@Id, @GuestEmail, @RoomNumber, @Start, @End, 0, 0);
",
new
{
Id = Guid.NewGuid().ToString(),
GuestEmail = "test@test.com",
RoomNumber = roomNumber,
Start = start.Date,
End = end.Date
});
}

private ReservationRepository CreateRepo()
{
return new ReservationRepository(_connection);
}

[Fact]
public async Task Should_Return_True_When_Dates_Overlap()
{
// Arrange
await InsertReservation(101, new DateTime(2026, 4, 10), new DateTime(2026, 4, 12));
var repo = CreateRepo();

// Act
var result = await repo.HasConflictingReservation(
"101",
new DateTime(2026, 4, 11),
new DateTime(2026, 4, 13));

// Assert
Assert.True(result);
}

[Fact]
public async Task Should_Return_False_When_No_Overlap_Right_Side()
{
// Arrange
await InsertReservation(101, new DateTime(2026, 4, 10), new DateTime(2026, 4, 12));
var repo = CreateRepo();

// Act
var result = await repo.HasConflictingReservation(
"101",
new DateTime(2026, 4, 12),
new DateTime(2026, 4, 14));

// Assert
Assert.False(result);
}

[Fact]
public async Task Should_Return_False_When_No_Overlap_Left_Side()
{
// Arrange
await InsertReservation(101, new DateTime(2026, 4, 10), new DateTime(2026, 4, 12));
var repo = CreateRepo();

// Act
var result = await repo.HasConflictingReservation(
"101",
new DateTime(2026, 4, 8),
new DateTime(2026, 4, 10));

// Assert
Assert.False(result);
}

[Fact]
public async Task Should_Return_True_When_New_Reservation_Fully_Covers_Existing()
{
// Arrange
await InsertReservation(101, new DateTime(2026, 4, 10), new DateTime(2026, 4, 12));
var repo = CreateRepo();

// Act
var result = await repo.HasConflictingReservation(
"101",
new DateTime(2026, 4, 9),
new DateTime(2026, 4, 13));

// Assert
Assert.True(result);
}

[Fact]
public async Task Should_Return_False_For_Different_Room()
{
// Arrange
await InsertReservation(101, new DateTime(2026, 4, 10), new DateTime(2026, 4, 12));
var repo = CreateRepo();

// Act
var result = await repo.HasConflictingReservation(
"102",
new DateTime(2026, 4, 11),
new DateTime(2026, 4, 13));

// Assert
Assert.False(result);
}
}
25 changes: 25 additions & 0 deletions Reservations.Tests/Reservations.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\api\api.csproj" />
</ItemGroup>

</Project>
82 changes: 82 additions & 0 deletions Reservations.Tests/Unit/Validators/EmailValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Validators;

namespace Reservations.Tests.Unit.Validators;

public class EmailValidatorTests
{
//Validate tests
[Fact]
public void Validate_ShouldThrow_WhenEmailIsNull()
{
Assert.Throws<ArgumentException>(() =>
EmailValidator.Validate(null));
}

[Fact]
public void Validate_ShouldThrow_WhenEmailIsEmpty()
{
Assert.Throws<ArgumentException>(() =>
EmailValidator.Validate(""));
}

[Fact]
public void Validate_ShouldThrow_WhenEmailIsWhitespace()
{
Assert.Throws<ArgumentException>(() =>
EmailValidator.Validate(" "));
}

[Fact]
public void Validate_ShouldThrow_WhenEmailIsInvalid()
{
Assert.Throws<ArgumentException>(() =>
EmailValidator.Validate("invalid-email"));
}

[Fact]
public void Validate_ShouldPass_WhenEmailIsValid()
{
EmailValidator.Validate("test@example.com");
}

// IsValid tests
[Fact]
public void IsValid_ShouldReturnTrue_ForValidEmail()
{
var result = EmailValidator.IsValid("test@example.com");

Assert.True(result);
}

[Fact]
public void IsValid_ShouldReturnFalse_WhenMissingAt()
{
var result = EmailValidator.IsValid("testexample.com");

Assert.False(result);
}

[Fact]
public void IsValid_ShouldReturnFalse_WhenMissingDomainDot()
{
var result = EmailValidator.IsValid("test@example");

Assert.False(result);
}

[Fact]
public void IsValid_ShouldReturnFalse_ForInvalidFormat()
{
var result = EmailValidator.IsValid("test@.com");

Assert.False(result);
}

[Fact]
public void IsValid_ShouldReturnFalse_WhenNull()
{
var result = EmailValidator.IsValid(null);

Assert.False(result);
}
}
90 changes: 90 additions & 0 deletions Reservations.Tests/Unit/Validators/ReservationValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Validators;
using Models;

namespace Reservations.Tests.Unit.Validators;

public class ReservationValidatorTests
{
[Fact]
public void ValidateForCreate_ShouldThrow_WhenEndDateIsBeforeStartDate()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 10),
end: new DateTime(2026, 4, 9));

var ex = Assert.Throws<ArgumentException>(() =>
ReservationValidator.ValidateForCreate(reservation));

Assert.Equal("End date must be after start date.", ex.Message);
}

[Fact]
public void ValidateForCreate_ShouldThrow_WhenReservationIsLessThanOneDay()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 10),
end: new DateTime(2026, 4, 10));

var ex = Assert.Throws<ArgumentException>(() =>
ReservationValidator.ValidateForCreate(reservation));

Assert.Equal("Reservation must be at least 1 day long.", ex.Message);
}

[Fact]
public void ValidateForCreate_ShouldThrow_WhenReservationExceedsThirtyDays()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 1),
end: new DateTime(2026, 5, 2)); // 31 days

var ex = Assert.Throws<ArgumentException>(() =>
ReservationValidator.ValidateForCreate(reservation));

Assert.Equal("Reservation cannot exceed 30 days.", ex.Message);
}

[Fact]
public void ValidateForCreate_ShouldPass_WhenReservationIsOneDayLong()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 10),
end: new DateTime(2026, 4, 11));

ReservationValidator.ValidateForCreate(reservation);
}

[Fact]
public void ValidateForCreate_ShouldPass_WhenReservationIsThirtyDaysLong()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 1),
end: new DateTime(2026, 5, 1)); // 30 days

ReservationValidator.ValidateForCreate(reservation);
}

[Fact]
public void ValidateForCreate_ShouldIgnoreTimePart_WhenComparingDates()
{
var reservation = CreateReservation(
start: new DateTime(2026, 4, 10, 23, 0, 0),
end: new DateTime(2026, 4, 11, 1, 0, 0));

ReservationValidator.ValidateForCreate(reservation);
}

private static Reservation CreateReservation(DateTime start, DateTime end)
{
return new Reservation
{
Id = Guid.NewGuid(),
RoomNumber = "101",
GuestEmail = "test@example.com",
Start = start,
End = end,
CheckedIn = false,
CheckedOut = false
};
}
}
Loading
Loading