|
| 1 | +import sys |
| 2 | +import os |
| 3 | + |
| 4 | +import pytest |
| 5 | +from graphene.test import Client |
| 6 | +from graphql import GraphQLScalarType |
| 7 | +from graphql.language import ast |
| 8 | +from graphql import GraphQLError |
| 9 | + |
| 10 | +import unittest |
| 11 | + |
| 12 | +# This is the only way I could get imports to work for unit testing. TODO: See if there is a better way! |
| 13 | +PACKAGE_PARENT = '..' |
| 14 | +SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) |
| 15 | +sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) |
| 16 | + |
| 17 | +from scalars.email_address import * |
| 18 | + |
| 19 | + |
| 20 | +class TestEmailAddressScalar(unittest.TestCase): |
| 21 | + |
| 22 | + def test_valid_email_serialize(self): |
| 23 | + test_email = 'test.account@canada.ca' |
| 24 | + assert EmailAddress.serialize(test_email) |
| 25 | + |
| 26 | + def test_valid_email_parse_value(self): |
| 27 | + test_email = "test.account@canada.ca" |
| 28 | + assert EmailAddress.parse_value(test_email) |
| 29 | + |
| 30 | + def test_valid_email_parse_literal(self): |
| 31 | + assert EmailAddress.parse_literal(ast.StringValue( |
| 32 | + value="test.account@canada.ca" |
| 33 | + )) |
| 34 | + |
| 35 | + def test_invalid_email_serialize_not_email(self): |
| 36 | + test_value = 'This Will Fail' |
| 37 | + with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value)): |
| 38 | + EmailAddress.serialize(test_value) |
| 39 | + |
| 40 | + def test_invalid_email_serialize_wrong_type(self): |
| 41 | + test_value = 1234 |
| 42 | + with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)): |
| 43 | + EmailAddress.serialize(test_value) |
| 44 | + |
| 45 | + def test_invalid_email_parse_value_not_email(self): |
| 46 | + test_value = 'This Will Fail' |
| 47 | + with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value)): |
| 48 | + EmailAddress.parse_value(test_value) |
| 49 | + |
| 50 | + def test_invalid_email_parse_value_wrong_type(self): |
| 51 | + test_value = 1234 |
| 52 | + with self.assertRaisesRegex(GraphQLError, scalar_error_type("String", test_value)): |
| 53 | + EmailAddress.parse_value(test_value) |
| 54 | + |
| 55 | + def test_invalid_email_parse_literal_not_email(self): |
| 56 | + test_value = ast.StringValue( |
| 57 | + value='This Will Fail' |
| 58 | + ) |
| 59 | + with self.assertRaisesRegex(GraphQLError, scalar_error_type("email address", test_value.value)): |
| 60 | + EmailAddress.parse_literal(test_value) |
| 61 | + |
| 62 | + def test_invalid_email_parse_literal_wrong_ast_type(self): |
| 63 | + test_value = ast.IntValue( |
| 64 | + value="1234" |
| 65 | + ) |
| 66 | + with self.assertRaisesRegex(GraphQLError, scalar_error_only_types("strings", "email address", str(ast.Type))): |
| 67 | + EmailAddress.parse_literal(test_value) |
0 commit comments