1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
"""PyTest Fixtures"""
import logging
import os
import pytest
from pyodata.v2.model import schema_from_xml, Types
def contents_of_fixtures_file(file_name):
path_to_current_file = os.path.realpath(__file__)
current_directory = os.path.split(path_to_current_file)[0]
path_to_file = os.path.join(current_directory, file_name)
with open(path_to_file, 'rb') as md_file:
return md_file.read()
@pytest.fixture
def metadata():
"""Example OData metadata"""
return contents_of_fixtures_file("metadata.xml")
@pytest.fixture
def xml_builder_factory():
"""Skeleton OData metadata"""
class XMLBuilder:
"""Helper class for building XML metadata document"""
# pylint: disable=too-many-instance-attributes,line-too-long
def __init__(self):
self.reference_is_enabled = True
self.data_services_is_enabled = True
self.schema_is_enabled = True
self.namespaces = {
'edmx': "http://schemas.microsoft.com/ado/2007/06/edmx",
'sap': 'http://www.sap.com/Protocols/SAPData',
'edm': 'http://schemas.microsoft.com/ado/2008/09/edm',
'm': 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
'd': 'http://schemas.microsoft.com/ado/2007/08/dataservices',
}
self.custom_edmx_prologue = None
self.custom_edmx_epilogue = None
self.custom_data_services_prologue = None
self.custom_data_services_epilogue = None
self._reference = '\n<edmx:Reference xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Uri="https://example.sap.corp/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/Vocabularies(TechnicalName=\'%2FIWBEP%2FVOC_COMMON\',Version=\'0001\',SAP__Origin=\'LOCAL\')/$value">' + \
'\n<edmx:Include Namespace="com.sap.vocabularies.Common.v1" Alias="Common"/>' + \
'\n</edmx:Reference>'
self._schemas = ''
def add_schema(self, namespace, xml_definition):
"""Add schema element"""
self._schemas += f""""\n<Schema xmlns:d="{self.namespaces["d"]}" xmlns:m="{self.namespaces["m"]}" xmlns="{
self.namespaces["edm"]}" Namespace="{namespace}" xml:lang="en" sap:schema-version="1">"""
self._schemas += "\n" + xml_definition
self._schemas += '\n</Schema>'
def serialize(self):
"""Returns full metadata XML document"""
result = self._edmx_prologue()
if self.reference_is_enabled:
result += self._reference
if self.data_services_is_enabled:
result += self._data_services_prologue()
if self.schema_is_enabled:
result += self._schemas
if self.data_services_is_enabled:
result += self._data_services_epilogue()
result += self._edmx_epilogue()
return result
def _edmx_prologue(self):
if self.custom_edmx_prologue:
prologue = self.custom_edmx_prologue
else:
prologue = f"""<edmx:Edmx xmlns:edmx="{self.namespaces["edmx"]}" xmlns:m="{self.namespaces["m"]}" xmlns:sap="{self.namespaces["sap"]}" Version="1.0">"""
return prologue
def _edmx_epilogue(self):
if self.custom_edmx_epilogue:
epilogue = self.custom_edmx_epilogue
else:
epilogue = '\n</edmx:Edmx>'
return epilogue
def _data_services_prologue(self):
if self.custom_data_services_prologue:
prologue = self.custom_data_services_prologue
else:
prologue = '\n<edmx:DataServices m:DataServiceVersion="2.0">'
return prologue
def _data_services_epilogue(self):
if self.custom_data_services_epilogue:
prologue = self.custom_data_services_epilogue
else:
prologue = '\n</edmx:DataServices>'
return prologue
return XMLBuilder
@pytest.fixture
def schema(metadata):
"""Parsed metadata"""
# pylint: disable=redefined-outer-name
return schema_from_xml(metadata)
def assert_logging_policy(mock_warning, *args):
"""Assert if an warning was outputted by PolicyWarning """
assert logging.Logger.warning is mock_warning
mock_warning.assert_called_with('[%s] %s', *args)
def assert_request_contains_header(headers, name, value):
assert name in headers
assert headers[name] == value
@pytest.fixture
def type_date_time():
return Types.from_name('Edm.DateTime')
@pytest.fixture
def type_date_time_offset():
return Types.from_name('Edm.DateTimeOffset')
|