Skip to content

Commit

Permalink
Merge pull request #12 from a-givertzman/Add-ApiRequests-tests
Browse files Browse the repository at this point in the history
ApiRequest | add tests
  • Loading branch information
a-givertzman authored Dec 25, 2023
2 parents 4eb3bf2 + 32844e2 commit 7bef023
Showing 1 changed file with 109 additions and 0 deletions.
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();
});
});
}

0 comments on commit 7bef023

Please sign in to comment.