Beispiel #1
0
    def test_update_dimension(self):
        # get dimension from tm1
        d = self.tm1.dimensions.get(dimension_name=DIMENSION_NAME)
        # create element objects
        elements = [Element(name='e1', element_type='Consolidated'),
                    Element(name='e2', element_type='Numeric'),
                    Element(name='e3', element_type='Numeric'),
                    Element(name='e4', element_type='Numeric')]
        # create edge objects
        edges = {
            ('e1', 'e2'): 1,
            ('e1', 'e3'): 1,
            ('e1', 'e4'): 1}
        # create the element_attributes objects
        element_attributes = [ElementAttribute(name='Name Long', attribute_type='Alias'),
                              ElementAttribute(name='Name Short', attribute_type='Alias'),
                              ElementAttribute(name='Currency', attribute_type='String')]
        # create hierarchy object
        hierarchy = Hierarchy(name=DIMENSION_NAME, dimension_name=DIMENSION_NAME, elements=elements,
                              element_attributes=element_attributes, edges=edges)

        # replace existing hierarchy with new hierarchy
        d.remove_hierarchy(DIMENSION_NAME)
        d.add_hierarchy(hierarchy)

        # update dimension in TM1
        self.tm1.dimensions.update(d)

        # Test
        dimension = self.tm1.dimensions.get(DIMENSION_NAME)
        self.assertEqual(len(dimension.hierarchies[0].elements), len(elements))
Beispiel #2
0
    def test1_create_dimension(self):
        root_element = Element(name='Root', element_type='Consolidated')
        elements = [root_element]
        edges = {}
        for i in range(1000):
            element_name = str(uuid.uuid4())
            elements.append(Element(name=element_name, element_type='Numeric'))
            edges[('Root', element_name)] = i
        element_attributes = [
            ElementAttribute(name='Name Long', attribute_type='Alias'),
            ElementAttribute(name='Name Short', attribute_type='Alias')
        ]
        h = Hierarchy(name=self.dimension_name,
                      dimension_name=self.dimension_name,
                      elements=elements,
                      edges=edges,
                      element_attributes=element_attributes)
        d = Dimension(name=self.dimension_name, hierarchies=[h])
        self.tm1.dimensions.create(d)

        # Test
        dimensions = self.tm1.dimensions.get_all_names()
        self.assertIn(self.dimension_name, dimensions)

        # Get it
        d = self.tm1.dimensions.get(dimension_name=self.dimension_name)
        h = d.hierarchies[0]
        # Test
        self.assertEqual(len(h.elements), 1001)
        self.assertEqual(len(h.element_attributes), 2)
Beispiel #3
0
    def test_add_element_attributes_multi(self):
        element_attribute1 = ElementAttribute(name="Attribute1", attribute_type="String")
        element_attribute2 = ElementAttribute(name="Attribute2", attribute_type="String")
        self.tm1.elements.add_element_attributes(
            self.dimension_name,
            self.dimension_name,
            [element_attribute1, element_attribute2])

        self.assertIn(
            element_attribute1,
            self.tm1.elements.get_element_attributes(self.dimension_name, self.dimension_name))
        self.assertIn(
            element_attribute2,
            self.tm1.elements.get_element_attributes(self.dimension_name, self.dimension_name))
