Skip to content

Commit

Permalink
fixed code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Azad99-9 committed Sep 2, 2024
1 parent a83b373 commit 89c3ece
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 67 deletions.
131 changes: 76 additions & 55 deletions lib/services/hive_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@ class HiveManager {
/// **returns**:
/// None
static Future<void> 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.
Expand All @@ -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<void> _registerAdapters() async {
static Future<void> registerAdapter<T>(TypeAdapter<T> adapter) async {
try {
Hive
..registerAdapter<User>(UserAdapter())
..registerAdapter<OrgInfo>(OrgInfoAdapter())
..registerAdapter<AsymetricKeys>(AsymetricKeysAdapter())
..registerAdapter<CachedUserAction>(CachedUserActionAdapter())
..registerAdapter<CachedOperationType>(CachedOperationTypeAdapter())
..registerAdapter<CachedUserActionStatus>(
CachedUserActionStatusAdapter(),
)
..registerAdapter<Post>(PostAdapter())
..registerAdapter<Event>(EventAdapter())
..registerAdapter<LikedBy>(LikedByAdapter())
..registerAdapter<Attendee>(AttendeeAdapter())
..registerAdapter<Comment>(CommentAdapter())
..registerAdapter<Comments>(CommentsAdapter());
Hive.registerAdapter<T>(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<void> openBox<T>(String boxName) async {
try {
await Hive.openBox<T>(boxName);
} catch (e) {
print('Failed to open box $boxName');
}
}

/// Closes the [boxName] named box.
///
/// **params**:
/// * `boxName`: Name of the box.
///
/// **returns**:
/// None
static Future<void> _openBoxes() async {
static Future<void> closeBox<T>(String boxName) async {
try {
await Hive.openBox<User>(HiveKeys.userBoxKey);
await Hive.openBox<OrgInfo>(HiveKeys.orgBoxKey);
await Hive.openBox<AsymetricKeys>(HiveKeys.asymetricKeyBoxKey);
await Hive.openBox(HiveKeys.pluginBoxKey);
await Hive.openBox(HiveKeys.urlBoxKey);
await Hive.openBox<CachedUserAction>(HiveKeys.offlineActionQueueKey);
await Hive.openBox<Post>(HiveKeys.postFeedKey);
await Hive.openBox<Event>(HiveKeys.eventFeedKey);
await Hive.box<T>(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<void> registerAdapters() async {
registerAdapter<User>(UserAdapter());
registerAdapter<OrgInfo>(OrgInfoAdapter());
registerAdapter<AsymetricKeys>(AsymetricKeysAdapter());
registerAdapter<CachedUserAction>(CachedUserActionAdapter());
registerAdapter<CachedOperationType>(CachedOperationTypeAdapter());
registerAdapter<CachedUserActionStatus>(CachedUserActionStatusAdapter());
registerAdapter<Post>(PostAdapter());
registerAdapter<Event>(EventAdapter());
registerAdapter<LikedBy>(LikedByAdapter());
registerAdapter<Attendee>(AttendeeAdapter());
registerAdapter<Comment>(CommentAdapter());
registerAdapter<Comments>(CommentsAdapter());
}

/// Opens the necessary Hive boxes for storing various types of data.
///
/// **params**:
/// None
///
/// **returns**:
/// None
static Future<void> _openBoxes() async {
await openBox<User>(HiveKeys.userBoxKey);
await openBox<OrgInfo>(HiveKeys.orgBoxKey);
await openBox<AsymetricKeys>(HiveKeys.asymetricKeyBoxKey);
await openBox(HiveKeys.pluginBoxKey);
await openBox(HiveKeys.urlBoxKey);
await openBox<CachedUserAction>(HiveKeys.offlineActionQueueKey);
await openBox<Post>(HiveKeys.postFeedKey);
await openBox<Event>(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
Expand All @@ -108,12 +137,8 @@ class HiveManager {
/// **returns**:
/// None
static Future<void> 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.
Expand All @@ -124,17 +149,13 @@ class HiveManager {
/// **returns**:
/// None
static Future<void> _closeBoxes() async {
try {
await Hive.box<User>(HiveKeys.userBoxKey).close();
await Hive.box<OrgInfo>(HiveKeys.orgBoxKey).close();
await Hive.box<AsymetricKeys>(HiveKeys.asymetricKeyBoxKey).close();
await Hive.box(HiveKeys.pluginBoxKey).close();
await Hive.box(HiveKeys.urlBoxKey).close();
await Hive.box<CachedUserAction>(HiveKeys.offlineActionQueueKey).close();
await Hive.box<Post>(HiveKeys.postFeedKey).close();
await Hive.box<Event>(HiveKeys.eventFeedKey).close();
} catch (e) {
print('Failed to close a Hive box: $e');
}
await closeBox<User>(HiveKeys.userBoxKey);
await closeBox<OrgInfo>(HiveKeys.orgBoxKey);
await closeBox<AsymetricKeys>(HiveKeys.asymetricKeyBoxKey);
await closeBox(HiveKeys.pluginBoxKey);
await closeBox(HiveKeys.urlBoxKey);
await closeBox<CachedUserAction>(HiveKeys.offlineActionQueueKey);
await closeBox<Post>(HiveKeys.postFeedKey);
await closeBox<Event>(HiveKeys.eventFeedKey);
}
}
39 changes: 29 additions & 10 deletions test/service_tests/hive_manager_test.dart
Original file line number Diff line number Diff line change
@@ -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<OrgInfo>(HiveKeys.orgBoxKey);
await HiveManager.openBox<User>(HiveKeys.orgBoxKey);
});

test('closeHiveBox', () async {
await HiveManager.closeBox('xyz');
});

test('registerAdapter', () async {
await HiveManager.registerAdapter(UserAdapter());
});
});
}
16 changes: 15 additions & 1 deletion test/service_tests/post_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 89c3ece

Please sign in to comment.