Skip to content

Commit fdbdbb0

Browse files
committed
test all bugs fixed
1 parent 6b8a1d2 commit fdbdbb0

15 files changed

+548
-1
lines changed

lib/core/model/post_model.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ class PostModel extends Post {
3434
isSelected: json["isSelected"],
3535
createdAt: json["createdAt"],
3636
);
37+
38+
3739
}

lib/layers/presentation/post/read_posts/bloc/read_posts_bloc.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ReadPostsBloc extends Bloc<ReadPostsEvent, ReadPostsState> {
1515
final DeletePost deletePost;
1616
final FindPosts findPosts;
1717
List<Post> selectedPosts = [];
18+
ReadPostsState get initialState => PostInitialState();
1819
ReadPostsBloc({
1920
required this.getPosts,
2021
required this.updatePost,

lib/layers/presentation/post/read_posts/bloc/read_posts_state.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class PostLoadedSuccessState extends ReadPostsState {
1818
}
1919
}
2020

21-
class PostErrorState extends ReadPostsState {}
21+
class PostErrorState extends ReadPostsState {
22+
String message;
23+
PostErrorState(this.message);
24+
}
2225

2326
class PostNavigateToAddPostActionState extends PostActionState {}
2427

test/core/model/post_model_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:blog_app/core/model/post_model.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'dart:convert';
4+
import '../../fixtures/fixture_reader.dart';
5+
6+
void main() {
7+
final tPostModel = PostModel(
8+
id: 37,
9+
content: '',
10+
isSelected: 0,
11+
title: '',
12+
);
13+
14+
test('should be a subclass of Post model', () async {
15+
//assert
16+
expect(tPostModel, isA<PostModel>());
17+
});
18+
19+
group('fromJson', () {
20+
test('should return a valid model', () async {
21+
//arrange
22+
final Map<String, dynamic> jsonMap = json.decode(fixture('Post.json'));
23+
//act
24+
final result = PostModel.fromJson(jsonMap);
25+
//assert
26+
expect(result.id, equals(tPostModel.id));
27+
});
28+
test('should return a valid model when the JSON duration is regarded as a double', () async {
29+
//arrange
30+
final Map<String, dynamic> jsonMap = json.decode(fixture('Post_duration_double.json'));
31+
//act
32+
final result = PostModel.fromJson(jsonMap);
33+
//assert
34+
expect(result, equals(tPostModel));
35+
});
36+
});
37+
38+
group('toJson', () {
39+
test('should return ', () async {
40+
//act
41+
final result = tPostModel.toMap();
42+
//assert
43+
final expectedMap = {
44+
"id": 37,
45+
"content": '',
46+
"isSelected": 0,
47+
"title": '',
48+
};
49+
expect(result, equals(expectedMap));
50+
});
51+
});
52+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'package:blog_app/core/util/input_converter.dart';
2+
import 'package:dartz/dartz.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
late InputConverter inputConverter;
7+
setUp(() {
8+
inputConverter = InputConverter();
9+
});
10+
11+
group('stringToUnsignedInt', () {
12+
test('should return an integer when the string represents an unsigned integer', () async {
13+
//arrange
14+
const str = '123';
15+
//act
16+
final result = inputConverter.stringToUnsignedInteger(str);
17+
//assert
18+
expect(result, equals(const Right(123)));
19+
});
20+
21+
test('should return a Failure when the string is not an integer', () async {
22+
//arrange
23+
const str = 'abc';
24+
//act
25+
final result = inputConverter.stringToUnsignedInteger(str);
26+
//assert
27+
expect(result, equals(Left(InvalidInputFailure())));
28+
});
29+
30+
test('should return a Failure when the string is a negative integer', () async {
31+
//arrange
32+
const str = '-123';
33+
//act
34+
final result = inputConverter.stringToUnsignedInteger(str);
35+
//assert
36+
expect(result, equals(Left(InvalidInputFailure())));
37+
});
38+
});
39+
}

test/fixtures/fixture_reader.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'dart:io';
2+
3+
String fixture(String name) => File('test/fixtures/$name').readAsStringSync();

test/fixtures/post.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 37,
3+
"name": "string",
4+
"description": "This is for random",
5+
"difficulty": "easy",
6+
"duration": 10,
7+
"source": "pre_loaded",
8+
"exercises": []
9+
}

test/fixtures/post_cached.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 37,
3+
"name": "string",
4+
"description": "This is for random",
5+
"difficulty": "easy",
6+
"duration": 10,
7+
"source": "pre_loaded",
8+
"exercises": []
9+
}

test/fixtures/post_title_long.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 37,
3+
"name": "string",
4+
"description": "This is for random",
5+
"difficulty": "easy",
6+
"duration": 10.0,
7+
"source": "pre_loaded",
8+
"exercises": []
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'dart:convert';
2+
import 'package:blog_app/core/db/db_helper.dart';
3+
import 'package:blog_app/core/errors/exceptions.dart';
4+
import 'package:blog_app/core/model/post_model.dart';
5+
import 'package:blog_app/layers/data/post/data_sources/posts_local_data_sources.dart';
6+
import 'package:mockito/annotations.dart';
7+
import 'package:mockito/mockito.dart';
8+
import 'package:flutter_test/flutter_test.dart';
9+
import '../../../../fixtures/fixture_reader.dart';
10+
11+
@GenerateMocks([
12+
DatabaseHelper
13+
], customMocks: [
14+
MockSpec<DatabaseHelper>(as: #MockDatabaseHelperForTest, returnNullOnMissingStub: true),
15+
])
16+
void main() {
17+
late PostLocalDataSourceImpl dataSource;
18+
19+
setUp(() {
20+
dataSource = PostLocalDataSourceImpl();
21+
});
22+
23+
group('getLastPost', () {
24+
final tPostModel = PostModel.fromJson(json.decode(fixture('post_cached.json')));
25+
test('should return Post from local db when there is one in the cache', () async {
26+
//arrange
27+
when(await DatabaseHelper.getPosts()).thenReturn([tPostModel]);
28+
//act
29+
final result = await dataSource.readPosts();
30+
//assert
31+
verify(DatabaseHelper.getPosts());
32+
expect(result, equals(tPostModel));
33+
});
34+
35+
test('should throw a CacheException when there is not a cached value', () async {
36+
//arrange
37+
when(await DatabaseHelper.getPosts()).thenReturn([tPostModel]);
38+
//act
39+
final call = dataSource.readPosts;
40+
//assert
41+
expect(() => call(), throwsA(const TypeMatcher<CacheException>()));
42+
});
43+
});
44+
45+
group('cachePostModel', () {
46+
final tPostModel = PostModel(
47+
id: 37,
48+
content: '',
49+
isSelected: 0,
50+
title: '',
51+
);
52+
test('should call db to cache the data', () async {
53+
//act
54+
dataSource.createPost(tPostModel);
55+
//assert
56+
verify(DatabaseHelper.insertPost(tPostModel));
57+
});
58+
});
59+
}

0 commit comments

Comments
 (0)