diff --git a/lib/services/hive_manager.dart b/lib/services/hive_manager.dart index c87f99140..01626edb8 100644 --- a/lib/services/hive_manager.dart +++ b/lib/services/hive_manager.dart @@ -25,14 +25,9 @@ class HiveManager { /// **returns**: /// None static Future initializeHive({required Directory dir}) async { - try { - _initHive(dir); - await _registerAdapters(); - await _openBoxes(); - } catch (e) { - // Handle initialization error - print('Hive initialization failed: $e'); - } + _initHive(dir); + await registerAdapters(); + await _openBoxes(); } /// Initializes Hive with the specified directory path. @@ -46,57 +41,91 @@ class HiveManager { Hive.init(dir.path); } - /// Registers the necessary Hive adapters for the models used in the application. + /// Registers the [adapter] named adapter. /// /// **params**: - /// None + /// * `adapter`: Adapter to register. /// /// **returns**: /// None - static Future _registerAdapters() async { + static Future registerAdapter(TypeAdapter adapter) async { try { - Hive - ..registerAdapter(UserAdapter()) - ..registerAdapter(OrgInfoAdapter()) - ..registerAdapter(AsymetricKeysAdapter()) - ..registerAdapter(CachedUserActionAdapter()) - ..registerAdapter(CachedOperationTypeAdapter()) - ..registerAdapter( - CachedUserActionStatusAdapter(), - ) - ..registerAdapter(PostAdapter()) - ..registerAdapter(EventAdapter()) - ..registerAdapter(LikedByAdapter()) - ..registerAdapter(AttendeeAdapter()) - ..registerAdapter(CommentAdapter()) - ..registerAdapter(CommentsAdapter()); + Hive.registerAdapter(adapter); } catch (e) { print('Failed to register Hive adapters: $e'); } } - /// Opens the necessary Hive boxes for storing various types of data. + /// Opens the [boxName] named box. /// /// **params**: + /// * `boxName`: Name of the box. + /// + /// **returns**: /// None + static Future openBox(String boxName) async { + try { + await Hive.openBox(boxName); + } catch (e) { + print('Failed to open box $boxName'); + } + } + + /// Closes the [boxName] named box. + /// + /// **params**: + /// * `boxName`: Name of the box. /// /// **returns**: /// None - static Future _openBoxes() async { + static Future closeBox(String boxName) async { try { - await Hive.openBox(HiveKeys.userBoxKey); - await Hive.openBox(HiveKeys.orgBoxKey); - await Hive.openBox(HiveKeys.asymetricKeyBoxKey); - await Hive.openBox(HiveKeys.pluginBoxKey); - await Hive.openBox(HiveKeys.urlBoxKey); - await Hive.openBox(HiveKeys.offlineActionQueueKey); - await Hive.openBox(HiveKeys.postFeedKey); - await Hive.openBox(HiveKeys.eventFeedKey); + await Hive.box(boxName).close(); } catch (e) { - print('Failed to open Hive boxes: $e'); + print('Failed to close the box $boxName'); } } + /// Registers the necessary Hive adapters for the models used in the application. + /// + /// **params**: + /// None + /// + /// **returns**: + /// None + static Future registerAdapters() async { + registerAdapter(UserAdapter()); + registerAdapter(OrgInfoAdapter()); + registerAdapter(AsymetricKeysAdapter()); + registerAdapter(CachedUserActionAdapter()); + registerAdapter(CachedOperationTypeAdapter()); + registerAdapter(CachedUserActionStatusAdapter()); + registerAdapter(PostAdapter()); + registerAdapter(EventAdapter()); + registerAdapter(LikedByAdapter()); + registerAdapter(AttendeeAdapter()); + registerAdapter(CommentAdapter()); + registerAdapter(CommentsAdapter()); + } + + /// Opens the necessary Hive boxes for storing various types of data. + /// + /// **params**: + /// None + /// + /// **returns**: + /// None + static Future _openBoxes() async { + await openBox(HiveKeys.userBoxKey); + await openBox(HiveKeys.orgBoxKey); + await openBox(HiveKeys.asymetricKeyBoxKey); + await openBox(HiveKeys.pluginBoxKey); + await openBox(HiveKeys.urlBoxKey); + await openBox(HiveKeys.offlineActionQueueKey); + await openBox(HiveKeys.postFeedKey); + await openBox(HiveKeys.eventFeedKey); + } + /// Closes all opened Hive boxes and the Hive instance itself. /// /// This method ensures that all Hive boxes are properly closed to avoid potential data corruption @@ -108,12 +137,8 @@ class HiveManager { /// **returns**: /// None static Future teardownHive() async { - try { - await _closeBoxes(); - await Hive.close(); - } catch (e) { - print('Failed to close Hive: $e'); - } + await _closeBoxes(); + await Hive.close(); } /// Closes all opened Hive boxes. @@ -124,17 +149,13 @@ class HiveManager { /// **returns**: /// None static Future _closeBoxes() async { - try { - await Hive.box(HiveKeys.userBoxKey).close(); - await Hive.box(HiveKeys.orgBoxKey).close(); - await Hive.box(HiveKeys.asymetricKeyBoxKey).close(); - await Hive.box(HiveKeys.pluginBoxKey).close(); - await Hive.box(HiveKeys.urlBoxKey).close(); - await Hive.box(HiveKeys.offlineActionQueueKey).close(); - await Hive.box(HiveKeys.postFeedKey).close(); - await Hive.box(HiveKeys.eventFeedKey).close(); - } catch (e) { - print('Failed to close a Hive box: $e'); - } + await closeBox(HiveKeys.userBoxKey); + await closeBox(HiveKeys.orgBoxKey); + await closeBox(HiveKeys.asymetricKeyBoxKey); + await closeBox(HiveKeys.pluginBoxKey); + await closeBox(HiveKeys.urlBoxKey); + await closeBox(HiveKeys.offlineActionQueueKey); + await closeBox(HiveKeys.postFeedKey); + await closeBox(HiveKeys.eventFeedKey); } } diff --git a/test/service_tests/hive_manager_test.dart b/test/service_tests/hive_manager_test.dart index 646da4b7c..89eb4e8ad 100644 --- a/test/service_tests/hive_manager_test.dart +++ b/test/service_tests/hive_manager_test.dart @@ -1,19 +1,38 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:hive/hive.dart'; import 'package:talawa/constants/constants.dart'; +import 'package:talawa/models/organization/org_info.dart'; +import 'package:talawa/models/user/user_info.dart'; import 'package:talawa/services/hive_manager.dart'; void main() { - test('tearDownHive', () async { - await HiveManager.teardownHive(); + group('test HiveManager', () { + test('tearDownHive', () async { + await HiveManager.teardownHive(); - expect(Hive.isBoxOpen(HiveKeys.userBoxKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.orgBoxKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.asymetricKeyBoxKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.pluginBoxKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.urlBoxKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.offlineActionQueueKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.postFeedKey), isFalse); - expect(Hive.isBoxOpen(HiveKeys.eventFeedKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.userBoxKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.orgBoxKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.asymetricKeyBoxKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.pluginBoxKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.urlBoxKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.offlineActionQueueKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.postFeedKey), isFalse); + expect(Hive.isBoxOpen(HiveKeys.eventFeedKey), isFalse); + + await HiveManager.teardownHive(); + }); + + test('openBox', () async { + await HiveManager.openBox(HiveKeys.orgBoxKey); + await HiveManager.openBox(HiveKeys.orgBoxKey); + }); + + test('closeHiveBox', () async { + await HiveManager.closeBox('xyz'); + }); + + test('registerAdapter', () async { + await HiveManager.registerAdapter(UserAdapter()); + }); }); } diff --git a/test/service_tests/post_service_test.dart b/test/service_tests/post_service_test.dart index efd67e676..26da8e0bb 100644 --- a/test/service_tests/post_service_test.dart +++ b/test/service_tests/post_service_test.dart @@ -580,12 +580,26 @@ void main() { const Duration(seconds: 1), ); // Adjust the delay as needed + when( + dataBaseMutationFunctions.gqlAuthQuery( + queryNewOrg, + ), + ).thenAnswer( + (_) async => QueryResult( + options: QueryOptions(document: gql(query)), + data: null, + source: QueryResultSource.network, + ), + ); + + await service.getPosts(); + // Verify that refresh token was called to check getPost method was called correctly. verify( dataBaseMutationFunctions.gqlAuthQuery( queryNewOrg, ), - ).called(1); + ).called(2); // Close the stream controller to avoid memory leaks await orgInfoStreamController.close(); diff --git a/test/view_model_tests/after_auth_view_model_tests/event_view_model_tests/explore_events_view_model_test.dart b/test/view_model_tests/after_auth_view_model_tests/event_view_model_tests/explore_events_view_model_test.dart index 4b494bf26..1866ab8e5 100644 --- a/test/view_model_tests/after_auth_view_model_tests/event_view_model_tests/explore_events_view_model_test.dart +++ b/test/view_model_tests/after_auth_view_model_tests/event_view_model_tests/explore_events_view_model_test.dart @@ -78,7 +78,7 @@ void main() { endDate: '2024-01-14', startTime: '08:01:00.000Z', endTime: '08:50:00.000Z', - creator: User(id: 'Test Id'), + creator: User(id: 'xzy1'), isPublic: true, isRegistered: true, isRegisterable: true,