blob: a8b7803af5aaca85b78402d5454505079ca490a8 [file] [log] [blame]
library googleapis.kgsearch.v1.test;
import "dart:core" as core;
import "dart:async" as async;
import "dart:convert" as convert;
import 'package:http/http.dart' as http;
import 'package:test/test.dart' as unittest;
import 'package:googleapis/kgsearch/v1.dart' as api;
class HttpServerMock extends http.BaseClient {
core.Function _callback;
core.bool _expectJson;
void register(core.Function callback, core.bool expectJson) {
_callback = callback;
_expectJson = expectJson;
}
async.Future<http.StreamedResponse> send(http.BaseRequest request) {
if (_expectJson) {
return request
.finalize()
.transform(convert.UTF8.decoder)
.join('')
.then((core.String jsonString) {
if (jsonString.isEmpty) {
return _callback(request, null);
} else {
return _callback(request, convert.JSON.decode(jsonString));
}
});
} else {
var stream = request.finalize();
if (stream == null) {
return _callback(request, []);
} else {
return stream.toBytes().then((data) {
return _callback(request, data);
});
}
}
}
}
http.StreamedResponse stringResponse(core.int status,
core.Map<core.String, core.String> headers, core.String body) {
var stream = new async.Stream.fromIterable([convert.UTF8.encode(body)]);
return new http.StreamedResponse(stream, status, headers: headers);
}
buildUnnamed1052() {
var o = new core.List<core.Object>();
o.add({
'list': [1, 2, 3],
'bool': true,
'string': 'foo'
});
o.add({
'list': [1, 2, 3],
'bool': true,
'string': 'foo'
});
return o;
}
checkUnnamed1052(core.List<core.Object> o) {
unittest.expect(o, unittest.hasLength(2));
var casted1 = (o[0]) as core.Map;
unittest.expect(casted1, unittest.hasLength(3));
unittest.expect(casted1["list"], unittest.equals([1, 2, 3]));
unittest.expect(casted1["bool"], unittest.equals(true));
unittest.expect(casted1["string"], unittest.equals('foo'));
var casted2 = (o[1]) as core.Map;
unittest.expect(casted2, unittest.hasLength(3));
unittest.expect(casted2["list"], unittest.equals([1, 2, 3]));
unittest.expect(casted2["bool"], unittest.equals(true));
unittest.expect(casted2["string"], unittest.equals('foo'));
}
core.int buildCounterSearchResponse = 0;
buildSearchResponse() {
var o = new api.SearchResponse();
buildCounterSearchResponse++;
if (buildCounterSearchResponse < 3) {
o.P_context = {
'list': [1, 2, 3],
'bool': true,
'string': 'foo'
};
o.P_type = {
'list': [1, 2, 3],
'bool': true,
'string': 'foo'
};
o.itemListElement = buildUnnamed1052();
}
buildCounterSearchResponse--;
return o;
}
checkSearchResponse(api.SearchResponse o) {
buildCounterSearchResponse++;
if (buildCounterSearchResponse < 3) {
var casted3 = (o.P_context) as core.Map;
unittest.expect(casted3, unittest.hasLength(3));
unittest.expect(casted3["list"], unittest.equals([1, 2, 3]));
unittest.expect(casted3["bool"], unittest.equals(true));
unittest.expect(casted3["string"], unittest.equals('foo'));
var casted4 = (o.P_type) as core.Map;
unittest.expect(casted4, unittest.hasLength(3));
unittest.expect(casted4["list"], unittest.equals([1, 2, 3]));
unittest.expect(casted4["bool"], unittest.equals(true));
unittest.expect(casted4["string"], unittest.equals('foo'));
checkUnnamed1052(o.itemListElement);
}
buildCounterSearchResponse--;
}
buildUnnamed1053() {
var o = new core.List<core.String>();
o.add("foo");
o.add("foo");
return o;
}
checkUnnamed1053(core.List<core.String> o) {
unittest.expect(o, unittest.hasLength(2));
unittest.expect(o[0], unittest.equals('foo'));
unittest.expect(o[1], unittest.equals('foo'));
}
buildUnnamed1054() {
var o = new core.List<core.String>();
o.add("foo");
o.add("foo");
return o;
}
checkUnnamed1054(core.List<core.String> o) {
unittest.expect(o, unittest.hasLength(2));
unittest.expect(o[0], unittest.equals('foo'));
unittest.expect(o[1], unittest.equals('foo'));
}
buildUnnamed1055() {
var o = new core.List<core.String>();
o.add("foo");
o.add("foo");
return o;
}
checkUnnamed1055(core.List<core.String> o) {
unittest.expect(o, unittest.hasLength(2));
unittest.expect(o[0], unittest.equals('foo'));
unittest.expect(o[1], unittest.equals('foo'));
}
main() {
unittest.group("obj-schema-SearchResponse", () {
unittest.test("to-json--from-json", () {
var o = buildSearchResponse();
var od = new api.SearchResponse.fromJson(o.toJson());
checkSearchResponse(od);
});
});
unittest.group("resource-EntitiesResourceApi", () {
unittest.test("method--search", () {
var mock = new HttpServerMock();
api.EntitiesResourceApi res = new api.KgsearchApi(mock).entities;
var arg_languages = buildUnnamed1053();
var arg_ids = buildUnnamed1054();
var arg_limit = 42;
var arg_prefix = true;
var arg_query = "foo";
var arg_types = buildUnnamed1055();
var arg_indent = true;
var arg_$fields = "foo";
mock.register(unittest.expectAsync2((http.BaseRequest req, json) {
var path = (req.url).path;
var pathOffset = 0;
var index;
var subPart;
unittest.expect(
path.substring(pathOffset, pathOffset + 1), unittest.equals("/"));
pathOffset += 1;
unittest.expect(path.substring(pathOffset, pathOffset + 18),
unittest.equals("v1/entities:search"));
pathOffset += 18;
var query = (req.url).query;
var queryOffset = 0;
var queryMap = {};
addQueryParam(n, v) => queryMap.putIfAbsent(n, () => []).add(v);
parseBool(n) {
if (n == "true") return true;
if (n == "false") return false;
if (n == null) return null;
throw new core.ArgumentError("Invalid boolean: $n");
}
if (query.length > 0) {
for (var part in query.split("&")) {
var keyvalue = part.split("=");
addQueryParam(core.Uri.decodeQueryComponent(keyvalue[0]),
core.Uri.decodeQueryComponent(keyvalue[1]));
}
}
unittest.expect(queryMap["languages"], unittest.equals(arg_languages));
unittest.expect(queryMap["ids"], unittest.equals(arg_ids));
unittest.expect(core.int.parse(queryMap["limit"].first),
unittest.equals(arg_limit));
unittest.expect(
queryMap["prefix"].first, unittest.equals("$arg_prefix"));
unittest.expect(queryMap["query"].first, unittest.equals(arg_query));
unittest.expect(queryMap["types"], unittest.equals(arg_types));
unittest.expect(
queryMap["indent"].first, unittest.equals("$arg_indent"));
unittest.expect(queryMap["fields"].first, unittest.equals(arg_$fields));
var h = {
"content-type": "application/json; charset=utf-8",
};
var resp = convert.JSON.encode(buildSearchResponse());
return new async.Future.value(stringResponse(200, h, resp));
}), true);
res
.search(
languages: arg_languages,
ids: arg_ids,
limit: arg_limit,
prefix: arg_prefix,
query: arg_query,
types: arg_types,
indent: arg_indent,
$fields: arg_$fields)
.then(unittest.expectAsync1(((api.SearchResponse response) {
checkSearchResponse(response);
})));
});
});
}