Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock repositories #73

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ 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 {
PostFirestore createPostAt(
/// Helper class to create mock post data to be used in tests
class MockPostFirestore {
static PostFirestore createPostAt(
PostData data,
GeoPoint location, {
id = "post_id",
Expand All @@ -24,7 +25,7 @@ class MockFirestorePost {
);
}

List<PostData> generatePostData(int count) {
static List<PostData> generatePostData(int count) {
return List.generate(count, (i) {
return PostData(
description: "description_$i",
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 {
static List<UserData> generateUserData(int count) {
return List.generate(count, (i) {
return UserData(
displayName: "display_name_$i",
username: "username_$i",
joinTime: Timestamp.fromMillisecondsSinceEpoch(1000 * i),
);
});
}

static 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),
);
}
}
26 changes: 12 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,12 @@ 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;

const kmRadius = 0.1;

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

final post = PostFirestore(
Expand Down Expand Up @@ -93,8 +91,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 = MockPostFirestore.generatePostData(1).first;
final expectedPost = MockPostFirestore.createPostAt(postData, postPoint);

await setPostFirestore(expectedPost);

Expand All @@ -108,8 +106,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 = MockPostFirestore.generatePostData(1).first;
final expectedPost = MockPostFirestore.createPostAt(postData, postPoint);

await setPostFirestore(expectedPost);

Expand All @@ -125,8 +123,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 = MockPostFirestore.generatePostData(1).first;
final expectedPost = MockPostFirestore.createPostAt(postData, postPoint);

await setPostFirestore(expectedPost);

Expand All @@ -142,8 +140,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 = MockPostFirestore.generatePostData(1).first;
final expectedPost = MockPostFirestore.createPostAt(postData, postPoint);

await setPostFirestore(expectedPost);

Expand All @@ -157,7 +155,7 @@ void main() {
final userGeoFirePoint =
GeoFirePoint(userPosition.latitude, userPosition.longitude);

final postData = mockFirestorePost.generatePostData(1).first;
final postData = MockPostFirestore.generatePostData(1).first;

await postRepository.addPost(postData, userPosition);

Expand Down Expand Up @@ -186,10 +184,10 @@ void main() {
return GeoPoint(40.0001 + i * 0.0001, 20.0001 + i * 0.0001);
});

final postsData = mockFirestorePost.generatePostData(nbPosts);
final postsData = MockPostFirestore.generatePostData(nbPosts);

final allPosts = List.generate(nbPosts, (i) {
return mockFirestorePost.createPostAt(
return MockPostFirestore.createPostAt(
postsData[i],
pointList[i],
id: "post_$i",
Expand Down