Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F73458980
webinterface_tests.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jul 21, 10:30
Size
4 KB
Mime Type
text/x-python
Expires
Tue, Jul 23, 10:30 (2 d)
Engine
blob
Format
Raw Data
Handle
19125628
Attached To
R3600 invenio-infoscience
webinterface_tests.py
View Options
## $Id$
##
## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 CERN.
##
## CDS Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""Unit tests for the webinterface module."""
__revision__
=
"$Id$"
import
unittest
,
sys
,
cgi
# SLIPPERY SLOPE AHEAD
#
# Trick mod_python into believing there is already an _apache module
# available, which is used only for its parse_qs functions anyway.
#
# This must be done early, as many imports somehow end up importing
# apache in turn, which makes the trick useless.
class
_FakeApache
(
object
):
SERVER_RETURN
=
'RETURN'
def
__init__
(
self
):
self
.
table
=
None
self
.
log_error
=
None
self
.
table
=
None
self
.
config_tree
=
None
self
.
server_root
=
None
self
.
mpm_query
=
None
self
.
exists_config_define
=
None
self
.
stat
=
None
self
.
AP_CONN_UNKNOWN
=
None
self
.
AP_CONN_CLOSE
=
None
self
.
AP_CONN_KEEPALIVE
=
None
self
.
APR_NOFILE
=
None
self
.
APR_REG
=
None
self
.
APR_DIR
=
None
self
.
APR_CHR
=
None
self
.
APR_BLK
=
None
self
.
APR_PIPE
=
None
self
.
APR_LNK
=
None
self
.
APR_SOCK
=
None
self
.
APR_UNKFILE
=
None
def
parse_qs
(
self
,
*
args
,
**
kargs
):
return
cgi
.
parse_qs
(
*
args
,
**
kargs
)
def
parse_qsl
(
self
,
*
args
,
**
kargs
):
return
cgi
.
parse_qsl
(
*
args
,
**
kargs
)
class
_FakeReq
(
object
):
def
__init__
(
self
,
q
):
self
.
args
=
q
self
.
method
=
"GET"
return
_current_module
=
sys
.
modules
.
get
(
'mod_python._apache'
)
sys
.
modules
[
'mod_python._apache'
]
=
_FakeApache
()
from
mod_python.util
import
FieldStorage
if
_current_module
:
sys
.
modules
[
'mod_python._apache'
]
=
_current_module
else
:
del
sys
.
modules
[
'mod_python._apache'
]
# --------------------------------------------------
from
invenio
import
webinterface_handler
from
invenio.config
import
CFG_SITE_LANG
class
TestWashArgs
(
unittest
.
TestCase
):
"""webinterface - Test for washing of URL query arguments"""
def
_check
(
self
,
query
,
default
,
expected
):
req
=
_FakeReq
(
query
)
form
=
FieldStorage
(
req
,
keep_blank_values
=
True
)
result
=
webinterface_handler
.
wash_urlargd
(
form
,
default
)
if
not
'ln'
in
expected
:
expected
[
'ln'
]
=
CFG_SITE_LANG
self
.
failUnlessEqual
(
result
,
expected
)
def
test_single_string
(
self
):
""" webinterface - check retrieval of a single string field """
default
=
{
'c'
:
(
str
,
'default'
)}
self
.
_check
(
'c=Test1'
,
default
,
{
'c'
:
'Test1'
})
self
.
_check
(
'd=Test1'
,
default
,
{
'c'
:
'default'
})
self
.
_check
(
'c=Test1&c=Test2'
,
default
,
{
'c'
:
'Test1'
})
def
test_string_list
(
self
):
""" webinterface - check retrieval of a list of values """
default
=
{
'c'
:
(
list
,
[
'default'
])}
self
.
_check
(
'c=Test1'
,
default
,
{
'c'
:
[
'Test1'
]})
self
.
_check
(
'c=Test1&c=Test2'
,
default
,
{
'c'
:
[
'Test1'
,
'Test2'
]})
self
.
_check
(
'd=Test1'
,
default
,
{
'c'
:
[
'default'
]})
def
test_int_casting
(
self
):
""" webinterface - check casting into an int. """
default
=
{
'jrec'
:
(
int
,
-
1
)}
self
.
_check
(
'jrec=12'
,
default
,
{
'jrec'
:
12
})
self
.
_check
(
'jrec='
,
default
,
{
'jrec'
:
-
1
})
self
.
_check
(
'jrec=foo'
,
default
,
{
'jrec'
:
-
1
})
self
.
_check
(
'jrec=foo&jrec=1'
,
default
,
{
'jrec'
:
-
1
})
self
.
_check
(
'jrec=12&jrec=foo'
,
default
,
{
'jrec'
:
12
})
def
create_test_suite
():
"""Return test suite for the search engine."""
return
unittest
.
TestSuite
((
unittest
.
makeSuite
(
TestWashArgs
),))
if
__name__
==
"__main__"
:
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
create_test_suite
())
Event Timeline
Log In to Comment