Skip to content

Commit 1f714c0

Browse files
committed
Added few tests to increase the test coverage
1 parent d319f7c commit 1f714c0

File tree

5 files changed

+164
-6
lines changed

5 files changed

+164
-6
lines changed

Database/SqlKataDatabase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public SqlKataDatabase(IDbConnection dbConnection, Compiler compiler)
2626

2727
public void Dispose()
2828
{
29-
// TODO check whether dispose is called
30-
Console.WriteLine("Disposing SqlKataDatabase");
3129
_dbConnection.Close();
3230
}
3331
}

Tests/Integration/Repository/MarketOrderTest.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,62 @@ public void Delete_Deletes()
239239
// assert
240240
Assert.Null(marketOrderRepositoryFixture.MarketOrderRepository.Get(marketOrder1.Id));
241241
Assert.Equal(marketOrder2, marketOrderRepositoryFixture.MarketOrderRepository.Get(marketOrder2.Id));
242-
Assert.Equal(1, marketOrderRepositoryFixture.MarketOrderRepository.GetAll().Count);
242+
Assert.Single(marketOrderRepositoryFixture.MarketOrderRepository.GetAll());
243+
}
244+
245+
[Fact]
246+
public void DeleteEntryOrders_Deletes_All_Orders()
247+
{
248+
// fixture unique to this test
249+
var marketOrderRepositoryFixture = new SqlKataMarketOrderRepositoryFixture();
250+
251+
// arrange
252+
var marketOrder1 = new MarketOrder(new Decimal(10000.39), 10, new Decimal(1.1), DateTime.Now, true,
253+
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
254+
var marketOrder2 = new MarketOrder(new Decimal(11000.39), 11, new Decimal(1.2),
255+
DateTime.Now.Subtract(TimeSpan.FromSeconds(3600)), true,
256+
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
257+
var marketOrder3 = new MarketOrder(new Decimal(12000.39), 12, new Decimal(1.3),
258+
DateTime.Now.Subtract(TimeSpan.FromDays(30)), false,
259+
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
260+
261+
var marketOrder4 = new MarketOrder(new Decimal(12005.39), 15, new Decimal(12),
262+
DateTime.Now.Subtract(TimeSpan.FromDays(11)), false,
263+
PortfolioEntryId: marketOrderRepositoryFixture.SecondaryPortfolioEntryId);
264+
265+
var marketOrder5 = new MarketOrder(new Decimal(12006.39), 16, new Decimal(1.5),
266+
DateTime.Now.Subtract(TimeSpan.FromDays(39)), false,
267+
PortfolioEntryId: marketOrderRepositoryFixture.SecondaryPortfolioEntryId);
268+
269+
// act
270+
marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder1);
271+
marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder2);
272+
marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder3);
273+
274+
marketOrder4 = marketOrder4 with
275+
{
276+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder4)
277+
};
278+
marketOrder5 = marketOrder5 with
279+
{
280+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder5)
281+
};
282+
283+
marketOrderRepositoryFixture.MarketOrderRepository.DeletePortfolioEntryOrders(marketOrderRepositoryFixture
284+
.DefaultPortfolioEntryId);
285+
286+
// assert
287+
var loadedPortfolios =
288+
marketOrderRepositoryFixture.MarketOrderRepository.GetAllByPortfolioEntryId(marketOrderRepositoryFixture
289+
.DefaultPortfolioEntryId);
290+
291+
Assert.Empty(loadedPortfolios);
292+
293+
var loadedPortfoliosSecondary =
294+
marketOrderRepositoryFixture.MarketOrderRepository.GetAllByPortfolioEntryId(marketOrderRepositoryFixture
295+
.SecondaryPortfolioEntryId);
296+
Assert.Equal(2, loadedPortfoliosSecondary.Count);
297+
Assert.Equal(new List<MarketOrder> {marketOrder4, marketOrder5}, loadedPortfoliosSecondary);
243298
}
244299
}
245300
}

