Skip to content

Commit

Permalink
JSONSource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thuydotm committed Aug 25, 2022
1 parent a53752a commit 8c50ba6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions lumen/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ class JSONSource(FileSource):
source_type = 'json'

def _resolve_template_vars(self, template):
template = str(template)
template_vars = self._template_re.findall(template)
template_values = []
for m in template_vars:
Expand Down
18 changes: 17 additions & 1 deletion lumen/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from bokeh.document import Document

from lumen.config import config
from lumen.sources import FileSource, Source
from lumen.sources import FileSource, JSONSource, Source
from lumen.state import state
from lumen.variables import Variables

Expand All @@ -37,6 +37,22 @@ def create(root, **kwargs):
source.clear_cache()
state.global_sources.clear()


@pytest.fixture
def make_jsonsource():
root = config._root
def create(root, **kwargs):
config._root = root
source = JSONSource(tables={'local': './test.json'}, **kwargs)
state.sources['original'] = source
return source
yield create
config._root = root
for source in state.global_sources.values():
source.clear_cache()
state.global_sources.clear()


@pytest.fixture
def make_variable_filesource():
root = config._root
Expand Down
1 change: 1 addition & 0 deletions lumen/tests/sources/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"A":{"0":0.0,"1":1.0,"2":2.0,"3":3.0,"4":4.0},"B":{"0":0.0,"1":1.0,"2":0.0,"3":1.0,"4":0.0},"C":{"0":"foo1","1":"foo2","2":"foo3","3":"foo4","4":"foo5"},"D":{"0":"2009-01-01","1":"2009-01-02","2":"2009-01-05","3":"2009-01-06","4":"2009-01-07"}}
2 changes: 0 additions & 2 deletions lumen/tests/sources/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def test_file_source_table_cache_key(source, table, filter_col_A, filter_col_B,
"table_column_value_type", [
('test', 'A', 1, 'single_value'),
('test', 'A', (1, 3), 'range'),
('test', 'A', (1, 2), 'range'),
('test', 'A', [(0, 1), (3, 4)], 'range_list'),
('test', 'C', 'foo2', 'single_value'),
('test', 'C', ['foo1', 'foo3'], 'list'),
Expand All @@ -76,7 +75,6 @@ def test_file_source_filter(source, table_column_value_type, dask, expected_filt
"table_column_value_type", [
('test', 'A', 1, 'single_value'),
('test', 'A', (1, 3), 'range'),
('test', 'A', (1, 2), 'range'),
('test', 'A', [(0, 1), (3, 4)], 'range_list'),
('test', 'C', 'foo2', 'single_value'),
('test', 'C', ['foo1', 'foo3'], 'list'),
Expand Down
48 changes: 48 additions & 0 deletions lumen/tests/sources/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os

import pandas as pd
import pytest

from lumen.sources import JSONSource

from .utils import source_filter, source_get_cache_no_query


@pytest.fixture
def source(make_jsonsource):
root = os.path.dirname(__file__)
return make_jsonsource(root)


@pytest.fixture
def source_tables():
df = pd._testing.makeMixedDataFrame()
df['D'] = pd.to_datetime(df['D']).dt.date
df = df.astype({'A': int, 'B': int, "D": 'str'})
return {'local': df}


def test_json_source_resolve_module_type():
assert JSONSource._get_type('lumen.sources.JSONSource') is JSONSource
assert JSONSource.source_type == 'json'


@pytest.mark.parametrize(
"table_column_value_type", [
('local', 'A', 1.0, 'single_value'),
('local', 'A', (1, 3), 'range'),
('local', 'A', [(0, 1), (3, 4)], 'range_list'),
('local', 'C', 'foo2', 'single_value'),
('local', 'C', ['foo1', 'foo3'], 'list'),
]
)
@pytest.mark.parametrize("dask", [True, False])
def test_json_source_filter(source, table_column_value_type, dask, expected_filtered_df):
assert source_filter(source, table_column_value_type, dask, expected_filtered_df)


@pytest.mark.parametrize("dask", [True, False])
def test_file_source_get_cache_no_query(source, dask, source_tables):
assert source_get_cache_no_query(
source, 'local', source_tables['local'], dask, use_dask=False
)

0 comments on commit 8c50ba6

Please sign in to comment.