def testGet_ErrorFetching_ReportsError(self, mock_logging_error): self.testapp.get('/update_test_metadata', status=500) self.assertEqual(anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('ms')) self.assertEqual(anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('score')) self.assertEqual(2, mock_logging_error.call_count)
def testGet_UpdatesImprovementDirection(self): self.testapp.get('/update_test_metadata') self.assertEqual(anomaly.DOWN, units_to_direction.GetImprovementDirection('ms')) self.assertEqual(anomaly.UP, units_to_direction.GetImprovementDirection('score')) self.assertEqual( anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('does-not-exist'))
def testUpdateFromJson_ExistingUnit_ChangesDirection(self): units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'down'}, }) self.assertEqual( anomaly.DOWN, units_to_direction.GetImprovementDirection('ms')) units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'up'}, }) self.assertEqual( anomaly.UP, units_to_direction.GetImprovementDirection('ms'))
def testUpdateFromJson_ExistingUnitNotInNewList_RemovesUnit(self): units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'down'}, 'score': {'improvement_direction': 'up'}, }) self.assertEqual( anomaly.UP, units_to_direction.GetImprovementDirection('score')) units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'down'}, }) self.assertEqual( anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('score'))
def testUpdateFromJson_UnknownUnit_Added(self): units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'down'}, }) self.assertEqual( anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('runs/s')) units_to_direction.UpdateFromJson({ 'ms': {'improvement_direction': 'down'}, 'runs/s': {'improvement_direction': 'up'}, }) self.assertEqual( anomaly.UP, units_to_direction.GetImprovementDirection('runs/s'))
def testUpdateFromJson_SetsImprovementDirections(self): units_to_direction.UpdateFromJson({ 'description': 'this is ignored', 'ms': {'improvement_direction': 'down'}, 'score': {'improvement_direction': 'up'}, }) self.assertEqual( anomaly.DOWN, units_to_direction.GetImprovementDirection('ms')) self.assertEqual( anomaly.UP, units_to_direction.GetImprovementDirection('score')) self.assertEqual( anomaly.UNKNOWN, units_to_direction.GetImprovementDirection('does-not-exist'))
def _GetOrCreateTest(name, parent_test_path, properties): """Either gets an entity if it already exists, or creates one. If the entity already exists but the properties are different than the ones specified, then the properties will be updated first. This implies that a new point is being added for an existing TestMetadata, so if the TestMetadata has been previously marked as deprecated then it can be updated and marked as non-deprecated. If the entity doesn't yet exist, a new one will be created with the given properties. Args: name: The string ID of the Test to get or create. parent_test_path: The test_path of the parent entity. properties: A dictionary of properties that should be set. Returns: An entity (which has already been put). Raises: datastore_errors.BadRequestError: Something went wrong getting the entity. """ test_path = '%s/%s' % (parent_test_path, name) existing = graph_data.TestMetadata.get_by_id(test_path) if not existing: # Add improvement direction if this is a new test. if 'units' in properties and 'improvement_direction' not in properties: units = properties['units'] direction = units_to_direction.GetImprovementDirection(units) properties['improvement_direction'] = direction elif 'units' not in properties or properties['units'] is None: properties['improvement_direction'] = anomaly.UNKNOWN else: print properties new_entity = graph_data.TestMetadata(id=test_path, **properties) new_entity.put() # TODO(sullivan): Consider putting back Test entity in a scoped down # form so we can check if it exists here. return new_entity # Flag indicating whether we want to re-put the entity before returning. properties_changed = False if existing.deprecated: existing.deprecated = False properties_changed = True # Special case to update improvement direction from units for TestMetadata # entities when units are being updated. If an improvement direction is # explicitly provided in the properties, then we can skip this check since it # will get overwritten below. Additionally, by skipping we avoid # touching the entity and setting off an expensive put() operation. if properties.get('improvement_direction') is None: units = properties.get('units') if units: direction = units_to_direction.GetImprovementDirection(units) if direction != existing.improvement_direction: properties['improvement_direction'] = direction # Go through the list of general properties and update if necessary. for prop, value in properties.items(): if (hasattr(existing, prop) and value is not None and getattr(existing, prop) != value): setattr(existing, prop, value) properties_changed = True if properties_changed: existing.put() return existing
def _GetOrCreateTest(name, parent_key, properties): """Either gets an entity if it already exists, or creates one. If the entity already exists but the properties are different than the ones specified, then the properties will be updated first. This implies that a new point is being added for an existing Test, so if the Test has been previously marked as deprecated or associated with a stoppage alert, then it can be updated and marked as non-deprecated. If the entity doesn't yet exist, a new one will be created with the given properties. Args: name: The string ID of the Test to get or create. parent_key: The key of the parent entity. properties: A dictionary of properties that should be set. Returns: An entity (which has already been put). Raises: datastore_errors.BadRequestError: Something went wrong getting the entity. """ existing = graph_data.Test.get_by_id(name, parent_key) if not existing: # Add improvement direction if this is a new test. if 'units' in properties: units = properties['units'] direction = units_to_direction.GetImprovementDirection(units) properties['improvement_direction'] = direction new_entity = graph_data.Test(id=name, parent=parent_key, **properties) new_entity.put() return new_entity # Flag indicating whether we want to re-put the entity before returning. properties_changed = False if existing.deprecated: existing.deprecated = False properties_changed = True if existing.stoppage_alert: alert = existing.stoppage_alert.get() if alert: alert.recovered = True alert.put() else: logging.warning('Stoppage alert %s not found.', existing.stoppage_alert) existing.stoppage_alert = None properties_changed = True # Special case to update improvement direction from units for Test entities # when units are being updated. If an improvement direction is explicitly # provided in the properties, then it will be updated again below. units = properties.get('units') if units: direction = units_to_direction.GetImprovementDirection(units) if direction != existing.improvement_direction: existing.improvement_direction = direction properties_changed = True # Go through the list of general properties and update if necessary. for prop, value in properties.items(): if (hasattr(existing, prop) and value is not None and getattr(existing, prop) != value): setattr(existing, prop, value) properties_changed = True if properties_changed: existing.put() return existing