Debian Codegraph @

contrib/isn/sql/isn.sql

raw
1 --
2 -- Test ISN extension
3 --
4
5 CREATE EXTENSION isn;
6
7 -- Check whether any of our opclasses fail amvalidate
8 -- ... they will, because of missing cross-type operators
9 SELECT amname, opcname
10 FROM (SELECT amname, opcname, opc.oid
11 FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
12 WHERE opc.oid >= 16384
13 ORDER BY 1, 2 OFFSET 0) ss
14 WHERE NOT amvalidate(oid);
15
16 --
17 -- test valid conversions
18 --
19 SELECT '9780123456786'::EAN13, -- old book
20 '9790123456785'::EAN13, -- music
21 '9791234567896'::EAN13, -- new book
22 '9771234567898'::EAN13, -- serial
23 '0123456789012'::EAN13, -- upc
24 '1234567890128'::EAN13;
25
26 SELECT '9780123456786'::ISBN,
27 '123456789X'::ISBN,
28 '9780123456786'::ISBN13::ISBN,
29 '9780123456786'::EAN13::ISBN;
30
31 SELECT -- new books, shown as ISBN13 even for ISBN...
32 '9791234567896'::ISBN,
33 '9791234567896'::ISBN13::ISBN,
34 '9791234567896'::EAN13::ISBN;
35
36 SELECT '9780123456786'::ISBN13,
37 '123456789X'::ISBN13,
38 '9791234567896'::ISBN13,
39 '9791234567896'::EAN13::ISBN13;
40
41 SELECT '9790123456785'::ISMN,
42 '9790123456785'::EAN13::ISMN,
43 'M123456785'::ISMN,
44 'M-1234-5678-5'::ISMN;
45
46 SELECT '9790123456785'::ISMN13,
47 'M123456785'::ISMN13,
48 'M-1234-5678-5'::ISMN13;
49
50 SELECT '9771234567003'::ISSN,
51 '12345679'::ISSN;
52
53 SELECT '9771234567003'::ISSN13,
54 '12345679'::ISSN13,
55 '9771234567898'::ISSN13,
56 '9771234567898'::EAN13::ISSN13;
57
58 SELECT '0123456789012'::UPC,
59 '0123456789012'::EAN13::UPC;
60
61 --
62 -- test invalid checksums
63 --
64 SELECT '1234567890'::ISBN;
65 SELECT 'M123456780'::ISMN;
66 SELECT '12345670'::ISSN;
67 SELECT '9780123456780'::ISBN;
68 SELECT '9791234567890'::ISBN13;
69 SELECT '0123456789010'::UPC;
70 SELECT '1234567890120'::EAN13;
71
72 --
73 -- test invalid conversions
74 --
75 SELECT '9790123456785'::ISBN; -- not a book
76 SELECT '9771234567898'::ISBN; -- not a book
77 SELECT '0123456789012'::ISBN; -- not a book
78
79 SELECT '9790123456785'::ISBN13; -- not a book
80 SELECT '9771234567898'::ISBN13; -- not a book
81 SELECT '0123456789012'::ISBN13; -- not a book
82
83 SELECT '9780123456786'::ISMN; -- not music
84 SELECT '9771234567898'::ISMN; -- not music
85 SELECT '9791234567896'::ISMN; -- not music
86 SELECT '0123456789012'::ISMN; -- not music
87
88 SELECT '9780123456786'::ISSN; -- not serial
89 SELECT '9790123456785'::ISSN; -- not serial
90 SELECT '9791234567896'::ISSN; -- not serial
91 SELECT '0123456789012'::ISSN; -- not serial
92
93 SELECT '9780123456786'::UPC; -- not a product
94 SELECT '9771234567898'::UPC; -- not a product
95 SELECT '9790123456785'::UPC; -- not a product
96 SELECT '9791234567896'::UPC; -- not a product
97
98 SELECT 'postgresql...'::EAN13;
99 SELECT 'postgresql...'::ISBN;
100 SELECT 9780123456786::EAN13;
101 SELECT 9780123456786::ISBN;
102
103 --
104 -- test some comparisons, must yield true
105 --
106 SELECT '12345679'::ISSN = '9771234567003'::EAN13 AS "ok",
107 'M-1234-5678-5'::ISMN = '9790123456785'::EAN13 AS "ok",
108 '9791234567896'::EAN13 != '123456789X'::ISBN AS "nope";
109
110 -- test non-error-throwing input API
111 SELECT str as isn, typ as "type",
112 pg_input_is_valid(str,typ) as ok,
113 errinfo.sql_error_code,
114 errinfo.message,
115 errinfo.detail,
116 errinfo.hint
117 FROM (VALUES ('9780123456786', 'UPC'),
118 ('postgresql...','EAN13'),
119 ('9771234567003','ISSN'))
120 AS a(str,typ),
121 LATERAL pg_input_error_info(a.str, a.typ) as errinfo;
122
123 --
124 -- test weak mode
125 --
126 SELECT '2222222222221'::ean13; -- fail
127 SET isn.weak TO TRUE;
128 SELECT '2222222222221'::ean13;
129 SELECT is_valid('2222222222221'::ean13);
130 SELECT make_valid('2222222222221'::ean13);
131
132 SELECT isn_weak(); -- backwards-compatibility wrappers for accessing the GUC
133 SELECT isn_weak(false);
134 SHOW isn.weak;
135
136 --
137 -- cleanup
138 --
139 DROP EXTENSION isn;