From edb32c7852c399b1acbc5b6992ad8aff11a7fa69 Mon Sep 17 00:00:00 2001 From: theandrunique Date: Wed, 24 Jan 2024 20:18:00 +0300 Subject: [PATCH] update tests and project settings --- .github/workflows/dotnet.yml | 5 - SQLModel.Tests/SqliteProviderTests.cs | 145 ++++++++++++++++---------- SQLModel/SQLModel.csproj | 2 + 3 files changed, 94 insertions(+), 58 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 8be7787..975c905 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -32,10 +32,5 @@ jobs: - name: Test run: dotnet test SQLModel.Tests/SQLModel.Tests.csproj -c Release --verbosity normal - - name: Check version.md - run: | - git diff --name-only ${{github.event.before}} ${{github.sha}} | grep 'version.md' && echo "VERSION_CHANGED=true" >> $GITHUB_ENV - - name: Publish the package to GPR - if: ${{ env.VERSION_CHANGED == 'true' }} run: dotnet nuget push SQLModel/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --skip-duplicate --api-key ${{secrets.NUGET_API_KEY}} diff --git a/SQLModel.Tests/SqliteProviderTests.cs b/SQLModel.Tests/SqliteProviderTests.cs index c7dcec3..6340fcd 100644 --- a/SQLModel.Tests/SqliteProviderTests.cs +++ b/SQLModel.Tests/SqliteProviderTests.cs @@ -1,5 +1,4 @@ - - +using Microsoft.Data.Sqlite; using NLog; using NLog.Targets; @@ -7,11 +6,28 @@ namespace SQLModel.Tests { public class SqliteProviderTests { - Logger log = LogManager.GetCurrentClassLogger(); - + public class LoginsTable : BaseModel + { + public static new string Tablename = "logins"; + [PrimaryKey()] + public int Id { get; set; } + [ForeignKey("profiles.Id")] + public int ProfileId { get; set; } + } + public class ProfilesTable : BaseModel + { + public static new string Tablename = "profiles"; + [PrimaryKey()] + public int Id { get; set; } + [Field()] + public string? Name { get; set; } + [Field()] + public string? Description { get; set; } + } [Fact] - public void CreateTablesSqliteTest() + public async void SqliteTests() { + // deleting the logs and database of the last test if (File.Exists("orm.log")) { File.Delete("orm.log"); @@ -20,11 +36,11 @@ public void CreateTablesSqliteTest() { File.Delete("test.db"); } - if (File.Exists("testasync.db")) - { - File.Delete("testasync.db"); - } + // creating an instance of logger + Logger log = LogManager.GetCurrentClassLogger(); + + // setting up the logger var config = new NLog.Config.LoggingConfiguration(); var logFile = new FileTarget("logFile") { FileName = $"orm.log" }; var logconsole = new ConsoleTarget("logconsole"); @@ -33,108 +49,131 @@ public void CreateTablesSqliteTest() config.AddRule(LogLevel.Info, LogLevel.Fatal, logFile); LogManager.Configuration = config; + // creating an instance of core Core core = new Core(DatabaseEngine.Sqlite, "Data Source=test.db", log, dropErrors: true); + // creating tables core.Metadata.CreateAll(); - } - [Fact] - public void CheckTables() - { - Core core = new Core(DatabaseEngine.Sqlite, "Data Source=test.db", log, dropErrors: true); + + // start a new session using (var session = core.CreateSession()) { - var profile = new ProfilesTable() + // creating an instance of ProfilesTable + ProfilesTable profile = new ProfilesTable() { Name = "Name", Description = "Description", }; + + // add element to the table session.Add(profile); + // creating an instance of LoginsTable var login = new LoginsTable() { ProfileId = 1, }; + + // add element to the table session.Add(login); } using (var session = core.CreateSession()) { - var login1 = session.GetById(2); - + // an attempt to get an object that does not exist + LoginsTable login1 = session.GetById(2); Assert.Null(login1); - var login = session.GetById(1); - + // get an existing element + LoginsTable login = session.GetById(1); Assert.NotNull(login); - var profile = session.GetById(login.ProfileId); + ProfilesTable profile = session.GetById(login.ProfileId); + // update the fields profile.Name = "new name"; - profile.Description = "new description"; + // save changes session.Update(profile); } - } - [Fact] - public async void CrudAsyncOperationsTests() - { - Core core = new Core(DatabaseEngine.Sqlite, "Data Source=testasync.db", log, dropErrors: true); - core.Metadata.CreateAll(); + using (var session = core.CreateSession()) + { + List listLogins = session.GetAll(); + Assert.Single(listLogins); + List listProfiles = session.GetAll(); + Assert.Single(listProfiles); + + Assert.Equal("new name", listProfiles[0].Name); + Assert.Equal("new description", listProfiles[0].Description); + + // an error due to a foreign key constraint + Assert.Throws(() => + { + session.Delete(listProfiles[0]); + }); + + session.Delete(listLogins[0]); + session.Delete(listProfiles[0]); + } + + + // the same test with the AsyncSession using (var session = await core.CreateAsyncSession()) { - var profile = new ProfilesTable() + ProfilesTable profile = new ProfilesTable() { Name = "Name", Description = "Description", }; + await session.Add(profile); var login = new LoginsTable() { - ProfileId = 1, + ProfileId = 2, }; + await session.Add(login); } using (var session = await core.CreateAsyncSession()) { - var login1 = await session.GetById(2); - + LoginsTable login1 = await session.GetById(3); Assert.Null(login1); - var login = await session.GetById(1); - + LoginsTable login = await session.GetById(2); Assert.NotNull(login); - var profile = await session.GetById(login.ProfileId); + ProfilesTable profile = await session.GetById(login.ProfileId); profile.Name = "new name"; - profile.Description = "new description"; await session.Update(profile); } - } - public class LoginsTable : BaseModel - { - public static new string Tablename = "logins"; - [PrimaryKey()] - public int Id { get; set; } - [ForeignKey("profiles.Id")] - public int ProfileId { get; set; } - } - public class ProfilesTable : BaseModel - { - public static new string Tablename = "profiles"; - [PrimaryKey()] - public int Id { get; set; } - [Field()] - public string? Name { get; set; } - [Field()] - public string? Description { get; set; } + + using (var session = await core.CreateAsyncSession()) + { + List listLogins = await session.GetAll(); + Assert.Single(listLogins); + + List listProfiles = await session.GetAll(); + Assert.Single(listProfiles); + + Assert.Equal("new name", listProfiles[0].Name); + Assert.Equal("new description", listProfiles[0].Description); + + await Assert.ThrowsAsync(async () => + { + await session.Delete(listProfiles[0]); + }); + + await session.Delete(listLogins[0]); + await session.Delete(listProfiles[0]); + } } } } \ No newline at end of file diff --git a/SQLModel/SQLModel.csproj b/SQLModel/SQLModel.csproj index 24bd5d8..658cfc1 100644 --- a/SQLModel/SQLModel.csproj +++ b/SQLModel/SQLModel.csproj @@ -8,6 +8,8 @@ net48;net5.0;net7.0 True true + TheandruDotnetOrm + https://github.com/theandrunique/dotnet_orm