Skip to content

Commit e7337c8

Browse files
Merge pull request #91 from Ard2025/master
Add new unit test
2 parents 28942c0 + 2d693f3 commit e7337c8

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

TestServer/TestServer/Components/Layout/NavMenu.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
1414
</NavLink>
1515
</div>
16+
17+
<div class="nav-item px-3">
18+
<NavLink class="nav-link" href="CarTests" Match="NavLinkMatch.All">
19+
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home Cars
20+
</NavLink>
21+
</div>
1622

1723
<div class="nav-item px-3">
1824
<NavLink class="nav-link" href="counter">
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
@page "/CarTests"
2+
@using Magic.IndexedDb
3+
@using Magic.IndexedDb.Testing.Helpers
4+
@using System.Text.Json
5+
@using Magic.IndexedDb.Testing.Models
6+
@using TestWasm.Models
7+
@using TestWasm.Repository
8+
@using System.Linq;
9+
@rendermode InteractiveServer
10+
11+
@inject IMagicIndexedDb _MagicDb
12+
13+
@{
14+
string storageQuotaText;
15+
string storageUsageText;
16+
if (storageQuota >= 1024)
17+
{
18+
storageQuota = storageQuota / 1024.0;
19+
// Round the value to two decimal places
20+
storageQuota = Math.Round(storageQuota, 2);
21+
// Display the value in GB
22+
storageQuotaText = $"{storageQuota} GB";
23+
}
24+
else
25+
{
26+
// Display the value in MB
27+
storageQuotaText = $"{Math.Round(storageQuota, 2)} MB";
28+
}
29+
30+
if (storageUsage >= 1024)
31+
{
32+
storageUsage = storageUsage / 1024.0;
33+
// Round the value to two decimal places
34+
storageUsage = Math.Round(storageUsage, 2);
35+
// Display the value in GB
36+
storageUsageText = $"{storageUsage} GB";
37+
}
38+
else
39+
{
40+
// Display the value in MB
41+
storageUsageText = $"{Math.Round(storageUsage, 2)} MB";
42+
}
43+
44+
// Display the storage size on the front-end
45+
<p>Storage Used: @storageUsageText</p>
46+
<p>Storage Quota: @storageQuotaText</p>
47+
48+
}
49+
50+
<PageTitle>Example</PageTitle>
51+
52+
<h3>Unit Tests</h3>
53+
@foreach (var (testName, response, countResults) in TestResults)
54+
{
55+
<div>
56+
@if (response.Success)
57+
{
58+
<strong>✅ @testName</strong> @($" - {countResults}")
59+
}
60+
else
61+
{
62+
<strong>❌ @testName</strong> @($"- {countResults}")
63+
64+
@if (!string.IsNullOrWhiteSpace(response.Message))
65+
{
66+
var messages = response.Message.Split('\n', StringSplitOptions.RemoveEmptyEntries);
67+
foreach (var message in messages)
68+
{
69+
<div style="margin-left: 20px;">🔍 @message</div>
70+
}
71+
}
72+
}
73+
</div>
74+
}
75+
76+
<br />
77+
<br />
78+
79+
<h3>People In IndexedDb!</h3>
80+
81+
<table class="table">
82+
<thead>
83+
<tr>
84+
<th>ID</th>
85+
<th>Name</th>
86+
</tr>
87+
</thead>
88+
<tbody>
89+
@foreach (Car car in allCars.OrderBy(x => x.Created))
90+
{
91+
<tr>
92+
<td>@car.Created</td>
93+
<td>@car.Brand</td>
94+
</tr>
95+
}
96+
</tbody>
97+
</table>
98+
99+
@code {
100+
public List<(string, TestResponse, string)> TestResults = new();
101+
102+
private static readonly Random _random = new Random();
103+
104+
public static int GetRandomYear()
105+
{
106+
return _random.Next(1980, 2024); // Upper bound is exclusive
107+
}
108+
109+
public static DateTime GetDateWithSameMonthDay(int year)
110+
{
111+
DateTime today = DateTime.Today;
112+
return new DateTime(year, today.Month, today.Day);
113+
}
114+
115+
private void RunTest<T>(string testName,
116+
IEnumerable<T> indexDbResults, IEnumerable<T> correctResults) where T : class
117+
{
118+
var result = TestValidator.ValidateLists(correctResults, indexDbResults);
119+
120+
string countResults = $"Results: {indexDbResults.Count()}";
121+
if (result.Success == false)
122+
{
123+
countResults = $"IndexDB Results: {indexDbResults.Count()} / Correct Results: {correctResults.Count()}";
124+
}
125+
TestResults.Add((testName, result, countResults));
126+
StateHasChanged();
127+
}
128+
129+
private List<Car> allCars { get; set; } = new List<Car>();
130+
private double storageQuota { get; set; }
131+
private double storageUsage { get; set; }
132+
protected override async Task OnAfterRenderAsync(bool firstRender)
133+
{
134+
if (firstRender)
135+
{
136+
// Targets the default database automatically if called without any parameters.
137+
IMagicQuery<Car> carQuery = await _MagicDb.Query<Car>();
138+
await carQuery.ClearTable();
139+
140+
List<Car> cars = [];
141+
cars.Add(new Car{ Brand = "Audi"});
142+
cars.Add(new Car{ Brand = "Toyota"});
143+
cars.Add(new Car{ Brand = "Ferrari"});
144+
145+
await carQuery.AddRangeAsync(cars);
146+
147+
allCars = await carQuery.ToListAsync();
148+
149+
RunTest("Order date (index) with skip 1",
150+
await carQuery.OrderBy(car => car.Created).Skip(1).ToListAsync(),
151+
allCars.OrderBy(car => car.Created).ToList());
152+
153+
StateHasChanged();
154+
}
155+
}
156+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Magic.IndexedDb;
2+
using TestWasm.Repository;
3+
4+
namespace TestWasm.Models;
5+
6+
public class Car : MagicTableTool<Car>, IMagicTable<Person.DbSets>
7+
{
8+
public DateTime Created { get; set; } = DateTime.UtcNow;
9+
10+
public string Brand { get; set; } = string.Empty;
11+
12+
public List<IMagicCompoundIndex> GetCompoundIndexes() => [];
13+
14+
public IMagicCompoundKey GetKeys() => CreatePrimaryKey(x => x.Created, false);
15+
16+
public string GetTableName() => "Car";
17+
public IndexedDbSet GetDefaultDatabase() => IndexDbContext.Client;
18+
public Person.DbSets Databases { get; } = new();
19+
}

0 commit comments

Comments
 (0)