Skip to content

Commit

Permalink
create quiz repo and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davishoang96 committed Dec 1, 2024
1 parent aac09dd commit ebf6ae0
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 227 deletions.
226 changes: 0 additions & 226 deletions QuizApp.Database/Migrations/QuizContextModelSnapshot.cs

This file was deleted.

3 changes: 3 additions & 0 deletions QuizApp.Database/Models/BaseModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace QuizApp.Database.Models;

public class BaseModel
{
[Key]
public int Id { get; set; }
}
1 change: 0 additions & 1 deletion QuizApp.Database/QuizApp.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Repositories\" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions QuizApp.Database/Repositories/IQuizRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace QuizApp.Database.Repositories;

public interface IQuizRepository
{
Task<int> CreateQuiz(string name);
Task<int> CreateQuestion(int quizId, string question);
}
47 changes: 47 additions & 0 deletions QuizApp.Database/Repositories/QuizRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using QuizApp.Database.Models;

namespace QuizApp.Database.Repositories;

public class QuizRepository : IQuizRepository
{
private readonly QuizContext db;

public QuizRepository(QuizContext db)
{
this.db = db;
}

public async Task<int> CreateQuiz(string name)
{
var model = db.Add(new Quiz
{
QuizName = name,
});

await db.SaveChangesAsync();

return model.Entity.Id;
}

public async Task<int> CreateQuestion(int quizId, string question)
{
var quizModel = db.Quizzes.SingleOrDefault(x => x.Id == quizId);
if (quizModel is null)
{
throw new InvalidOperationException($"Cannot found quiz id = {quizId}");
}

var questionModel = new Question
{
QuizId = quizModel.Id,
Quiz = quizModel,
Title = question,
};

var result = db.Questions.Add(questionModel);

await db.SaveChangesAsync();

return result.Entity.Id;
}
}
52 changes: 52 additions & 0 deletions QuizApp.Test/BaseRepoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using QuizApp.Database;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace QuizApp.Test;

public class BaseRepoTest : IDisposable
{
internal SqliteConnection Connection { get; }
internal DbContextOptions<QuizContext> Options { get; }

public BaseRepoTest()
{
// Create and open a shared SqliteConnection
Connection = new SqliteConnection("DataSource=:memory:");
Connection.Open();

// Configure DbContextOptions to use this connection
Options = new DbContextOptionsBuilder<QuizContext>()
.UseSqlite(Connection)
.Options;

// Ensure the database schema is created
using var context = new QuizContext(Options);
context.Database.EnsureCreated();
}

public void Dispose()
{
// Clean up the connection after all tests are done
Connection.Close();
}

#region IDisposable Support
private bool disposedValue = false;

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Connection.Close();
Connection.Dispose();
}

disposedValue = true;
}
}

#endregion
}
30 changes: 30 additions & 0 deletions QuizApp.Test/QuizApp.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.6" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="xunit" Version="2.9.2" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QuizApp.Database\QuizApp.Database.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit ebf6ae0

Please sign in to comment.