diff --git a/test/services/database/mock_post_data.dart b/test/models/database/post/mock_post_data.dart similarity index 93% rename from test/services/database/mock_post_data.dart rename to test/models/database/post/mock_post_data.dart index 8c7061d2..1088b4bd 100644 --- a/test/services/database/mock_post_data.dart +++ b/test/models/database/post/mock_post_data.dart @@ -6,7 +6,8 @@ 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 { +/// Helper class to create mock post data to be used in tests +class MockPostFirestore { PostFirestore createPostAt( PostData data, GeoPoint location, { 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..39f44834 --- /dev/null +++ b/test/models/database/user/mock_user_data.dart @@ -0,0 +1,27 @@ +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"; + +/// Helper class to generate mock user data to be used in tests +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(); + } +} 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), + ); + } +} diff --git a/test/services/database/post_repository_test.dart b/test/services/database/post_repository_test.dart index 4c652804..e7e970ba 100644 --- a/test/services/database/post_repository_test.dart +++ b/test/services/database/post_repository_test.dart @@ -9,13 +9,13 @@ 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", () { 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",