From b207dbf3fe9c30357ad65f2049fa60fcb7a88a58 Mon Sep 17 00:00:00 2001 From: Yoann Lafore Date: Sun, 7 Apr 2024 22:18:11 +0200 Subject: [PATCH 1/4] feat(mock): add mocks for the repositories --- .../mock_post_repository_service.dart | 60 +++++++++++++++++++ .../mock_user_repository_service.dart | 43 +++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 test/services/database/mock_post_repository_service.dart create mode 100644 test/services/database/mock_user_repository_service.dart diff --git a/test/services/database/mock_post_repository_service.dart b/test/services/database/mock_post_repository_service.dart new file mode 100644 index 00000000..02ae779d --- /dev/null +++ b/test/services/database/mock_post_repository_service.dart @@ -0,0 +1,60 @@ +import "package:cloud_firestore/cloud_firestore.dart"; +import "package:flutter_test/flutter_test.dart"; +import "package:mockito/mockito.dart"; +import "package:proxima/models/database/post/post_data.dart"; +import "package:proxima/models/database/post/post_firestore.dart"; +import "package:proxima/models/database/post/post_id_firestore.dart"; +import "package:proxima/models/database/post/post_location_firestore.dart"; +import "package:proxima/models/database/user/user_id_firestore.dart"; +import "package:proxima/services/database/post_repository_service.dart"; + +/// Not a coherent representation of a [PostFirestore] +/// This is just here as a placeholder value that will be overridden in the tests +final _mockEmptyFirestorePost = PostFirestore( + id: const PostIdFirestore(value: ""), + location: const PostLocationFirestore( + geoPoint: GeoPoint(0, 0), + geohash: "", + ), + data: PostData( + ownerId: const UserIdFirestore(value: ""), + title: "", + description: "", + publicationTime: Timestamp.fromMillisecondsSinceEpoch(0), + voteScore: 0, + ), +); + +class MockPostRepositoryService extends Mock implements PostRepositoryService { + @override + Future addPost(PostData postData, GeoPoint position) { + return super.noSuchMethod( + Invocation.method(#addPost, [postData, position]), + returnValue: Future.value(), + ); + } + + @override + Future deletePost(PostIdFirestore postId) { + return super.noSuchMethod( + Invocation.method(#deletePost, [postId]), + returnValue: Future.value(), + ); + } + + @override + Future getPost(PostIdFirestore postId) { + return super.noSuchMethod( + Invocation.method(#getPost, [postId]), + returnValue: Future.value(_mockEmptyFirestorePost), + ); + } + + @override + Future> getNearPosts(GeoPoint point, double radius) { + return super.noSuchMethod( + Invocation.method(#getNearPosts, [point, radius]), + returnValue: Future.value(List.empty()), + ); + } +} diff --git a/test/services/database/mock_user_repository_service.dart b/test/services/database/mock_user_repository_service.dart new file mode 100644 index 00000000..72e08c1a --- /dev/null +++ b/test/services/database/mock_user_repository_service.dart @@ -0,0 +1,43 @@ +import "package:cloud_firestore/cloud_firestore.dart"; +import "package:mockito/mockito.dart"; +import "package:proxima/models/database/user/user_data.dart"; +import "package:proxima/models/database/user/user_firestore.dart"; +import "package:proxima/models/database/user/user_id_firestore.dart"; +import "package:proxima/services/database/user_repository_service.dart"; + +/// Not a coherent representation of a [UserFirestore] +/// This is just here as a placeholder value that will be overridden in the tests +final _mockEmptyFirestoreUser = UserFirestore( + data: UserData( + displayName: "", + username: "", + joinTime: Timestamp.fromMillisecondsSinceEpoch(0), + ), + uid: const UserIdFirestore(value: ""), +); + +class MockUserRepositoryService extends Mock implements UserRepositoryService { + @override + Future getUser(UserIdFirestore uid) { + return super.noSuchMethod( + Invocation.method(#getUser, [uid]), + returnValue: Future.value(_mockEmptyFirestoreUser), + ); + } + + @override + Future setUser(UserIdFirestore uid, UserData userData) { + return super.noSuchMethod( + Invocation.method(#setUser, [uid, userData]), + returnValue: Future.value(), + ); + } + + @override + Future doesUserExist(UserIdFirestore uid) { + return super.noSuchMethod( + Invocation.method(#doesUserExist, [uid]), + returnValue: Future.value(false), + ); + } +} From cebea67716c2fb8efef75cf40009232178ff9a70 Mon Sep 17 00:00:00 2001 From: Yoann Lafore Date: Sun, 7 Apr 2024 22:19:19 +0200 Subject: [PATCH 2/4] feat(mock): add helper class to create mock users --- test/models/database/user/mock_user_data.dart | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/models/database/user/mock_user_data.dart diff --git a/test/models/database/user/mock_user_data.dart b/test/models/database/user/mock_user_data.dart new file mode 100644 index 00000000..62cfb3e9 --- /dev/null +++ b/test/models/database/user/mock_user_data.dart @@ -0,0 +1,26 @@ +import "package:cloud_firestore/cloud_firestore.dart"; +import "package:collection/collection.dart"; +import "package:proxima/models/database/user/user_data.dart"; +import "package:proxima/models/database/user/user_firestore.dart"; +import "package:proxima/models/database/user/user_id_firestore.dart"; + +class MockUserFirestore { + List generateUserData(int count) { + return List.generate(count, (i) { + return UserData( + displayName: "display_name_$i", + username: "username_$i", + joinTime: Timestamp.fromMillisecondsSinceEpoch(1000 * i), + ); + }); + } + + List generateUserFirestore(int count) { + return generateUserData(count).mapIndexed((i, data) { + return UserFirestore( + uid: UserIdFirestore(value: "user_id_$i"), + data: data, + ); + }).toList(); + } +} From ffd5e05e4867b4a063ad1f48962876b63b0325ab Mon Sep 17 00:00:00 2001 From: Yoann Lafore Date: Mon, 8 Apr 2024 00:38:20 +0200 Subject: [PATCH 3/4] refactor(mock): rename MockFirestorePost to MockPostFirestore for consistency --- test/services/database/mock_post_data.dart | 2 +- .../database/post_repository_test.dart | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/test/services/database/mock_post_data.dart b/test/services/database/mock_post_data.dart index 8c7061d2..8018bf6f 100644 --- a/test/services/database/mock_post_data.dart +++ b/test/services/database/mock_post_data.dart @@ -6,7 +6,7 @@ import "package:proxima/models/database/post/post_id_firestore.dart"; import "package:proxima/models/database/post/post_location_firestore.dart"; import "package:proxima/models/database/user/user_id_firestore.dart"; -class MockFirestorePost { +class MockPostFirestore { PostFirestore createPostAt( PostData data, GeoPoint location, { diff --git a/test/services/database/post_repository_test.dart b/test/services/database/post_repository_test.dart index 4c652804..a76fdf33 100644 --- a/test/services/database/post_repository_test.dart +++ b/test/services/database/post_repository_test.dart @@ -15,7 +15,7 @@ void main() { group("Post Repository testing", () { late FakeFirebaseFirestore firestore; late PostRepositoryService postRepository; - late MockFirestorePost mockFirestorePost; + late MockPostFirestore mockPostFirstore; const kmRadius = 0.1; @@ -46,7 +46,7 @@ void main() { postRepository = PostRepositoryService( firestore: firestore, ); - mockFirestorePost = MockFirestorePost(); + mockPostFirstore = MockPostFirestore(); }); final post = PostFirestore( @@ -93,8 +93,8 @@ void main() { const userPosition = GeoPoint(40, 20); const postPoint = GeoPoint(40.0001, 20.0001); // 14m away - final postData = mockFirestorePost.generatePostData(1).first; - final expectedPost = mockFirestorePost.createPostAt(postData, postPoint); + final postData = mockPostFirstore.generatePostData(1).first; + final expectedPost = mockPostFirstore.createPostAt(postData, postPoint); await setPostFirestore(expectedPost); @@ -108,8 +108,8 @@ void main() { const postPoint = GeoPoint(40.001, 20.001); // about 140m away - final postData = mockFirestorePost.generatePostData(1).first; - final expectedPost = mockFirestorePost.createPostAt(postData, postPoint); + final postData = mockPostFirstore.generatePostData(1).first; + final expectedPost = mockPostFirstore.createPostAt(postData, postPoint); await setPostFirestore(expectedPost); @@ -125,8 +125,8 @@ void main() { 52.001188563379976 - 1e-5, ); // just below 100m away - final postData = mockFirestorePost.generatePostData(1).first; - final expectedPost = mockFirestorePost.createPostAt(postData, postPoint); + final postData = mockPostFirstore.generatePostData(1).first; + final expectedPost = mockPostFirstore.createPostAt(postData, postPoint); await setPostFirestore(expectedPost); @@ -142,8 +142,8 @@ void main() { 52.001188563379976 + 1e-5, ); // just above 100m away - final postData = mockFirestorePost.generatePostData(1).first; - final expectedPost = mockFirestorePost.createPostAt(postData, postPoint); + final postData = mockPostFirstore.generatePostData(1).first; + final expectedPost = mockPostFirstore.createPostAt(postData, postPoint); await setPostFirestore(expectedPost); @@ -157,7 +157,7 @@ void main() { final userGeoFirePoint = GeoFirePoint(userPosition.latitude, userPosition.longitude); - final postData = mockFirestorePost.generatePostData(1).first; + final postData = mockPostFirstore.generatePostData(1).first; await postRepository.addPost(postData, userPosition); @@ -186,10 +186,10 @@ void main() { return GeoPoint(40.0001 + i * 0.0001, 20.0001 + i * 0.0001); }); - final postsData = mockFirestorePost.generatePostData(nbPosts); + final postsData = mockPostFirstore.generatePostData(nbPosts); final allPosts = List.generate(nbPosts, (i) { - return mockFirestorePost.createPostAt( + return mockPostFirstore.createPostAt( postsData[i], pointList[i], id: "post_$i", From dcc541d37a5ec24d3d5ab366f8fe36fc5c32faae Mon Sep 17 00:00:00 2001 From: Yoann Lafore Date: Mon, 8 Apr 2024 00:41:03 +0200 Subject: [PATCH 4/4] refactor(mock): relocate MockPostFirestore for consistency --- .../database => models/database/post}/mock_post_data.dart | 1 + test/models/database/user/mock_user_data.dart | 1 + test/services/database/post_repository_test.dart | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) rename test/{services/database => models/database/post}/mock_post_data.dart (95%) diff --git a/test/services/database/mock_post_data.dart b/test/models/database/post/mock_post_data.dart similarity index 95% rename from test/services/database/mock_post_data.dart rename to test/models/database/post/mock_post_data.dart index 8018bf6f..1088b4bd 100644 --- a/test/services/database/mock_post_data.dart +++ b/test/models/database/post/mock_post_data.dart @@ -6,6 +6,7 @@ import "package:proxima/models/database/post/post_id_firestore.dart"; import "package:proxima/models/database/post/post_location_firestore.dart"; import "package:proxima/models/database/user/user_id_firestore.dart"; +/// Helper class to create mock post data to be used in tests class MockPostFirestore { PostFirestore createPostAt( PostData data, diff --git a/test/models/database/user/mock_user_data.dart b/test/models/database/user/mock_user_data.dart index 62cfb3e9..39f44834 100644 --- a/test/models/database/user/mock_user_data.dart +++ b/test/models/database/user/mock_user_data.dart @@ -4,6 +4,7 @@ import "package:proxima/models/database/user/user_data.dart"; import "package:proxima/models/database/user/user_firestore.dart"; import "package:proxima/models/database/user/user_id_firestore.dart"; +/// Helper class to generate mock user data to be used in tests class MockUserFirestore { List generateUserData(int count) { return List.generate(count, (i) { diff --git a/test/services/database/post_repository_test.dart b/test/services/database/post_repository_test.dart index a76fdf33..e7e970ba 100644 --- a/test/services/database/post_repository_test.dart +++ b/test/services/database/post_repository_test.dart @@ -9,7 +9,7 @@ import "package:proxima/models/database/post/post_location_firestore.dart"; import "package:proxima/models/database/user/user_id_firestore.dart"; import "package:proxima/services/database/post_repository_service.dart"; -import "mock_post_data.dart"; +import "../../models/database/post/mock_post_data.dart"; void main() { group("Post Repository testing", () {