diff --git a/pytabular/column.py b/pytabular/column.py index 8eb3e7a..341570a 100644 --- a/pytabular/column.py +++ b/pytabular/column.py @@ -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. diff --git a/pytabular/pytabular.py b/pytabular/pytabular.py index 4ef475f..3d3dba6 100644 --- a/pytabular/pytabular.py +++ b/pytabular/pytabular.py @@ -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 diff --git a/pytabular/query.py b/pytabular/query.py index 7886ee9..226f472 100644 --- a/pytabular/query.py +++ b/pytabular/query.py @@ -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}") diff --git a/test/test_2tabular.py b/test/test_2tabular.py index dc96df7..7d08f98 100644 --- a/test/test_2tabular.py +++ b/test/test_2tabular.py @@ -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) @@ -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 diff --git a/test/test_3custom.py b/test/test_3custom.py index 474c11f..265ebda 100644 --- a/test/test_3custom.py +++ b/test/test_3custom.py @@ -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 @@ -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