Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiRequest | add tests #12

Merged
merged 10 commits into from
Dec 25, 2023
109 changes: 109 additions & 0 deletions test/unit/api_client/request/api_request/api_request_socket_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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({String authToken = '', bool debug = false}) => _query;

// 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,
authToken: '+++',
query: FakeApiQueryType(
valid: true,
query: query,
),
);
final result = await apiRequest.fetch();
expect(
result,
isA<Ok>(),
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,
authToken: '+++',
query: FakeApiQueryType(
valid: false,
query: query,
),
);
final result = await apiRequest.fetch();
expect(
result,
isA<Err>(),
reason: 'Api request with invalid query should return Err as Result',
);
}
});

tearDown(() async {
serverSocket.close();
});
});
}
Loading