]> pd.if.org Git - uuid/blob - postgres/t/uuidtest.sql
Added tap tests for postgres extension.
[uuid] / postgres / t / uuidtest.sql
1 set client_min_messages to warning;
2 \set ECHO
3 \set QUIET 1
4 -- Turn off echo and keep things quiet.
5
6 -- Format the output for nice TAP.
7 \pset format unaligned
8 \pset tuples_only true
9 \pset pager
10
11 -- Revert all changes on failure.
12 \set ON_ERROR_ROLLBACK 1
13 \set ON_ERROR_STOP true
14
15 begin;
16
17 select plan(33);
18
19 select lives_ok('create temp table uuidtest (id uuid primary key)', 'create temp table');
20 select lives_ok('insert into uuidtest select uuid_v4()', 'generate v4 uuid');
21 select is(uuid_recent(), (select * from uuidtest), 'uuid_recent works');
22 select lives_ok('select uuid_v4()', 'generate v4 uuid');
23 select isnt(uuid_recent(), (select * from uuidtest), 'uuid_recent still works');
24
25 --Generation Functions:
26
27 select lives_ok('select uuid_v1()', 'generate v1 uuid');
28 select lives_ok('select uuid_v1mc()', 'generate v1 random mac uuid');
29 select lives_ok(E'select uuid_v3(uuid_recent(), \'llll\'::text)', 'generate v3 uuid');
30 select lives_ok('select uuid_v4()', 'generate v4 uuid');
31 select lives_ok(E'select uuid_v5(uuid_recent(), \'llll\'::text)', 'generate v5 uuid');
32 select lives_ok(E'select uuid_url(\'http://pd.if.org\'::text)', 'generate url uuid');
33 select lives_ok(E'select uuid_dns(\'pd.if.org\'::text)', 'generate url uuid');
34 select lives_ok(E'select uuid_oid(\'2.25\'::text)', 'generate url uuid');
35
36 --uuid_x500(text)
37 --Returns a version 5 uuid using the DNS namespace.
38
39 -- lookups
40 select is(uuid_nil(), '00000000-0000-0000-0000-000000000000'::uuid, 'get nil uuid');
41 select is(uuid_ns_dns(), '6ba7b810-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get dns uuid');
42 select is(uuid_ns_url(), '6ba7b811-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get url uuid');
43 select is(uuid_ns_oid(), '6ba7b812-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get oid uuid');
44 select is(uuid_ns_x500(), '6ba7b813-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get x500 uuid');
45
46 -- casts
47 select has_cast('uuid','numeric');
48 select has_cast('uuid','bytea');
49 select has_cast('uuid','bit');
50 select has_cast('uuid','bit varying');
51 select has_cast('numeric', 'uuid');
52 select has_cast('bytea', 'uuid');
53 select has_cast('bit','uuid');
54 select has_cast('bit varying','uuid');
55 select is(uuid_ns_dns()::bytea, '\x6ba7b8109dad11d180b400c04fd430c8'::bytea, 'bytea cast');
56 select is('\x6ba7b8109dad11d180b400c04fd430c8'::bytea::uuid, uuid_ns_dns(), 'bytea uncast');
57
58 select is(uuid_recent()::numeric::uuid, uuid_recent(), 'reversible numeric cast');
59 select is(uuid_recent()::bytea::uuid, uuid_recent(), 'reversible bytea cast');
60 select is(uuid_recent()::bit(128)::uuid, uuid_recent(), 'reversible bit(128) cast');
61 select is(uuid_recent()::bit varying::uuid, uuid_recent(), 'reversible bit varying cast');
62
63 -- create a whole bunch of timestamp uuids
64 select lives_ok('insert into uuidtest select uuid_v1() from generate_series(1,10000)', '10000 unique');
65
66 select * from finish();
67
68 rollback;
69 \q
70
71 functions in purepguuid.sql.
72
73 UUID Lookup Functions:
74
75 uuid_recent()
76 returns the most recent uuid generated by this backend.  Returns
77 the nil uuid if no uuids have been generated yet.
78
79 Numeric: treats the UUID as a 128 bit number and converts it to a numeric.
80
81 Bit(128) and bit varying: treats the uuid as a 128 bit vector.
82 Casts to bit(n) where n is shorter than 128 will truncate
83 the uuid.  Where n is longer than 128, the bit vector
84 will be zero extended.
85
86 Field extraction functions:
87
88 uuid_version(uuid)
89 Returns an integer corresponding to the 4 bit version number.
90
91 uuid_macaddr(uuid)
92 Returns a macaddr type corresponding to the mac address part of the
93 uuid.  This function can be called on any uuid, not just version 1 uuids.
94
95 uuid_timestamp(uuid)
96 Returns a timestamp corresponding to the timestamp field in the uuid.  You
97 probably want to use uuid_timestamptz().
98
99 uuid_timestamptz(uuid)
100 Returns a timestamp with time zonecorresponding to the timestamp field in the
101 uuid.