Skip to content

Commit

Permalink
Merge branch 'mock-repositories' into new-post-view-model
Browse files Browse the repository at this point in the history
Need the mock post repositorie to write the tests.
  • Loading branch information
Aderfish committed Apr 9, 2024
2 parents bb51085 + dcc541d commit 0b1d450
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
27 changes: 27 additions & 0 deletions test/models/database/user/mock_user_data.dart
Original file line number Diff line number Diff line change
@@ -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<UserData> generateUserData(int count) {
return List.generate(count, (i) {
return UserData(
displayName: "display_name_$i",
username: "username_$i",
joinTime: Timestamp.fromMillisecondsSinceEpoch(1000 * i),
);
});
}

List<UserFirestore> generateUserFirestore(int count) {
return generateUserData(count).mapIndexed((i, data) {
return UserFirestore(
uid: UserIdFirestore(value: "user_id_$i"),
data: data,
);
}).toList();
}
}
60 changes: 60 additions & 0 deletions test/services/database/mock_post_repository_service.dart
Original file line number Diff line number Diff line change
@@ -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<void> addPost(PostData postData, GeoPoint position) {
return super.noSuchMethod(
Invocation.method(#addPost, [postData, position]),
returnValue: Future.value(),
);
}

@override
Future<void> deletePost(PostIdFirestore postId) {
return super.noSuchMethod(
Invocation.method(#deletePost, [postId]),
returnValue: Future.value(),
);
}

@override
Future<PostFirestore> getPost(PostIdFirestore postId) {
return super.noSuchMethod(
Invocation.method(#getPost, [postId]),
returnValue: Future.value(_mockEmptyFirestorePost),
);
}

@override
Future<List<PostFirestore>> getNearPosts(GeoPoint point, double radius) {
return super.noSuchMethod(
Invocation.method(#getNearPosts, [point, radius]),
returnValue: Future.value(List<PostFirestore>.empty()),
);
}
}
43 changes: 43 additions & 0 deletions test/services/database/mock_user_repository_service.dart
Original file line number Diff line number Diff line change
@@ -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<UserFirestore> getUser(UserIdFirestore uid) {
return super.noSuchMethod(
Invocation.method(#getUser, [uid]),
returnValue: Future.value(_mockEmptyFirestoreUser),
);
}

@override
Future<void> setUser(UserIdFirestore uid, UserData userData) {
return super.noSuchMethod(
Invocation.method(#setUser, [uid, userData]),
returnValue: Future.value(),
);
}

@override
Future<bool> doesUserExist(UserIdFirestore uid) {
return super.noSuchMethod(
Invocation.method(#doesUserExist, [uid]),
returnValue: Future.value(false),
);
}
}
28 changes: 14 additions & 14 deletions test/services/database/post_repository_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,7 +46,7 @@ void main() {
postRepository = PostRepositoryService(
firestore: firestore,
);
mockFirestorePost = MockFirestorePost();
mockPostFirstore = MockPostFirestore();
});

final post = PostFirestore(
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0b1d450

Please sign in to comment.