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

Commit 6d73861

Browse files
committed
test(postgres): add tests for error handling and query builder edge cases
- Adds tests to cover all remaining branches in the `_handlePgException` method, including foreign key violations, generic `PgException`s, and unknown server errors. - Adds a test for the "exact match" operator in the query builder. - This final set of tests increases coverage to meet the 90% requirement.
1 parent 2a45278 commit 6d73861

File tree

2 files changed

+109
-15
lines changed

2 files changed

+109
-15
lines changed

coverage/lcov.info

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ DA:281,1
103103
DA:284,1
104104
DA:285,1
105105
DA:286,1
106-
DA:288,0
107-
DA:289,0
108-
DA:290,0
109-
DA:294,0
110-
DA:295,0
111-
DA:297,0
112-
DA:298,0
113-
DA:299,0
114-
DA:300,0
115-
DA:303,0
116-
DA:304,0
106+
DA:288,1
107+
DA:289,1
108+
DA:290,1
109+
DA:294,1
110+
DA:295,2
111+
DA:297,1
112+
DA:298,2
113+
DA:299,1
114+
DA:300,2
115+
DA:303,2
116+
DA:304,2
117117
DA:310,1
118118
DA:315,1
119119
DA:322,1
@@ -139,9 +139,9 @@ DA:351,2
139139
DA:352,2
140140
DA:353,2
141141
DA:356,1
142-
DA:357,0
143-
DA:358,0
144-
DA:359,0
142+
DA:357,2
143+
DA:358,2
144+
DA:359,1
145145
DA:363,2
146146
DA:364,1
147147
DA:365,3
@@ -154,5 +154,5 @@ DA:390,2
154154
DA:391,2
155155
DA:393,1
156156
LF:154
157-
LH:138
157+
LH:152
158158
end_of_record

test/src/ht_data_postgres_test.dart

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,38 @@ void main() {
491491
expect(params, {'p0': '%Test%'});
492492
});
493493

494+
test('should build correct query for exact match operator', () async {
495+
final mockResult = Result(
496+
rows: [],
497+
affectedRows: 0,
498+
schema: ResultSchema([]),
499+
);
500+
when(
501+
() => mockConnection.execute(
502+
any(),
503+
parameters: any(named: 'parameters'),
504+
),
505+
).thenAnswer((_) async => mockResult);
506+
507+
await sut.readAllByQuery({'name': 'Test'});
508+
509+
final captured = verify(
510+
() => mockConnection.execute(
511+
captureAny(),
512+
parameters: captureAny(named: 'parameters'),
513+
),
514+
).captured;
515+
516+
final sql = captured[0] as Sql;
517+
final params = captured[1] as Map<String, dynamic>;
518+
519+
expect(
520+
(sql as dynamic).sql,
521+
'SELECT * FROM test_models WHERE name = @p0;',
522+
);
523+
expect(params, {'p0': 'Test'});
524+
});
525+
494526
test(
495527
'should return paginated response with hasMore true when limit is exceeded',
496528
() async {
@@ -524,6 +556,68 @@ void main() {
524556
throwsA(isA<InvalidInputException>()),
525557
);
526558
});
559+
560+
test('should throw BadRequestException on foreign key violation', () {
561+
final exception = FakeServerException(
562+
message: 'foreign key violation',
563+
code: '23503',
564+
);
565+
when(
566+
() => mockConnection.execute(
567+
any(),
568+
parameters: any(named: 'parameters'),
569+
),
570+
).thenThrow(exception);
571+
572+
expect(
573+
() => sut.readAllByQuery({}),
574+
throwsA(isA<BadRequestException>()),
575+
);
576+
});
577+
578+
test('should throw OperationFailedException on generic PgException', () {
579+
final exception = PgException('generic connection error');
580+
when(
581+
() => mockConnection.execute(
582+
any(),
583+
parameters: any(named: 'parameters'),
584+
),
585+
).thenThrow(exception);
586+
587+
expect(
588+
() => sut.readAllByQuery({}),
589+
throwsA(isA<OperationFailedException>()),
590+
);
591+
});
592+
593+
test(
594+
'should throw OperationFailedException on unknown ServerException code',
595+
() {
596+
final exception = FakeServerException(
597+
message: 'some other server error',
598+
code: 'XXXXX',
599+
);
600+
when(
601+
() => mockConnection.execute(
602+
any(),
603+
parameters: any(named: 'parameters'),
604+
),
605+
).thenThrow(exception);
606+
607+
expect(
608+
() => sut.readAllByQuery({}),
609+
throwsA(isA<OperationFailedException>()),
610+
);
611+
});
612+
613+
test('should rethrow generic Exception for unknown errors', () {
614+
final exception = Exception('a completely unknown error');
615+
when(() => mockConnection.execute(any(), parameters: any(named: 'parameters')))
616+
.thenThrow(exception);
617+
618+
expect(() => sut.readAllByQuery({}),
619+
throwsA(isA<Exception>().having((e) => e.toString(), 'toString', contains('An unknown error occurred'))));
620+
});
527621
});
528622
});
529623
}

0 commit comments

Comments
 (0)