Skip to content

Commit

Permalink
RSDK-9180: Change TabularDataBySQL/MQL return type to raw BSON in Flu…
Browse files Browse the repository at this point in the history
…tter SDK (#286)

* initial SQL and MQL changes but no test data change

* added timestamp to tests
  • Loading branch information
jckras authored Nov 1, 2024
1 parent 538157e commit 019edba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/src/app/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:async/async.dart';
import 'package:bson/bson.dart' hide Timestamp;
import 'package:collection/collection.dart';
import 'package:fixnum/fixnum.dart';

Expand Down Expand Up @@ -82,12 +83,13 @@ class DataClient {
/// Obtain unified tabular data and metadata, queried with SQL.
///
/// For more information, see [Data Client API](https://docs.viam.com/appendix/apis/data-client/).
// List<List<int>>
Future<List<Map<String, dynamic>>> tabularDataBySql(String organizationId, String query) async {
final request = TabularDataBySQLRequest()
..organizationId = organizationId
..sqlQuery = query;
final response = await _dataClient.tabularDataBySQL(request);
return response.data.map((e) => e.toMap()).toList();
return response.rawData.map((e) => BsonCodec.deserialize(BsonBinary.from(e))).toList();
}

/// Obtain unified tabular data and metadata, queried with MQL.
Expand All @@ -98,7 +100,7 @@ class DataClient {
..organizationId = organizationId
..mqlBinary.addAll(query);
final response = await _dataClient.tabularDataByMQL(request);
return response.data.map((e) => e.toMap()).toList();
return response.rawData.map((e) => BsonCodec.deserialize(BsonBinary.from(e))).toList();
}

/// Delete tabular data older than a provided number of days from an organization.
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
collection: ^1.17.1
async: ^2.11.0
bonsoir: ^5.1.8
bson: ^5.0.5

dev_dependencies:
flutter_test:
Expand Down
18 changes: 11 additions & 7 deletions test/unit_test/app/data_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import 'package:bson/bson.dart' hide Timestamp;
import 'package:fixnum/fixnum.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:grpc/src/client/call.dart';
Expand All @@ -13,7 +14,6 @@ import 'package:viam_sdk/src/gen/app/data/v1/data.pb.dart';
import 'package:viam_sdk/src/gen/app/data/v1/data.pbgrpc.dart';
import 'package:viam_sdk/src/gen/google/protobuf/timestamp.pb.dart';
import 'package:viam_sdk/src/media/image.dart';
import 'package:viam_sdk/src/utils.dart';

import '../mocks/mock_response_future.dart';
import '../mocks/service_clients_mocks.mocks.dart';
Expand Down Expand Up @@ -142,36 +142,40 @@ void main() {
});

test('tabularDataBySql', () async {
final startDate = DateTime.utc(2020, 12, 31);
final List<Map<String, dynamic>> data = [
{
'key1': 1,
'key1': startDate,
'key2': '2',
'key3': [1, 2, 3],
'key4': {'key4sub1': 1}
},
];

when(serviceClient.tabularDataBySQL(any))
.thenAnswer((_) => MockResponseFuture.value(TabularDataBySQLResponse()..data.addAll(data.map((e) => e.toStruct()))));
when(serviceClient.tabularDataBySQL(any)).thenAnswer(
(_) => MockResponseFuture.value(TabularDataBySQLResponse()..rawData.addAll(data.map((e) => BsonCodec.serialize(e).byteList))));

final response = await dataClient.tabularDataBySql('some_org_id', 'some_query');
expect(response[0]['key1'], equals(data[0]['key1']));
expect(response, equals(data));
});

test('tabularDataByMql', () async {
final startDate = DateTime.utc(2020, 12, 31);
final List<Map<String, dynamic>> data = [
{
'key1': 1,
'key1': startDate.toUtc(),
'key2': '2',
'key3': [1, 2, 3],
'key4': {'key4sub1': 1}
},
];

when(serviceClient.tabularDataByMQL(any))
.thenAnswer((_) => MockResponseFuture.value(TabularDataByMQLResponse()..data.addAll(data.map((e) => e.toStruct()))));
when(serviceClient.tabularDataByMQL(any)).thenAnswer(
(_) => MockResponseFuture.value(TabularDataByMQLResponse()..rawData.addAll(data.map((e) => BsonCodec.serialize(e).byteList))));

final response = await dataClient.tabularDataByMql('some_org_id', [Uint8List.fromList('some_query'.codeUnits)]);
expect(response[0]['key1'], equals(data[0]['key1']));
expect(response, equals(data));
});

Expand Down

0 comments on commit 019edba

Please sign in to comment.