Skip to content

Commit 41ffff2

Browse files
Merge branch 'google:master' into master
2 parents 2e374c4 + 15c4135 commit 41ffff2

File tree

8 files changed

+75
-13
lines changed

8 files changed

+75
-13
lines changed

tools/abel/model/guid_to_entity_map.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ def AddEntity(self, entity: ...) -> None:
6565
"""
6666
if entity is None:
6767
raise ValueError('Cannot add None values to the guid to entity map.')
68-
if not entity.bc_guid:
68+
elif not entity.bc_guid:
6969
raise AttributeError(f'{entity.code}: guid missing')
70-
if entity.bc_guid not in self._guid_to_entity_map:
70+
elif entity.bc_guid not in list(self._guid_to_entity_map):
7171
self._guid_to_entity_map[entity.bc_guid] = entity
72+
elif self._guid_to_entity_map.get(entity.bc_guid) == entity:
73+
# Do nothing, this mapping already exists
74+
pass
7275
else:
7376
raise KeyError(
7477
f'{entity.bc_guid} maps to {self._guid_to_entity_map[entity.bc_guid]}'

tools/abel/model/model_helper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from model import entity_field as ef
2121
from model import entity_operation
2222
from model import model_builder as mb
23+
from model import site as site_lib
2324
from validate import field_translation as ft
2425

2526

@@ -304,7 +305,9 @@ def GetConnectionDependencies(entity, dependencies, split_entities):
304305
A list of entities connected to entity.
305306
"""
306307
split_entities.append(entity)
307-
if not entity.connections and entity not in split_entities:
308+
if isinstance(entity, site_lib.Site):
309+
return dependencies
310+
elif not entity.connections and entity not in split_entities:
308311
dependencies.append(entity)
309312
return dependencies
310313
else:

tools/abel/model/site.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def FromDict(cls, site_dict: Dict[str, object]) -> ...:
7878
)
7979
return site_instance
8080

81+
@property
82+
def bc_guid(self) -> str:
83+
"""Returns the BC GUID for a site."""
84+
return self.guid
85+
8186
@property
8287
def entities(self) -> List[str]:
8388
"""Returns a list of entity guids contained in a site."""
@@ -104,10 +109,14 @@ def AddEntity(self, entity: Entity) -> None:
104109
# pylint: disable=unused-argument
105110
def GetSpreadsheetRowMapping(self, *args) -> Dict[str, str]:
106111
"""Returns a dictionary of Site attributes by spreadsheet headers."""
107-
return {
112+
site_row_mapping = {
108113
VALUES: [
109114
{USER_ENTERED_VALUE: {STRING_VALUE: self.code}},
110115
{USER_ENTERED_VALUE: {STRING_VALUE: self.guid}},
111-
{USER_ENTERED_VALUE: {STRING_VALUE: self.etag}},
112116
]
113117
}
118+
if self.etag:
119+
site_row_mapping.get(VALUES).append(
120+
{USER_ENTERED_VALUE: {STRING_VALUE: self.etag}}
121+
)
122+
return site_row_mapping

tools/abel/tests/guid_to_entity_map_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ def testAddEntityRaisesAttributeError(self):
8383
_TEST_REPORTING_ENTITY.bc_guid = TEST_REPORTING_GUID
8484

8585
def testAddEntityRaisesKeyError(self):
86+
"""Test adding a different entity with a already existing guid."""
8687
self.guid_to_entity_map.AddEntity(_TEST_REPORTING_ENTITY)
88+
test_virtual_entity = VirtualEntity.FromDict(TEST_VIRTUAL_ENTITY_DICT)
89+
test_virtual_entity.bc_guid = TEST_REPORTING_GUID
8790

8891
with self.assertRaises(KeyError):
89-
self.guid_to_entity_map.AddEntity(_TEST_REPORTING_ENTITY)
92+
self.guid_to_entity_map.AddEntity(test_virtual_entity)
9093

9194
def testGetEntityByGuidRaisesKeyError(self):
9295
with self.assertRaises(KeyError):

tools/validators/instance_validator/tests/entity_instance_test.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,19 +849,31 @@ def testInstance_InvalidLinkFieldMissing_Fails(self):
849849
combination_validator.Validate(entity_instances.get('ENTITY-NAME-GUID'))
850850
)
851851