Beispiel #4
0
    def setup_class(cls):
        cls.tm1 = TM1Service(**test_config)

        # Do random stuff
        cls.private = bool(random.getrandbits(1))

        # Define Names
        cls.prefix = 'TM1py_unittest_dimension_'
        cls.dimension_name = cls.prefix + str(uuid.uuid4())
        cls.subset_name_static = cls.prefix + str(uuid.uuid4())
        cls.subset_name_dynamic = cls.prefix + str(uuid.uuid4())

        # Instantiate Subsets
        cls.static_subset = Subset(dimension_name=cls.dimension_name,
                                   subset_name=cls.subset_name_static,
                                   elements=['USD', 'EUR', 'NZD'])
        cls.dynamic_subset = Subset(
            dimension_name=cls.dimension_name,
            subset_name=cls.subset_name_dynamic,
            expression='{ HIERARCHIZE( {TM1SUBSETALL( [' + cls.dimension_name +
            '] )} ) }')

        elements = [
            Element('USD', 'Numeric'),
            Element('EUR', 'Numeric'),
            Element('JPY', 'Numeric'),
            Element('CNY', 'Numeric'),
            Element('GBP', 'Numeric'),
            Element('NZD', 'Numeric')
        ]
        element_attributes = [ElementAttribute('Currency Name', 'String')]
        h = Hierarchy(cls.dimension_name, cls.dimension_name, elements,
                      element_attributes)
        d = Dimension(cls.dimension_name, hierarchies=[h])
        cls.tm1.dimensions.create(d)
Beispiel #5
0
    def setUp(self):
        # Instantiate Subsets
        self.static_subset = Subset(dimension_name=self.dimension_name,
                                    subset_name=self.subset_name_static,
                                    elements=['USD', 'EUR', 'NZD', 'Dum\'my'])
        self.dynamic_subset = Subset(
            dimension_name=self.dimension_name,
            subset_name=self.subset_name_dynamic,
            expression='{ HIERARCHIZE( {TM1SUBSETALL( [' +
            self.dimension_name + '] )} ) }')

        elements = [
            Element('USD', 'Numeric'),
            Element('EUR', 'Numeric'),
            Element('JPY', 'Numeric'),
            Element('CNY', 'Numeric'),
            Element('GBP', 'Numeric'),
            Element('NZD', 'Numeric'),
            Element('Dum\'my', 'Numeric')
        ]
        element_attributes = [ElementAttribute('Currency Name', 'String')]
        h = Hierarchy(self.dimension_name, self.dimension_name, elements,
                      element_attributes)
        d = Dimension(self.dimension_name, hierarchies=[h])
        self.tm1.dimensions.create(d)

        for private in (True, False):
            self.tm1.dimensions.subsets.create(subset=self.static_subset,
                                               private=private)
            self.tm1.dimensions.subsets.create(subset=self.dynamic_subset,
                                               private=private)
Beispiel #6
0
    def test_add_element_attributes_single(self):
        element_attribute = ElementAttribute(name="Attribute1", attribute_type="String")
        self.tm1.elements.add_element_attributes(self.dimension_name, self.dimension_name, [element_attribute])

        self.assertIn(
            element_attribute,
            self.tm1.elements.get_element_attributes(self.dimension_name, self.dimension_name))
Beispiel #7
0
    def test_add_element_attributes_fail(self):
        with self.assertRaises(TM1pyRestException) as _:
            element_attribute = ElementAttribute(name=self.attributes[0],
                                                 attribute_type="String")

            self.tm1.elements.add_element_attributes(self.dimension_name,
                                                     self.dimension_name,
                                                     [element_attribute])
Beispiel #8
0
    def test_create_element_attribute(self):
        element_attribute = ElementAttribute("NewAttribute", "String")
        self.tm1.dimensions.hierarchies.elements.create_element_attribute(
            DIMENSION_NAME, DIMENSION_NAME, element_attribute)

        element_attributes = self.tm1.dimensions.hierarchies.elements.get_element_attributes(
            DIMENSION_NAME, DIMENSION_NAME)

        self.assertIn(element_attribute, element_attributes)
Beispiel #9
0
    def test_create_element_attribute(self):
        element_attribute = ElementAttribute("NewAttribute", "String")
        self.tm1.dimensions.hierarchies.elements.create_element_attribute(
            self.dimension_name, self.dimension_name, element_attribute)

        element_attributes = self.tm1.dimensions.hierarchies.elements.get_element_attributes(
            self.dimension_name, self.dimension_name)

        self.assertIn(element_attribute, element_attributes)
