forked from Urigo/graphql-scalars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPCPatent.test.ts
163 lines (153 loc) · 6.45 KB
/
IPCPatent.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Kind } from 'graphql';
import { GraphQLIPCPatent } from '../src';
// Selection of random IPC Class Symbols/ClassNames
// generated by typing "new" into
// the search field at this link
// https://patentscope.wipo.int/search/en/search.jsf
const classNames = [
'A61K 31/185',
'B60C 11/24',
'G06F 17/00',
'H04M 11/00',
'B62D 21/00',
'B60R 13/01',
'F16K 27/02',
'B01D 17/032',
'G06F 40/2011',
'E04B 1/68',
'C25B 9/17',
'A61K 9/107',
'A61K 31/495',
'C07K 7/64',
];
const IPC_PATENT_REGEX = /^[A-H]\d{2}[A-Z] \d{1,2}\/\d{2,4}$/;
//Incorrect due to use of lower-case
const lowercaseIPC = classNames.map(className => className.toLowerCase());
const noSpaceIPC = classNames.map(className => className.split(' ').join(''));
const outsideLetterRangeIPC = classNames.map(className =>
className.replace(className[0], className[0] + 8),
);
const tooManyDigitsBeforeIPC = classNames.map(className => {
const parts = className.split(' ');
const firstPart = parts[0];
let secondPart = parts[1];
secondPart = '999' + secondPart;
return firstPart + ' ' + secondPart;
});
describe(`IPC`, () => {
describe(`valid`, () => {
it(`serialize`, () => {
for (const className of classNames) {
expect(GraphQLIPCPatent.serialize(className)).toEqual(className);
}
});
it(`parseValue`, () => {
for (const className of classNames) {
expect(GraphQLIPCPatent.parseValue(className)).toEqual(className);
}
});
it(`parseLiteral`, () => {
for (const className of classNames) {
expect(
GraphQLIPCPatent.parseLiteral(
{
value: className,
kind: Kind.STRING,
},
{},
),
).toEqual(className);
}
});
});
describe(`invalid`, () => {
describe(`not a valid IPC Class Symbol`, () => {
it(`serialize`, () => {
expect(() => GraphQLIPCPatent.serialize(123)).toThrow(/Value is not string/);
expect(() => GraphQLIPCPatent.serialize(`this is not an IPC Class Symbol`)).toThrow(
/Value is not a valid IPC Class Symbol/,
);
/* NOTE: These tests should be passing or failing due to their value, but are failing due to createGraphQLError
for (const className of lowercaseIPC) {
expect(GraphQLIPCPatent.serialize(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect values with no space
for (const className of noSpaceIPC) {
expect(GraphQLIPCPatent.serialize(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with letters outside valid range
for (const className of outsideLetterRangeIPC) {
expect(GraphQLIPCPatent.serialize(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with two many digits before "/"
for (const className of tooManyDigitsBeforeIPC) {
expect(GraphQLIPCPatent.serialize(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
} */
for (const className of lowercaseIPC) {
expect(IPC_PATENT_REGEX.test(className)).toBeFalsy();
}
// Incorrect values with no space
for (const className of noSpaceIPC) {
expect(IPC_PATENT_REGEX.test(className)).toBeFalsy();
}
// Incorrect value with letters outside valid range
for (const className of outsideLetterRangeIPC) {
expect(IPC_PATENT_REGEX.test(className)).toBeFalsy();
}
// Incorrect value with two many digits before "/"
for (const className of tooManyDigitsBeforeIPC) {
expect(IPC_PATENT_REGEX.test(className)).toBeFalsy();
}
});
it(`parseValue`, () => {
expect(() => GraphQLIPCPatent.parseValue(123)).toThrow(/Value is not string/);
expect(() => GraphQLIPCPatent.parseValue(`this is not an IPC Class Symbol`)).toThrow(
/Value is not a valid IPC Class Symbol/,
);
// Incorrect lowercase values
/*for (const className of lowercaseIPC) {
expect(GraphQLIPCPatent.parseValue(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect values with no space
for (const className of noSpaceIPC) {
expect(GraphQLIPCPatent.parseValue(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with letters outside valid range
for (const className of outsideLetterRangeIPC) {
expect(GraphQLIPCPatent.parseValue(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with two many digits before "/"
for (const className of tooManyDigitsBeforeIPC) {
expect(GraphQLIPCPatent.parseValue(className)).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
} */
});
it(`parseLiteral`, () => {
expect(() =>
GraphQLIPCPatent.parseLiteral({ value: 123, kind: Kind.INT } as any, {}),
).toThrow(/Can only validate strings as an IPC Class Symbol but got a/);
expect(() =>
GraphQLIPCPatent.parseLiteral(
{ value: `this is not an ipv4 address`, kind: Kind.STRING },
{},
),
).toThrow(/Value is not a valid IPC Class Symbol/);
// Incorrect lowercase values
/*for (const className of lowercaseIPC) {
expect(GraphQLIPCPatent.parseLiteral({value: className, kind: Kind.STRING}, {})).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect values with no space
for (const className of noSpaceIPC) {
expect(GraphQLIPCPatent.parseLiteral({value: className, kind: Kind.STRING}, {})).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with letters outside valid range
for (const className of outsideLetterRangeIPC) {
expect(GraphQLIPCPatent.parseLiteral({value: className, kind: Kind.STRING}, {})).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
}
// Incorrect value with two many digits before "/"
for (const className of tooManyDigitsBeforeIPC) {
expect(GraphQLIPCPatent.parseLiteral({value: className, kind: Kind.STRING}, {})).toThrow(`Value is not a valid IPC Class Symbol: ${className}`);
} */
});
});
});
});