Skip to content

Commit 8145320

Browse files
Curts0Curtis Stallings
andauthored
0.5.1 - find() fix w/ extra pytest added. (#94)
* working * Revert "working" This reverts commit 6810de1. * find_method fix --------- Co-authored-by: Curtis Stallings <curtis.stallings@rockwellautomation.com>
1 parent b34121b commit 8145320

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "python_tabular"
7-
version = "0.5.0"
7+
version = "0.5.1"
88
authors = [
99
{ name="Curtis Stallings", email="curtisrstallings@gmail.com" },
1010
]

pytabular/column.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ def query_all(self, query_function: str = "COUNTROWS(VALUES(_))") -> pd.DataFram
155155
dax_identifier = f"'{table_name}'[{column_name}]"
156156
query_str += f"ROW(\"Table\",\"{table_name}\",\
157157
\"Column\",\"{column_name}\",\"{query_function}\",\
158-
{query_function.replace('_',dax_identifier)}),\n" # noqa: E231, E261
158+
{query_function.replace('_',dax_identifier)}),\n" # noqa: E231, E261
159159
query_str = f"{query_str[:-2]})"
160160
return self[0].Table.Model.query(query_str)

pytabular/measure.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ class PyMeasures(PyObjects):
6060
`model.Measures.find('ratio')`.
6161
"""
6262

63-
def __init__(self, objects, parent) -> None:
63+
def __init__(self, objects, parent=None) -> None:
6464
"""Extends init from `PyObjects`."""
6565
super().__init__(objects, parent)
6666

6767
def __call__(self, *args, **kwargs):
6868
"""Made `PyMeasures` just sends args through to `add_measure`."""
6969
return self.add_measure(*args, **kwargs)
7070

71-
def add_measure(self, name: str, expression: str, **kwargs) -> PyMeasure:
71+
def add_measure(
72+
self, name: str, expression: str, auto_save: bool = True, **kwargs
73+
) -> PyMeasure:
7274
"""Add or replace measures from `PyMeasures` class.
7375
7476
Required is just `name` and `expression`.
@@ -98,6 +100,8 @@ def add_measure(self, name: str, expression: str, **kwargs) -> PyMeasure:
98100
Args:
99101
name (str): Name of the measure. Brackets ARE NOT required.
100102
expression (str): DAX expression for the measure.
103+
auto_save (bool, optional): Automatically save changes after measure creations.
104+
Defaults to `True`
101105
"""
102106
if isinstance(self.parent._object, Table):
103107
table = self.parent
@@ -128,6 +132,8 @@ def add_measure(self, name: str, expression: str, **kwargs) -> PyMeasure:
128132
if new:
129133
measures = table.get_Measures()
130134
measures.Add(new_measure)
131-
132-
model.save_changes()
133-
return model.Measures[new_measure.Name]
135+
if auto_save:
136+
model.save_changes()
137+
return model.Measures[new_measure.Name]
138+
else:
139+
return True

pytabular/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def query_all(self, query_function: str = "COUNTROWS(_)") -> pd.DataFrame:
181181
table_name = table.get_Name()
182182
dax_table_identifier = f"'{table_name}'"
183183
query_str += f"ROW(\"Table\",\"{table_name}\",\"{query_function}\",\
184-
{query_function.replace('_',dax_table_identifier)}),\n" # noqa: E231, E261
184+
{query_function.replace('_',dax_table_identifier)}),\n" # noqa: E231, E261
185185
query_str = f"{query_str[:-2]})"
186186
return self[0].Model.query(query_str)
187187

test/test_2object.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ def test_iadd_table(model):
107107
b = model.Tables.find("Date")[0]
108108
a += b
109109
assert len(a.find("Date")) > 0
110+
111+
112+
@pytest.mark.parametrize("model", testing_parameters)
113+
def test_find_measure(model):
114+
"""Tests `find()` with a `PyMeasure()`."""
115+
a = model.Measures[0].Name
116+
b = model.Measures.find(a)
117+
assert len(b) > 0

test/test_4measure.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Bulk of pytests for `PyMeasure()` class."""
2+
import pytest
3+
from test.config import testing_parameters
4+
5+
6+
@pytest.mark.parametrize("model", testing_parameters)
7+
def test_create_measure(model):
8+
"""Test Creating Measure."""
9+
name = "Test Measure"
10+
expression = "1 + 4"
11+
folder = "Testing"
12+
model.Measures(name, expression, DisplayFolder=folder)
13+
query = model.query(f"EVALUATE {{[{name}]}}")
14+
ans = 5
15+
new_measure = model.Measures[name]._object
16+
new_measure.Parent.Measures.Remove(new_measure)
17+
model.save_changes()
18+
assert query == ans

0 commit comments

Comments
 (0)