Révision a485f930
Ajouté par Frédéric Péters il y a presque 2 ans
tests/utilities.py | ||
---|---|---|
1 | 1 |
import configparser |
2 | 2 |
import os |
3 |
import random |
|
4 |
import shutil |
|
3 | 5 |
import tempfile |
4 | 6 |
|
7 |
import psycopg2 |
|
8 |
|
|
5 | 9 |
from webtest import TestApp |
6 | 10 |
from quixote import cleanup, get_publisher |
7 | 11 |
from django.conf import settings |
... | ... | |
10 | 14 |
import wcs |
11 | 15 |
import wcs.wsgi |
12 | 16 |
from wcs.qommon.http_request import HTTPRequest |
13 |
from wcs import publisher, compat |
|
17 |
from wcs import publisher, compat, sql
|
|
14 | 18 |
|
15 | 19 |
import wcs.middleware |
16 | 20 |
|
... | ... | |
31 | 35 |
compat.CompatWcsPublisher.configure(config) |
32 | 36 |
compat.CompatWcsPublisher.init_publisher_class() |
33 | 37 |
pub = compat.CompatWcsPublisher.create_publisher() |
34 |
# allow saving the user |
|
35 | 38 |
pub.app_dir = os.path.join(APP_DIR, 'example.net') |
36 | 39 |
os.mkdir(pub.app_dir) |
40 |
|
|
41 |
# set classes |
|
42 |
pub.user_class = sql.SqlUser |
|
43 |
pub.role_class = sql.Role |
|
44 |
pub.token_class = sql.Token |
|
45 |
pub.tracking_code_class = sql.TrackingCode |
|
46 |
pub.session_class = sql.Session |
|
47 |
pub.custom_view_class = sql.CustomView |
|
48 |
pub.snapshot_class = sql.Snapshot |
|
49 |
pub.loggederror_class = sql.LoggedError |
|
50 |
|
|
51 |
conn = psycopg2.connect(user=os.environ['USER'], dbname='postgres') |
|
52 |
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) |
|
53 |
i = 0 |
|
54 |
while True: |
|
55 |
dbname = 'wcstests%d' % random.randint(0, 100000) |
|
56 |
try: |
|
57 |
cur = conn.cursor() |
|
58 |
cur.execute('CREATE DATABASE %s' % dbname) |
|
59 |
break |
|
60 |
except psycopg2.Error: |
|
61 |
if i < 5: |
|
62 |
i += 1 |
|
63 |
continue |
|
64 |
raise |
|
65 |
finally: |
|
66 |
cur.close() |
|
67 |
|
|
68 |
pub.cfg['postgresql'] = {'database': dbname, 'user': os.environ['USER']} |
|
69 |
pub.write_cfg() |
|
70 |
|
|
71 |
sql.do_user_table() |
|
72 |
sql.do_role_table() |
|
73 |
sql.do_tokens_table() |
|
74 |
sql.do_tracking_code_table() |
|
75 |
sql.do_session_table() |
|
76 |
sql.do_transient_data_table() |
|
77 |
sql.do_custom_views_table() |
|
78 |
sql.do_snapshots_table() |
|
79 |
sql.do_loggederrors_table() |
|
80 |
sql.do_meta_table() |
|
81 |
sql.init_global_table() |
|
82 |
conn.close() |
|
83 |
|
|
37 | 84 |
return pub |
38 | 85 |
|
39 | 86 |
|
87 |
def clean_temporary_pub(): |
|
88 |
pub = get_publisher() |
|
89 |
conn = psycopg2.connect(user=os.environ['USER'], dbname='postgres') |
|
90 |
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) |
|
91 |
try: |
|
92 |
cur = conn.cursor() |
|
93 |
cur.execute('DROP DATABASE %s' % pub.cfg['postgresql']['database']) |
|
94 |
cur.close() |
|
95 |
except psycopg2.Error as e: |
|
96 |
print(e) |
|
97 |
shutil.rmtree(pub.app_dir) |
|
98 |
pub.cleanup() |
|
99 |
|
|
100 |
|
|
40 | 101 |
def get_app(pub, https=False): |
41 | 102 |
extra_environ = {'HTTP_HOST': 'example.net', 'REMOTE_ADDR': '127.0.0.1'} |
42 | 103 |
if https: |
Formats disponibles : Unified diff
tests: run against postgresql (#67383)