Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jmagman committed Aug 28, 2019
1 parent ab5a82e commit 8445d82
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app_dart/lib/src/model/appengine/agent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Agent extends Model {
this.agentId,
this.healthCheckTimestamp,
this.isHealthy,
this.isHidden,
this.isHidden = false,
this.capabilities,
this.healthDetails,
this.authToken,
Expand Down
82 changes: 82 additions & 0 deletions app_dart/test/request_handlers/get_status_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:typed_data';

import 'package:cocoon_service/src/model/appengine/agent.dart';
import 'package:cocoon_service/src/request_handlers/get_status.dart';
import 'package:cocoon_service/src/request_handling/body.dart';
import 'package:cocoon_service/src/service/build_status_provider.dart';
import 'package:cocoon_service/src/service/datastore.dart';
import 'package:test/test.dart';

import '../src/datastore/fake_cocoon_config.dart';
import '../src/datastore/fake_datastore.dart';
import '../src/service/fake_build_status_provider.dart';

void main() {
group('GetStatus', () {
FakeConfig config;
FakeDatastoreDB db;
FakeBuildStatusProvider buildStatusProvider;
GetStatus handler;

Future<Map<String, dynamic>> decodeHandlerBody() async {
final Body body = await handler.get();
final List<Uint8List> bytes = await body.serialize().toList();
final String decodedBody =

This comment has been minimized.

Copy link
@tvolkert

tvolkert Aug 28, 2019

Contributor

The streams API leaves something to be desired... this will do the same thing with less code:

final String decodedBody = await utf8.decoder.bind(body.serialize()).join();

Or even:

return utf8.decoder.bind(body.serialize()).transform(json.decoder).single;
utf8.decode(bytes.expand((Uint8List bytes) => bytes).toList());
return json.decode(decodedBody);
}

setUp(() {
config = FakeConfig();
buildStatusProvider =

This comment has been minimized.

Copy link
@tvolkert

tvolkert Aug 28, 2019

Contributor

Nit: dartfmt -l 100

FakeBuildStatusProvider(commitStatuses: <CommitStatus>[]);
db = FakeDatastoreDB();
handler = GetStatus(
config,
datastoreProvider: () => DatastoreService(db: db),
buildStatusProvider: buildStatusProvider,
);
});

test('no statuses or agents', () async {
final Map<String, dynamic> result = await decodeHandlerBody();
expect(result['Statuses'], isEmpty);
expect(result['AgentStatuses'], isEmpty);
});

test('reports agents', () async {
final Agent linux1 = Agent(agentId: 'linux1');
final Agent mac1 = Agent(agentId: 'mac1');
final Agent linux100 = Agent(agentId: 'linux100');
final Agent linux5 = Agent(agentId: 'linux5');
final Agent windows1 = Agent(agentId: 'windows1', isHidden: true);

final List<Agent> reportedAgents = <Agent>[
linux1,
mac1,
linux100,
linux5,
windows1,
];

db.addOnQuery<Agent>((Iterable<Agent> agents) => reportedAgents);
final Map<String, dynamic> result = await decodeHandlerBody();

expect(result['Statuses'], isEmpty);

final List<dynamic> expectedOrderedAgents = <dynamic>[
linux1.toJson(),
linux5.toJson(),
linux100.toJson(),
mac1.toJson(),
];

expect(result['AgentStatuses'], equals(expectedOrderedAgents));
});
});
}

0 comments on commit 8445d82

Please sign in to comment.