Skip to content

Commit 582b8e9

Browse files
committed
Added Tests project, added repository integration tests
1 parent f37d8e8 commit 582b8e9

File tree

11 files changed

+135
-47
lines changed

11 files changed

+135
-47
lines changed

.idea/.idea.CryptoTracker/.idea/riderModule.iml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CryptoTracker.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Re
1818
EndProject
1919
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database", "Database\Database.csproj", "{0FAABB54-8CF7-42E5-9971-3B9E2C9AD24C}"
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{BA8D3187-DED2-467F-AF29-D47C827505A3}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +46,10 @@ Global
4446
{0FAABB54-8CF7-42E5-9971-3B9E2C9AD24C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4547
{0FAABB54-8CF7-42E5-9971-3B9E2C9AD24C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4648
{0FAABB54-8CF7-42E5-9971-3B9E2C9AD24C}.Release|Any CPU.Build.0 = Release|Any CPU
49+
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
52+
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Release|Any CPU.Build.0 = Release|Any CPU
4753
EndGlobalSection
4854
GlobalSection(SolutionProperties) = preSolution
4955
HideSolutionNode = FALSE

Database/SqlSchema.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace Database
24
{
35
public class SqlSchema

Repository/IRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ public interface IRepository<T>
66
{
77
public object ToRow(T entry);
88

9-
public void Add(T entry);
9+
public int Add(T entry);
10+
11+
public T Get(int id);
1012

1113
public void Update(T entry);
1214

Repository/Repository.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="5.0.4" />
15+
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.4" />
16+
<PackageReference Include="SqlKata" Version="2.3.2" />
1617
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
1718
<PackageReference Include="SqlKata.Execution" Version="2.3.2" />
1819
<PackageReference Include="xunit.assert" Version="2.4.1" />

Repository/SqlKataRepository.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ public SqlKataRepository(SqlKataDatabase db, string tableName)
2323

2424
protected abstract int _getEntryId(T entry);
2525

26-
public void Add(T entry)
26+
public int Add(T entry)
2727
{
2828
var id = Db.Get().Query(_tableName).InsertGetId<int>(ToRow(entry));
29-
30-
Console.WriteLine($"added! {id}");
31-
Db.Get().Query(_tableName).AsInsert(ToRow(entry)).Get();
32-
id = Db.Get().Query(_tableName).InsertGetId<int>(ToRow(entry));
33-
Console.WriteLine($"added! {id}");
29+
return id;
3430
}
3531

3632
public void Update(T entry)
@@ -43,13 +39,23 @@ public void Delete(T entry)
4339
Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).AsDelete();
4440
}
4541

42+
public T Get(int id)
43+
{
44+
var result = Db.Get().Query(_tableName).Where("id", id).First();
45+
if (result == null)
46+
{
47+
return default;
48+
}
49+
50+
return FromRow(result);
51+
}
52+
4653
public List<T> All()
4754
{
4855
List<T> items = new List<T>();
4956
foreach (var row in Db.Get().Query(_tableName).Select().Get())
5057
{
5158
items.Add(FromRow(row));
52-
5359
}
5460

5561
return items;

Repository/Tests/Tests.csproj

Lines changed: 0 additions & 22 deletions
This file was deleted.

Repository/Tests/UnitTest1.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

CryptoStatsSource/Tests/UnitTest1.cs renamed to Tests/Integration/CryptoStatsSource/CryptoStatsSource.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
using CryptoStatsSource;
12
using Xunit;
23

3-
namespace CryptoStatsSource.Tests
4+
namespace Tests.Integration.CryptoStatsSource
45
{
56
public class GetMarketEntriesTest
67
{
@@ -22,7 +23,7 @@ public async void SimpleOneEntry()
2223
{
2324
var entries = await _source.GetMarketEntries("usd", "bitcoin");
2425

25-
Assert.Equal(1, entries.Count);
26+
Assert.Single(entries);
2627
Assert.Equal("btc", entries[0].Symbol);
2728
// 🙏🏻
2829
Assert.True(entries[0].CurrentPrice > 10000);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using Database;
3+
using Microsoft.Data.Sqlite;
4+
using Model;
5+
using Repository;
6+
using SqlKata.Compilers;
7+
using Xunit;
8+
9+
namespace Tests.Integration.Repository
10+
{
11+
public class SqlKataPortfolioRepositoryFixture : IDisposable
12+
{
13+
public SqlKataPortfolioRepository PortfolioRepository;
14+
private SqliteConnection _dbConnection;
15+
16+
public SqlKataPortfolioRepositoryFixture()
17+
{
18+
_dbConnection = new SqliteConnection("Data Source=:memory:");
19+
_dbConnection.Open();
20+
var db = new SqlKataDatabase(_dbConnection, new SqliteCompiler());
21+
this.PortfolioRepository = new(db);
22+
}
23+
24+
public void Dispose()
25+
{
26+
_dbConnection.Close();
27+
}
28+
}
29+
30+
public class UnitTest1 : IClassFixture<SqlKataPortfolioRepositoryFixture>
31+
{
32+
private SqlKataPortfolioRepositoryFixture _portfolioFixture;
33+
34+
public UnitTest1(SqlKataPortfolioRepositoryFixture portfolioFixture)
35+
{
36+
this._portfolioFixture = portfolioFixture;
37+
}
38+
39+
[Fact]
40+
public void Add_ReturnsNonZeroId()
41+
{
42+
// arrange
43+
var portfolio = new Portfolio("My new portfolio", "Lorem ipsum dolor sit amet");
44+
45+
// act
46+
int id = _portfolioFixture.PortfolioRepository.Add(new Portfolio("My new portfolio",
47+
"Lorem ipsum dolor sit amet"));
48+
49+
// assert
50+
Assert.True(id > 0);
51+
}
52+
53+
[Fact]
54+
public void Added_And_Get_AreEqual()
55+
{
56+
// arrange
57+
var portfolio = new Portfolio("My new portfolio", "Lorem ipsum dolor sit amet");
58+
59+
// act
60+
int id = _portfolioFixture.PortfolioRepository.Add(portfolio);
61+
var loaded = _portfolioFixture.PortfolioRepository.Get(id);
62+
portfolio = portfolio with
63+
{
64+
Id = loaded.Id
65+
};
66+
67+
// assert
68+
Assert.True(id > 0);
69+
Assert.Equal(portfolio, loaded);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)