Skip to content

Commit b9efc0f

Browse files
Merge pull request #52 from yueyinqiu/tests
fix unit tests
2 parents c76a980 + c9693e6 commit b9efc0f

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

E2eTest/SingleRecordBasicTest.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public async Task AddTest()
2424
[
2525
{
2626
"id":12,
27-
"Normal":"Norm",
27+
"normal":"Norm",
2828
"Renamed":"R",
2929
"Index":"I",
3030
"UniqueIndex":"633a97d2-0c92-4c68-883b-364f94ad6030",
31-
"Enum":0,
32-
"Nested":{"Value":1234},
33-
"LargeNumber":9007199254740991
31+
"enum":0,
32+
"nested":{"value":1234},
33+
"largeNumber":9007199254740991
3434
}
3535
]
3636
""", records);
@@ -67,13 +67,13 @@ public async Task UpdateTest()
6767
[
6868
{
6969
"id":12,
70-
"Normal":"Updated",
70+
"normal":"Updated",
7171
"Renamed":"R",
7272
"Index":"I",
7373
"UniqueIndex":"633a97d2-0c92-4c68-883b-364f94ad6030",
74-
"Enum":0,
75-
"Nested":{"Value":1234},
76-
"LargeNumber":9007199254740991
74+
"enum":0,
75+
"nested":{"value":1234},
76+
"largeNumber":9007199254740991
7777
}
7878
]
7979
""", records);
@@ -103,7 +103,7 @@ public async Task GetAllTest()
103103
"Id":12,
104104
"Normal":"Norm",
105105
"ShouldBeRenamed":"R",
106-
"Ignored":true,
106+
"Ignored":false,
107107
"Index":"I",
108108
"UniqueIndex":"633a97d2-0c92-4c68-883b-364f94ad6030",
109109
"Enum":0,

E2eTestWebApp/TestPages/SingleRecordBasicTestPage.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ namespace E2eTestWebApp.TestPages;
1212
[Route("/SingleRecordBasicTest")]
1313
public class SingleRecordBasicTestPage(IMagicDbFactory magic) : TestPageBase
1414
{
15-
private record NestedItem(int Value);
15+
private class NestedItem
16+
{
17+
public int Value { get; set; }
18+
}
1619

1720
[MagicTable("Records", null)]
1821
private class Record
@@ -49,7 +52,7 @@ private class Record
4952
Index = "I",
5053
UniqueIndex = Guid.Parse("633A97D2-0C92-4C68-883B-364F94AD6030"),
5154
Enum = DayOfWeek.Sunday,
52-
Nested = new(1234),
55+
Nested = new() { Value = 1234 },
5356
LargeNumber = 9007199254740991
5457
};
5558

Magic.IndexedDb/Factories/MagicDbFactory.cs

+28-10
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,65 @@ namespace Magic.IndexedDb.Factories
66
{
77
public class MagicDbFactory : IMagicDbFactory, IAsyncDisposable
88
{
9-
readonly Task<IJSObjectReference> _jsRuntime;
9+
Lazy<Task<IJSObjectReference>>? _jsRuntime;
1010
readonly IServiceProvider _serviceProvider;
11-
readonly IDictionary<string, IndexedDbManager> _databases = new Dictionary<string, IndexedDbManager>();
11+
readonly Dictionary<string, IndexedDbManager> _databases = new();
1212

1313
public MagicDbFactory(IServiceProvider serviceProvider, IJSRuntime jSRuntime)
1414
{
1515
_serviceProvider = serviceProvider;
16-
this._jsRuntime = jSRuntime.InvokeAsync<IJSObjectReference>(
16+
this._jsRuntime = new(() => jSRuntime.InvokeAsync<IJSObjectReference>(
1717
"import",
18-
"./_content/Magic.IndexedDb/magicDB.js").AsTask();
18+
"./_content/Magic.IndexedDb/magicDB.js").AsTask());
1919
}
2020
public async ValueTask DisposeAsync()
2121
{
22-
var js = await _jsRuntime;
22+
var js = _jsRuntime;
23+
_jsRuntime = null;
24+
25+
if (js is null || !js.IsValueCreated)
26+
return;
27+
28+
IJSObjectReference module;
2329
try
2430
{
25-
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
26-
await js.InvokeVoidAsync(IndexedDbFunctions.CLOSE_ALL, timeout.Token);
31+
module = await js.Value;
2732
}
2833
catch
2934
{
30-
// do nothing here
35+
return;
36+
}
37+
38+
try
39+
{
40+
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
41+
await module.InvokeVoidAsync(IndexedDbFunctions.CLOSE_ALL, timeout.Token);
42+
}
43+
finally
44+
{
45+
await module.DisposeAsync();
3146
}
32-
await js.DisposeAsync();
3347
}
3448

3549
public async ValueTask<IndexedDbManager> OpenAsync(
3650
DbStore dbStore, bool force = false,
3751
CancellationToken cancellationToken = default)
3852
{
53+
ObjectDisposedException.ThrowIf(_jsRuntime is null, this);
54+
3955
if (force || !_databases.ContainsKey(dbStore.Name))
4056
{
4157
var db = await IndexedDbManager.CreateAndOpenAsync(
42-
dbStore, await _jsRuntime, cancellationToken);
58+
dbStore, await _jsRuntime.Value, cancellationToken);
4359
_databases[dbStore.Name] = db;
4460
}
4561
return _databases[dbStore.Name];
4662
}
4763

4864
public IndexedDbManager Get(string dbName)
4965
{
66+
ObjectDisposedException.ThrowIf(_jsRuntime is null, this);
67+
5068
if (_databases.TryGetValue(dbName, out var db))
5169
return db;
5270
throw new MagicException(

TestWasm/Models/Person.cs

+7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
namespace TestWasm.Models
55
{
6+
public class Nested
7+
{
8+
public string Value { get; set; } = "abc";
9+
}
10+
611
[MagicTable("Person", DbNames.Client)]
712
public class Person
813
{
14+
public Nested Nested { get; set; } = new Nested();
15+
916
[MagicPrimaryKey("id")]
1017
public int _Id { get; set; }
1118

0 commit comments

Comments
 (0)