Skip to content

Commit

Permalink
update tests and project settings
Browse files Browse the repository at this point in the history
  • Loading branch information
theandrunique committed Jan 24, 2024
1 parent 63ed8cf commit edb32c7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 58 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
145 changes: 92 additions & 53 deletions SQLModel.Tests/SqliteProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@


using Microsoft.Data.Sqlite;
using NLog;
using NLog.Targets;

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");
Expand All @@ -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");
Expand All @@ -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<LoginsTable>(2);

// an attempt to get an object that does not exist
LoginsTable login1 = session.GetById<LoginsTable>(2);
Assert.Null(login1);

var login = session.GetById<LoginsTable>(1);

// get an existing element
LoginsTable login = session.GetById<LoginsTable>(1);
Assert.NotNull(login);

var profile = session.GetById<ProfilesTable>(login.ProfileId);
ProfilesTable profile = session.GetById<ProfilesTable>(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<LoginsTable> listLogins = session.GetAll<LoginsTable>();
Assert.Single(listLogins);

List<ProfilesTable> listProfiles = session.GetAll<ProfilesTable>();
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<SqliteException>(() =>
{
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<LoginsTable>(2);

LoginsTable login1 = await session.GetById<LoginsTable>(3);
Assert.Null(login1);

var login = await session.GetById<LoginsTable>(1);

LoginsTable login = await session.GetById<LoginsTable>(2);
Assert.NotNull(login);

var profile = await session.GetById<ProfilesTable>(login.ProfileId);
ProfilesTable profile = await session.GetById<ProfilesTable>(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<LoginsTable> listLogins = await session.GetAll<LoginsTable>();
Assert.Single(listLogins);

List<ProfilesTable> listProfiles = await session.GetAll<ProfilesTable>();
Assert.Single(listProfiles);

Assert.Equal("new name", listProfiles[0].Name);
Assert.Equal("new description", listProfiles[0].Description);

await Assert.ThrowsAsync<SqliteException>(async () =>
{
await session.Delete(listProfiles[0]);
});

await session.Delete(listLogins[0]);
await session.Delete(listProfiles[0]);
}
}
}
}
2 changes: 2 additions & 0 deletions SQLModel/SQLModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<TargetFrameworks>net48;net5.0;net7.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>TheandruDotnetOrm</RootNamespace>
<RepositoryUrl>https://github.com/theandrunique/dotnet_orm</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit edb32c7

Please sign in to comment.