-
Notifications
You must be signed in to change notification settings - Fork 16
Arkadii Ushakov #12
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
Open
arkadyushakov
wants to merge
6
commits into
MewsSystems:main
Choose a base branch
from
arkadyushakov:feature/reservations-improvements-arkadii
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Arkadii Ushakov #12
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f7b0fa6
RE-001: Implemented reservation booking with validation and guest han…
a626ee6
RE-001: Added sln file and unit tests for validators
1ddf738
RE-002: Added conflict detection for reservations and integration tests
78bb506
RE-003: Implemented staff authentication and reservations view
576b2ae
Code cleanup
5e45d2e
Small refactoring to move exception handling to middleware
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
Reservations.Tests/Integration/Repositories/ReservationRepositoryIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
90
Reservations.Tests/Unit/Validators/ReservationValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Validatorsbut this integration test usesTests.Integration.Repositories. For consistency and to match the project structure, this should beReservations.Tests.Integration.Repositories.