From 2ff531eb4f5ed1252acf034b21afb69a3b9ee442 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 3 Aug 2018 17:25:16 -0400 Subject: [PATCH] Added basic validation tests to graphql_schema --- .../tests_in_validation_test_dart.xml | 7 ++++ graphql_schema/test/validation_test.dart | 32 +++++++++++++++++++ graphql_server/test/common.dart | 5 +++ 3 files changed, 44 insertions(+) create mode 100644 .idea/runConfigurations/tests_in_validation_test_dart.xml create mode 100644 graphql_schema/test/validation_test.dart create mode 100644 graphql_server/test/common.dart diff --git a/.idea/runConfigurations/tests_in_validation_test_dart.xml b/.idea/runConfigurations/tests_in_validation_test_dart.xml new file mode 100644 index 00000000..863374eb --- /dev/null +++ b/.idea/runConfigurations/tests_in_validation_test_dart.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/graphql_schema/test/validation_test.dart b/graphql_schema/test/validation_test.dart new file mode 100644 index 00000000..3645f7de --- /dev/null +++ b/graphql_schema/test/validation_test.dart @@ -0,0 +1,32 @@ +import 'package:graphql_schema/graphql_schema.dart'; +import 'package:test/test.dart'; + +void main() { + var pokemonType = objectType('Pokémon', fields: [ + field('name', type: graphQLString.nonNullable()), + ]); + + var isValidPokemon = predicate( + (x) => + pokemonType.validate('@root', x as Map).successful, + 'is a valid Pokémon'); + + var throwsATypeError = + throwsA(predicate((x) => x is TypeError, 'is a type error')); + + test('mismatched scalar type', () { + expect(() => pokemonType.validate('@root', {'name': 24}), throwsATypeError); + }); + + test('empty passed for non-nullable', () { + expect({}, isNot(isValidPokemon)); + }); + + test('null passed for non-nullable', () { + expect({'name': null}, isNot(isValidPokemon)); + }); + + test('rejects extraneous fields', () { + expect({'name': 'Vulpix', 'foo': 'bar'}, isNot(isValidPokemon)); + }); +} diff --git a/graphql_server/test/common.dart b/graphql_server/test/common.dart new file mode 100644 index 00000000..9be47053 --- /dev/null +++ b/graphql_server/test/common.dart @@ -0,0 +1,5 @@ +import 'package:graphql_schema/graphql_schema.dart'; +import 'package:test/test.dart'; + +final Matcher throwsAGraphQLException = + throwsA(predicate((x) => x is GraphQLException, 'is a GraphQL exception'));