Skip to content

Commit 9308ba9

Browse files
authored
Fix test failures (#65)
1 parent c4ed76e commit 9308ba9

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

sphinxcontrib/websupport/builder.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import html
6+
import os
57
from os import path
68
import posixpath
79
import shutil
@@ -54,7 +56,7 @@ def init(self) -> None:
5456
raise RuntimeError('websupport builder must be used with '
5557
'the builtin templates')
5658
# add our custom JS
57-
self.script_files.append('_static/websupport.js')
59+
self.add_js_file('websupport.js')
5860

5961
@property
6062
def versioning_method(self):
@@ -133,6 +135,41 @@ def pathto(otheruri: str, resource: bool = False,
133135
self.add_sidebars(pagename, ctx)
134136
ctx.update(addctx)
135137

138+
def css_tag(css) -> str:
139+
attrs = []
140+
for key, value in css.attributes.items():
141+
if value is not None:
142+
attrs.append(f'{key}="{html.escape(value, quote=True)}"')
143+
uri = pathto(os.fspath(css.filename), resource=True)
144+
return f'<link {" ".join(sorted(attrs))} href="{uri}" />'
145+
146+
ctx['css_tag'] = css_tag
147+
148+
def js_tag(js) -> str:
149+
if not hasattr(js, 'filename'):
150+
# str value (old styled)
151+
return f'<script src="{pathto(js, resource=True)}"></script>'
152+
153+
attrs = []
154+
body = js.attributes.get('body', '')
155+
for key, value in js.attributes.items():
156+
if key == 'body':
157+
continue
158+
if value is not None:
159+
attrs.append(f'{key}="{html.escape(value, quote=True)}"')
160+
161+
if not js.filename:
162+
if attrs:
163+
return f'<script {" ".join(sorted(attrs))}>{body}</script>'
164+
return f'<script>{body}</script>'
165+
166+
uri = pathto(os.fspath(js.filename), resource=True)
167+
if attrs:
168+
return f'<script {" ".join(sorted(attrs))} src="{uri}"></script>'
169+
return f'<script src="{uri}"></script>'
170+
171+
ctx['js_tag'] = js_tag
172+
136173
newtmpl = self.app.emit_firstresult('html-page-context', pagename,
137174
templatename, ctx, event_arg)
138175
if newtmpl:

sphinxcontrib/websupport/storage/sqlalchemy_db.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
from sqlalchemy import Column, Integer, Text, String, Boolean, \
1111
ForeignKey, DateTime
12-
from sqlalchemy.orm import relation, sessionmaker, aliased
13-
from sqlalchemy.ext.declarative import declarative_base
12+
from sqlalchemy.orm import relationship, sessionmaker, aliased, \
13+
declarative_base
1414

1515
Base = declarative_base()
1616
Session = sessionmaker()
@@ -127,10 +127,10 @@ class Comment(Base): # type: ignore
127127
path = Column(String(256), index=True)
128128

129129
node_id = Column(String(32), ForeignKey(db_prefix + 'nodes.id'))
130-
node = relation(Node, backref="comments")
130+
node = relationship(Node, backref="comments")
131131

132-
votes = relation(CommentVote, backref="comment",
133-
cascade="all")
132+
votes = relationship(CommentVote, backref="comment",
133+
cascade="all")
134134

135135
def __init__(self, text, displayed, username, rating, time,
136136
proposal, proposal_diff):

sphinxcontrib/websupport/storage/sqlalchemystorage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
Comment, CommentVote, Session
1414
from sphinxcontrib.websupport.storage.differ import CombinedHtmlDiff
1515

16-
if sqlalchemy.__version__[:3] < '0.5': # type: ignore
17-
raise ImportError('SQLAlchemy version 0.5 or greater is required for this '
16+
if sqlalchemy.__version__[:3] < '1.4': # type: ignore
17+
raise ImportError('SQLAlchemy version 1.4 or greater is required for this '
1818
'storage backend; you have version %s' % sqlalchemy.__version__)
1919

2020

@@ -26,7 +26,7 @@ class SQLAlchemyStorage(StorageBackend):
2626
def __init__(self, uri):
2727
self.engine = sqlalchemy.create_engine(uri)
2828
Base.metadata.bind = self.engine
29-
Base.metadata.create_all()
29+
Base.metadata.create_all(bind=self.engine)
3030
Session.configure(bind=self.engine)
3131

3232
def pre_build(self):
@@ -109,7 +109,7 @@ def get_metadata(self, docname, moderator):
109109
func.count('*').label('comment_count')).group_by(
110110
Comment.node_id).subquery()
111111
nodes = session.query(Node.id, subquery.c.comment_count).outerjoin(
112-
(subquery, Node.id == subquery.c.node_id)).filter(
112+
subquery, Node.id == subquery.c.node_id).filter(
113113
Node.document == docname)
114114
session.close()
115115
session.commit()

tests/test_searchadapters.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
"""Test the Web Support Package search adapters."""
22

3-
import shutil
43
from io import StringIO
54

6-
import pytest
7-
85
from sphinxcontrib.websupport import WebSupport
96

107
from test_websupport import skip_if_sqlalchemy_missing
118
from util import skip_unless_importable
129

1310

14-
def teardown_module(tmp_path):
15-
shutil.rmtree(tmp_path / 'websupport', ignore_errors=True)
16-
17-
18-
@pytest.fixture
1911
def search_adapter_helper(rootdir, tmp_path, adapter):
2012
support = WebSupport(
2113
srcdir=rootdir / 'test-searchadapters',
@@ -61,5 +53,5 @@ def test_xapian(rootdir, tmp_path):
6153

6254
@skip_unless_importable('whoosh', 'needs whoosh package installed')
6355
@skip_if_sqlalchemy_missing
64-
def test_whoosh(rootdir, tmp_path, adapter):
56+
def test_whoosh(rootdir, tmp_path):
6557
search_adapter_helper(rootdir, tmp_path, 'whoosh')

tests/test_websupport.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
@pytest.fixture
2525
def support(rootdir, tmp_path, request):
2626
settings = {
27-
'srcdir': rootdir / 'test-root',
27+
'srcdir': rootdir / 'test-root' / 'root',
2828
# to use same directory for 'builddir' in each 'support' fixture, using
2929
# 'tempdir' (static) value instead of 'tempdir' fixture value.
3030
# each test expect result of db value at previous test case.
@@ -61,6 +61,7 @@ def test_build(support):
6161
@skip_if_sqlalchemy_missing
6262
@with_support()
6363
def test_get_document(support):
64+
support.build()
6465
with pytest.raises(DocumentNotFoundError):
6566
support.get_document('nonexisting')
6667

@@ -72,6 +73,7 @@ def test_get_document(support):
7273
@skip_if_sqlalchemy_missing
7374
@with_support()
7475
def test_comments(support):
76+
support.build()
7577
session = Session()
7678
nodes = session.query(Node).all()
7779
first_node = nodes[0]
@@ -127,6 +129,7 @@ def get_comment():
127129
session.close()
128130
return support.get_data(node.id)['comments'][0]
129131

132+
test_comments(support)
130133
comment = get_comment()
131134
assert comment['username'] == 'user_one'
132135
# Make sure other normal users can't delete someone elses comments.
@@ -150,6 +153,7 @@ def moderation_callback(comment):
150153
@skip_if_sqlalchemy_missing
151154
@with_support(moderation_callback=moderation_callback)
152155
def test_moderation(support):
156+
support.build()
153157
session = Session()
154158
nodes = session.query(Node).all()
155159
node = nodes[7]
@@ -182,6 +186,7 @@ def get_comment():
182186
session.close()
183187
return support.get_data(node.id, moderator=True)['comments'][1]
184188

189+
test_comments(support)
185190
comment = get_comment()
186191
support.delete_comment(comment['id'], username='user_two',
187192
moderator=True)
@@ -192,6 +197,7 @@ def get_comment():
192197
@skip_if_sqlalchemy_missing
193198
@with_support()
194199
def test_update_username(support):
200+
test_comments(support)
195201
support.update_username('user_two', 'new_user_two')
196202
session = Session()
197203
comments = session.query(Comment).\
@@ -211,6 +217,7 @@ def test_update_username(support):
211217
@skip_if_sqlalchemy_missing
212218
@with_support()
213219
def test_proposals(support):
220+
support.build()
214221
session = Session()
215222
node = session.query(Node).first()
216223

@@ -227,6 +234,7 @@ def test_proposals(support):
227234
@skip_if_sqlalchemy_missing
228235
@with_support()
229236
def test_voting(support):
237+
test_comments(support)
230238
session = Session()
231239
nodes = session.query(Node).all()
232240
node = nodes[0]

0 commit comments

Comments
 (0)