Skip to content

Commit 65db1af

Browse files
rettichschnidifilak-sap
authored andcommitted
model: Extract property 'FixedLength'
Could not find any documentation on the default value if missing, but it seems is quite obvious that false, as opposed to true, is the only reasonable choice.
1 parent b5b0b40 commit 65db1af

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### Added
1010
- Client: Accept content type application/atom+xml - OmniTroid
11+
- Model: Extract property 'FixedLength' - Reto Schneider
1112

1213
## [1.9.0]
1314

pyodata/v2/model.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def from_json(self, value):
780780
class VariableDeclaration(Identifier):
781781
MAXIMUM_LENGTH = -1
782782

783-
def __init__(self, name, type_info, nullable, max_length, precision, scale):
783+
def __init__(self, name, type_info, nullable, max_length, precision, scale, fixed_length=None):
784784
super(VariableDeclaration, self).__init__(name)
785785

786786
self._type_info = type_info
@@ -804,6 +804,7 @@ def __init__(self, name, type_info, nullable, max_length, precision, scale):
804804
else:
805805
self._scale = int(scale)
806806
self._check_scale_value()
807+
self._fixed_length = bool(fixed_length)
807808

808809
@property
809810
def type_info(self):
@@ -839,6 +840,10 @@ def precision(self):
839840
def scale(self):
840841
return self._scale
841842

843+
@property
844+
def fixed_length(self):
845+
return self._fixed_length
846+
842847
def from_literal(self, value):
843848
if value is None:
844849
if not self.nullable:
@@ -1797,8 +1802,9 @@ class StructTypeProperty(VariableDeclaration):
17971802

17981803
# pylint: disable=too-many-locals
17991804
def __init__(self, name, type_info, nullable, max_length, precision, scale, uncode, label, creatable, updatable,
1800-
sortable, filterable, filter_restr, req_in_filter, text, visible, display_format, value_list):
1801-
super(StructTypeProperty, self).__init__(name, type_info, nullable, max_length, precision, scale)
1805+
sortable, filterable, filter_restr, req_in_filter, text, visible, display_format, value_list,
1806+
fixed_length=None):
1807+
super(StructTypeProperty, self).__init__(name, type_info, nullable, max_length, precision, scale, fixed_length)
18021808

18031809
self._value_helper = None
18041810
self._struct_type = None
@@ -1933,7 +1939,10 @@ def from_etree(entity_type_property_node):
19331939
sap_attribute_get_string(entity_type_property_node, 'text'),
19341940
sap_attribute_get_bool(entity_type_property_node, 'visible', True),
19351941
sap_attribute_get_string(entity_type_property_node, 'display-format'),
1936-
sap_attribute_get_string(entity_type_property_node, 'value-list'), )
1942+
sap_attribute_get_string(entity_type_property_node, 'value-list'),
1943+
# Back to regular, non-SAP attributes.
1944+
attribute_get_bool(entity_type_property_node, 'FixedLength', False),
1945+
)
19371946

19381947

19391948
class NavigationTypeProperty(VariableDeclaration):
@@ -1956,7 +1965,7 @@ class NavigationTypeProperty(VariableDeclaration):
19561965
"""
19571966

19581967
def __init__(self, name, from_role_name, to_role_name, association_info):
1959-
super(NavigationTypeProperty, self).__init__(name, None, False, None, None, None)
1968+
super(NavigationTypeProperty, self).__init__(name, None, False, None, None, None, None)
19601969

19611970
self.from_role_name = from_role_name
19621971
self.to_role_name = to_role_name
@@ -2629,7 +2638,7 @@ class FunctionImportParameter(VariableDeclaration):
26292638
Modes = Enum('Modes', 'In Out InOut')
26302639

26312640
def __init__(self, name, type_info, nullable, max_length, precision, scale, mode):
2632-
super(FunctionImportParameter, self).__init__(name, type_info, nullable, max_length, precision, scale)
2641+
super(FunctionImportParameter, self).__init__(name, type_info, nullable, max_length, precision, scale, None)
26332642

26342643
self._mode = mode
26352644

tests/metadata.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
<PropertyRef Name="Name"/>
106106
</Key>
107107
<Property Name="Name" Type="Edm.String" Nullable="false"/>
108+
<Property Name="ID" Type="Edm.String" MaxLength="5" FixedLength="true" />
109+
<Property Name="City" Type="Edm.String" MaxLength="15" FixedLength="false" />
108110
<NavigationProperty Name="Orders" Relationship="EXAMPLE_SRV.CustomerOrders" FromRole="CustomerRole"
109111
ToRole="OrdersRole"/>
110112
</EntityType>

tests/test_model_v2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ def test_schema_entity_type_nullable(schema):
171171
assert nickname_property.nullable
172172

173173

174+
@pytest.mark.parametrize('property_name,is_fixed_length,comment', [
175+
('Name', False, 'Name has no FixedLength property, defaults to false'),
176+
('ID', True, 'Customer ID length is fixed'),
177+
('City', False, 'City names have arbitrary lengths'),
178+
])
179+
def test_schema_entity_type_fixed_length(schema, property_name, is_fixed_length, comment):
180+
customer_entity = schema.entity_type('Customer')
181+
182+
property_ = customer_entity.proprty(property_name)
183+
assert property_.fixed_length == is_fixed_length, comment
184+
185+
174186
def test_schema_entity_sets(schema):
175187
"""Test Schema methods for EntitySets"""
176188

tests/test_model_v2_EdmStructTypeSerializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def define_complex_type(complex_type_property_declarations, nullable = True):
2222

2323
for name, prop_decl in complex_type_property_declarations.items():
2424
prop = StructTypeProperty(name, prop_decl[0], nullable, None, None, None,
25-
None, None, None, None, None, None, None, None, None, None, None, None)
25+
None, None, None, None, None, None, None, None, None, None, None, None, None)
2626

2727
prop.typ = Types.from_name(prop.type_info.name)
2828
complex_typ._properties[prop.name] = prop

tests/test_model_v2_VariableDeclaration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
@pytest.fixture
99
def variable_of_string_nullable():
10-
variable = VariableDeclaration('TestVariable', Types.parse_type_name('Edm.String'), True, None, None, None)
10+
variable = VariableDeclaration('TestVariable', Types.parse_type_name('Edm.String'), True, None, None, None, None)
1111
variable.typ = Types.from_name(variable.type_info.name)
1212
return variable
1313

1414
@pytest.fixture
1515
def variable_of_string():
16-
variable = VariableDeclaration('TestVariable', Types.parse_type_name('Edm.String'), False, None, None, None)
16+
variable = VariableDeclaration('TestVariable', Types.parse_type_name('Edm.String'), False, None, None, None, None)
1717
variable.typ = Types.from_name(variable.type_info.name)
1818
return variable
1919

0 commit comments

Comments
 (0)