Beispiel #10
0
 def create_dimension(cls):
     root_element = Element(name='Root', element_type='Consolidated')
     elements = [root_element]
     edges = {}
     for i in range(1, 1001):
         element_name = "Element {}".format(i)
         elements.append(Element(name=element_name, element_type='Numeric'))
         edges[('Root', element_name)] = i
     element_attributes = [
         ElementAttribute(name='Name Long', attribute_type='Alias'),
         ElementAttribute(name='Name Short', attribute_type='Alias')]
     h = Hierarchy(
         name=DIMENSION_NAME,
         dimension_name=DIMENSION_NAME,
         elements=elements,
         edges=edges,
         element_attributes=element_attributes)
     d = Dimension(name=DIMENSION_NAME, hierarchies=[h])
     cls.tm1.dimensions.create(d)
Beispiel #11
0
    def test07_create_element_attribute(self):
        attribute = ElementAttribute('Leap Year', 'Numeric')
        self.tm1.dimensions.hierarchies.elements.create_element_attribute(self.dimension_name,
                                                                          self.hierarchy_name, attribute)

        # write one element attribute value
        self.tm1.cubes.cells.write_value(1, '}ElementAttributes_' + self.dimension_name, ('1992', 'Leap Year'))
        elements = self.tm1.dimensions.hierarchies.elements.get_elements_filtered_by_attribute(self.dimension_name,
                                                                                               self.hierarchy_name,
                                                                                               'Leap Year', 1)
        self.assertIn('1992', elements)
        self.assertNotIn('1989', elements)
Beispiel #12
0
 def get_element_attributes(self, dimension_name, hierarchy_name):
     """ Get element attributes from hierarchy
 
     :param dimension_name:
     :param hierarchy_name:
     :return:
     """
     request = '/api/v1/Dimensions(\'{}\')/Hierarchies(\'{}\')/ElementAttributes'.format(dimension_name,
                                                                                         hierarchy_name)
     response = self._rest.GET(request, '')
     element_attributes = [ElementAttribute.from_dict(ea) for ea in response.json()['value']]
     return element_attributes
Beispiel #13
0
 def setup_class(cls):
     elements = [
         Element('USD', 'Numeric'),
         Element('EUR', 'Numeric'),
         Element('JPY', 'Numeric'),
         Element('CNY', 'Numeric'),
         Element('GBP', 'Numeric'),
         Element('NZD', 'Numeric')
     ]
     element_attributes = [ElementAttribute('Currency Name', 'String')]
     h = Hierarchy(cls.dimension_name, cls.dimension_name, elements,
                   element_attributes)
     d = Dimension(cls.dimension_name, hierarchies=[h])
     cls.tm1.dimensions.create(d)
