Skip to content

Commit

Permalink
Add tests for GitProject
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Oct 2, 2022
1 parent 2a82ba0 commit 340beae
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 37 deletions.
74 changes: 38 additions & 36 deletions TM1py/Objects/GitProject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
from typing import Optional, Dict, List

from TM1py import TM1Service
from TM1py.Objects.TM1Object import TM1Object


Expand Down Expand Up @@ -62,10 +61,7 @@ def __init__(self, task_name: str, chore: str = None, process: str = None, param
self.parameters = parameters
self.dependencies = dependencies

def body(self):
return json.dumps(self.construct_body())

def construct_body(self):
def construct_body(self) -> Dict:
inner_body = dict()

if self.dependencies:
Expand Down Expand Up @@ -137,13 +133,16 @@ def __init__(
self._preconditions = preconditions

def add_task(self, project_task: TM1ProjectTask):
if self._tasks is None:
self._tasks = []

if project_task.task_name in self._tasks:
raise ValueError(f"Task with name '{project_task.task_name}' already exists in TM1 project. "
f"Task name must be unique")

self._tasks.append(project_task)

def include_attribute_dimensions(self, tm1: TM1Service):
def include_attribute_dimensions(self, tm1):
"""
Add an ignore-exception for each attribute dimension
Expand All @@ -163,14 +162,15 @@ def add_ignore_exceptions(self, object_class: str, object_names: List[str]):
Args:
object_class: class of the object e.g., "Dimensions"
object_names: name of the objects e.g., ["Product", "Customer", "Region"]
object_names: names of the objects e.g., ["Product", "Customer", "Region"]
Example of the ignore property in the tm1project:
Exclude all Dimensions that start with 'Dim', except for dimension 'DimB'
Exclude all Dimensions that start with 'Dim', except for dimension 'DimB', 'DimA'
"Ignore":
[
"Dimensions('Dim*')",
"!Dimensions('DimA')",
"!Dimensions('DimB')"
]
"""
Expand All @@ -183,7 +183,10 @@ def add_ignore_exceptions(self, object_class: str, object_names: List[str]):
if object_name:
ignore_entry += f"('{object_name}')"

if not ignore_entry in self.ignore:
if self.ignore is None:
self.ignore = []

if ignore_entry not in self.ignore:
self.ignore.append(ignore_entry)

def add_ignore(self, object_class: str, object_name: str):
Expand Down Expand Up @@ -216,6 +219,9 @@ def add_ignore(self, object_class: str, object_name: str):
if object_name.startswith("}") and "*" in object_name:
raise ValueError("'*' character must not be used in object_name for control objects")

if self.ignore is None:
self.ignore = []

ignore_entry = object_class
if object_name:
ignore_entry += f"('{object_name}')"
Expand Down Expand Up @@ -255,22 +261,39 @@ def from_dict(cls, tm1project_as_dict: Dict) -> 'TM1Project':
dependencies=tm1project_as_dict.get('Dependencies')
)

@classmethod
def to_dict(cls, tm1_project) -> 'Dict':
tm1_project_as_dict = tm1_project.__dict__
return tm1_project_as_dict

@classmethod
def from_file(cls, filename: str) -> 'TM1Project':
with open(filename, 'r') as file_object:
json_file = json.load(file_object)

return cls.from_dict(json_file)

# construct self.body (json) from the class-attributes
def _construct_body(self) -> Dict:
body = {
'Version': self._version,
'Name': self._name,
'Settings': self._settings,
'Tasks': [task.construct_body() for task in self._tasks] if self._tasks else None,
'Objects': self._objects,
'Ignore': self._ignore,
'Files': self._files,
'Deployment': self._deployment,
'PrePush': self._pre_push,
'PostPush': self._post_push,
'PrePull': self._pre_pull,
'PostPull': self._post_pull,
'Dependencies': self._dependencies}
return clean_null_terms(body)

@property
def body(self) -> str:
def body_as_dict(self) -> Dict:
return self._construct_body()

@property
def body(self) -> str:
return json.dumps(self.body_as_dict, ensure_ascii=False)

@property
def version(self) -> int:
return self._version
Expand Down Expand Up @@ -374,24 +397,3 @@ def preconditions(self) -> str:
@preconditions.setter
def preconditions(self, value: str):
self._preconditions = value

# construct self.body (json) from the class-attributes
def _construct_body(self) -> str:
# general parameters
body_as_dict_complete = {
'Version': self._version,
'Name': self._name,
'Settings': self._settings,
'Tasks': [task.construct_body() for task in self._tasks],
'Objects': self._objects,
'Ignore': self._ignore,
'Files': self._files,
'Deployment': self._deployment,
'PrePush': self._pre_push,
'PostPush': self._post_push,
'PrePull': self._pre_pull,
'PostPull': self._post_pull,
'Dependencies': self._dependencies}

body_as_dict = clean_null_terms(body_as_dict_complete)
return json.dumps(body_as_dict, ensure_ascii=False)
2 changes: 1 addition & 1 deletion TM1py/Objects/TM1Object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TM1Object:

@property
@abstractmethod
def body(self):
def body(self) -> str:
pass

def __hash__(self):
Expand Down
1 change: 1 addition & 0 deletions TM1py/Objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from TM1py.Objects.Dimension import Dimension
from TM1py.Objects.Element import Element
from TM1py.Objects.ElementAttribute import ElementAttribute
from TM1py.Objects.GitProject import TM1Project, TM1ProjectTask
from TM1py.Objects.Hierarchy import Hierarchy
from TM1py.Objects.MDXView import MDXView
from TM1py.Objects.NativeView import NativeView
Expand Down
Empty file removed Tests/GitProject.py
Empty file.
44 changes: 44 additions & 0 deletions Tests/TM1Project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest

from TM1py.Objects.GitProject import TM1Project, TM1ProjectTask


class TestGitProject(unittest.TestCase):

def test_add_task_process(self):
project = TM1Project(name="TM1py Tests")

project.add_task(TM1ProjectTask("TaskA", process="bedrock.server.savedataall"))

expected_body = {
"Version": 1.0,
"Name": "TM1py Tests",
"Tasks": [{"TaskA": {"Process": "bedrock.server.savedataall", "Parameters": None}}]}

self.assertEqual(expected_body, project.body_as_dict)

def test_add_ignore(self):
project = TM1Project(name="TM1py Tests")
project.add_ignore(object_class="Dimensions", object_name="Dim*")

expected_body = {"Version": 1.0, "Name": "TM1py Tests", "Ignore": ["Dimensions('Dim*')"]}
self.assertEqual(
expected_body,
project.body_as_dict)

def test_add_ignore_exception(self):
project = TM1Project(name="TM1py Tests")
project.add_ignore(object_class="Dimensions", object_name="Dim*")
project.add_ignore_exceptions(object_class="Dimensions", object_names=["DimA", "DimB"])

expected_body = {
"Version": 1.0,
"Name": "TM1py Tests",
"Ignore": [
"Dimensions('Dim*')",
"!Dimensions('DimA')",
"!Dimensions('DimB')"]
}
self.assertEqual(
expected_body,
project.body_as_dict)

0 comments on commit 340beae

Please sign in to comment.