Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit a6fa6eb

Browse files
committed
test(postgres): fix mock and verification for postgres v3 API
- Adds a `FakeServerException` class to allow creating test instances, as the real `ServerException` has no public constructor. - Corrects SQL verification in tests by dynamically accessing the `.sql` property on the `Sql` object, as it is not exposed in the public API. - Fixes multiple compilation errors related to these issues.
1 parent bba22fd commit a6fa6eb

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

test/src/ht_data_postgres_test.dart

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,27 @@ class MockLogger extends Mock implements Logger {}
1616

1717
class MockResult extends Mock implements Result {}
1818

19-
class MockRow extends Mock implements Row {}
19+
class MockResultRow extends Mock implements ResultRow {}
2020

2121
class FakeSql extends Fake implements Sql {}
2222

23+
class FakeServerException extends Fake implements ServerException {
24+
FakeServerException({
25+
required this.message,
26+
this.code,
27+
this.severity = Severity.error,
28+
});
29+
30+
@override
31+
final String? code;
32+
33+
@override
34+
final String message;
35+
36+
@override
37+
final Severity severity;
38+
}
39+
2340
class TestModel extends Equatable {
2441
const TestModel({required this.id, required this.name});
2542

@@ -68,9 +85,9 @@ void main() {
6885
group('create', () {
6986
test('should return created item on success', () async {
7087
final mockResult = MockResult();
71-
final mockRow = MockRow();
72-
when(() => mockRow.toColumnMap()).thenReturn(testModelJson);
73-
when(() => mockResult.first).thenReturn(mockRow);
88+
final mockResultRow = MockResultRow();
89+
when(() => mockResultRow.toColumnMap()).thenReturn(testModelJson);
90+
when(() => mockResult.first).thenReturn(mockResultRow);
7491
when(
7592
() => mockConnection.execute(
7693
any(),
@@ -85,8 +102,8 @@ void main() {
85102
() => mockConnection.execute(
86103
any(
87104
that: isA<Sql>().having(
88-
(s) => s.statement,
89-
'statement',
105+
(s) => (s as dynamic).sql as String,
106+
'sql',
90107
'INSERT INTO test_models (id, name) VALUES (@id, @name) RETURNING *;',
91108
),
92109
),
@@ -98,10 +115,9 @@ void main() {
98115
});
99116

100117
test('should throw ConflictException on unique violation', () {
101-
final exception = ServerException(
102-
'unique violation',
118+
final exception = FakeServerException(
119+
message: 'unique violation',
103120
code: '23505',
104-
severity: Severity.error,
105121
);
106122
when(
107123
() => mockConnection.execute(
@@ -121,10 +137,10 @@ void main() {
121137
group('read', () {
122138
test('should return item when found', () async {
123139
final mockResult = MockResult();
124-
final mockRow = MockRow();
125-
when(() => mockRow.toColumnMap()).thenReturn(testModelJson);
140+
final mockResultRow = MockResultRow();
141+
when(() => mockResultRow.toColumnMap()).thenReturn(testModelJson);
126142
when(() => mockResult.isEmpty).thenReturn(false);
127-
when(() => mockResult.first).thenReturn(mockRow);
143+
when(() => mockResult.first).thenReturn(mockResultRow);
128144
when(
129145
() => mockConnection.execute(
130146
any(),
@@ -139,8 +155,8 @@ void main() {
139155
() => mockConnection.execute(
140156
any(
141157
that: isA<Sql>().having(
142-
(s) => s.statement,
143-
'statement',
158+
(s) => (s as dynamic).sql as String,
159+
'sql',
144160
'SELECT * FROM test_models WHERE id = @id;',
145161
),
146162
),
@@ -169,10 +185,10 @@ void main() {
169185
group('update', () {
170186
test('should return updated item on success', () async {
171187
final mockResult = MockResult();
172-
final mockRow = MockRow();
173-
when(() => mockRow.toColumnMap()).thenReturn(testModelJson);
188+
final mockResultRow = MockResultRow();
189+
when(() => mockResultRow.toColumnMap()).thenReturn(testModelJson);
174190
when(() => mockResult.isEmpty).thenReturn(false);
175-
when(() => mockResult.first).thenReturn(mockRow);
191+
when(() => mockResult.first).thenReturn(mockResultRow);
176192
when(
177193
() => mockConnection.execute(
178194
any(),
@@ -187,8 +203,8 @@ void main() {
187203
() => mockConnection.execute(
188204
any(
189205
that: isA<Sql>().having(
190-
(s) => s.statement,
191-
'statement',
206+
(s) => (s as dynamic).sql as String,
207+
'sql',
192208
'UPDATE test_models SET name = @name WHERE id = @id RETURNING *;',
193209
),
194210
),
@@ -255,7 +271,7 @@ void main() {
255271
final params = captured[1] as Map<String, dynamic>;
256272

257273
expect(
258-
sql.statement,
274+
(sql as dynamic).sql,
259275
'SELECT * FROM test_models WHERE id IN (@p0, @p1, @p2) LIMIT 11;',
260276
);
261277
expect(params, {'p0': '1', 'p1': '2', 'p2': '3'});

0 commit comments

Comments
 (0)