Add many tests to graphql_schema
This commit is contained in:
parent
9e98e92ba4
commit
fb65f0fe0a
9 changed files with 325 additions and 6 deletions
|
@ -0,0 +1,9 @@
|
||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="objects in equality_test.dart" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true" nameIsGenerated="true">
|
||||||
|
<option name="filePath" value="$PROJECT_DIR$/graphql_schema/test/equality_test.dart" />
|
||||||
|
<option name="scope" value="GROUP_OR_TEST_BY_NAME" />
|
||||||
|
<option name="testName" value="objects" />
|
||||||
|
<option name="testRunnerOptions" value="-j4" />
|
||||||
|
<method />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
8
.idea/runConfigurations/tests_in_graphql_schema.xml
Normal file
8
.idea/runConfigurations/tests_in_graphql_schema.xml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="tests in graphql_schema" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true" nameIsGenerated="true">
|
||||||
|
<option name="filePath" value="$PROJECT_DIR$/graphql_schema" />
|
||||||
|
<option name="scope" value="FOLDER" />
|
||||||
|
<option name="testRunnerOptions" value="-j4" />
|
||||||
|
<method />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
|
@ -17,8 +17,8 @@ class GraphQLObjectField<Value, Serialized> {
|
||||||
this.deprecationReason,
|
this.deprecationReason,
|
||||||
this.description}) {
|
this.description}) {
|
||||||
assert(type != null, 'GraphQL fields must specify a `type`.');
|
assert(type != null, 'GraphQL fields must specify a `type`.');
|
||||||
assert(
|
// assert(
|
||||||
resolve != null, 'GraphQL fields must specify a `resolve` callback.');
|
// resolve != null, 'GraphQL fields must specify a `resolve` callback.');
|
||||||
this.inputs.addAll(arguments ?? <GraphQLFieldInput>[]);
|
this.inputs.addAll(arguments ?? <GraphQLFieldInput>[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ GraphQLObjectField<T, Serialized> field<T, Serialized>(
|
||||||
String deprecationReason, String description}) {
|
String deprecationReason, String description}) {
|
||||||
return new GraphQLObjectField<T, Serialized>(name, type,
|
return new GraphQLObjectField<T, Serialized>(name, type,
|
||||||
arguments: inputs,
|
arguments: inputs,
|
||||||
resolve: resolve ?? (_, __) => null,
|
resolve: resolve,
|
||||||
description: description,
|
description: description,
|
||||||
deprecationReason: deprecationReason);
|
deprecationReason: deprecationReason);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,9 @@ class GraphQLUnionType
|
||||||
Map<String, dynamic> serialize(Map<String, dynamic> value) {
|
Map<String, dynamic> serialize(Map<String, dynamic> value) {
|
||||||
for (var type in possibleTypes) {
|
for (var type in possibleTypes) {
|
||||||
try {
|
try {
|
||||||
return type.serialize(value);
|
if (type.validate('@root', value).successful) {
|
||||||
|
return type.serialize(value);
|
||||||
|
}
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
113
graphql_schema/test/equality_test.dart
Normal file
113
graphql_schema/test/equality_test.dart
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
import 'package:graphql_schema/graphql_schema.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
/// Note: this doesn't test for scalar types, which are final, and therefore use built-in equality.
|
||||||
|
void main() {
|
||||||
|
group('equality', () {
|
||||||
|
test('enums', () {
|
||||||
|
expect(enumTypeFromStrings('A', ['B', 'C']),
|
||||||
|
enumTypeFromStrings('A', ['B', 'C']));
|
||||||
|
expect(enumTypeFromStrings('A', ['B', 'C']),
|
||||||
|
isNot(enumTypeFromStrings('B', ['B', 'C'])));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('objects', () {
|
||||||
|
expect(
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
isNot(objectType('BD', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
])),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
isNot(objectType('B', fields: [
|
||||||
|
field('ba', graphQLString.nonNullable()),
|
||||||
|
])),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
isNot(objectType('B', fields: [
|
||||||
|
field('a', graphQLFloat.nonNullable()),
|
||||||
|
])),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('input type', () {});
|
||||||
|
|
||||||
|
test('union type', () {
|
||||||
|
expect(
|
||||||
|
new GraphQLUnionType('A', [
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
new GraphQLUnionType('A', [
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
new GraphQLUnionType('A', [
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
isNot(new GraphQLUnionType('AA', [
|
||||||
|
objectType('B', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
])),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
new GraphQLUnionType('A', [
|
||||||
|
objectType('BB', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
isNot(new GraphQLUnionType('AA', [
|
||||||
|
objectType('BDD', fields: [
|
||||||
|
field('b', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
objectType('C', fields: [
|
||||||
|
field('c', graphQLString.nonNullable()),
|
||||||
|
]),
|
||||||
|
])),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
67
graphql_schema/test/inheritance_test.dart
Normal file
67
graphql_schema/test/inheritance_test.dart
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import 'package:graphql_schema/graphql_schema.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('interface', () {
|
||||||
|
var a = objectType(
|
||||||
|
'A',
|
||||||
|
isInterface: true,
|
||||||
|
fields: [
|
||||||
|
field('text', graphQLString.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
var b = objectType(
|
||||||
|
'B',
|
||||||
|
isInterface: true,
|
||||||
|
interfaces: [a],
|
||||||
|
fields: [
|
||||||
|
field('text', graphQLString.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
var c = objectType(
|
||||||
|
'C',
|
||||||
|
isInterface: true,
|
||||||
|
interfaces: [b],
|
||||||
|
fields: [
|
||||||
|
field('text', graphQLString.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
test('child implements parent', () {
|
||||||
|
expect(b.isImplementationOf(a), true);
|
||||||
|
expect(c.isImplementationOf(b), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parent does not implement child', () {
|
||||||
|
expect(a.isImplementationOf(b), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('child interfaces contains parent', () {
|
||||||
|
expect(b.interfaces, contains(a));
|
||||||
|
expect(c.interfaces, contains(b));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parent possibleTypes contains child', () {
|
||||||
|
expect(a.possibleTypes, contains(b));
|
||||||
|
expect(b.possibleTypes, contains(c));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('grandchild implements grandparent', () {
|
||||||
|
expect(c.isImplementationOf(a), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('grandparent does not implement grandchild', () {
|
||||||
|
expect(a.isImplementationOf(c), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('grandchild interfaces contains grandparent', () {
|
||||||
|
expect(c.interfaces, contains(a));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('grandparent possibleTypes contains grandchild', () {
|
||||||
|
expect(a.possibleTypes, contains(c));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,11 +1,36 @@
|
||||||
import 'package:graphql_schema/graphql_schema.dart';
|
import 'package:graphql_schema/graphql_schema.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
test('scalar', () {
|
test('int', () {
|
||||||
expect(graphQLString.serialize('a'), 'a');
|
expect(graphQLInt.serialize(23), 23);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('float', () {
|
||||||
|
expect(graphQLFloat.serialize(23.0), 23.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('bool', () {
|
||||||
|
expect(graphQLBoolean.serialize(true), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('string', () {
|
||||||
|
expect(graphQLString.serialize('a'), 'a');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('enum', () {
|
||||||
|
var response = enumTypeFromStrings('Response', ['YES', 'NO']);
|
||||||
|
expect(response.serialize('YES'), 'YES');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('enum only serializes correct values', () {
|
||||||
|
var response = enumTypeFromStrings('Response', ['YES', 'NO']);
|
||||||
|
expect(() => response.serialize('MAYBE'), throwsStateError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('date', () {
|
||||||
var now = new DateTime.now();
|
var now = new DateTime.now();
|
||||||
expect(graphQLDate.serialize(now), now.toIso8601String());
|
expect(graphQLDate.serialize(now), now.toIso8601String());
|
||||||
});
|
});
|
||||||
|
@ -19,6 +44,21 @@ main() {
|
||||||
[today.toIso8601String(), tomorrow.toIso8601String()]);
|
[today.toIso8601String(), tomorrow.toIso8601String()]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('input object', () {
|
||||||
|
var type = inputObjectType(
|
||||||
|
'Foo',
|
||||||
|
inputFields: [
|
||||||
|
inputField('bar', graphQLString.nonNullable()),
|
||||||
|
inputField('baz', graphQLFloat.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
test('serializes valid input', () {
|
||||||
|
expect(
|
||||||
|
type.serialize({'bar': 'a', 'baz': 2.0}), {'bar': 'a', 'baz': 2.0});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('object', () {
|
test('object', () {
|
||||||
var catchDate = new DateTime.now();
|
var catchDate = new DateTime.now();
|
||||||
|
|
||||||
|
@ -28,6 +68,37 @@ main() {
|
||||||
{'species': 'Pikachu', 'catch_date': catchDate.toIso8601String()});
|
{'species': 'Pikachu', 'catch_date': catchDate.toIso8601String()});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('union type lets any of its types serialize', () {
|
||||||
|
var typeType = enumTypeFromStrings('Type', [
|
||||||
|
'FIRE',
|
||||||
|
'WATER',
|
||||||
|
'GRASS',
|
||||||
|
]);
|
||||||
|
|
||||||
|
var pokemonType = objectType('Pokémon', fields: [
|
||||||
|
field(
|
||||||
|
'name',
|
||||||
|
graphQLString.nonNullable(),
|
||||||
|
),
|
||||||
|
field(
|
||||||
|
'type',
|
||||||
|
typeType,
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
var digimonType = objectType(
|
||||||
|
'Digimon',
|
||||||
|
fields: [
|
||||||
|
field('size', graphQLFloat.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
var u = new GraphQLUnionType('Monster', [pokemonType, digimonType]);
|
||||||
|
|
||||||
|
expect(u.serialize({'size': 10.0}), {'size': 10.0});
|
||||||
|
expect(u.serialize({'name': 'Charmander', 'type': 'FIRE'}), {'name': 'Charmander', 'type': 'FIRE'});
|
||||||
|
});
|
||||||
|
|
||||||
test('nested object', () {
|
test('nested object', () {
|
||||||
var pikachuDate = new DateTime.now(),
|
var pikachuDate = new DateTime.now(),
|
||||||
charizardDate = pikachuDate.subtract(new Duration(days: 10));
|
charizardDate = pikachuDate.subtract(new Duration(days: 10));
|
||||||
|
|
|
@ -27,6 +27,10 @@ void main() {
|
||||||
var throwsATypeError =
|
var throwsATypeError =
|
||||||
throwsA(predicate((x) => x is TypeError, 'is a type error'));
|
throwsA(predicate((x) => x is TypeError, 'is a type error'));
|
||||||
|
|
||||||
|
test('object accepts valid input', () {
|
||||||
|
expect({'name': 'Charmander', 'type': 'FIRE'}, isValidPokemon);
|
||||||
|
});
|
||||||
|
|
||||||
test('mismatched scalar type', () {
|
test('mismatched scalar type', () {
|
||||||
expect(() => pokemonType.validate('@root', {'name': 24}), throwsATypeError);
|
expect(() => pokemonType.validate('@root', {'name': 24}), throwsATypeError);
|
||||||
});
|
});
|
||||||
|
@ -50,4 +54,49 @@ void main() {
|
||||||
test('enum rejects invalid value', () {
|
test('enum rejects invalid value', () {
|
||||||
expect(typeType.validate('@root', 'POISON').successful, false);
|
expect(typeType.validate('@root', 'POISON').successful, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('union type', () {
|
||||||
|
var digimonType = objectType(
|
||||||
|
'Digimon',
|
||||||
|
fields: [
|
||||||
|
field('size', graphQLFloat.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
var u = new GraphQLUnionType('Monster', [pokemonType, digimonType]);
|
||||||
|
|
||||||
|
test('any of its types returns valid', () {
|
||||||
|
expect(u.validate('@root', {'size': 32.0}).successful, true);
|
||||||
|
expect(
|
||||||
|
u.validate(
|
||||||
|
'@root', {'name': 'Charmander', 'type': 'FIRE'}).successful,
|
||||||
|
true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('input object', () {
|
||||||
|
var type = inputObjectType(
|
||||||
|
'Foo',
|
||||||
|
inputFields: [
|
||||||
|
inputField('bar', graphQLString.nonNullable()),
|
||||||
|
inputField('baz', graphQLFloat.nonNullable()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
test('accept valid input', () {
|
||||||
|
expect(type.validate('@root', {'bar': 'a', 'baz': 2.0}).value,
|
||||||
|
{'bar': 'a', 'baz': 2.0});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('error on missing non-null fields', () {
|
||||||
|
expect(type.validate('@root', {'bar': 'a'}).successful, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('error on unrecognized fields', () {
|
||||||
|
expect(
|
||||||
|
type.validate(
|
||||||
|
'@root', {'bar': 'a', 'baz': 2.0, 'franken': 'stein'}).successful,
|
||||||
|
false);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue