From c2b84c47b7d76ef23e1759747ef2405527850930 Mon Sep 17 00:00:00 2001 From: YiHungWONG <40423264+yihong1120@users.noreply.github.com> Date: Tue, 23 Jan 2024 09:41:07 +0000 Subject: [PATCH 1/3] Fix test unit: updateReport should return true when the http call completes successfully --- test/services/report_service_test.dart | 74 +++++++++++++------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/test/services/report_service_test.dart b/test/services/report_service_test.dart index da631cb..63c2f1b 100644 --- a/test/services/report_service_test.dart +++ b/test/services/report_service_test.dart @@ -34,9 +34,6 @@ void main() { license_plate: 'ABC123', violation: '紅線停車', status: 'Pending', - address: 'Main St and 1st Ave', - officer: 'Officer123', - mediaFiles: [], // Assuming no media files for simplicity address: 'Main St and 1st Ave', officer: 'Officer123', mediaFiles: [], // Assuming no media files for simplicity @@ -50,47 +47,52 @@ void main() { test( 'getReports should return a list of TrafficViolation when the http call completes successfully', () async { - when(client.get(isA() as Uri)) - .thenAnswer((_) async => http.Response( - jsonEncode([ - { - 'id': 1, - 'title': 'Parking Violation', - 'date': '2024-01-15', - 'time': '14:00', - 'licensePlate': 'ABC123', - 'violation': '紅線停車', - 'status': 'Pending', - 'location': 'Main St and 1st Ave', - 'officer': 'Officer123', - 'mediaFiles': [], - }, - // Add more violation reports if needed - ]), - 200)); + when(client.get(any)).thenAnswer((_) async => http.Response( + jsonEncode([ + { + 'id': 1, + 'title': 'Parking Violation', + 'date': '2024-01-15', + 'time': '14:00', + 'license_plate': 'ABC123', + 'violation': '紅線停車', + 'status': 'Pending', + 'address': 'Main St and 1st Ave', + 'officer': 'Officer123', + 'mediaFiles': [], + }, + // Add more violation reports if needed + ]), + 200)); - expect(await service.getReports(), equals([]); + final reports = await service.getReports(); + expect(reports, isNotEmpty); + expect(reports.first, isA()); }, ); test( - 'getViolation should throw an exception when the http call is unsuccessful', + 'getViolation should return a TrafficViolation when the http call completes successfully', () async { const int recordId = 1; - final response404 = http.Response('', 404); - when(client.get(isA())).thenAnswer((_) async => response404); - 'date': '2024-01-15', - 'time': '14:00', - 'licensePlate': 'ABC123', - 'violation': '紅線停車', - 'status': 'Pending', - 'location': 'Main St and 1st Ave', - 'officer': 'Officer123', - 'mediaFiles': [], - }), - 200)); + when(client.get(any)).thenAnswer((_) async => http.Response( + jsonEncode({ + 'id': recordId, + 'title': 'Parking Violation', + 'date': '2024-01-15', + 'time': '14:00', + 'license_plate': 'ABC123', + 'violation': '紅線停車', + 'status': 'Pending', + 'address': 'Main St and 1st Ave', + 'officer': 'Officer123', + 'mediaFiles': [], + }), + 200)); - expect(await service.getViolation(recordId), isA()); + final violation = await service.getViolation(recordId); + expect(violation, isA()); + expect(violation.id, recordId); }, ); From dce9c57bb729dd5e0ed491eb48a42918af05e9d1 Mon Sep 17 00:00:00 2001 From: YiHungWONG <40423264+yihong1120@users.noreply.github.com> Date: Tue, 23 Jan 2024 10:24:14 +0000 Subject: [PATCH 2/3] Fix: unuse 'any' --- test/services/report_service_test.dart | 72 +++++++++++++------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/test/services/report_service_test.dart b/test/services/report_service_test.dart index 63c2f1b..6ec6d49 100644 --- a/test/services/report_service_test.dart +++ b/test/services/report_service_test.dart @@ -47,27 +47,26 @@ void main() { test( 'getReports should return a list of TrafficViolation when the http call completes successfully', () async { - when(client.get(any)).thenAnswer((_) async => http.Response( - jsonEncode([ - { - 'id': 1, - 'title': 'Parking Violation', - 'date': '2024-01-15', - 'time': '14:00', - 'license_plate': 'ABC123', - 'violation': '紅線停車', - 'status': 'Pending', - 'address': 'Main St and 1st Ave', - 'officer': 'Officer123', - 'mediaFiles': [], - }, - // Add more violation reports if needed - ]), - 200)); + when(client.get(isA() as Uri)) + .thenAnswer((_) async => http.Response( + jsonEncode([ + { + 'id': 1, + 'title': 'Parking Violation', + 'date': '2024-01-15', + 'time': '14:00', + 'licensePlate': 'ABC123', + 'violation': '紅線停車', + 'status': 'Pending', + 'location': 'Main St and 1st Ave', + 'officer': 'Officer123', + 'mediaFiles': [], + }, + // Add more violation reports if needed + ]), + 200)); - final reports = await service.getReports(); - expect(reports, isNotEmpty); - expect(reports.first, isA()); + expect(await service.getReports(), isA>()); }, ); @@ -75,24 +74,23 @@ void main() { 'getViolation should return a TrafficViolation when the http call completes successfully', () async { const int recordId = 1; - when(client.get(any)).thenAnswer((_) async => http.Response( - jsonEncode({ - 'id': recordId, - 'title': 'Parking Violation', - 'date': '2024-01-15', - 'time': '14:00', - 'license_plate': 'ABC123', - 'violation': '紅線停車', - 'status': 'Pending', - 'address': 'Main St and 1st Ave', - 'officer': 'Officer123', - 'mediaFiles': [], - }), - 200)); + when(client.get(isA() as Uri)) + .thenAnswer((_) async => http.Response( + jsonEncode({ + 'id': recordId, + 'title': 'Parking Violation', + 'date': '2024-01-15', + 'time': '14:00', + 'licensePlate': 'ABC123', + 'violation': '紅線停車', + 'status': 'Pending', + 'location': 'Main St and 1st Ave', + 'officer': 'Officer123', + 'mediaFiles': [], + }), + 200)); - final violation = await service.getViolation(recordId); - expect(violation, isA()); - expect(violation.id, recordId); + expect(await service.getViolation(recordId), isA()); }, ); From 121ec1c81a9e02069fa7755f21096eda5a2234f8 Mon Sep 17 00:00:00 2001 From: yihong1120 Date: Wed, 24 Jan 2024 00:06:34 +0800 Subject: [PATCH 3/3] Change the strcuture of code to fetch input json file --- lib/screens/accounts/account_page.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/screens/accounts/account_page.dart b/lib/screens/accounts/account_page.dart index f03111b..abcfa47 100644 --- a/lib/screens/accounts/account_page.dart +++ b/lib/screens/accounts/account_page.dart @@ -43,8 +43,8 @@ class AccountPageState extends State { if (userInfo != null) { setState(() { - _username = userInfo['username'] ?? 'N/A'; // 根据你的API调整字段名 - _email = userInfo['email'] ?? 'N/A'; // 根据你的API调整字段名 + _username = userInfo['user']['username'] ?? 'N/A'; // 根据你的API调整字段名 + _email = userInfo['user']['email'] ?? 'N/A'; // 根据你的API调整字段名 }); } else { // 处理userInfo为空的情况,例如通过显示错误消息