1
+ #coding: utf-8
2
+ """
3
+ Test RUM index with big base 'pglist'
4
+
5
+ Copyright (c) 2015-2016, Postgres Professional
6
+ """
7
+ import unittest
8
+ import os
9
+ import sys
10
+ import gzip
11
+ import testgres as tg
12
+
13
+ if sys .version_info [0 ] < 3 :
14
+ import urllib as request
15
+ else :
16
+ import urllib .request as request
17
+
18
+ from os .path import expanduser
19
+
20
+ class PglistTests (unittest .TestCase ):
21
+ def setUp (self ):
22
+ self .node = tg .get_new_node ("pglist_select" )
23
+ try :
24
+ self .node .init ()
25
+ self .node .append_conf ("postgresql.conf" ,
26
+ "shared_buffers='4GB'\n "
27
+ "maintenance_work_mem='2GB'\n "
28
+ "max_wal_size='2GB'\n "
29
+ "work_mem='50MB'" )
30
+ self .node .start ()
31
+
32
+ self .init_pglist_data (self .node )
33
+ except Exception as e :
34
+ self .printlog (self .node .logs_dir + "/postgresql.log" )
35
+ raise e
36
+
37
+ def tearDown (self ):
38
+ tg .stop_all ()
39
+
40
+ def init_pglist_data (self , node ):
41
+ # Check if 'pglist' base exists
42
+ base_exists = False
43
+ bases = node .execute ("postgres" , "SELECT datname FROM pg_database" )
44
+ for base in bases :
45
+ if base [0 ].lower () == "pglist" :
46
+ base_exists = True
47
+ break
48
+
49
+ if base_exists :
50
+ return
51
+
52
+ # Check if 'pglist' dump exists
53
+ home = expanduser ("~" )
54
+ pglist_dump = os .path .join (home , "pglist-28-04-16.dump" )
55
+ if not os .path .isfile (pglist_dump ):
56
+ pglist_dumpgz = pglist_dump + ".gz"
57
+ if not os .path .isfile (pglist_dumpgz ):
58
+ print ("Downloading: %s" % pglist_dumpgz )
59
+ request .urlretrieve ("http://www.sai.msu.su/~megera/postgres/files/pglist-28-04-16.dump.gz" ,
60
+ pglist_dumpgz )
61
+
62
+ print ("Decompressing: %s" % pglist_dumpgz )
63
+ gz = gzip .open (pglist_dumpgz , 'rb' )
64
+ with open (pglist_dump , 'wb' ) as f :
65
+ f .write (gz .read ())
66
+
67
+ os .remove (pglist_dumpgz )
68
+
69
+ # Restore dump file
70
+ print ("Restoring 'pglist'" )
71
+ node .safe_psql ("postgres" , "CREATE DATABASE pglist" )
72
+ node .psql ("pglist" , filename = pglist_dump )
73
+
74
+ node .safe_psql ("pglist" , "CREATE EXTENSION rum" )
75
+
76
+ def printlog (self , logfile ):
77
+ with open (logfile , 'r' ) as log :
78
+ for line in log .readlines ():
79
+ print (line )
80
+
81
+ def test_order_by (self ):
82
+ """Tests SELECT constructions to 'pglist' base"""
83
+ try :
84
+ print ("Creating index 'rumidx_orderby_sent'" )
85
+
86
+ self .node .safe_psql ("pglist" ,
87
+ "CREATE INDEX rumidx_orderby_sent ON pglist USING rum ("
88
+ " fts rum_tsvector_timestamp_ops, sent) "
89
+ " WITH (attach=sent, to=fts, order_by_attach=t)" )
90
+
91
+ print ("Running tests" )
92
+
93
+ self .assertEqual (
94
+ self .node .safe_psql (
95
+ "pglist" ,
96
+ "SELECT sent, subject "
97
+ " FROM pglist "
98
+ " WHERE fts @@ to_tsquery('english', 'backend <-> crushed') "
99
+ " ORDER BY sent <=| '2016-01-01 00:01' LIMIT 5"
100
+ ),
101
+ b'1999-06-02 11:52:46|Re: [HACKERS] PID of backend\n '
102
+ )
103
+
104
+ self .assertEqual (
105
+ self .node .safe_psql (
106
+ "pglist" ,
107
+ "SELECT count(*) FROM pglist WHERE fts @@ to_tsquery('english', 'tom & lane')"
108
+ ),
109
+ b'222813\n '
110
+ )
111
+ except Exception as e :
112
+ self .printlog (self .node .logs_dir + "/postgresql.log" )
113
+ raise e
114
+
115
+ if __name__ == "__main__" :
116
+ unittest .main ()
0 commit comments