Skip to content

Commit

Permalink
Update SQLite readme. Add SQLite to nuget tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebjenkins committed Dec 29, 2023
1 parent 7bd61f4 commit 14d04c6
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 8 deletions.
2 changes: 2 additions & 0 deletions KeyValueRepo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{BBC36D7D-AFFC-4461-896C-FD6DCCC4EF90}"
ProjectSection(SolutionItems) = preProject
.github\workflows\benchamrk.yml = .github\workflows\benchamrk.yml
.github\workflows\dev-ci-sql-lite.yml = .github\workflows\dev-ci-sql-lite.yml
.github\workflows\dev-ci.yml = .github\workflows\dev-ci.yml
.github\workflows\main-publish-sql-lite.yml = .github\workflows\main-publish-sql-lite.yml
.github\workflows\main-publish.yml = .github\workflows\main-publish.yml
.github\workflows\working-branches.yml = .github\workflows\working-branches.yml
EndProjectSection
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# KeyValueRepo
# KeyValueRepo
[![.github/workflows/dev-ci.yml](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/dev-ci.yml/badge.svg?branch=develop)](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/dev-ci.yml)
[![.github/workflows/main-publish.yml](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/main-publish.yml/badge.svg?branch=main)](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/main-publish.yml)
[![NuGet](https://img.shields.io/nuget/dt/calebs.keyvaluerepo.svg)](https://www.nuget.org/packages/Calebs.KeyValueRepo)
Expand Down Expand Up @@ -43,10 +43,10 @@ The package `version` is defined in the `KeyValueRepo.csproj` file, using .NET S

## Release History & Development Goals
- 0.1.0 - initial release (in Memory)
- 0.2.0 - added SqlLite Implementation
- 0.2.0 - added SqlLite Implementation
- ✔ SQLite [Nuget Package](https://www.nuget.org/packages/Calebs.KeyValueRepo.SQLite/)
### Future Goals
---
- SqlLite Nuget Package
- New Feature: KV Meta Objects
- New Repo: Sql Server Implementation
- New Repo: Azure Tables Implementation
Expand Down
6 changes: 3 additions & 3 deletions src/KeyValueSqlLiteRepo/KeyValueSqlLiteRepo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
<None Include=".\README.md">
<PackagePath></PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions src/KeyValueSqlLiteRepo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# KeyValueRepo.SQLite
[![.github/workflows/dev-ci-sql-lite.yml](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/dev-ci-sql-lite.yml/badge.svg?branch=develop)](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/dev-ci.yml)
[![.github/workflows/main-publish-sql-lite.yml](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/main-publish.yml/badge.svg?branch=main)](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/main-publish-sql-lite.yml)
[![NuGet](https://img.shields.io/nuget/dt/calebs.keyvaluerepo.svg)](https://www.nuget.org/packages/Calebs.KeyValueRepo.SQLite)
[![NuGet](https://img.shields.io/nuget/vpre/calebs.keyvaluerepo.svg)](https://www.nuget.org/packages/Calebs.KeyValueRepo.SQLite)
[![.github/workflows/working-branches.yml](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/working-branches.yml/badge.svg)](https://github.com/calebjenkins/KeyValueRepo/actions/workflows/working-branches.yml)

A SqlLite implementation of a generic the KeyValueRepo interface for Key Value storage operations. Ideal for simple key-values that might migrate to SQL Server or NoSQL - a quick and easy way to create a KeyValue store.

By default, this library ships with an in-memory implementation. `KeyValueInMemory` which implements the `IKeyValueRepo` interface.

## Installing KeyValueRepo

You should install [Extensions with NuGet](https://www.nuget.org/packages/Calebs.KeyValueRepo.SQLite):

Install-Package Calebs.KeyValueRepo.SQLite

Or via the .NET Core command line interface:

dotnet add package Calebs.KeyValueRepo.SQLite

Either command, from Package Manager Console or .NET Core CLI, will download and install Calebs.KeyValueRepo and all required dependencies.

## Contributing
- PRs should be against the `Develop` branch.
- Merged PRs to `Develop` will trigger a `[version]-ci-[buildnumber]` deployment to nuget, assuming all unit tests pass.
- Merged PRs to `Main` will trigger a [version] deployment to nuget, assuming all of the tests pass.

## Versioning
The package `version` is defined in the `KeyValueSqlLiteRepo.csproj` file, using .NET SDK style structure. We follow `semantic versioning` for this package. Implementations should match Major releases with KeyValueRepo package.


## Methods in IKeyValueRepo

- Get<T>(string Id)
- Get<T>(int Id) => Get<T>(string Id)
- Note: This method has a _default implementation_ that performs a `ToString()` on the Id and calls the `Get<T>(string Id)` method.
- GetAll<T>()
- Update<T>(string Id, T object)
- Update<T>(int Id, T object) => Update(string Id, T object)
- Note This method has a _default implementation_ that performs a `ToSTring()` on the int Id and calls the `Update<T>(string Id, T object)` method.


## Release History & Development Goals
- 0.1.0 - initial release (in Memory)
- 0.2.0 - added SqlLite Implementation
- ✔ SQLite [Nuget Package](https://www.nuget.org/packages/Calebs.KeyValueRepo.SQLite/)
### Future Goals
---
- New Feature: KV Meta Objects
- New Repo: Sql Server Implementation
- New Repo: Azure Tables Implementation
- New Repo: Azure CosmoDB Implementation
4 changes: 3 additions & 1 deletion src/Tests.NugetSample/InMemeoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task RepoShouldHoldValues()
public async Task ShouldReturnVoidForUnknownTypes()
{
IKeyValueRepo repo = GetNewRepo();
var p = await repo.Get<Person>("1");
var p = await repo.Get<UnusedType>("1");

p.Should().BeNull();
}
Expand Down Expand Up @@ -87,3 +87,5 @@ public async Task GetAllShouldReturnAllInstances()

public record Person (string First, string Last, int Id);
public record Location (string Id, string Street, string City);

public record UnusedType();
119 changes: 119 additions & 0 deletions src/Tests.NugetSample/SqLiteTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@


namespace KeyValueRepoTests;

[Collection ("RepoTests")]
public class SqLiteTests : InMemeoryTests
{
ILogger<KeyValueSqLiteRepo> _logger = new Mock<ILogger<KeyValueSqLiteRepo>>().Object;
ILogger<SchemaValidator> _schemaLogger = new Mock<ILogger<SchemaValidator>>().Object;
ITestOutputHelper _out;

public SqLiteTests(ITestOutputHelper output)
{
_out = output ?? throw new ArgumentNullException(nameof(output));
}

public IKeyValueRepo GetNewRepo(string path)
{
var opt = new KeyValueSqlLiteOptions()
{
ConnectionString = path,
ColumnPrefix = "col"
};
var validator = new SchemaValidator(_schemaLogger);
return new KeyValueSqLiteRepo(_logger, validator, opt);
}
public override IKeyValueRepo GetNewRepo()
{
return GetNewRepo("Data Source=./Db.db");
}

[Fact]
public void ShouldBeAbleToInstanciate()
{
var db = GetNewRepo();
db.Should().NotBeNull();
}

[Fact]
public async Task ShouldCreateDbInstance()
{
var db = GetNewRepo();
var result = await db.AsKeyValueSqlLiteRepo().ValidateSchema();
result.Should().BeTrue();
}

[Fact]
public void FileNameIsAvailable()
{
var db = GetNewRepo();
var filepath = db.AsKeyValueSqlLiteRepo().DatabaseFileName;
filepath.Should().Contain("Db.db");
}

[Fact]
public async Task CorrectFileGetsCreated()
{
var db = GetNewRepo();
var filePath = db.AsKeyValueSqlLiteRepo().DatabaseFileName;

// Clear File
await removeDbFileIfExists(db.AsKeyValueSqlLiteRepo());

// Create a new File
var valid = await db.AsKeyValueSqlLiteRepo().ValidateSchema();
File.Exists(filePath).Should().BeTrue();

// Clean Up
await removeDbFileIfExists(db.AsKeyValueSqlLiteRepo());
}

private async Task removeDbFileIfExists(KeyValueSqLiteRepo Repo)
{
var filePath = Repo.DatabaseFileName;
Debug.WriteLine($"Database path: {filePath}");

var exists = File.Exists(filePath);

if (exists)
{
await Repo.ReleaseForCleanUp();
File.Delete(filePath);
File.Exists(filePath).Should().BeFalse();
}
}

[Fact]
public async Task ConfirmTableDoesNotExistAndCanBeCreated()
{
var tmpData = Guid.NewGuid().ToString().Substring(0, 5);
var db = GetNewRepo($"Data Source=./{tmpData}.db");
var filePath = db.AsKeyValueSqlLiteRepo().DatabaseFileName;

// Reset DB
await removeDbFileIfExists(db.AsKeyValueSqlLiteRepo());

var opt = new KeyValueSqlLiteOptions() { ColumnPrefix = "col" };
var defaultTableName = opt.DefaultTableName;

ILogger<SchemaValidator> _SchemaLogger = new Mock<ILogger<SchemaValidator>>().Object;

var verify = new SchemaValidator(_SchemaLogger);
bool exist = await verify.TablesExists(defaultTableName, db.AsKeyValueSqlLiteRepo().DbConn);
exist.Should().BeFalse();

bool tableCreated = await verify.CreateAllTables(opt, db.AsKeyValueSqlLiteRepo().DbConn);
tableCreated.Should().BeTrue();

exist = await verify.TablesExists(defaultTableName, db.AsKeyValueSqlLiteRepo().DbConn);
exist.Should().BeTrue();

var IsValid = await verify.ValidateSchema(opt, db.AsKeyValueSqlLiteRepo().DbConn);
IsValid.HasError.Should().BeFalse();
IsValid.Messages.Count.Should().Be(7);

// Reset DB
await removeDbFileIfExists(db.AsKeyValueSqlLiteRepo());
}
}
2 changes: 1 addition & 1 deletion src/Tests.NugetSample/Tests.NugetSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Calebs.Extensions" Version="1.5.0-ci-62" />
<PackageReference Include="Calebs.KeyValueRepo" Version="0.2.0-ci-9" />
<PackageReference Include="Calebs.KeyValueRepo.SQLite" Version="0.1.0-ci-6" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.1" />
Expand Down
1 change: 1 addition & 0 deletions src/Tests.NugetSample/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
global using Xunit.Abstractions;
global using FluentAssertions;
global using Calebs.Data.KeyValueRepo;
global using Calebs.Data.KeyValueRepo.SqlLite;
global using Moq;
global using Microsoft.Extensions.Logging;
global using System.Diagnostics;
Expand Down

0 comments on commit 14d04c6

Please sign in to comment.