|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Data; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace JetDatabaseReader.Tests |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Tests for all async methods: |
| 11 | + /// ListTablesAsync, ReadTableAsync, GetStatisticsAsync, ReadAllTablesAsync, ReadAllTablesAsStringsAsync. |
| 12 | + /// </summary> |
| 13 | + public class AccessReaderAsyncTests |
| 14 | + { |
| 15 | + // ── ListTablesAsync ─────────────────────────────────────────────── |
| 16 | + |
| 17 | + [Theory] |
| 18 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 19 | + public async Task ListTablesAsync_ReturnsNonEmptyList(string path) |
| 20 | + { |
| 21 | + using var reader = TestDatabases.Open(path); |
| 22 | + |
| 23 | + List<string> tables = await reader.ListTablesAsync(); |
| 24 | + |
| 25 | + tables.Should().NotBeNullOrEmpty(); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 30 | + public async Task ListTablesAsync_MatchesSyncListTables(string path) |
| 31 | + { |
| 32 | + using var reader = TestDatabases.Open(path); |
| 33 | + |
| 34 | + List<string> sync = reader.ListTables(); |
| 35 | + List<string> async_ = await reader.ListTablesAsync(); |
| 36 | + |
| 37 | + async_.Should().BeEquivalentTo(sync); |
| 38 | + } |
| 39 | + |
| 40 | + // ── ReadTableAsync ──────────────────────────────────────────────── |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 44 | + public async Task ReadTableAsync_ReturnsNonNullDataTable(string path) |
| 45 | + { |
| 46 | + using var reader = TestDatabases.Open(path); |
| 47 | + string table = reader.ListTables()[0]; |
| 48 | + |
| 49 | + DataTable dt = await reader.ReadTableAsync(table); |
| 50 | + |
| 51 | + dt.Should().NotBeNull(); |
| 52 | + } |
| 53 | + |
| 54 | + [Theory] |
| 55 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 56 | + public async Task ReadTableAsync_ColumnTypes_AreTyped(string path) |
| 57 | + { |
| 58 | + using var reader = TestDatabases.Open(path); |
| 59 | + string table = reader.ListTables()[0]; |
| 60 | + var meta = reader.GetColumnMetadata(table); |
| 61 | + |
| 62 | + DataTable dt = await reader.ReadTableAsync(table); |
| 63 | + |
| 64 | + for (int i = 0; i < meta.Count; i++) |
| 65 | + dt.Columns[i].DataType.Should().Be(meta[i].ClrType); |
| 66 | + } |
| 67 | + |
| 68 | + [Theory] |
| 69 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 70 | + public async Task ReadTableAsync_RowCount_MatchesSyncReadTable(string path) |
| 71 | + { |
| 72 | + using var reader = TestDatabases.Open(path); |
| 73 | + string table = reader.ListTables()[0]; |
| 74 | + |
| 75 | + DataTable syncDt = reader.ReadTable(table); |
| 76 | + DataTable asyncDt = await reader.ReadTableAsync(table); |
| 77 | + |
| 78 | + asyncDt.Rows.Count.Should().Be(syncDt.Rows.Count); |
| 79 | + } |
| 80 | + |
| 81 | + // ── GetStatisticsAsync ──────────────────────────────────────────── |
| 82 | + |
| 83 | + [Theory] |
| 84 | + [MemberData(nameof(TestDatabases.All), MemberType = typeof(TestDatabases))] |
| 85 | + public async Task GetStatisticsAsync_MatchesSyncGetStatistics(string path) |
| 86 | + { |
| 87 | + using var reader = TestDatabases.Open(path); |
| 88 | + |
| 89 | + DatabaseStatistics sync = reader.GetStatistics(); |
| 90 | + DatabaseStatistics async_ = await reader.GetStatisticsAsync(); |
| 91 | + |
| 92 | + async_.TotalPages.Should().Be(sync.TotalPages); |
| 93 | + async_.TableCount.Should().Be(sync.TableCount); |
| 94 | + async_.Version.Should().Be(sync.Version); |
| 95 | + } |
| 96 | + |
| 97 | + // ── ReadAllTablesAsync ──────────────────────────────────────────── |
| 98 | + |
| 99 | + [Theory] |
| 100 | + [MemberData(nameof(TestDatabases.Small), MemberType = typeof(TestDatabases))] |
| 101 | + public async Task ReadAllTablesAsync_ContainsAllTableNames(string path) |
| 102 | + { |
| 103 | + using var reader = TestDatabases.Open(path); |
| 104 | + List<string> expected = reader.ListTables(); |
| 105 | + |
| 106 | + Dictionary<string, DataTable> all = await reader.ReadAllTablesAsync(); |
| 107 | + |
| 108 | + all.Keys.Should().BeEquivalentTo(expected); |
| 109 | + } |
| 110 | + |
| 111 | + [Theory] |
| 112 | + [MemberData(nameof(TestDatabases.Small), MemberType = typeof(TestDatabases))] |
| 113 | + public async Task ReadAllTablesAsync_RowCounts_MatchSyncReadAllTables(string path) |
| 114 | + { |
| 115 | + using var reader = TestDatabases.Open(path); |
| 116 | + |
| 117 | + Dictionary<string, DataTable> sync = reader.ReadAllTables(); |
| 118 | + Dictionary<string, DataTable> async_ = await reader.ReadAllTablesAsync(); |
| 119 | + |
| 120 | + foreach (string name in sync.Keys) |
| 121 | + async_[name].Rows.Count.Should().Be(sync[name].Rows.Count, |
| 122 | + because: $"table '{name}' row count should match"); |
| 123 | + } |
| 124 | + |
| 125 | + // ── ReadAllTablesAsStringsAsync ──────────────────────────────────── |
| 126 | + |
| 127 | + [Theory] |
| 128 | + [MemberData(nameof(TestDatabases.Small), MemberType = typeof(TestDatabases))] |
| 129 | + public async Task ReadAllTablesAsStringsAsync_AllColumns_AreStringType(string path) |
| 130 | + { |
| 131 | + using var reader = TestDatabases.Open(path); |
| 132 | + |
| 133 | + Dictionary<string, DataTable> all = await reader.ReadAllTablesAsStringsAsync(); |
| 134 | + |
| 135 | + foreach (var (_, dt) in all) |
| 136 | + foreach (DataColumn col in dt.Columns) |
| 137 | + col.DataType.Should().Be(typeof(string)); |
| 138 | + } |
| 139 | + |
| 140 | + [Theory] |
| 141 | + [MemberData(nameof(TestDatabases.Small), MemberType = typeof(TestDatabases))] |
| 142 | + public async Task ReadAllTablesAsStringsAsync_RowCounts_MatchReadAllTablesAsync(string path) |
| 143 | + { |
| 144 | + using var reader = TestDatabases.Open(path); |
| 145 | + |
| 146 | + Dictionary<string, DataTable> typed = await reader.ReadAllTablesAsync(); |
| 147 | + Dictionary<string, DataTable> strings = await reader.ReadAllTablesAsStringsAsync(); |
| 148 | + |
| 149 | + foreach (string name in typed.Keys) |
| 150 | + strings[name].Rows.Count.Should().Be(typed[name].Rows.Count); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments