def test_query_filename_absolute(self): User.insert(values={'name': 'jardin'}) filename = os.path.join(os.environ['PWD'], 'tests/support/select_users.sql') df = jardin.query(filename=filename, db='jardin_test') self.assertEqual(len(df), 1) self.assertEqual(df.name.iloc[0], 'jardin')
def test_query_filename_relative(self): User.insert(values={'name': 'jardin'}) df = jardin.query( filename='tests/support/select_users.sql', db='jardin_test' ) self.assertEqual(len(df), 1) self.assertEqual(df.name.iloc[0], 'jardin')
def test_query_sql(self): User.insert(values={'name': 'jardin'}) df = jardin.query( sql='SELECT * FROM users;', db='jardin_test' ) self.assertEqual(len(df), 1) self.assertEqual(df.name.iloc[0], 'jardin')
def test_query_params(self): if User.db().db_config.scheme == 'sqlite': return User.insert(values={'name': 'jardin'}) df = jardin.query(sql='SELECT * FROM users WHERE name IN %(names)s;', params={'names': ['jardin']}, db='jardin_test') self.assertEqual(len(df), 1)
def test_query_cache_with_s3(s3): with TestTransaction(User): User.insert(values={'name': 'jardin_s3'}) with setup_s3(s3, "JARDIN_BUCKET"): cache = S3(bucket_name="JARDIN_BUCKET", path="cache", delete_expired_files=True) cache.clear() # clear the cache results, columns = [{"a": 1}], ["a"] # when caching is not configured jardin.cache_stores.STORES["s3"] = None with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3") df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3") assert_frame_equal(df1, df2, check_like=True) assert mock_method.call_count == 2 # when caching is configured jardin.cache_stores.STORES["s3"] = cache with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3") df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3") assert_frame_equal(df1, df2, check_like=True) assert mock_method.call_count == 1 cache.clear() # with ttl jardin.cache_stores.STORES["s3"] = cache with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3") time.sleep(2) df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="s3", ttl=1) assert_frame_equal(df1, df2, check_like=True) assert mock_method.call_count == 2
def test_cache_query_with_disk(self): with TestTransaction(User): User.insert(values={'name': 'jardin_disk'}) results, columns = [{"a": 1}], ["a"] # when caching is not configured jardin.cache_stores.STORES["disk"] = None with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") assert_frame_equal(df1, df2, check_like=True) self.assertEqual(mock_method.call_count, 2) # when caching is badly configured with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") assert_frame_equal(df1, df2, check_like=True) self.assertEqual(mock_method.call_count, 2) # when caching is configured jardin.cache_stores.STORES["disk"] = Disk(dir="/tmp/jardin_cache") jardin.cache_stores.STORES["disk"].clear() with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") assert_frame_equal(df1, df2, check_like=True) self.assertEqual(mock_method.call_count, 1) jardin.cache_stores.STORES["disk"].clear() # with ttl with patch.object(BaseClient, 'execute', return_value=(results, columns)) as mock_method: df1 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk") time.sleep(2) df2 = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk", ttl=1) assert_frame_equal(df1, df2, check_like=True) self.assertEqual(mock_method.call_count, 2)
def run_query(): df = jardin.query("select * from users limit 10", db="jardin_test", cache=True, cache_method="disk")