Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Commit 31a30e4

Browse files
committed
test data_repository
1 parent 87c1712 commit 31a30e4

File tree

4 files changed

+177
-10
lines changed

4 files changed

+177
-10
lines changed

lib/src/services/data_repository.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,11 @@ class DataRepository {
114114
}
115115

116116
Future<RecipeStub?> _fetchCategoryMainRecipe(Category category) async {
117-
try {
118-
final categoryRecipes = await fetchRecipesShort(category: category.name);
119-
if (categoryRecipes != null && categoryRecipes.isNotEmpty) {
120-
return categoryRecipes.first;
121-
}
122-
} catch (e) {
123-
log('Could not load main recipe of Category!');
124-
rethrow;
125-
}
117+
final categoryRecipes = await fetchRecipesShort(category: category.name);
126118

119+
if (categoryRecipes != null && categoryRecipes.isNotEmpty) {
120+
return categoryRecipes.first;
121+
}
127122
return null;
128123
}
129124

lib/src/services/services.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dart:async';
22
import 'dart:convert';
3-
import 'dart:developer';
43

54
import 'package:dio/dio.dart' as dio;
65
import 'package:dio/dio.dart';

test/data_repository_test.dart

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import 'package:built_collection/built_collection.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:mocktail/mocktail.dart';
4+
import 'package:nc_cookbook_api/nc_cookbook_api.dart';
5+
import 'package:nextcloud_cookbook_flutter/src/services/services.dart';
6+
7+
import 'helpers/translation_helpers.dart';
8+
import 'mocks/mocks.dart';
9+
10+
void main() {
11+
final api = ApiProvider.mocked(ApiMock());
12+
13+
setUpAll(setupL10n);
14+
15+
group(DataRepository, () {
16+
test('fetchRecipesShort', () async {
17+
when(() => api.recipeApi.listRecipes()).thenAnswer(
18+
(_) async => ResponseMock<BuiltList<FakeRecipeStub>>(data: BuiltList()),
19+
);
20+
when(
21+
() =>
22+
api.categoryApi.recipesInCategory(category: any(named: 'category')),
23+
).thenAnswer(
24+
(_) async => ResponseMock<BuiltList<FakeRecipeStub>>(data: BuiltList()),
25+
);
26+
27+
final allRecipes = await DataRepository()
28+
.fetchRecipesShort(category: DataRepository.categoryAll);
29+
30+
expect(allRecipes, isEmpty);
31+
verify(() => api.recipeApi.listRecipes()).called(1);
32+
33+
final uncategorized = await DataRepository()
34+
.fetchRecipesShort(category: DataRepository.categoryUncategorized);
35+
36+
expect(uncategorized, isEmpty);
37+
verify(
38+
() => api.categoryApi.recipesInCategory(category: '_'),
39+
).called(1);
40+
41+
final recipes =
42+
await DataRepository().fetchRecipesShort(category: 'category');
43+
44+
expect(recipes, isEmpty);
45+
verify(
46+
() => api.categoryApi.recipesInCategory(category: 'category'),
47+
).called(1);
48+
});
49+
50+
test('fetchRecipe', () async {
51+
when(() => api.recipeApi.recipeDetails(id: any(named: 'id'))).thenAnswer(
52+
(_) async => ResponseMock<FakeRecipe>(data: FakeRecipe()),
53+
);
54+
55+
final recipe = await DataRepository().fetchRecipe('id');
56+
57+
expect(recipe, isA<Recipe>());
58+
verify(() => api.recipeApi.recipeDetails(id: 'id')).called(1);
59+
});
60+
61+
test('updateRecipe', () async {
62+
final recipe = FakeRecipe();
63+
when(
64+
() => api.recipeApi.updateRecipe(id: recipe.id, recipe: recipe),
65+
).thenAnswer(
66+
(invocation) async => ResponseMock<String>(data: recipe.id),
67+
);
68+
69+
final id = await DataRepository().updateRecipe(recipe);
70+
71+
expect(id, recipe.id);
72+
verify(() => api.recipeApi.updateRecipe(id: recipe.id, recipe: recipe))
73+
.called(1);
74+
});
75+
76+
test('createRecipe', () async {
77+
final recipe = FakeRecipe();
78+
when(
79+
() => api.recipeApi.newRecipe(recipe: recipe),
80+
).thenAnswer(
81+
(invocation) async => ResponseMock<String>(data: recipe.id),
82+
);
83+
84+
final id = await DataRepository().createRecipe(recipe);
85+
86+
expect(id, recipe.id);
87+
verify(() => api.recipeApi.newRecipe(recipe: recipe)).called(1);
88+
});
89+
90+
test('deleteRecipe', () async {
91+
final recipe = FakeRecipe();
92+
when(
93+
() => api.recipeApi.deleteRecipe(id: recipe.id),
94+
).thenAnswer(
95+
(invocation) async => ResponseMock<String>(data: recipe.id),
96+
);
97+
98+
final id = await DataRepository().deleteRecipe(recipe);
99+
100+
expect(id, recipe.id);
101+
verify(() => api.recipeApi.deleteRecipe(id: recipe.id)).called(1);
102+
});
103+
104+
test('importRecipe', () async {
105+
final url = UrlBuilder()..url = 'testUrl';
106+
when(
107+
() => api.recipeApi.callImport(url: url.build()),
108+
).thenAnswer(
109+
(invocation) async => ResponseMock<FakeRecipe>(),
110+
);
111+
112+
await DataRepository().importRecipe(url.url!);
113+
114+
verify(() => api.recipeApi.callImport(url: url.build())).called(1);
115+
});
116+
117+
test('fetchCategories', () async {});
118+
119+
test('fetchCategoryMainRecipes', () async {});
120+
121+
test('fetchAllRecipes', () async {});
122+
123+
test('getUserAvatarUrl', () async {});
124+
125+
test('getMatchingCategoryNames', () async {});
126+
127+
test('fetchImage', () async {});
128+
});
129+
}

test/mocks/mocks.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:dio/dio.dart';
2+
import 'package:mocktail/mocktail.dart';
3+
import 'package:nc_cookbook_api/nc_cookbook_api.dart';
4+
import 'package:nextcloud_cookbook_flutter/src/services/services.dart';
5+
6+
class ApiMock extends Mock implements ApiProvider {
7+
final _recipesApi = RecipesApiMock();
8+
final _categoriesApi = CategoriesApiMock();
9+
final _miscApi = MiscApiMock();
10+
final _tagsApi = TagsApiMock();
11+
12+
@override
13+
RecipesApi get recipeApi => _recipesApi;
14+
@override
15+
CategoriesApi get categoryApi => _categoriesApi;
16+
@override
17+
MiscApi get miscApi => _miscApi;
18+
@override
19+
TagsApi get tagsApi => _tagsApi;
20+
}
21+
22+
class RecipesApiMock extends Mock implements RecipesApi {}
23+
24+
class CategoriesApiMock extends Mock implements CategoriesApi {}
25+
26+
class MiscApiMock extends Mock implements MiscApi {}
27+
28+
class TagsApiMock extends Mock implements TagsApi {}
29+
30+
class ResponseMock<T> extends Mock implements Response<T> {
31+
ResponseMock({this.data});
32+
33+
@override
34+
T? data;
35+
}
36+
37+
class FakeRecipeStub extends Fake implements RecipeStub {}
38+
39+
class FakeRecipe extends Fake implements Recipe {
40+
@override
41+
String get id => 'some ID';
42+
}
43+
44+
class FakeUrl extends Fake implements Url {}

0 commit comments

Comments
 (0)