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

Commit c282e0e

Browse files
committed
test data_repository
1 parent b54af5b commit c282e0e

File tree

4 files changed

+187
-10
lines changed

4 files changed

+187
-10
lines changed

lib/src/services/data_repository.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,11 @@ class DataRepository {
112112
}
113113

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

117+
if (categoryRecipes != null && categoryRecipes.isNotEmpty) {
118+
return categoryRecipes.first;
119+
}
125120
return null;
126121
}
127122

lib/src/services/services.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:convert';
2-
import 'dart:developer';
32

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

test/data_repository_test.dart

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

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)