Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setup(self):
self.teardown()
if self._connection.db_config.scheme == 'sqlite':
query(
sql='CREATE TABLE %s (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(256), created_at timestamp NULL, updated_at timestamp NULL, deleted_at timestamp NULL, destroyed_at timestamp NULL, num decimal);' % self._model._table_name(),
db='jardin_test'
)
else:
query(
sql='CREATE TABLE %s (id serial PRIMARY KEY, name varchar(256), created_at timestamp NULL, updated_at timestamp NULL, deleted_at timestamp NULL, destroyed_at timestamp NULL, num decimal);' % self._model._table_name(),
db='jardin_test'
)
def test_pg_array(self):
User.clear_caches()
query(
'CREATE TABLE users (id SERIAL PRIMARY KEY, ids TEXT ARRAY);',
db='jardin_test'
)
self.assertEqual(User.count(), 0)
User.insert(
values={'ids': ['a', 'b', 'c']}
)
self.assertEqual(User.count(), 1)
def test_pg_jsonb_list(self):
User.clear_caches()
query(
'CREATE TABLE users (id SERIAL PRIMARY KEY, ids JSONB);',
db='jardin_test'
)
self.assertEqual(User.count(), 0)
User.insert(
values={'ids': ['a', 'b', 'c']}
)
self.assertEqual(User.count(), 1)
def query(self, sql=None, filename=None, **kwargs):
""" run raw sql from sql or file against.
:param sql: Raw SQL query to pass directly to the connection.
:type sql: string
:param filename: Path to a file containing a SQL query. The path should be relative to CWD.
:type filename: string
:param db: `optional` Database name from your ``jardin_conf.py``, overrides the default database set in the model declaration.
:type db: string
:param role: `optional` One of ``('master', 'replica')`` to override the default.
:type role: string
:returns: ``jardin.Collection`` collection, which is a ``pandas.DataFrame``.
"""
results = query(
sql=sql,
filename=filename,
db=self.db_names[kwargs.get('role', 'replica')],
**kwargs
)
if results is None:
return None
else:
return self.collection_instance(results)