Tests/Integration/Repository/PortfolioEntryTest.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void AddUpdate_Updates()
176176
{
177177
// fixture unique to this test
178178
var portfolioEntryRepositoryFixture = new SqlKataPortfolioEntryRepositoryFixture();
179-
179+
180180
// arrange
181181
var btcEntry = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
182182
var ethEntry = new PortfolioEntry("eth", portfolioEntryRepositoryFixture.DefaultPortfolioId);
@@ -213,7 +213,7 @@ public void Delete_Deletes()
213213
{
214214
Id = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(firstEntry)
215215
};
216-
216+
217217
secondEntry = secondEntry with
218218
{
219219
Id = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(secondEntry)
@@ -223,7 +223,49 @@ public void Delete_Deletes()
223223
// assert
224224
Assert.Null(_portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(firstEntry.Id));
225225
Assert.Equal(secondEntry, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(secondEntry.Id));
226-
Assert.Equal(1, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAll().Count);
226+
Assert.Single(_portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAll());
227+
}
228+
229+
[Fact]
230+
public void DeletePortfolioEntries_Deletes_Correct_Entries()
231+
{
232+
// fixture unique to this test
233+
var portfolioEntryRepositoryFixture = new SqlKataPortfolioEntryRepositoryFixture();
234+
235+
// arrange
236+
var portfolioEntry1 = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
237+
var portfolioEntry2 = new PortfolioEntry("ada", portfolioEntryRepositoryFixture.DefaultPortfolioId);
238+
var portfolioEntry3 = new PortfolioEntry("ltc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
239+
240+
var portfolioEntry4 = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.SecondaryPortfolioId);
241+
var portfolioEntry5 = new PortfolioEntry("eth", portfolioEntryRepositoryFixture.SecondaryPortfolioId);
242+
243+
// act
244+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry1);
245+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry2);
246+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry3);
247+
248+
portfolioEntry4 = portfolioEntry4 with
249+
{
250+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry4)
251+
};
252+
portfolioEntry5 = portfolioEntry5 with
253+
{
254+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry5)
255+
};
256+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.DeletePortfolioEntries(
257+
portfolioEntryRepositoryFixture.DefaultPortfolioId);
258+
259+
// assert
260+
261+
Assert.Empty(portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAllByPortfolioId(portfolioEntryRepositoryFixture.DefaultPortfolioId));
262+
263+
var loadedPortfoliosSecondaryPortfolio =
264+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAllByPortfolioId(
265+
portfolioEntryRepositoryFixture.SecondaryPortfolioId);
266+
Assert.Equal(2, loadedPortfoliosSecondaryPortfolio.Count);
267+
Assert.Equal(new List<PortfolioEntry> {portfolioEntry4, portfolioEntry5},
268+
loadedPortfoliosSecondaryPortfolio);
227269
}
228270
}
229271
}

Tests/Unit/Service/MarketOrderServiceTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,20 @@ public void Delete_CallsRepository()
118118
// assert
119119
Assert.True(delete);
120120
}
121+
122+
[Fact]
123+
public void DeletePortfolioEntries_CallsRepository()
124+
{
125+
// arrange
126+
var portfolioEntryId = 15;
127+
var repositoryMock = new Mock<IMarketOrderRepository>();
128+
var service = new MarketOrderServiceImpl(repositoryMock.Object);
129+
130+
// act
131+
service.DeletePortfolioEntryOrders(portfolioEntryId);
132+
133+
// assert
134+
repositoryMock.Verify(x => x.DeletePortfolioEntryOrders(It.Is<int>(id => id == portfolioEntryId)));
135+
}
121136
}
122137
}

Tests/Unit/Service/SummaryServiceTest.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ public void GetMarketOrderSummary_WithFee_InLoss_Returns_Correct_Summary()
7070
), summary);
7171
}
7272

73+
[Fact]
74+
public void GetMarketOrderSummary_ZeroCost_Returns_Zero_Summary()
75+
{
76+
var service = new SummaryServiceImpl();
77+
MarketOrder order = new(10000m, 0m, 0m, DateTime.Now, true);
78+
var summary = service.GetMarketOrderSummary(order, 5000m);
79+
80+
Assert.Equal(new ISummaryService.Summary(
81+
0,
82+
0,
83+
0,
84+
0
85+
), summary);
86+
}
87+
7388
// PortfolioEntrySummary tests
7489

7590
[Fact]
@@ -126,6 +141,23 @@ public void GetPortfolioEntrySummary_InProfit_WithSell_Returns_Correct_Summary()
126141
), summary);
127142
}
128143

144+
[Fact]
145+
public void GetPortfolioEntrySummary_ZeroCost_Returns_Zero_Summary()
146+
{
147+
var service = new SummaryServiceImpl();
148+
var summary = service.GetPortfolioEntrySummary(new()
149+
{
150+
new(10000m, 0m, 0m, DateTime.Now, true),
151+
}, 40000);
152+
153+
Assert.Equal(new ISummaryService.Summary(
154+
0m,
155+
0m,
156+
0m,
157+
0m
158+
), summary);
159+
}
160+
129161
[Fact]
130162
public void GetPortfolioEntrySummary_WithFee_InProfit_WithSell_Returns_Correct_Summary()
131163
{
@@ -159,5 +191,21 @@ public void GetPortfolioSummary_Returns_Correct_Summary()
159191
summary
160192
);
161193
}
194+
195+
[Fact]
196+
public void GetPortfolioSummary_ZeroCost_Returns_ZeroSummary()
197+
{
198+
var service = new SummaryServiceImpl();
199+
var summary = service.GetPortfolioSummary(new()
200+
{
201+
new(10000m, 1, 20000m, 0),
202+
new(2000m, 2, 3000m, 0),
203+
});
204+
205+
Assert.Equal(
206+
new ISummaryService.Summary(0m, 0m, 0m, 0m),
207+
summary
208+
);
209+
}
162210
}
163211
}

0 commit comments

Comments
 (0)