Beispiel #14
0
    def get_element_attributes(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[ElementAttribute]:
        """ Get element attributes from hierarchy

        :param dimension_name:
        :param hierarchy_name:
        :return:
        """
        url = format_url(
            "/api/v1/Dimensions('{}')/Hierarchies('{}')/ElementAttributes",
            dimension_name,
            hierarchy_name)
        response = self._rest.GET(url, **kwargs)
        element_attributes = [ElementAttribute.from_dict(ea) for ea in response.json()['value']]
        return element_attributes
Beispiel #15
0
    def test_create_filter_and_delete_element_attribute(self):
        attribute = ElementAttribute('Leap Year', 'Numeric')
        self.tm1.dimensions.hierarchies.elements.create_element_attribute(
            DIMENSION_NAME, HIERARCHY_NAME, attribute)
        # write one element attribute value
        self.tm1.cubes.cells.write_value(
            1, '}ElementAttributes_' + DIMENSION_NAME, ('1992', 'Leap Year'))
        elements = self.tm1.dimensions.hierarchies.elements.get_elements_filtered_by_attribute(
            DIMENSION_NAME, HIERARCHY_NAME, 'Leap Year', 1)
        self.assertIn('1992', elements)
        self.assertEqual(len(elements), 1)

        self.tm1.dimensions.hierarchies.elements.delete_element_attribute(
            DIMENSION_NAME, HIERARCHY_NAME, "Leap Year")
Beispiel #16
0
# Connection to TM1. Needs IP, Port, Credentials, and SSL
with TM1Service(**config['tm1srv01']) as tm1:
    # create elements objects
    elements = [
        Element(name='Europe', element_type='Consolidated'),
        Element(name='CH', element_type='Numeric'),
        Element(name='UK', element_type='Numeric'),
        Element(name='BE', element_type='Numeric')
    ]

    # create edge object
    edges = {('Europe', 'CH'): 1, ('Europe', 'UK'): 1, ('Europe', 'BE'): 1}

    # create the element_attributes
    element_attributes = [
        ElementAttribute(name='Name Long', attribute_type='Alias'),
        ElementAttribute(name='Name Short', attribute_type='Alias'),
        ElementAttribute(name='Currency', attribute_type='String')
    ]

    # create hierarchy object
    hierarchy = Hierarchy(name=name,
                          dimension_name=name,
                          elements=elements,
                          element_attributes=element_attributes,
                          edges=edges)

    # create dimension object
    d = Dimension(name=name, hierarchies=[hierarchy])

    # create dimension in TM1 !
Beispiel #17
0
    def setUpClass(cls):
        """
        Establishes a connection to TM1 and creates TM! objects to use across all tests
        """

        # Connection to TM1
        cls.config = configparser.ConfigParser()
        cls.config.read(Path(__file__).parent.joinpath('config.ini'))
        cls.tm1 = TM1Service(**cls.config['tm1srv01'])

        # generate random coordinates
        cls.target_coordinates = list(
            zip(('Element ' + str(e) for e in range(1, 100)),
                ('Element ' + str(e) for e in range(1, 100)),
                ('Element ' + str(e) for e in range(1, 100))))

        # Build Dimensions
        for dimension_name in DIMENSION_NAMES:
            elements = [
                Element('Element {}'.format(str(j)), 'Numeric')
                for j in range(1, 1001)
            ]
            element_attributes = [
                ElementAttribute("Attr1", "String"),
                ElementAttribute("Attr2", "Numeric"),
                ElementAttribute("Attr3", "Numeric")
            ]
            hierarchy = Hierarchy(dimension_name=dimension_name,
                                  name=dimension_name,
                                  elements=elements,
                                  element_attributes=element_attributes)
            dimension = Dimension(dimension_name, [hierarchy])
            if cls.tm1.dimensions.exists(dimension.name):
                cls.tm1.dimensions.update(dimension)
            else:
                cls.tm1.dimensions.create(dimension)
            attribute_cube = "}ElementAttributes_" + dimension_name
            attribute_values = dict()
            for element in elements:
                attribute_values[(element.name, "Attr1")] = "TM1py"
                attribute_values[(element.name, "Attr2")] = "2"
                attribute_values[(element.name, "Attr3")] = "3"
            cls.tm1.cubes.cells.write_values(attribute_cube, attribute_values)

        # Build Cube
        cube = Cube(CUBE_NAME, DIMENSION_NAMES)
        if not cls.tm1.cubes.exists(CUBE_NAME):
            cls.tm1.cubes.create(cube)

        # Sum of all the values that we write in the cube. serves as a checksum.
        cls.total_value = 0

        # cellset of data that shall be written
        cls.cellset = {}
        for element1, element2, element3 in cls.target_coordinates:
            value = 1
            cls.cellset[(element1, element2, element3)] = value
            # update the checksum
            cls.total_value += value

        # Fill cube with values
        cls.tm1.cubes.cells.write_values(CUBE_NAME, cls.cellset)

        # Elements
        cls.years = ("No Year", "1989", "1990", "1991", "1992")
        cls.extra_year = "4321"
        # Element Attributes
        cls.attributes = ('Previous Year', 'Next Year')
        cls.alias_attributes = ("Financial Year", )

        # create dimension with a default hierarchy
        d = Dimension(DIMENSION_NAME)
        h = Hierarchy(DIMENSION_NAME, DIMENSION_NAME)
        h.add_element('Total Years', 'Consolidated')
        h.add_element('All Consolidations', 'Consolidated')
        h.add_edge("All Consolidations", "Total Years", 1)
        for year in cls.years:
            h.add_element(year, 'Numeric')
            h.add_edge('Total Years', year, 1)
        for attribute in cls.attributes:
            h.add_element_attribute(attribute, "String")
        for attribute in cls.alias_attributes:
            h.add_element_attribute(attribute, "Alias")
        d.add_hierarchy(h)
        cls.tm1.dimensions.update_or_create(d)

        # write attribute values
        cls.tm1.cubes.cells.write_value('1988',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1989', 'Previous Year'))
        cls.tm1.cubes.cells.write_value('1989',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1990', 'Previous Year'))
        cls.tm1.cubes.cells.write_value('1990',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1991', 'Previous Year'))
        cls.tm1.cubes.cells.write_value('1991',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1992', 'Previous Year'))

        cls.tm1.cubes.cells.write_value('1988/89',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1989', 'Financial Year'))
        cls.tm1.cubes.cells.write_value('1989/90',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1990', 'Financial Year'))
        cls.tm1.cubes.cells.write_value('1990/91',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1991', 'Financial Year'))
        cls.tm1.cubes.cells.write_value('1991/92',
                                        '}ElementAttributes_' + DIMENSION_NAME,
                                        ('1992', 'Financial Year'))
Beispiel #18
0
    def setup_class(cls):
        # Connection to TM1
        cls.tm1 = TM1Service(**config['tm1srv01'])

        # generate random coordinates
        cls.target_coordinates = list(
            zip(('Element ' + str(random.randint(1, 1000))
                 for _ in range(100)),
                ('Element ' + str(random.randint(1, 1000))
                 for _ in range(100)),
                ('Element ' + str(random.randint(1, 1000))
                 for _ in range(100))))

        # Build Dimensions
        for dimension_name in dimension_names:
            elements = [
                Element('Element {}'.format(str(j)), 'Numeric')
                for j in range(1, 1001)
            ]
            element_attributes = [
                ElementAttribute("Attr1", "String"),
                ElementAttribute("Attr2", "Numeric"),
                ElementAttribute("Attr3", "Numeric")
            ]
            hierarchy = Hierarchy(dimension_name=dimension_name,
                                  name=dimension_name,
                                  elements=elements,
                                  element_attributes=element_attributes)
            dimension = Dimension(dimension_name, [hierarchy])
            if not cls.tm1.dimensions.exists(dimension.name):
                cls.tm1.dimensions.create(dimension)
            attribute_cube = "}ElementAttributes_" + dimension_name
            attribute_values = dict()
            for element in elements:
                attribute_values[(element.name, "Attr1")] = "TM1py"
                attribute_values[(element.name, "Attr2")] = "2"
                attribute_values[(element.name, "Attr3")] = "3"
            cls.tm1.cubes.cells.write_values(attribute_cube, attribute_values)

        # Build Cube
        cube = Cube(cube_name, dimension_names)
        if not cls.tm1.cubes.exists(cube_name):
            cls.tm1.cubes.create(cube)

        # Build cube view
        view = NativeView(cube_name=cube_name,
                          view_name=view_name,
                          suppress_empty_columns=True,
                          suppress_empty_rows=True)
        subset = AnonymousSubset(dimension_name=dimension_names[0],
                                 expression='{[' + dimension_names[0] +
                                 '].Members}')
        view.add_row(dimension_name=dimension_names[0], subset=subset)
        subset = AnonymousSubset(dimension_name=dimension_names[1],
                                 expression='{[' + dimension_names[1] +
                                 '].Members}')
        view.add_row(dimension_name=dimension_names[1], subset=subset)
        subset = AnonymousSubset(dimension_name=dimension_names[2],
                                 expression='{[' + dimension_names[2] +
                                 '].Members}')
        view.add_column(dimension_name=dimension_names[2], subset=subset)
        cls.tm1.cubes.views.create(view, private=False)

        # Sum of all the values that we write in the cube. serves as a checksum
        cls.total_value = 0

        # cellset of data that shall be written
        cls.cellset = {}
        for element1, element2, element3 in cls.target_coordinates:
            value = random.randint(1, 1000)
            cls.cellset[(element1, element2, element3)] = value
            # update the checksum
            cls.total_value += value
Beispiel #19
0
def build_date_dimension(tm1, dimension_name, first_date, last_date,
                         overwrite):
    date_span = last_date - first_date
    dates = [
        str(first_date + timedelta(day)) for day in range(date_span.days + 1)
    ]
    # Build Leaves
    leaves = [Element(name=date, element_type='Numeric') for date in dates]

    # Build Consolidations
    years = [str(year) for year in range(first_date.year, last_date.year + 1)]
    consolidations = [
        Element(name=year, element_type='Consolidated') for year in years
    ]

    for year in years:
        for month in range(1, 13):
            year_month = year + "-" + str(month).zfill(2)
            consolidations.append(
                Element(name=year_month, element_type="Consolidated"))

    # Build Elements
    elements = leaves + consolidations

    # Build Edges
    edges = CaseAndSpaceInsensitiveTuplesDict()
    for year in years:
        for month in range(1, 13):
            year_month = year + "-" + str(month).zfill(2)
            edges[(year, year_month)] = 1
        for date in filter(lambda d: d[0:4] == year, dates):
            year_month = date[0:7]
            edges[(year_month, date)] = 1

    # Build Attribute
    attributes = [
        ElementAttribute('Alias', 'Alias'),
        ElementAttribute('Year', 'String'),
        ElementAttribute('Month', 'String'),
        ElementAttribute('Day', 'String'),
        ElementAttribute('Weekday', 'String')
    ]

    # write Aliases
    attribute_values = {}
    for date in dates:
        # Year, Month, Day Attributes
        attribute_values[(date, 'Year')] = date.split('-')[0]
        attribute_values[(date, 'Month')] = date.split('-')[1]
        attribute_values[(date, 'Day')] = date.split('-')[2]
        attribute_values[(
            date, 'Weekday')] = dateutil.parser.parse(date).weekday() + 1
        # US Notation as Alias
        year, month, day = [str(int(ymd)) for ymd in date.split('-')]
        attribute_values[(date, 'Alias')] = month + "/" + day + "/" + year

    # Build Hierarchy, Dimension
    hier = Hierarchy(name=dimension_name,
                     dimension_name=dimension_name,
                     elements=elements,
                     edges=edges,
                     element_attributes=attributes)
    dim = Dimension(name=dimension_name, hierarchies=[hier])

    # Interaction with TM1
    exists = tm1.dimensions.exists(dimension_name)
    if not exists:
        tm1.dimensions.create(dim)
    elif exists and overwrite:
        tm1.dimensions.update(dim)
    if not exists or overwrite:
        tm1.cubes.cells.write_values(cube_name="}ElementAttributes_" +
                                     dimension_name,
                                     cellset_as_dict=attribute_values)

    # Year Subsets
    for year in years:
        expr = "{ FILTER ( {TM1SubsetAll([Date])}, [Date].[Year] = '" + year + "' ) }"
        subset = Subset(year,
                        dimension_name=dimension_name,
                        hierarchy_name=dimension_name,
                        expression=expr)
        if not tm1.dimensions.hierarchies.subsets.exists(
                year, dimension_name, dimension_name, private=False):
            tm1.dimensions.hierarchies.subsets.create(subset, private=False)
        else:
            tm1.dimensions.hierarchies.subsets.update(subset, private=False)