forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
132
test/features/barcode/models/barcode_product_model_test.dart
Normal file
132
test/features/barcode/models/barcode_product_model_test.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'package:eatvio_app/features/barcode/models/barcode_product_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('BarcodeProductModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {
|
||||
'GTIN': '1234567890123',
|
||||
'Товарный знак (ru)': 'Test Brand',
|
||||
'Описание (ru)': 'Test Description',
|
||||
'Количество/вес': '100g',
|
||||
'Рынок сбыта': 'Russia',
|
||||
'Калорийность': 250.5,
|
||||
'Белки': 10.0,
|
||||
'Жиры': 5.0,
|
||||
'Углеводы': 30.0,
|
||||
};
|
||||
|
||||
final model = BarcodeProductModel.fromJson(json);
|
||||
|
||||
expect(model.GTIN, '1234567890123');
|
||||
expect(model.brandName, 'Test Brand');
|
||||
expect(model.description, 'Test Description');
|
||||
expect(model.quantity, '100g');
|
||||
expect(model.market, 'Russia');
|
||||
expect(model.calories, 250.5);
|
||||
expect(model.proteins, 10.0);
|
||||
expect(model.fats, 5.0);
|
||||
expect(model.carbohydrates, 30.0);
|
||||
});
|
||||
|
||||
test('fromJson handles missing optional fields', () {
|
||||
final json = {
|
||||
'GTIN': '1234567890123',
|
||||
};
|
||||
|
||||
final model = BarcodeProductModel.fromJson(json);
|
||||
|
||||
expect(model.GTIN, '1234567890123');
|
||||
expect(model.brandName, isNull);
|
||||
expect(model.description, isNull);
|
||||
expect(model.quantity, isNull);
|
||||
expect(model.market, isNull);
|
||||
expect(model.calories, isNull);
|
||||
expect(model.proteins, isNull);
|
||||
expect(model.fats, isNull);
|
||||
expect(model.carbohydrates, isNull);
|
||||
});
|
||||
|
||||
test('toJson serializes all fields correctly', () {
|
||||
const model = BarcodeProductModel(
|
||||
GTIN: '1234567890123',
|
||||
brandName: 'Test Brand',
|
||||
description: 'Test Description',
|
||||
quantity: '100g',
|
||||
market: 'Russia',
|
||||
calories: 250.5,
|
||||
proteins: 10.0,
|
||||
fats: 5.0,
|
||||
carbohydrates: 30.0,
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json['GTIN'], '1234567890123');
|
||||
expect(json['Товарный знак (ru)'], 'Test Brand');
|
||||
expect(json['Описание (ru)'], 'Test Description');
|
||||
expect(json['Количество/вес'], '100g');
|
||||
expect(json['Рынок сбыта'], 'Russia');
|
||||
expect(json['Калорийность'], 250.5);
|
||||
expect(json['Белки'], 10.0);
|
||||
expect(json['Жиры'], 5.0);
|
||||
expect(json['Углеводы'], 30.0);
|
||||
});
|
||||
});
|
||||
|
||||
group('BarcodeTaskStatusModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {
|
||||
'status': 'completed',
|
||||
'task_id': 'task-123',
|
||||
'data': {
|
||||
'GTIN': '1234567890123',
|
||||
'Товарный знак (ru)': 'Test Brand',
|
||||
},
|
||||
};
|
||||
|
||||
final model = BarcodeTaskStatusModel.fromJson(json);
|
||||
|
||||
expect(model.status, 'completed');
|
||||
expect(model.taskId, 'task-123');
|
||||
expect(model.data, isNotNull);
|
||||
expect(model.data?.GTIN, '1234567890123');
|
||||
expect(model.data?.brandName, 'Test Brand');
|
||||
});
|
||||
|
||||
test('fromJson handles missing optional fields', () {
|
||||
final json = {
|
||||
'status': 'pending',
|
||||
};
|
||||
|
||||
final model = BarcodeTaskStatusModel.fromJson(json);
|
||||
|
||||
expect(model.status, 'pending');
|
||||
expect(model.taskId, isNull);
|
||||
expect(model.data, isNull);
|
||||
});
|
||||
|
||||
test('toJson serializes all fields correctly', () {
|
||||
const productModel = BarcodeProductModel(GTIN: '1234567890123', brandName: 'Test Brand');
|
||||
const model = BarcodeTaskStatusModel(
|
||||
status: 'completed',
|
||||
taskId: 'task-123',
|
||||
data: productModel,
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json['status'], 'completed');
|
||||
expect(json['task_id'], 'task-123');
|
||||
expect(json['data'], isNotNull);
|
||||
if (json['data'] is Map) {
|
||||
final dataJson = json['data'] as Map<String, dynamic>;
|
||||
expect(dataJson['GTIN'], '1234567890123');
|
||||
} else {
|
||||
// If data is already a model, check it directly
|
||||
expect(model.data?.GTIN, '1234567890123');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user