From e6d85c4723693ff0e0c4e1e369252e997f01d6b3 Mon Sep 17 00:00:00 2001 From: mohitrajsinha Date: Tue, 22 Oct 2024 00:36:22 +0530 Subject: [PATCH 1/3] Created tbdex_quote_notifier_test.dart --- .../tbdex/tbdex_quote_notifier_test.dart | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/features/tbdex/tbdex_quote_notifier_test.dart diff --git a/test/features/tbdex/tbdex_quote_notifier_test.dart b/test/features/tbdex/tbdex_quote_notifier_test.dart new file mode 100644 index 00000000..3c9bc741 --- /dev/null +++ b/test/features/tbdex/tbdex_quote_notifier_test.dart @@ -0,0 +1,58 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import '../../helpers/mocks.dart'; +import '../../helpers/test_data.dart'; + +void main() async { + late MockTbdexService mockTbdexService; + late MockTbdexQuoteNotifier mockTbdexQuoteNotifier; + + await TestData.initializeDids(); + + setUpAll(() { + registerFallbackValue(MockBearerDid()); + registerFallbackValue(TestData.aliceDid); + }); + + setUp(() { + mockTbdexService = MockTbdexService(); + mockTbdexQuoteNotifier = MockTbdexQuoteNotifier(); + + when(() => mockTbdexService.getExchange(any(), any(), any())) + .thenAnswer((_) async => TestData.getExchange()); + }); + + group('TbdexQuoteNotifier Stress Tests', () { + test('High-frequency polling with exponential backoff', () async { + when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) + .thenAnswer((_) async => TestData.getQuote()); + + await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + + verify(() => mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId')) + .called(1); + }); + + test('Maximum retry attempts', () async { + when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) + .thenThrow(Exception('Network error')); + + await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + + verify(() => mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId')) + .called(1); + }); + + test('Handling network latency', () async { + when(() => mockTbdexQuoteNotifier.startPolling(any(), any())) + .thenAnswer((_) async { + await Future.delayed(const Duration(seconds: 5)); + return TestData.getQuote(); + }); + + await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + + verify(() => mockTbdexQuoteNotifier.startPolling(any(), any())).called(1); + }); + }); +} From ffd4ee5fa24a0f8ff17a859f61be052cc39bdfbf Mon Sep 17 00:00:00 2001 From: mohitrajsinha Date: Thu, 24 Oct 2024 01:20:22 +0530 Subject: [PATCH 2/3] Updated tbdex_quote_notifier_test.dart --- test/features/tbdex/tbdex_quote_notifier_test.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/features/tbdex/tbdex_quote_notifier_test.dart b/test/features/tbdex/tbdex_quote_notifier_test.dart index 3c9bc741..c13f779f 100644 --- a/test/features/tbdex/tbdex_quote_notifier_test.dart +++ b/test/features/tbdex/tbdex_quote_notifier_test.dart @@ -22,8 +22,9 @@ void main() async { .thenAnswer((_) async => TestData.getExchange()); }); - group('TbdexQuoteNotifier Stress Tests', () { - test('High-frequency polling with exponential backoff', () async { + group('TbdexQuoteNotifier', () { + test('should handle high-frequency polling with exponential backoff', + () async { when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) .thenAnswer((_) async => TestData.getQuote()); @@ -33,7 +34,7 @@ void main() async { .called(1); }); - test('Maximum retry attempts', () async { + test('should handle maximum retry attempts', () async { when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) .thenThrow(Exception('Network error')); @@ -43,7 +44,7 @@ void main() async { .called(1); }); - test('Handling network latency', () async { + test('should handle network latency', () async { when(() => mockTbdexQuoteNotifier.startPolling(any(), any())) .thenAnswer((_) async { await Future.delayed(const Duration(seconds: 5)); From f3201017d4e3607e698795bfe629e5b890a979a1 Mon Sep 17 00:00:00 2001 From: mohitrajsinha Date: Thu, 24 Oct 2024 14:29:12 +0530 Subject: [PATCH 3/3] update tbdex_quote_notifier_test.dart --- .../tbdex/tbdex_quote_notifier_test.dart | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/test/features/tbdex/tbdex_quote_notifier_test.dart b/test/features/tbdex/tbdex_quote_notifier_test.dart index c13f779f..fee27e00 100644 --- a/test/features/tbdex/tbdex_quote_notifier_test.dart +++ b/test/features/tbdex/tbdex_quote_notifier_test.dart @@ -1,59 +1,68 @@ +import 'package:didpay/features/did/did_provider.dart'; +import 'package:didpay/features/tbdex/tbdex_quote_notifier.dart'; +import 'package:didpay/features/tbdex/tbdex_service.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; + import '../../helpers/mocks.dart'; +import '../../helpers/riverpod_helpers.dart'; import '../../helpers/test_data.dart'; -void main() async { - late MockTbdexService mockTbdexService; - late MockTbdexQuoteNotifier mockTbdexQuoteNotifier; - +Future main() async { await TestData.initializeDids(); - setUpAll(() { - registerFallbackValue(MockBearerDid()); - registerFallbackValue(TestData.aliceDid); - }); + final did = TestData.aliceDid; + const pfiDid = '123'; + const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; - setUp(() { - mockTbdexService = MockTbdexService(); - mockTbdexQuoteNotifier = MockTbdexQuoteNotifier(); + group('TbdexQuoteNotifier`', () { + group('startPolling', () { + test('should return Quote if successful', () async { + final mockTbdexService = MockTbdexService(); + final quote = TestData.getQuote(); - when(() => mockTbdexService.getExchange(any(), any(), any())) - .thenAnswer((_) async => TestData.getExchange()); - }); + when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId)) + .thenAnswer((_) async => [quote]); - group('TbdexQuoteNotifier', () { - test('should handle high-frequency polling with exponential backoff', - () async { - when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) - .thenAnswer((_) async => TestData.getQuote()); + final container = createContainer( + overrides: [ + tbdexServiceProvider.overrideWith( + (ref) => mockTbdexService, + ), + didProvider.overrideWith((ref) => did), + ], + ); - await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + final tbdexQuoteNotifier = container.read(quoteProvider.notifier); - verify(() => mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId')) - .called(1); - }); + final result = + await tbdexQuoteNotifier.startPolling(pfiDid, exchangeId); - test('should handle maximum retry attempts', () async { - when(() => mockTbdexQuoteNotifier.pollForQuote(any(), any())) - .thenThrow(Exception('Network error')); + expect(result, quote); + }); - await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + test('should throw an Exception if not successful', () async { + final mockTbdexService = MockTbdexService(); - verify(() => mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId')) - .called(1); - }); + when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId)) + .thenThrow(Exception('Error')); - test('should handle network latency', () async { - when(() => mockTbdexQuoteNotifier.startPolling(any(), any())) - .thenAnswer((_) async { - await Future.delayed(const Duration(seconds: 5)); - return TestData.getQuote(); - }); + final container = createContainer( + overrides: [ + tbdexServiceProvider.overrideWith( + (ref) => mockTbdexService, + ), + didProvider.overrideWith((ref) => did), + ], + ); - await mockTbdexQuoteNotifier.startPolling('pfiId', 'exchangeId'); + final tbdexQuoteNotifier = container.read(quoteProvider.notifier); - verify(() => mockTbdexQuoteNotifier.startPolling(any(), any())).called(1); + expect( + () => tbdexQuoteNotifier.startPolling(pfiDid, exchangeId), + throwsA(isA()), + ); + }); }); }); }