work on test models part 1

This commit is contained in:
llsajjad
2025-10-06 16:26:53 +03:30
parent 2367899b54
commit 69623d82ba
15 changed files with 1206 additions and 63 deletions
+189
View File
@@ -0,0 +1,189 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:tm_app/data/services/model/feedback_data/feedback_data.dart';
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';
import 'package:tm_app/data/services/model/organization/organization.dart';
void main() {
group('FeedbackData Tests', () {
test('fromJson should parse JSON correctly', () {
final json = {
'company_id': 'COMP123',
'created_at': 1700000000,
'customer_id': 'CUST456',
'feedback_type': 'positive',
'id': 'FB001',
'tender_id': 'TND789',
'updated_at': 1750000000,
'tender': {
'success': true,
'message': 'ok',
'id': 'TND789',
'tender_id': 'TND789',
'notice_publication_id': 'NP123',
'publication_date': 1700000000,
'title': 'New Tender',
'description': 'Tender description',
'procurement_type_code': 'PT001',
'procedure_code': 'PRC001',
'estimated_value': 1000000,
'currency': 'USD',
'duration': '12',
'tender_deadline': 1710000000,
'submission_deadline': 1710000000,
'application_deadline': 1710000000,
'submission_url': 'https://example.com',
'country_code': 'IR',
'duration_unit': 'month',
'buyer_organization': {
'id': 'ORG001',
'name': 'Buyer Org',
},
'status': 'active',
},
};
final feedback = FeedbackData.fromJson(json);
expect(feedback.companyId, 'COMP123');
expect(feedback.feedbackType, 'positive');
expect(feedback.tender?.id, 'TND789');
expect(feedback.tender?.title, 'New Tender');
expect(feedback.tender?.buyerOrganization?.name, 'Buyer Org');
});
test('toJson should return valid JSON map', () {
final tender = const TenderData(
success: true,
message: 'ok',
id: 'TND123',
tenderId: 'TND123',
noticePublicationId: 'NP123',
publicationDate: 1700000000,
title: 'Tender Title',
description: 'Some description',
procurementTypeCode: 'PROC001',
procedureCode: 'PROCEDURE1',
estimatedValue: 500000,
currency: 'USD',
duration: '6',
tenderDeadline: 1710000000,
submissionDeadline: 1715000000,
applicationDeadline: 1712000000,
submissionUrl: 'https://example.com',
countryCode: 'IR',
durationUnit: 'month',
buyerOrganization: Organization(
name: 'Test Organization',
),
status: 'active',
);
final feedback = FeedbackData(
companyId: 'COMP001',
createdAt: 1700000000,
customerId: 'CUST001',
feedbackType: 'negative',
id: 'FB001',
tenderId: 'TND123',
tender: tender,
updatedAt: 1750000000,
);
final json = feedback.toJson();
expect(json['company_id'], 'COMP001');
expect(json['feedback_type'], 'negative');
final tenderJson = (json['tender'] as TenderData).toJson();
expect(tenderJson['id'], 'TND123');
expect(tenderJson['title'], 'Tender Title');
expect((tenderJson['buyer_organization'] as Organization).toJson()['name'], 'Test Organization');
});
test('copyWith should modify specific fields only', () {
final feedback = const FeedbackData(
companyId: 'COMP001',
createdAt: 1700000000,
customerId: 'CUST001',
feedbackType: 'neutral',
id: 'FB001',
tenderId: 'TND001',
tender: TenderData(
success: false,
message: 'fail',
id: 'TND001',
tenderId: 'TND001',
noticePublicationId: 'NP1',
publicationDate: 1700000000,
title: 'Old Tender',
description: 'Old desc',
procurementTypeCode: 'PT001',
procedureCode: 'PC001',
estimatedValue: 10000,
currency: 'USD',
duration: '3',
tenderDeadline: 1710000000,
submissionDeadline: 1715000000,
applicationDeadline: 1712000000,
submissionUrl: 'https://example.com',
countryCode: 'IR',
durationUnit: 'month',
buyerOrganization: Organization(
name: 'Old Org',
),
status: 'inactive',
),
updatedAt: 1750000000,
);
final updated = feedback.copyWith(feedbackType: 'positive');
expect(updated.feedbackType, 'positive');
expect(updated.companyId, 'COMP001');
expect(updated.tender?.title, 'Old Tender');
});
test('fromJson handles null fields safely', () {
final json = {
'company_id': null,
'created_at': null,
'customer_id': null,
'feedback_type': null,
'id': null,
'tender_id': null,
'tender': null,
'updated_at': null,
};
final feedback = FeedbackData.fromJson(json);
expect(feedback.companyId, isNull);
expect(feedback.tender, isNull);
expect(feedback.feedbackType, isNull);
});
test('fromJson -> toJson round-trip should preserve data', () {
final json = {
'company_id': 'C1',
'feedback_type': 'positive',
'tender': {
'id': 'T1',
'title': 'Tender 1',
'success': true,
'buyer_organization': {
'id': 'O1',
'name': 'Org 1',
},
},
};
final feedback = FeedbackData.fromJson(json);
final output = feedback.toJson();
expect(output['company_id'], 'C1');
final tenderJson = (output['tender'] as TenderData).toJson();
expect(tenderJson['title'], 'Tender 1');
expect((tenderJson['buyer_organization'] as Organization).toJson() ['name'], 'Org 1');
});
});
}