forked from eatvio/eatvio_app
feat(diary): tests
This commit is contained in:
@@ -1,228 +1,176 @@
|
||||
import 'package:eatvio_app/features/recipe/models/my_recipe_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('MyRecipeModel', () {
|
||||
const model = MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'https://example.com/img.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: '# Recipe\nDelicious',
|
||||
steps: [Step(image: 'step.jpg', description: 'Step 1')],
|
||||
videoUrl: 'https://youtube.com/watch?v=123',
|
||||
title: 'Test Recipe',
|
||||
slug: 'test-recipe',
|
||||
calories: 250.5,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
);
|
||||
|
||||
test('fields are accessible', () {
|
||||
expect(model.id, 1);
|
||||
expect(model.image, 'https://example.com/img.jpg');
|
||||
expect(model.state, 'active');
|
||||
expect(model.cookingTime, 30);
|
||||
expect(model.activeCookingTime, 15);
|
||||
expect(model.description, '# Recipe\nDelicious');
|
||||
expect(model.videoUrl, 'https://youtube.com/watch?v=123');
|
||||
expect(model.title, 'Test Recipe');
|
||||
expect(model.slug, 'test-recipe');
|
||||
expect(model.calories, 250.5);
|
||||
expect(model.likesCount, 10);
|
||||
expect(model.viewsCount, 100);
|
||||
expect(model.steps?.length, 1);
|
||||
});
|
||||
|
||||
test('fromJson parses correctly', () {
|
||||
final json = {
|
||||
'id': 2,
|
||||
'image': 'img.jpg',
|
||||
'state': 'draft',
|
||||
'cooking_time': 60,
|
||||
'active_time': 20,
|
||||
'description_md': 'Desc',
|
||||
'steps': [{'image': 's.jpg', 'description_md': 'S1'}],
|
||||
'video_url': null,
|
||||
'title': 'Title',
|
||||
'slug': 'slug',
|
||||
'energy_per_serving': 300.0,
|
||||
'total_likes': 5,
|
||||
'total_views': 50,
|
||||
};
|
||||
final parsed = MyRecipeModel.fromJson(json);
|
||||
expect(parsed.id, 2);
|
||||
expect(parsed.cookingTime, 60);
|
||||
expect(parsed.calories, 300.0);
|
||||
});
|
||||
|
||||
test('toRecipeModel converts correctly', () {
|
||||
final recipeModel = model.toRecipeModel();
|
||||
expect(recipeModel.title, 'Test Recipe');
|
||||
expect(recipeModel.description, '# Recipe\nDelicious');
|
||||
expect(recipeModel.totalCookingTime, 30);
|
||||
expect(recipeModel.activeCookTime, 15);
|
||||
expect(recipeModel.energyPerServing, 250);
|
||||
expect(recipeModel.state, 'active');
|
||||
});
|
||||
|
||||
test('toRecipe converts correctly', () {
|
||||
final recipe = model.toRecipe();
|
||||
expect(recipe.id, 1);
|
||||
expect(recipe.image, 'https://example.com/img.jpg');
|
||||
expect(recipe.state, 'active');
|
||||
expect(recipe.cookingTime, 30);
|
||||
expect(recipe.slug, 'test-recipe');
|
||||
expect(recipe.calories, 250.5);
|
||||
expect(recipe.likesCount, 10);
|
||||
expect(recipe.type, 'recipe');
|
||||
expect(recipe.commentsCount, 0);
|
||||
expect(recipe.wasLikedByMe, false);
|
||||
});
|
||||
|
||||
test('toRecipeModel with null steps', () {
|
||||
const noSteps = MyRecipeModel(
|
||||
id: 1, image: '', state: 'active', cookingTime: 10,
|
||||
activeCookingTime: 5, description: '', steps: null,
|
||||
videoUrl: null, title: null, slug: 'slug', calories: 0,
|
||||
likesCount: 0, viewsCount: 0,
|
||||
);
|
||||
final rm = noSteps.toRecipeModel();
|
||||
expect(rm.steps, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('MyRecipePaginationModel', () {
|
||||
test('creates MyRecipePaginationModel with empty list', () {
|
||||
const model = MyRecipePaginationModel(data: []);
|
||||
|
||||
expect(model.data, isEmpty);
|
||||
});
|
||||
|
||||
test('creates MyRecipePaginationModel with recipes', () {
|
||||
final recipes = [
|
||||
const MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'image1.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test recipe 1',
|
||||
steps: null,
|
||||
videoUrl: null,
|
||||
title: 'Recipe 1',
|
||||
slug: 'recipe-1',
|
||||
calories: 250.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
),
|
||||
const MyRecipeModel(
|
||||
id: 2,
|
||||
image: 'image2.jpg',
|
||||
state: 'draft',
|
||||
cookingTime: 45,
|
||||
activeCookingTime: 20,
|
||||
description: 'Test recipe 2',
|
||||
steps: null,
|
||||
videoUrl: 'video.mp4',
|
||||
title: 'Recipe 2',
|
||||
slug: 'recipe-2',
|
||||
calories: 300.0,
|
||||
likesCount: 5,
|
||||
viewsCount: 50,
|
||||
),
|
||||
];
|
||||
|
||||
final model = MyRecipePaginationModel(data: recipes);
|
||||
|
||||
expect(model.data.length, 2);
|
||||
expect(model.data[0].id, 1);
|
||||
expect(model.data[0].title, 'Recipe 1');
|
||||
expect(model.data[1].id, 2);
|
||||
expect(model.data[1].title, 'Recipe 2');
|
||||
});
|
||||
|
||||
test('creates MyRecipePaginationModel from JSON', () {
|
||||
test('fromJson parses correctly', () {
|
||||
final json = {
|
||||
'data': [
|
||||
{
|
||||
'id': 1,
|
||||
'image': 'image1.jpg',
|
||||
'state': 'published',
|
||||
'image': 'img.jpg',
|
||||
'state': 'active',
|
||||
'cooking_time': 30,
|
||||
'active_time': 15,
|
||||
'description_md': 'Test recipe',
|
||||
'description_md': 'Desc',
|
||||
'steps': null,
|
||||
'video_url': null,
|
||||
'title': 'Recipe 1',
|
||||
'slug': 'recipe-1',
|
||||
'energy_per_serving': 250.0,
|
||||
'total_likes': 10,
|
||||
'total_views': 100,
|
||||
'title': 'Title',
|
||||
'slug': 'slug',
|
||||
'energy_per_serving': 100.0,
|
||||
'total_likes': 0,
|
||||
'total_views': 0,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
final model = MyRecipePaginationModel.fromJson(json);
|
||||
|
||||
expect(model.data.length, 1);
|
||||
expect(model.data[0].id, 1);
|
||||
expect(model.data[0].title, 'Recipe 1');
|
||||
final parsed = MyRecipePaginationModel.fromJson(json);
|
||||
expect(parsed.data.length, 1);
|
||||
expect(parsed.data[0].title, 'Title');
|
||||
});
|
||||
});
|
||||
|
||||
group('MyRecipeModel', () {
|
||||
test('creates MyRecipeModel with all parameters', () {
|
||||
const recipe = MyRecipeModel(
|
||||
group('RecipeToRecipeModelX', () {
|
||||
test('Recipe.toRecipeModel converts correctly', () {
|
||||
const recipe = Recipe(
|
||||
id: 1,
|
||||
image: 'image.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test recipe',
|
||||
steps: null,
|
||||
image: 'img.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 45,
|
||||
activeCookingTime: 20,
|
||||
ingredients: [
|
||||
Ingredient(name: 'Flour', portion: '200g', productId: 1),
|
||||
],
|
||||
description: 'Recipe description',
|
||||
steps: [Step(image: 's.jpg', description: 'Step')],
|
||||
videoUrl: null,
|
||||
title: 'Recipe 1',
|
||||
slug: 'recipe-1',
|
||||
calories: 250.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
title: 'My Recipe',
|
||||
type: 'recipe',
|
||||
slug: 'my-recipe',
|
||||
commentsCount: 3,
|
||||
wasLikedByMe: true,
|
||||
isFavorite: false,
|
||||
calories: 350.0,
|
||||
likesCount: 20,
|
||||
viewsCount: 200,
|
||||
tags: [Tag(name: 'healthy')],
|
||||
author: Author(authorSlug: 'user1', authorName: 'User', authorProfileIcon: '', authorIsMe: true),
|
||||
);
|
||||
|
||||
expect(recipe.id, 1);
|
||||
expect(recipe.image, 'image.jpg');
|
||||
expect(recipe.state, 'published');
|
||||
expect(recipe.cookingTime, 30);
|
||||
expect(recipe.activeCookingTime, 15);
|
||||
expect(recipe.description, 'Test recipe');
|
||||
expect(recipe.steps, null);
|
||||
expect(recipe.videoUrl, null);
|
||||
expect(recipe.title, 'Recipe 1');
|
||||
expect(recipe.slug, 'recipe-1');
|
||||
expect(recipe.calories, 250.0);
|
||||
expect(recipe.likesCount, 10);
|
||||
expect(recipe.viewsCount, 100);
|
||||
final rm = recipe.toRecipeModel();
|
||||
expect(rm.title, 'My Recipe');
|
||||
expect(rm.totalCookingTime, 45);
|
||||
expect(rm.activeCookTime, 20);
|
||||
expect(rm.energyPerServing, 350);
|
||||
expect(rm.state, 'active');
|
||||
expect(rm.ingredients?.length, 1);
|
||||
expect(rm.tags?.length, 1);
|
||||
});
|
||||
|
||||
test('creates MyRecipeModel with steps and videoUrl', () {
|
||||
final steps = [
|
||||
const Step(image: 'step1.jpg', description: 'Step 1'),
|
||||
const Step(image: 'step2.jpg', description: 'Step 2'),
|
||||
];
|
||||
|
||||
final recipe = MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'image.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test recipe',
|
||||
steps: steps,
|
||||
videoUrl: 'video.mp4',
|
||||
title: 'Recipe 1',
|
||||
slug: 'recipe-1',
|
||||
calories: 250.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
test('Recipe.toRecipeModel with null ingredients', () {
|
||||
const recipe = Recipe(
|
||||
id: 1, image: '', state: 'active', cookingTime: 10,
|
||||
activeCookingTime: 5, ingredients: null,
|
||||
description: '', steps: null, videoUrl: null,
|
||||
title: 'T', type: 'recipe', slug: 's',
|
||||
commentsCount: 0, wasLikedByMe: false, isFavorite: false,
|
||||
calories: 0, likesCount: 0, viewsCount: 0,
|
||||
tags: [], author: Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
);
|
||||
|
||||
expect(recipe.steps, isNotNull);
|
||||
expect(recipe.steps!.length, 2);
|
||||
expect(recipe.videoUrl, 'video.mp4');
|
||||
});
|
||||
|
||||
test('creates MyRecipeModel from JSON', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'image': 'image.jpg',
|
||||
'state': 'published',
|
||||
'cooking_time': 30,
|
||||
'active_time': 15,
|
||||
'description_md': 'Test recipe',
|
||||
'steps': null,
|
||||
'video_url': null,
|
||||
'title': 'Recipe 1',
|
||||
'slug': 'recipe-1',
|
||||
'energy_per_serving': 250.0,
|
||||
'total_likes': 10,
|
||||
'total_views': 100,
|
||||
};
|
||||
|
||||
final recipe = MyRecipeModel.fromJson(json);
|
||||
|
||||
expect(recipe.id, 1);
|
||||
expect(recipe.title, 'Recipe 1');
|
||||
expect(recipe.cookingTime, 30);
|
||||
expect(recipe.calories, 250.0);
|
||||
});
|
||||
|
||||
test('toJson serializes MyRecipeModel correctly', () {
|
||||
const recipe = MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'image.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test recipe',
|
||||
steps: null,
|
||||
videoUrl: null,
|
||||
title: 'Recipe 1',
|
||||
slug: 'recipe-1',
|
||||
calories: 250.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
);
|
||||
|
||||
final json = recipe.toJson();
|
||||
|
||||
expect(json['id'], 1);
|
||||
expect(json['title'], 'Recipe 1');
|
||||
expect(json['slug'], 'recipe-1');
|
||||
expect(json['cooking_time'], 30);
|
||||
expect(json['active_time'], 15);
|
||||
expect(json['energy_per_serving'], 250.0);
|
||||
expect(json['total_likes'], 10);
|
||||
expect(json['total_views'], 100);
|
||||
});
|
||||
});
|
||||
|
||||
group('MyRecipePaginationModel', () {
|
||||
test('toJson serializes MyRecipePaginationModel correctly', () {
|
||||
final recipes = [
|
||||
const MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'image1.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test recipe 1',
|
||||
steps: null,
|
||||
videoUrl: null,
|
||||
title: 'Recipe 1',
|
||||
slug: 'recipe-1',
|
||||
calories: 250.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
),
|
||||
];
|
||||
|
||||
final model = MyRecipePaginationModel(data: recipes);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json.containsKey('data'), isTrue);
|
||||
expect((json['data'] as List).length, 1);
|
||||
final rm = recipe.toRecipeModel();
|
||||
expect(rm.ingredients, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user