852+
def testInstance_TypeInstanceWithNoFields_Fails(self):
853+
parsed, default_operation = _Helper(
854+
[path.join(_TESTCASE_PATH, 'BAD', 'type_expecting_fields.yaml')]
855+
)
856+
857+
bad_entity_guid, bad_entity_parsed = next(iter(parsed.items()))
858+
bad_entity = entity_instance.EntityInstance.FromYaml(
859+
bad_entity_guid, bad_entity_parsed, default_operation=default_operation
860+
)
861+
862+
self.assertFalse(self.init_validator.Validate(bad_entity))
863+
852864
def testInstance_ValidLinkEntityName_Success(self):
853865
parsed, default_operation = _Helper(
854-
[path.join(_TESTCASE_PATH, 'GOOD', 'links.yaml')]
866+
[path.join(_TESTCASE_PATH, 'GOOD', 'links.yaml')]
855867
)
856868

857869
entity_instances = {}
858870
for entity_guid, entity_parsed in parsed.items():
859871
entity = entity_instance.EntityInstance.FromYaml(
860-
entity_guid, entity_parsed, default_operation=default_operation
872+
entity_guid, entity_parsed, default_operation=default_operation
861873
)
862874
entity_instances[entity.guid] = entity
863875
combination_validator = entity_instance.CombinationValidator(
864-
self.config_universe, _INIT_CFG, entity_instances
876+
self.config_universe, _INIT_CFG, entity_instances
865877
)
866878

867879
for _, instance in entity_instances.items():
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the License);
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an AS IS BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
a3c2fe35-aa03-48f1-a1ef-426203a74177:
16+
type: HVAC/FAN_SS
17+
code: FAN_1
18+
connections:
19+
46033ce6-7070-4434-98ab-0d8e6a463d46: CONTAINS
20+
21+
# Building
22+
46033ce6-7070-4434-98ab-0d8e6a463d46:
23+
type: FACILITIES/BUILDING
24+
code: US-SEA-BLDG1

tools/validators/instance_validator/validate/entity_instance.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ def _ValidateTranslation(
498498
Returns:
499499
Returns true when the translation is valid on a reporting entity.
500500
"""
501-
502501
if entity.translation is None:
503502
return True
504503

@@ -1067,6 +1066,17 @@ def Validate(self, entity: EntityInstance, is_udmi: bool = True) -> bool:
10671066
)
10681067
is_valid = False
10691068

1069+
entity_type = self.universe.GetEntityType(
1070+
entity.namespace, entity.type_name
1071+
)
1072+
if entity_type:
1073+
if entity_type.GetAllFields():
1074+
if not entity.translation and not entity.links:
1075+
print(f'[ERROR]\tEntity ({entity.guid}: {entity.code}) Has a type '
1076+
'which has defined fields but this instance has neither links '
1077+
'nor a translation.')
1078+
is_valid = False
1079+
10701080
if not self._EntityOperationAndConfigModeValid(entity):
10711081
is_valid = False
10721082

@@ -1085,8 +1095,6 @@ def Validate(self, entity: EntityInstance, is_udmi: bool = True) -> bool:
10851095
if not self._IsFaciltitiesEntitiesMatchPattern(entity):
10861096
is_valid = False
10871097

1088-
# TODO(berkoben): ADD entity needs transl'n or links if type has fields
1089-
10901098
return is_valid
10911099

10921100

tools/validators/instance_validator/validate/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838

3939
def FileNameEnumerationHelper(filename: str) -> str:
40-
"""Adds an UTC timestamp enurmation prefix to the filename.
40+
"""Adds a UTC timestamp enumeration prefix to the filename.
4141
4242
Args:
4343
filename: string representing the filename to be enumerated with a local

0 commit comments

Comments
 (0)