From 9bc58314d2bdc320f219f286d9e9eba9d6b790aa Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Thu, 30 Nov 2023 17:47:34 +0300 Subject: [PATCH 1/8] add tests for ApiRequests --- .../api_request/api_request_socket_test.dart | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 test/unit/api_client/request/api_request/api_request_socket_test.dart diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart new file mode 100644 index 0000000..df07d07 --- /dev/null +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -0,0 +1,119 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:ext_rw/ext_rw.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:hmi_core/hmi_core_result_new.dart'; + +class FakeApiQueryType implements ApiQueryType { + final bool _valid; + final String _query; + + FakeApiQueryType({ + required bool valid, + required String query, + }) : _valid = valid, + _query = query; + + @override + bool valid() => _valid; + + @override + String buildJson() => _query; + + // insert directly into _query in fake implementation + @override + String get authToken => ''; + + // insert directly into _query in fake implementation + @override + String get id => ''; +} + +void main() { + group('ApiRequest socket', () { + late ApiAddress address; + late ServerSocket serverSocket; + setUp(() async { + // bind socket server to unused port and start listening + serverSocket = await ServerSocket.bind( + InternetAddress.loopbackIPv4.host, + 0, + ); + // respond with received data + serverSocket.listen((socket) { + socket.listen((data) { + socket.add(data); + }); + }); + address = ApiAddress( + host: serverSocket.address.host, + port: serverSocket.port, + ); + }); + test('.fetch() with valid query', () async { + final queryList = [ + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": []}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"stringDataKey":"dataValue"}]}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"booleanDataKey":true}]}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"listDataKey":[1,"2",{"lik":"liv"},[]]}]}', + ]; + + for (final query in queryList) { + final apiRequest = ApiRequest( + address: address, + query: FakeApiQueryType( + valid: true, + query: query, + ), + ); + + final result = await apiRequest.fetch(); + + expect( + result, + isA(), + reason: 'valid api request should return Ok as Result', + ); + + if (result case Ok(value: final reply)) { + final replyAsJson = + '{"authToken": "${reply.authToken}", "id": "${reply.id}", "query": "${reply.sql}", "data": ${json.encode(reply.data)}}'; + expect( + replyAsJson, + query, + reason: 'data should be transferred correctly', + ); + } + } + }); + + test('.fetch() with invalid query', () async { + final queryList = [ + '', + ]; + + for (final query in queryList) { + final apiRequest = ApiRequest( + address: address, + query: FakeApiQueryType( + valid: false, + query: query, + ), + ); + + final result = await apiRequest.fetch(); + + expect( + result, + isA(), + reason: 'Api request with invalid query should return Err as Result', + ); + } + }); + + tearDown(() async { + serverSocket.close(); + }); + }); +} From 094855ecafe908988e20823945f8425a176541b9 Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Fri, 1 Dec 2023 12:17:31 +0300 Subject: [PATCH 2/8] add FakeApiAddress for tests --- .../api_request/api_request_socket_test.dart | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart index df07d07..1a2e337 100644 --- a/test/unit/api_client/request/api_request/api_request_socket_test.dart +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -5,6 +5,21 @@ import 'package:ext_rw/ext_rw.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hmi_core/hmi_core_result_new.dart'; +class FakeApiAddress implements ApiAddress { + final String _host; + final int _port; + + FakeApiAddress({required String host, required int port}) + : _host = host, + _port = port; + + @override + String get host => _host; + + @override + int get port => _port; +} + class FakeApiQueryType implements ApiQueryType { final bool _valid; final String _query; @@ -32,7 +47,7 @@ class FakeApiQueryType implements ApiQueryType { void main() { group('ApiRequest socket', () { - late ApiAddress address; + late FakeApiAddress address; late ServerSocket serverSocket; setUp(() async { // bind socket server to unused port and start listening @@ -46,7 +61,7 @@ void main() { socket.add(data); }); }); - address = ApiAddress( + address = FakeApiAddress( host: serverSocket.address.host, port: serverSocket.port, ); From 8fc125ade42e178c47d21f95ac7dd91779913628 Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Fri, 1 Dec 2023 12:35:16 +0300 Subject: [PATCH 3/8] fix flutter analyze warnings --- lib/src/table_schema/schema.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/src/table_schema/schema.dart b/lib/src/table_schema/schema.dart index 4f1a1b4..ef86dd3 100644 --- a/lib/src/table_schema/schema.dart +++ b/lib/src/table_schema/schema.dart @@ -1,7 +1,4 @@ import 'package:ext_rw/ext_rw.dart'; -import 'package:ext_rw/src/sql/sql.dart'; -import 'package:ext_rw/src/table_schema/field.dart'; -import 'package:ext_rw/src/table_schema/schema_entry.dart'; import 'package:hmi_core/hmi_core_failure.dart'; import 'package:hmi_core/hmi_core_log.dart'; import 'package:hmi_core/hmi_core_result_new.dart'; From 4310853331ba182e6467ace499f3e4fc2c1731a1 Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Thu, 30 Nov 2023 17:47:34 +0300 Subject: [PATCH 4/8] add tests for ApiRequests --- .../api_request/api_request_socket_test.dart | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 test/unit/api_client/request/api_request/api_request_socket_test.dart diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart new file mode 100644 index 0000000..df07d07 --- /dev/null +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -0,0 +1,119 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:ext_rw/ext_rw.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:hmi_core/hmi_core_result_new.dart'; + +class FakeApiQueryType implements ApiQueryType { + final bool _valid; + final String _query; + + FakeApiQueryType({ + required bool valid, + required String query, + }) : _valid = valid, + _query = query; + + @override + bool valid() => _valid; + + @override + String buildJson() => _query; + + // insert directly into _query in fake implementation + @override + String get authToken => ''; + + // insert directly into _query in fake implementation + @override + String get id => ''; +} + +void main() { + group('ApiRequest socket', () { + late ApiAddress address; + late ServerSocket serverSocket; + setUp(() async { + // bind socket server to unused port and start listening + serverSocket = await ServerSocket.bind( + InternetAddress.loopbackIPv4.host, + 0, + ); + // respond with received data + serverSocket.listen((socket) { + socket.listen((data) { + socket.add(data); + }); + }); + address = ApiAddress( + host: serverSocket.address.host, + port: serverSocket.port, + ); + }); + test('.fetch() with valid query', () async { + final queryList = [ + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": []}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"stringDataKey":"dataValue"}]}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"booleanDataKey":true}]}', + '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"listDataKey":[1,"2",{"lik":"liv"},[]]}]}', + ]; + + for (final query in queryList) { + final apiRequest = ApiRequest( + address: address, + query: FakeApiQueryType( + valid: true, + query: query, + ), + ); + + final result = await apiRequest.fetch(); + + expect( + result, + isA(), + reason: 'valid api request should return Ok as Result', + ); + + if (result case Ok(value: final reply)) { + final replyAsJson = + '{"authToken": "${reply.authToken}", "id": "${reply.id}", "query": "${reply.sql}", "data": ${json.encode(reply.data)}}'; + expect( + replyAsJson, + query, + reason: 'data should be transferred correctly', + ); + } + } + }); + + test('.fetch() with invalid query', () async { + final queryList = [ + '', + ]; + + for (final query in queryList) { + final apiRequest = ApiRequest( + address: address, + query: FakeApiQueryType( + valid: false, + query: query, + ), + ); + + final result = await apiRequest.fetch(); + + expect( + result, + isA(), + reason: 'Api request with invalid query should return Err as Result', + ); + } + }); + + tearDown(() async { + serverSocket.close(); + }); + }); +} From 20eb03b941c1e77fec8228481af269ac60c5952b Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Fri, 1 Dec 2023 12:17:31 +0300 Subject: [PATCH 5/8] add FakeApiAddress for tests --- .../api_request/api_request_socket_test.dart | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart index df07d07..1a2e337 100644 --- a/test/unit/api_client/request/api_request/api_request_socket_test.dart +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -5,6 +5,21 @@ import 'package:ext_rw/ext_rw.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hmi_core/hmi_core_result_new.dart'; +class FakeApiAddress implements ApiAddress { + final String _host; + final int _port; + + FakeApiAddress({required String host, required int port}) + : _host = host, + _port = port; + + @override + String get host => _host; + + @override + int get port => _port; +} + class FakeApiQueryType implements ApiQueryType { final bool _valid; final String _query; @@ -32,7 +47,7 @@ class FakeApiQueryType implements ApiQueryType { void main() { group('ApiRequest socket', () { - late ApiAddress address; + late FakeApiAddress address; late ServerSocket serverSocket; setUp(() async { // bind socket server to unused port and start listening @@ -46,7 +61,7 @@ void main() { socket.add(data); }); }); - address = ApiAddress( + address = FakeApiAddress( host: serverSocket.address.host, port: serverSocket.port, ); From 1aeadaa611e8abfafe363d41d2730b6f2bfc7b75 Mon Sep 17 00:00:00 2001 From: Nikita Sermyagin Date: Fri, 1 Dec 2023 12:35:16 +0300 Subject: [PATCH 6/8] fix flutter analyze warnings --- lib/src/table_schema/schema.dart | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/src/table_schema/schema.dart b/lib/src/table_schema/schema.dart index 31e5be4..2ac3b90 100644 --- a/lib/src/table_schema/schema.dart +++ b/lib/src/table_schema/schema.dart @@ -1,9 +1,4 @@ -import 'package:ext_rw/src/sql/sql.dart'; -import 'package:ext_rw/src/table_schema/field.dart'; -import 'package:ext_rw/src/table_schema/schema_entry.dart'; -import 'package:ext_rw/src/api_client/address/api_address.dart'; -import 'package:ext_rw/src/api_client/query/sql_query.dart'; -import 'package:ext_rw/src/api_client/request/api_request.dart'; +import 'package:ext_rw/ext_rw.dart'; import 'package:hmi_core/hmi_core_failure.dart'; import 'package:hmi_core/hmi_core_log.dart'; import 'package:hmi_core/hmi_core_result_new.dart'; From b3b4f9b864fb3899eb10e3c712d235ed9f81b690 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Mon, 25 Dec 2023 14:53:51 +0300 Subject: [PATCH 7/8] api_request_socket_test fixes --- .../api_request/api_request_socket_test.dart | 31 +++---------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart index 1a2e337..1b7b89f 100644 --- a/test/unit/api_client/request/api_request/api_request_socket_test.dart +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -5,21 +5,6 @@ import 'package:ext_rw/ext_rw.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hmi_core/hmi_core_result_new.dart'; -class FakeApiAddress implements ApiAddress { - final String _host; - final int _port; - - FakeApiAddress({required String host, required int port}) - : _host = host, - _port = port; - - @override - String get host => _host; - - @override - int get port => _port; -} - class FakeApiQueryType implements ApiQueryType { final bool _valid; final String _query; @@ -47,7 +32,7 @@ class FakeApiQueryType implements ApiQueryType { void main() { group('ApiRequest socket', () { - late FakeApiAddress address; + late ApiAddress address; late ServerSocket serverSocket; setUp(() async { // bind socket server to unused port and start listening @@ -61,7 +46,7 @@ void main() { socket.add(data); }); }); - address = FakeApiAddress( + address = ApiAddress( host: serverSocket.address.host, port: serverSocket.port, ); @@ -73,7 +58,6 @@ void main() { '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"booleanDataKey":true}]}', '{"authToken": "testToken", "id": "testID", "query": "testQuery", "data": [{"listDataKey":[1,"2",{"lik":"liv"},[]]}]}', ]; - for (final query in queryList) { final apiRequest = ApiRequest( address: address, @@ -82,18 +66,14 @@ void main() { query: query, ), ); - final result = await apiRequest.fetch(); - expect( result, isA(), reason: 'valid api request should return Ok as Result', ); - if (result case Ok(value: final reply)) { - final replyAsJson = - '{"authToken": "${reply.authToken}", "id": "${reply.id}", "query": "${reply.sql}", "data": ${json.encode(reply.data)}}'; + final replyAsJson = '{"authToken": "${reply.authToken}", "id": "${reply.id}", "query": "${reply.sql}", "data": ${json.encode(reply.data)}}'; expect( replyAsJson, query, @@ -102,12 +82,11 @@ void main() { } } }); - + /// test('.fetch() with invalid query', () async { final queryList = [ '', ]; - for (final query in queryList) { final apiRequest = ApiRequest( address: address, @@ -116,9 +95,7 @@ void main() { query: query, ), ); - final result = await apiRequest.fetch(); - expect( result, isA(), From 32844e2017da9c4b98afac2f0f0b353caef058f1 Mon Sep 17 00:00:00 2001 From: a-givertzman Date: Mon, 25 Dec 2023 15:14:03 +0300 Subject: [PATCH 8/8] static analyzer issues fixed --- .../request/api_request/api_request_socket_test.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/unit/api_client/request/api_request/api_request_socket_test.dart b/test/unit/api_client/request/api_request/api_request_socket_test.dart index 1b7b89f..5298cac 100644 --- a/test/unit/api_client/request/api_request/api_request_socket_test.dart +++ b/test/unit/api_client/request/api_request/api_request_socket_test.dart @@ -19,11 +19,7 @@ class FakeApiQueryType implements ApiQueryType { bool valid() => _valid; @override - String buildJson() => _query; - - // insert directly into _query in fake implementation - @override - String get authToken => ''; + String buildJson({String authToken = '', bool debug = false}) => _query; // insert directly into _query in fake implementation @override @@ -61,6 +57,7 @@ void main() { for (final query in queryList) { final apiRequest = ApiRequest( address: address, + authToken: '+++', query: FakeApiQueryType( valid: true, query: query, @@ -90,6 +87,7 @@ void main() { for (final query in queryList) { final apiRequest = ApiRequest( address: address, + authToken: '+++', query: FakeApiQueryType( valid: false, query: query,