Skip to content

Commit

Permalink
Fixes PalisadoesFoundation#1535 Tests for Post services.dart (Palisad…
Browse files Browse the repository at this point in the history
…oesFoundation#2103)

* Add:Tests for post_service.dart

* Fix: Format

* Fix: lint issues

* Add:Fixed Format

* Add:Hive.deleteFromDisk() wherever Hive is used

* Remove: Unused Hive Box
  • Loading branch information
AyushRaghuvanshi authored and palisadian committed Jan 10, 2024
1 parent 4c5d85a commit d449837
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 22 deletions.
193 changes: 193 additions & 0 deletions test/service_tests/post_service_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:mockito/mockito.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/models/post/post_model.dart';
import 'package:talawa/services/database_mutation_functions.dart';
import 'package:talawa/services/post_service.dart';
import 'package:talawa/utils/post_queries.dart';
import '../helpers/test_helpers.dart';

/// Tests post_service.dart.
///
/// **params**:
/// None
///
/// **returns**:
/// None
void main() {
setUp(() {
registerServices();
});
final demoJson = {
'postsByOrganization': [
{
'__typename': 'Post',
'_id': '1',
'text': 'text #hastag',
'createdAt': '2023-11-13T19:28:21.095Z',
'imageUrl': 'https://imageurl',
'videoUrl': 'https://videoUrl',
'title': 'demo title',
'commentCount': 0,
'likeCount': 0,
'creator': {
'__typename': 'User',
'_id': '1',
'firstName': 'Ayush',
'lastName': 'Raghuwanshi',
'image': 'https://imageUrl',
},
'organization': {'__typename': 'Organization', '_id': '1'},
'likedBy': [],
'comments': [],
},
{
'__typename': 'Post',
'_id': '2',
'text': 'text #hastag',
'createdAt': '2023-11-13T19:28:21.095Z',
'imageUrl': 'https://imageurl',
'videoUrl': 'https://videoUrl',
'title': 'demo title',
'commentCount': 0,
'likeCount': 0,
'creator': {
'__typename': 'User',
'_id': '1',
'firstName': 'Ayush',
'lastName': 'Raghuwanshi',
'image': 'https://imageUrl',
},
'organization': {'__typename': 'Organization', '_id': '1'},
'likedBy': [],
'comments': [],
}
],
};
//Fake CurrentOrgID
const currentOrgID = 'XYZ';
//Fake PostID
const postID = '1';

group('Test PostService', () {
test('Test getPosts Method', () async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();
//Setting up Demo data to be returned
final query = PostQueries().getPostsById(currentOrgID);
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
await service.getPosts();
//Fetching Post Stream
final List<Post> posts = await service.postStream.first;
//Testing if Two Mock posts got added
expect(posts.length, 2);
});

test('Test addLike Method', () async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();

final query = PostQueries().getPostsById(currentOrgID);
//Mocking GetPosts
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
//Populating posts Stream
await service.getPosts();
//Calling AddLike
await service.addLike(postID);
//Fetching Post Stream
final List<Post> posts = await service.postStream.first;
//Finding The Post which is supposed to be Liked
final Post likedPost =
posts.firstWhere((element) => element.sId == postID);
//Testing if the post got liked
expect(likedPost.likedBy!.length, 1);
});

test('Test removeLike Method', () async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();

final query = PostQueries().getPostsById(currentOrgID);
//Mocking GetPosts
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
//Populating posts Stream
await service.getPosts();
//Liking Post which is to be Unliked
await service.addLike(postID);
//Unliking Post
await service.removeLike(postID);
//Fetching Post Stream
final List<Post> posts = await service.postStream.first;
//Finding The Post which is supposed to be Unliked
final Post likedPost =
posts.firstWhere((element) => element.sId == postID);
//Testing if the post got unliked
expect(likedPost.likedBy!.length, 0);
});

test('Test addCommentLocally Method', () async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();

final query = PostQueries().getPostsById(currentOrgID);
//Mocking GetPosts
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
//Populating posts Stream
await service.getPosts();
//Adding Comment to a Post
service.addCommentLocally(postID);
//Fetching Post Stream
final List<Post> posts = await service.postStream.first;
//Finding The Post which is supposed to be commented
final Post commentedPost =
posts.firstWhere((element) => element.sId == postID);
//Testing if the post got a comment
expect(commentedPost.comments!.length, 1);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// ignore_for_file: talawa_api_doc
// ignore_for_file: talawa_good_doc_comments

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:talawa/constants/constants.dart';
import 'package:talawa/constants/custom_theme.dart';
import 'package:talawa/models/language/language_model.dart';
import 'package:talawa/models/organization/org_info.dart';
import 'package:talawa/models/user/user_info.dart';
import 'package:talawa/router.dart' as router;
import 'package:talawa/services/graphql_config.dart';
import 'package:talawa/services/navigation_service.dart';
Expand Down Expand Up @@ -93,17 +88,6 @@ Future<void> main() async {
TestWidgetsFlutterBinding.ensureInitialized();
locator<GraphqlConfig>().test();
locator<SizeConfig>().test();
final Directory dir = Directory('temporaryPath');
Hive
..init(dir.path)
..registerAdapter(UserAdapter())
..registerAdapter(OrgInfoAdapter());
await Hive.openBox<User>('currentUser');
await Hive.openBox<OrgInfo>('currentOrg');
await Hive.openBox('url');
tearDown(() async {
await Hive.close();
});

group('Setting Page Screen Widget Test in dark mode', () {
testWidgets("Testing if Settings Screen shows up", (tester) async {
Expand Down Expand Up @@ -243,12 +227,6 @@ Future<void> main() async {
.scaffoldBackgroundColor,
TalawaTheme.darkTheme.scaffoldBackgroundColor,
);
File('temporaryPath/currentorg.hive').delete();
File('temporaryPath/currentorg.lock').delete();
File('temporaryPath/currentuser.hive').delete();
File('temporaryPath/currentuser.lock').delete();
File('temporaryPath/url.hive').delete();
File('temporaryPath/url.lock').delete();
});
});
}

0 comments on commit d449837

Please sign in to comment.