Skip to content

Commit

Permalink
test the single statement function
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Aug 28, 2012
1 parent 3c3ac9e commit 6971fd8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckanext/datastore/db.py
Expand Up @@ -464,7 +464,7 @@ def format_results(context, results, data_dict):
return data_dict

def is_single_statement(sql):
return True
return not ';' in sql.strip(';')

def create(context, data_dict):
'''
Expand Down
53 changes: 53 additions & 0 deletions ckanext/datastore/tests/test_datastore.py
Expand Up @@ -707,3 +707,56 @@ def test_search_full_text(self):
assert res_dict['result']['total'] == 2, pprint.pformat(res_dict)


class TestDatastoreSQL(tests.WsgiAppCase):
sysadmin_user = None
normal_user = None

@classmethod
def setup_class(cls):
p.load('datastore')
ctd.CreateTestData.create()
cls.sysadmin_user = model.User.get('testsysadmin')
cls.normal_user = model.User.get('annafan')
resource = model.Package.get('annakarenina').resources[0]
cls.data = {
'resource_id': resource.id,
'fields': [{'id': u'b\xfck', 'type': 'text'},
{'id': 'author', 'type': 'text'},
{'id': 'published'}],
'records': [{u'b\xfck': 'annakarenina', 'author': 'tolstoy', 'published': '2005-03-01', 'nested': ['b', {'moo': 'moo'}]},
{u'b\xfck': 'warandpeace', 'author': 'tolstoy', 'nested': {'a':'b'}}
]
}
postparams = '%s=1' % json.dumps(cls.data)
auth = {'Authorization': str(cls.sysadmin_user.apikey)}
res = cls.app.post('/api/action/datastore_create', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['success'] is True

cls.expected_records = [{u'published': u'2005-03-01T00:00:00',
u'_id': 1,
u'nested': [u'b', {u'moo': u'moo'}], u'b\xfck': u'annakarenina', u'author': u'tolstoy'},
{u'published': None,
u'_id': 2,
u'nested': {u'a': u'b'}, u'b\xfck': u'warandpeace', u'author': u'tolstoy'}]

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

def test_is_single_statement(self):
singles = ['SELECT * FROM footable',
'SELECT * FROM "bartable"',
'SELECT * FROM "bartable";',
"select 'foo'||chr(59)||'bar'"]

for single in singles:
assert db.is_single_statement(single) is True

multiples = ['SELECT * FROM abc; SET LOCAL statement_timeout to'
'SET LOCAL statement_timeout to; SELECT * FROM abc',
'SELECT * FROM "foo"; SELECT * FROM "abc"']

for multiple in multiples:
assert db.is_single_statement(multiple) is False

0 comments on commit 6971fd8

Please sign in to comment.