Skip to content

Commit a189b37

Browse files
Refactor/sql (#9)
1 parent a1cc305 commit a189b37

60 files changed

Lines changed: 4575 additions & 2304 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ on:
77
jobs:
88
run-all-test:
99
runs-on: ubuntu-latest
10+
container:
11+
image: mcr.microsoft.com/playwright:v1.47.0-focal
1012
strategy:
1113
matrix:
1214
python-version:
@@ -22,7 +24,5 @@ jobs:
2224
python-version: ${{ matrix.python-version }}
2325
- name: Install the project
2426
run: uv sync --locked --all-extras --dev
25-
- name: install dependencies
26-
run: uv run -m playwright install --with-deps
2727
- name: Run tests
2828
run: uv run pytest __tests

__tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@pytest.fixture(scope="session")
1010
def browser():
1111
pw = sync_playwright().start()
12-
browser = pw.chromium.launch(headless=HEADLESS)
12+
browser = pw.chromium.launch(headless=HEADLESS, timeout=3000)
1313
yield browser
1414
browser.close()
1515
pw.stop()

__tests/testing_web/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import duckdb
44
from __tests.testing_web.context import Context
55
from __tests.testing_web.server import TestServer
6+
from __tests.testing_web.memory_db import MemoryDb
67

78

89
@pytest.fixture(scope="function")
@@ -18,6 +19,18 @@ def context(browser: Browser, start_server: TestServer):
1819
page.close()
1920

2021

22+
@pytest.fixture(scope="session")
23+
def global_memory_db():
24+
dataset = MemoryDb()
25+
yield dataset
26+
27+
28+
@pytest.fixture(scope="function")
29+
def memory_db(global_memory_db: MemoryDb):
30+
global_memory_db.clear()
31+
yield global_memory_db
32+
33+
2134
@pytest.fixture(scope="session", autouse=True)
2235
def start_server():
2336
server = TestServer()

__tests/testing_web/memory_db.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Dict
2+
from pybi.link_sql.duckdb_dataset import DuckdbDataFrameDataSet
3+
4+
5+
try:
6+
import pandas
7+
except ImportError as e:
8+
raise e
9+
10+
11+
class MemoryDb:
12+
def __init__(self):
13+
self.ds = DuckdbDataFrameDataSet()
14+
15+
def clear(self):
16+
conn = self.ds._conn
17+
18+
tables = [
19+
n[0]
20+
for n in conn.execute("SELECT table_name FROM duckdb_tables()").fetchall()
21+
]
22+
for table_name in tables:
23+
conn.execute(f"DROP TABLE IF EXISTS {table_name};")
24+
25+
def from_dataframe(
26+
self, dataframes_map: Dict[str, "pandas.DataFrame"]
27+
) -> DuckdbDataFrameDataSet:
28+
self.ds.import_dataframe(dataframes_map)
29+
return self.ds

__tests/testing_web/test_data_by_duckdb.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __tests.testing_web.context import Context
2-
from instaui import ui
32
import pandas as pd
43
import pybi
54
from . import utils
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __tests.testing_web.context import Context
2+
from __tests.testing_web.memory_db import MemoryDb
3+
import pandas as pd
4+
from __tests.utils import display, ListBox
5+
import pybi
6+
7+
8+
def test_distinct_from_data_view(context: Context, memory_db: MemoryDb):
9+
data = {"Name": ["foo", "foo", "bar"], "Age": [18, 19, 20]}
10+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
11+
12+
@context.register_page
13+
def index():
14+
dv1 = dataset["df"]
15+
display.list_box(dv1["Name"].distinct())
16+
17+
context.open()
18+
ListBox(context).should_have_text(["foo", "bar"])
19+
20+
21+
def test_distinct_from_query(context: Context, memory_db: MemoryDb):
22+
data = {"Name": ["foo", "foo", "bar"], "Age": [18, 19, 20]}
23+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
24+
25+
@context.register_page
26+
def index():
27+
dv1 = dataset["df"]
28+
query = pybi.query(f"SELECT Name FROM {dv1}")
29+
display.list_box(query["Name"].distinct())
30+
31+
context.open()
32+
ListBox(context).should_have_text(["foo", "bar"])
33+
34+
35+
def test_flat_values_from_data_view(context: Context, memory_db: MemoryDb):
36+
data = {"Name": ["foo", "bar"], "Age": [18, 19]}
37+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
38+
39+
@context.register_page
40+
def index():
41+
dv1 = dataset["df"]
42+
display.list_box(dv1["Name"].flat_values())
43+
44+
context.open()
45+
ListBox(context).should_have_text(["foo", "bar"])
46+
47+
48+
def test_flat_values_from_query(context: Context, memory_db: MemoryDb):
49+
data = {"Name": ["foo", "bar"], "Age": [18, 19]}
50+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
51+
52+
@context.register_page
53+
def index():
54+
dv1 = dataset["df"]
55+
query = pybi.query(f"SELECT Name FROM {dv1}")
56+
display.list_box(query["Name"].flat_values())
57+
58+
context.open()
59+
ListBox(context).should_have_text(["foo", "bar"])

__tests/testing_web/test_data_view.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __tests.testing_web.context import Context
2+
from __tests.testing_web.memory_db import MemoryDb
23
from instaui import ui
34
import pandas as pd
45
import pybi
5-
from __tests.utils import Table, Select
6+
from __tests.utils import Table, Select, display, ListBox
67

78

8-
def test_base(context: Context):
9+
def test_base(context: Context, memory_db: MemoryDb):
910
data = {"Name": ["foo", "foo", "bar"], "Age": [18, 19, 20]}
10-
11-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
11+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
1212

1313
@context.register_page
1414
def index():
@@ -21,10 +21,11 @@ def index():
2121
Table(context).should_values_any_cell("18.5")
2222

2323

24-
def test_upstream_data_view_update_affects_downstream(context: Context):
24+
def test_upstream_data_view_update_affects_downstream(
25+
context: Context, memory_db: MemoryDb
26+
):
2527
data = {"Name": ["foo", "foo", "bar"], "Age": [18, 19, 20]}
26-
27-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
28+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
2829

2930
@context.register_page
3031
def index():
@@ -47,10 +48,9 @@ def index():
4748
table.should_values_not_any_cell("bar")
4849

4950

50-
def test_selected_multiple_columns(context: Context):
51+
def test_selected_multiple_columns(context: Context, memory_db: MemoryDb):
5152
data = {"Name": ["foo"], "Age": [18], "class": [1]}
52-
53-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
53+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
5454

5555
@context.register_page
5656
def index():
@@ -65,41 +65,35 @@ def index():
6565
table.should_values_not_any_cell("1")
6666

6767

68-
def test_computed_binding(context: Context):
68+
def test_computed_binding(context: Context, memory_db: MemoryDb):
6969
data = {"Name": ["foo", "bar"], "Age": [18, 19]}
70-
71-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
70+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
7271

7372
@context.register_page
7473
def index():
7574
table = dataset["df"]
7675
dv = pybi.data_view(f"SELECT * FROM {table}")
7776

7877
@ui.computed(inputs=[dv])
79-
def result(names):
80-
return str(names)
78+
def value_r0_c0(source):
79+
return source["values"][0][0]
8180

82-
pybi.label(result)
81+
ui.label(value_r0_c0)
8382

8483
context.open()
85-
context.should_see("[['foo', 18], ['bar', 19]]", equal_to=True)
84+
context.should_see("foo")
8685

8786

88-
def test_selected_column_computed_binding(context: Context):
87+
def test_selected_column_computed_binding(context: Context, memory_db: MemoryDb):
8988
data = {"Name": ["foo", "bar"]}
90-
91-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
89+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
9290

9391
@context.register_page
9492
def index():
9593
table = dataset["df"]
9694
dv = pybi.data_view(f"SELECT * FROM {table}")
9795

98-
@ui.computed(inputs=[dv["Name"]])
99-
def result(names):
100-
return str(names)
101-
102-
pybi.label(result)
96+
display.list_box(dv["Name"])
10397

10498
context.open()
105-
context.should_see("['foo', 'bar']", equal_to=True)
99+
ListBox(context).should_have_text(["foo", "bar"])

__tests/testing_web/test_query.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __tests.testing_web.context import Context
2-
from instaui import ui
2+
from __tests.testing_web.memory_db import MemoryDb
33
import pandas as pd
44
import pybi
55
from __tests.utils import Table
66

77

8-
def test_base(context: Context):
8+
def test_base(context: Context, memory_db: MemoryDb):
99
data = {"Name": ["foo", "foo", "bar"], "Age": [18, 19, 20]}
10-
11-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
10+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
1211

1312
@context.register_page
1413
def index():
@@ -21,10 +20,9 @@ def index():
2120
Table(context).should_values_any_cell("18.5")
2221

2322

24-
def test_select_columns(context: Context):
23+
def test_select_columns(context: Context, memory_db: MemoryDb):
2524
data = {"Name": ["foo"], "Age": [18], "class": [1]}
26-
27-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
25+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
2826

2927
@context.register_page
3028
def index():

__tests/testing_web/test_select.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __tests.testing_web.context import Context
2+
from __tests.testing_web.memory_db import MemoryDb
23
from instaui import ui
34
import pandas as pd
45
import pybi
5-
from __tests.utils import Select
6+
from __tests.utils import Select, display, ListBox
67

78

8-
def test_options(context: Context):
9+
def test_options(context: Context, memory_db: MemoryDb):
910
data = {"Name": ["foo", "foo", "bar"]}
10-
11-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
11+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
1212

1313
@context.register_page
1414
def index():
@@ -21,47 +21,40 @@ def index():
2121
select.should_options_have_count(2)
2222
select.should_options_have_text("foo", "bar")
2323

24-
def test_no_option_selected(context: Context):
25-
data = {"Name": ['foo', 'bar']}
26-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
24+
25+
def test_no_option_selected(context: Context, memory_db: MemoryDb):
26+
data = {"Name": ["foo", "bar"]}
27+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
2728

2829
@context.register_page
2930
def index():
3031
table = dataset["df"]
3132
dv = pybi.data_view(f"SELECT * FROM {table}")
3233

33-
@ui.computed(inputs=[dv["Name"]])
34-
def result(names):
35-
return str(names)
36-
3734
pybi.select(dv["Name"])
38-
pybi.label(result)
35+
display.list_box(dv["name"])
3936

4037
context.open()
4138
select = Select(context)
4239
select.should_not_selected_any()
43-
context.should_see("['foo', 'bar']", equal_to=True)
40+
ListBox(context).should_have_text(["foo", "bar"])
4441

45-
def test_selection_impact(context: Context):
46-
data = {"Name":['foo','bar']}
47-
dataset = pybi.duckdb.from_pandas({"df": pd.DataFrame(data)})
42+
43+
def test_selection_impact(context: Context, memory_db: MemoryDb):
44+
data = {"Name": ["foo", "bar"]}
45+
dataset = memory_db.from_dataframe({"df": pd.DataFrame(data)})
4846

4947
@context.register_page
5048
def index():
5149
table = dataset["df"]
5250
dv = pybi.data_view(f"SELECT * FROM {table}")
5351

54-
@ui.computed(inputs=[dv["Name"]])
55-
def result(names):
56-
return str(names)
57-
5852
pybi.select(dv["Name"])
59-
pybi.label(result)
53+
display.list_box(dv["Name"])
6054

6155
context.open()
6256
select = Select(context)
63-
context.should_see("['foo', 'bar']", equal_to=True)
57+
list_box = ListBox(context)
58+
list_box.should_have_text(["foo", "bar"])
6459
select.select_item("foo")
65-
context.should_see("['foo']", equal_to=True)
66-
67-
60+
list_box.should_have_text(["foo"])

__tests/testing_web/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from pathlib import Path
22
import os
3-
import re
43
import pandas as pd
54
import duckdb
65

7-
from __tests.testing_web.context import Context
86

97
__DIR__ = Path(__file__).parent / ".dataset"
108

0 commit comments

Comments
 (0)