Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions pytabular/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,34 @@ def __init__(self, object, table) -> None:
def get_sample_values(self, top_n: int = 3) -> pd.DataFrame:
"""Get sample values of column."""
column_to_sample = f"'{self.Table.Name}'[{self.Name}]"
dax_query = f"""EVALUATE
TOPNSKIP(
{top_n},
0,
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample}) && LEN({column_to_sample}) > 0
),
1
)
ORDER BY {column_to_sample}
"""
return self.Table.Model.Query(dax_query)
try:
# adding temporary try except. TOPNSKIP will not work for directquery mode.
# Need an efficient way to identify if query is direct query or not.
dax_query = f"""EVALUATE
TOPNSKIP(
{top_n},
0,
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample}) && LEN({column_to_sample}) > 0
),
1
)
ORDER BY {column_to_sample}
"""
return self.Table.Model.Query(dax_query)
except Exception:
dax_query = f"""
EVALUATE
TOPN(
{top_n},
FILTER(
VALUES({column_to_sample}),
NOT ISBLANK({column_to_sample}) && LEN({column_to_sample}) > 0
)
)
"""
return self.Table.Model.Query(dax_query)

def Distinct_Count(self, No_Blank=False) -> int:
"""Get [DISTINCTCOUNT](https://learn.microsoft.com/en-us/dax/distinctcount-function-dax) of Column.
Expand Down
4 changes: 2 additions & 2 deletions pytabular/pytabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ def Query(

try:
conn = self.Effective_Users[Effective_User]
if isinstance(conn, Connection):
conn.Query(Query_Str)
logger.debug(f"Effective user found querying as... {Effective_User}")
except Exception:
logger.debug(f"Creating new connection with {Effective_User}")
conn = Connection(self.Server, Effective_User=Effective_User)
self.Effective_Users[Effective_User] = conn

Expand Down
4 changes: 3 additions & 1 deletion pytabular/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def Query(self, Query_Str: str) -> Union[pd.DataFrame, str, int]:
with open(Query_Str, "r") as file:
Query_Str = str(file.read())

if self.State.value__ == 0:
if str(self.get_State()) != "Open":
# Works for now, need to update to handle different types of conneciton properties
# https://learn.microsoft.com/en-us/dotnet/api/system.data.connectionstate?view=net-7.0
logger.info("Checking initial Adomd Connection...")
self.Open()
logger.info(f"Connected! Session ID - {self.SessionID}")
Expand Down
20 changes: 19 additions & 1 deletion test/test_2tabular.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import pandas as pd
import pytabular as p
from test.config import testingtablename, testing_parameters, get_test_path
from test.config import testingtablename, testing_parameters, get_test_path, LOCAL_FILE


@pytest.mark.parametrize("model", testing_parameters)
Expand Down Expand Up @@ -81,3 +81,21 @@ def test_Table_Last_Refresh_Times(model):
def test_Return_Zero_Row_Tables(model):
"""Testing that `Return_Zero_Row_Tables`"""
assert isinstance(p.Return_Zero_Row_Tables(model), list) is True


@pytest.mark.parametrize("model", testing_parameters)
def test_get_dependencies(model):
if len(model.Measures) > 0:
dependencies = model.Measures[0].get_dependencies()
assert len(dependencies) > 0
else:
assert True


@pytest.mark.parametrize("model", testing_parameters)
def test_get_sample_values(model):
if LOCAL_FILE[0] == "AdventureWorks Sales":
df = model.Columns["Country"].get_sample_values()
assert len(df) > 0
else:
assert True
20 changes: 1 addition & 19 deletions test/test_3custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
These were designed selfishly for my own uses.
So seperating out, to one day sunset and remove.
"""
from test.config import testing_parameters, testingtablename, LOCAL_FILE
from test.config import testing_parameters, testingtablename
import pytest


Expand Down Expand Up @@ -44,21 +44,3 @@ def test_revert_table(model):
)
== 1
)


@pytest.mark.parametrize("model", testing_parameters)
def test_get_dependencies(model):
if len(model.Measures) > 0:
dependencies = model.Measures[0].get_dependencies()
assert len(dependencies) > 0
else:
assert True


@pytest.mark.parametrize("model", testing_parameters)
def test_get_sample_values(model):
if LOCAL_FILE[0] == "AdventureWorks Sales":
df = model.Columns["Country"].get_sample_values()
assert len(df) > 0
else:
assert True