test(tests): add comprehensive unit tests for various models and repositories, enhancing test coverage and ensuring correct functionality

This commit is contained in:
mso7nueh
2025-12-09 16:19:48 +03:00
parent edd064d20d
commit 70e4a42e98
25 changed files with 2565 additions and 58 deletions

View File

@@ -1,44 +1,76 @@
import 'package:eatvio_app/features/create_recipe/models/ingredient.dart';
import 'package:eatvio_app/features/create_recipe/models/recipe_model.dart';
import 'package:eatvio_app/features/create_recipe/models/step.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('RecipeModel', () {
test('создает RecipeModel с правильными значениями', () {
const ingredient = Ingredient(
name: 'Apple',
unit: 'gr',
portion: '100',
productId: 1,
);
const step = Step(
image: 'https://example.com/image.jpg',
description: 'Step description',
);
const model = RecipeModel(
ingredients: [ingredient],
steps: [step],
tags: ['breakfast'],
type: 'main',
state: 'draft',
ingredients: [],
steps: [],
tags: ['tag1', 'tag2'],
type: 'breakfast',
state: 'active',
title: 'Test Recipe',
description: 'Test description',
description: 'Test Description',
videoUrl: 'https://example.com/video.mp4',
image: 'https://example.com/image.jpg',
totalCookingTime: 30,
activeCookTime: 15,
activeCookTime: 20,
servings: 4,
energyPerServing: 300,
energyPerServing: 250,
);
expect(model.title, 'Test Recipe');
expect(model.ingredients?.length, 1);
expect(model.steps?.length, 1);
expect(model.tags?.length, 1);
expect(model.description, 'Test Description');
expect(model.totalCookingTime, 30);
expect(model.servings, 4);
});
test('создает RecipeModel с null значениями', () {
const model = RecipeModel();
expect(model.ingredients, isNull);
expect(model.steps, isNull);
expect(model.tags, isNull);
expect(model.type, isNull);
expect(model.state, isNull);
expect(model.title, isNull);
});
test('fromJson создает RecipeModel из JSON', () {
final json = {
'ingredients': [],
'steps': [],
'tags': ['tag1', 'tag2'],
'type': 'breakfast',
'state': 'active',
'title': 'Test Recipe',
'description': 'Test Description',
'video_url': 'https://example.com/video.mp4',
'image': 'https://example.com/image.jpg',
'total_cooking_time': 30,
'active_cook_time': 20,
'servings': 4,
'energy_per_serving': 250,
};
final model = RecipeModel.fromJson(json);
expect(model.title, 'Test Recipe');
expect(model.description, 'Test Description');
expect(model.videoUrl, 'https://example.com/video.mp4');
expect(model.tags, ['tag1', 'tag2']);
});
test('fromJson создает RecipeModel из JSON с null значениями', () {
final json = <String, dynamic>{};
final model = RecipeModel.fromJson(json);
expect(model.ingredients, isNull);
expect(model.steps, isNull);
expect(model.tags, isNull);
expect(model.title, isNull);
});
});
}

View File

@@ -51,6 +51,58 @@ void main() {
expect(model.data.length, 1);
expect(model.data.first.name, 'Breakfast');
});
test('fromJson создает RecipeTagModel из JSON', () {
final json = {
'data': [
{
'id': 1,
'name': 'Breakfast',
'recipes_count': 10,
'created_at': '2024-01-01T00:00:00Z',
'updated_at': '2024-01-02T00:00:00Z',
}
],
'links': {
'first': 'https://example.com?page=1',
'last': 'https://example.com?page=1',
'prev': null,
'next': null,
},
'meta': {
'current_page': 1,
'from': 1,
'last_page': 1,
'links': [],
'per_page': 10,
'to': 1,
'total': 1,
},
};
final model = RecipeTagModel.fromJson(json);
expect(model.data.length, 1);
expect(model.data.first.name, 'Breakfast');
});
});
group('RecipeTag', () {
test('fromJson создает RecipeTag из JSON', () {
final json = {
'id': 1,
'name': 'Breakfast',
'recipes_count': 10,
'created_at': '2024-01-01T00:00:00Z',
'updated_at': '2024-01-02T00:00:00Z',
};
final tag = RecipeTag.fromJson(json);
expect(tag.id, 1);
expect(tag.name, 'Breakfast');
expect(tag.recipesCount, 10);
});
});
}

View File

@@ -6,22 +6,39 @@ void main() {
test('создает Step с правильными значениями', () {
const step = Step(
image: 'https://example.com/image.jpg',
description: 'Step description',
description: 'Test step description',
);
expect(step.image, 'https://example.com/image.jpg');
expect(step.description, 'Step description');
expect(step.description, 'Test step description');
});
test('создает Step с null значениями', () {
const step = Step(
image: null,
description: null,
);
const step = Step();
expect(step.image, isNull);
expect(step.description, isNull);
});
test('fromJson создает Step из JSON', () {
final json = {
'image': 'https://example.com/image.jpg',
'description': 'Test step description',
};
final step = Step.fromJson(json);
expect(step.image, 'https://example.com/image.jpg');
expect(step.description, 'Test step description');
});
test('fromJson создает Step из JSON с null значениями', () {
final json = <String, dynamic>{};
final step = Step.fromJson(json);
expect(step.image, isNull);
expect(step.description, isNull);
});
});
}