Python google.appengine.api.datastore_errors.BadValueError() Examples

The following are 30 code examples of google.appengine.api.datastore_errors.BadValueError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.api.datastore_errors , or try the search function .
Example #1
Source File: model.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def validate(self, value):
    """Validate value.

    Args:
      value: model value.

    Returns:
      Whether the specified value is valid data type value.

    Raises:
      BadValueError: when value is not of self.data_type type.
    """
    if value is not None and not isinstance(value, self.data_type):
      raise datastore_errors.BadValueError(
          "Property %s must be convertible to a %s instance (%s)" %
          (self.name, self.data_type, value))
    return super(JsonProperty, self).validate(value) 
Example #2
Source File: tag_api.py    From loaner with Apache License 2.0 6 votes vote down vote up
def create(self, request):
    """Creates a new tag and inserts the instance into datastore."""
    self.check_xsrf_token(self.request_state)
    try:
      tag_model.Tag.create(
          user_email=user.get_user_email(),
          name=request.tag.name,
          hidden=request.tag.hidden,
          color=request.tag.color,
          protect=request.tag.protect,
          description=request.tag.description)
    except datastore_errors.BadValueError as err:
      raise endpoints.BadRequestException(
          'Tag creation failed due to: %s' % err)

    return message_types.VoidMessage() 
Example #3
Source File: template_model.py    From loaner with Apache License 2.0 6 votes vote down vote up
def create(cls, name, title=None, body=None):
    """Creates a model and entity."""
    if not name:
      raise datastore_errors.BadValueError(
          'The Template name must not be empty.')
    entity = cls(title=title,
                 body=body)
    template = cls.get_by_id(name)
    if template is not None:
      raise datastore_errors.BadValueError(
          'Create template: A Template entity with name %r already exists.' %
          name)
    entity.key = ndb.Key(cls, name)
    entity.put()
    logging.info('Creating a new template with name %r.', name)
    cls.cached_templates = []
    return entity 
Example #4
Source File: shelf_api.py    From loaner with Apache License 2.0 6 votes vote down vote up
def enroll(self, request):
    """Enrolls a shelf in the program."""
    user_email = user.get_user_email()
    self.check_xsrf_token(self.request_state)
    try:
      shelf_model.Shelf.enroll(
          user_email=user_email,
          friendly_name=request.friendly_name,
          location=request.location,
          latitude=request.latitude,
          longitude=request.longitude,
          altitude=request.altitude,
          capacity=request.capacity,
          audit_notification_enabled=request.audit_notification_enabled,
          responsible_for_audit=request.responsible_for_audit,
          audit_interval_override=request.audit_interval_override,
      )
    except (shelf_model.EnrollmentError, datastore_errors.BadValueError) as err:
      raise endpoints.BadRequestException(str(err))

    return message_types.VoidMessage() 
Example #5
Source File: tag_api.py    From loaner with Apache License 2.0 6 votes vote down vote up
def update(self, request):
    """Updates an existing tag."""
    self.check_xsrf_token(self.request_state)
    key = api_utils.get_ndb_key(urlsafe_key=request.tag.urlsafe_key)
    tag = key.get()
    if tag.protect:
      raise endpoints.BadRequestException(
          'Cannot update tag %s because it is protected.' % tag.name)
    try:
      tag.update(
          user_email=user.get_user_email(),
          name=request.tag.name,
          hidden=request.tag.hidden,
          protect=request.tag.protect,
          color=request.tag.color,
          description=request.tag.description)
    except datastore_errors.BadValueError as err:
      raise endpoints.BadRequestException(
          'Tag update failed due to: %s' % str(err))
    return message_types.VoidMessage() 
Example #6
Source File: tag_model.py    From loaner with Apache License 2.0 6 votes vote down vote up
def update(self, user_email, **kwargs):
    """Updates an existing tag.

    Args:
      user_email: str, email of the user creating the tag.
      **kwargs: kwargs for the update API.

    Raises:
      datastore_errors.BadValueError: If the tag name is an empty string.
    """
    if not kwargs['name']:
      raise datastore_errors.BadValueError('The tag name must not be empty.')

    if kwargs['name'] != self.name:
      logging.info(
          'Renaming the tag with name %r to %r.', self.name, kwargs['name'])

    self.populate(**kwargs)
    self.put()
    logging.info(
        'Updating a tag with urlsafe key %r and name %r.',
        self.key.urlsafe(), self.name)
    self.stream_to_bq(
        user_email, 'Updated a tag with name %r.' % self.name) 
Example #7
Source File: api_utils.py    From loaner with Apache License 2.0 6 votes vote down vote up
def get_datastore_cursor(urlsafe_cursor):
  """Builds a datastore.Cursor from a urlsafe cursor.

  Args:
    urlsafe_cursor: str, The urlsafe representation of a datastore.Cursor.

  Returns:
    datastore.Cursor instance.

  Raises:
    endpoints.BadRequestException: if the creation of the datastore.Cursor
        fails.
  """
  try:
    return datastore_query.Cursor(urlsafe=urlsafe_cursor)
  except datastore_errors.BadValueError:
    raise endpoints.BadRequestException(_MALFORMED_PAGE_TOKEN_MSG) 
Example #8
Source File: json_util.py    From locality-sensitive-hashing with MIT License 6 votes vote down vote up
def validate(self, value):
    """Validate value.

    Args:
      value: model value.

    Returns:
      Whether the specified value is valid data type value.

    Raises:
      BadValueError: when value is not of self.data_type type.
    """
    if value is not None and not isinstance(value, self.data_type):
      raise datastore_errors.BadValueError(
          "Property %s must be convertible to a %s instance (%s)" % 
          (self.name, self.data_type, value))
    return super(JsonProperty, self).validate(value) 
Example #9
Source File: bq_state.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _pre_put_hook(self):
    super(BqState, self)._pre_put_hook()
    if bool(self.recent) != bool(self.oldest):
      raise datastore_errors.BadValueError(
          'Internal error; recent and oldest must both be set')
    if self.oldest:
      if self.oldest >= self.recent:
        raise datastore_errors.BadValueError('Internal error; oldest >= recent')
      if self.oldest.second or self.oldest.microsecond:
        raise datastore_errors.BadValueError(
            'Internal error; oldest has seconds')
      if self.recent.second or self.recent.microsecond:
        raise datastore_errors.BadValueError(
            'Internal error; recent has seconds')


### Private APIs. 
Example #10
Source File: bq_state_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_BqState(self):
    now = datetime.datetime(2020, 1, 2, 3, 4)
    bq_state.BqState(id='foo').put()
    bq_state.BqState(
        id='foo', recent=now,
        oldest=now - datetime.timedelta(seconds=60)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      bq_state.BqState(id='foo', oldest=now).put()
    with self.assertRaises(datastore_errors.BadValueError):
      bq_state.BqState(id='foo', recent=now).put()
    with self.assertRaises(datastore_errors.BadValueError):
      bq_state.BqState(id='foo', recent=now, oldest=now).put()
    with self.assertRaises(datastore_errors.BadValueError):
      bq_state.BqState(id='foo', recent=now, oldest=now).put()
    with self.assertRaises(datastore_errors.BadValueError):
      bq_state.BqState(
          id='foo',
          recent=now - datetime.timedelta(seconds=60.1),
          oldest=now - datetime.timedelta(seconds=60)).put() 
Example #11
Source File: task_result_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_new_run_result_duration_no_exit_code(self):
    request = _gen_request()
    to_run = task_to_run.new_task_to_run(request, 0)
    actual = task_result.new_run_result(request, to_run, u'localhost', u'abc', {
        u'id': [u'localhost'],
        u'foo': [u'bar', u'biz']
    })
    actual.completed_ts = self.now
    actual.modified_ts = self.now
    actual.started_ts = self.now
    actual.duration = 1.
    actual.state = task_result.State.COMPLETED
    # Trigger _pre_put_hook().
    with self.assertRaises(datastore_errors.BadValueError):
      actual.put()
    actual.state = task_result.State.TIMED_OUT
    actual.put()
    expected = self._gen_result(
        completed_ts=self.now,
        duration=1.,
        modified_ts=self.now,
        failure=True,
        started_ts=self.now,
        state=task_result.State.TIMED_OUT)
    self.assertEqual(expected, actual.to_dict()) 
Example #12
Source File: task_queues_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_BotTaskDimensions(self):
    cls = task_queues.BotTaskDimensions
    now = datetime.datetime(2010, 1, 2, 3, 4, 5)
    with self.assertRaises(datastore_errors.BadValueError):
      cls(dimensions_flat=['a:b']).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(valid_until_ts=now).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(valid_until_ts=now, dimensions_flat=['a:b', 'a:b']).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(valid_until_ts=now, dimensions_flat=['c:d', 'a:b']).put()

    a = cls(valid_until_ts=now, dimensions_flat=['a:b'])
    a.put()
    self.assertEqual(True, a.is_valid({'a': ['b']}))
    self.assertEqual(True, a.is_valid({'a': ['b', 'c']}))
    self.assertEqual(False, a.is_valid({'x': ['c']})) 
Example #13
Source File: task_queues_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_TaskDimensions(self):
    cls = task_queues.TaskDimensions
    setcls = task_queues.TaskDimensionsSet
    now = datetime.datetime(2010, 1, 2, 3, 4, 5)
    with self.assertRaises(datastore_errors.BadValueError):
      cls().put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(sets=[setcls(valid_until_ts=now)]).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(sets=[setcls(dimensions_flat=['a:b'])]).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(sets=[
        setcls(valid_until_ts=now, dimensions_flat=['a:b', 'a:b'])]).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(sets=[
        setcls(valid_until_ts=now, dimensions_flat=['c:d', 'a:b'])]).put()
    with self.assertRaises(datastore_errors.BadValueError):
      cls(sets=[
        setcls(valid_until_ts=now, dimensions_flat=['a:b', 'c:d']),
        setcls(valid_until_ts=now, dimensions_flat=['a:b', 'c:d']),
        ]).put()
    cls(sets=[setcls(valid_until_ts=now, dimensions_flat=['a:b'])]).put() 
Example #14
Source File: task_request_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_request_bad_named_cache_and_cipd_input(self):
    # A CIPD package and named caches cannot be mapped to the same path.
    req = _gen_request(
        properties=_gen_properties(
            caches=[
              task_request.CacheEntry(name='git_chromium', path='git_cache'),
            ],
            cipd_input=_gen_cipd_input(
                packages=[
                  task_request.CipdPackage(
                      package_name='foo', path='git_cache', version='latest'),
                ])))
    with self.assertRaises(datastore_errors.BadValueError):
      req.put()
    req = _gen_request(
        properties=_gen_properties(
            caches=[
                task_request.CacheEntry(name='git_chromium', path='git_cache1'),
            ],
            cipd_input=_gen_cipd_input(packages=[
                task_request.CipdPackage(
                    package_name='foo', path='git_cache2', version='latest'),
            ]))).put() 
Example #15
Source File: task_request_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_request_bad_execution_timeout(self):
    # When used locally, it is set to 1, which means it's impossible to test
    # below _MIN_TIMEOUT_SECS but above 0.
    self.mock(task_request, '_MIN_TIMEOUT_SECS', 30)
    p = _gen_request(properties=_gen_properties(execution_timeout_secs=0))
    with self.assertRaises(datastore_errors.BadValueError):
      # Only termination task may have 0.
      p.put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(
            execution_timeout_secs=task_request._MIN_TIMEOUT_SECS-1))
    _gen_request(
        properties=_gen_properties(
          execution_timeout_secs=task_request._MIN_TIMEOUT_SECS))
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(
              execution_timeout_secs=task_request.MAX_TIMEOUT_SECS+1))
    _gen_request(
        properties=_gen_properties(
            execution_timeout_secs=task_request.MAX_TIMEOUT_SECS)).put() 
Example #16
Source File: task_queues.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _pre_put_hook(self):
    super(TaskDimensions, self)._pre_put_hook()
    sets = set()
    for s in self.sets:
      s._pre_put_hook()
      sets.add('\000'.join(s.dimensions_flat))
    if len(sets) != len(self.sets):
      # Make sure there's no duplicate TaskDimensionsSet.
      raise datastore_errors.BadValueError(
          '%s.sets must all be unique' % self.__class__.__name__)


### Private APIs.


# Limit in rebuild_task_cache_async. Meant to be overridden in unit test. 
Example #17
Source File: task_request.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _validate_env_prefixes(prop, value):
  # pylint: disable=protected-access
  maxlen = 1024
  for k, values in value.items():
    _validate_env_key(prop, k)
    if (not isinstance(values, list) or
        not all(isinstance(v, unicode) for v in values)):
      raise TypeError(
          '%s must have list unicode value for key %r, not %r' %
          (prop._name, k, values))
    for path in values:
      if len(path) > maxlen:
        raise datastore_errors.BadValueError(
            '%s: value for key %r is too long: %d > %d' %
            (prop._name, k, len(path), maxlen))
      _validate_rel_path('Env Prefix', path)

  if len(value) > 64:
    raise datastore_errors.BadValueError(
        '%s can have up to 64 keys' % prop._name) 
Example #18
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_pubsub(self):
    _gen_request(pubsub_topic=u'projects/a/topics/abc').put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(pubsub_topic=u'a')
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(pubsub_topic=u'projects/a/topics/ab').put()
    _gen_request(pubsub_topic=u'projects/' + u'a'*1004 + u'/topics/abc').put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          pubsub_topic=u'projects/' + u'a' * 1005 + u'/topics/abc').put() 
Example #19
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_env(self):
    # Type error.
    with self.assertRaises(TypeError):
      _gen_request(properties=_gen_properties(env=[]))
    with self.assertRaises(TypeError):
      _gen_request(properties=_gen_properties(env={u'a': 1}))
    _gen_request(properties=_gen_properties(env={})).put()
    e = {u'k': u'v'}
    _gen_request(properties=_gen_properties(env=e)).put()
    # Key length.
    e = {u'k'*64: u'v'}
    _gen_request(properties=_gen_properties(env=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k'*65: u'v'}
      _gen_request(properties=_gen_properties(env=e)).put()
    # # keys.
    e = {u'k%s' % i: u'v' for i in range(64)}
    _gen_request(properties=_gen_properties(env=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k%s' % i: u'v' for i in range(65)}
      _gen_request(properties=_gen_properties(env=e)).put()
    # Value length.
    e = {u'k': u'v'*1024}
    _gen_request(properties=_gen_properties(env=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k': u'v'*1025}
      _gen_request(properties=_gen_properties(env=e)).put() 
Example #20
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_dimensions_key(self):
    # Max # keys.
    d = {u'a%s' % string.ascii_letters[i]: [unicode(i)] for i in range(31)}
    d[u'pool'] = [u'a']
    _gen_request(properties=_gen_properties(dimensions=d)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      d = {u'a%s' % string.ascii_letters[i]: [unicode(i)] for i in range(32)}
      d[u'pool'] = [u'a']
      _gen_request(properties=_gen_properties(dimensions=d)).put()

    with self.assertRaises(datastore_errors.BadValueError):
      # Key regexp.
      d = {u'pool': [u'default'], u'1': [u'value']}
      _gen_request(properties=_gen_properties(dimensions=d)).put()
    # Key length.
    d = {
        u'pool': [u'default'],
        u'v' * config.DIMENSION_KEY_LENGTH: [u'v'],
    }
    _gen_request(properties=_gen_properties(dimensions=d)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      d = {
          u'pool': [u'default'],
          u'v' * (config.DIMENSION_KEY_LENGTH + 1): [u'value'],
      }
      _gen_request(properties=_gen_properties(dimensions=d)).put() 
Example #21
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_dimensions(self):
    # Type error.
    with self.assertRaises(TypeError):
      _gen_request(properties=_gen_properties(dimensions=[]))
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(properties=_gen_properties(dimensions={}))
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(dimensions={u'id': u'b', u'a:': u'b'}))
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(dimensions={u'id': u'b', u'a.': u'b'}))
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(dimensions={u'id': u'b', u'a': [u'b']}))
    # >1 value for id.
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(properties=_gen_properties(dimensions={u'id': [u'a', u'b']}))
    # >1 value for pool.
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(
          properties=_gen_properties(dimensions={u'pool': [u'b', u'b']}))
    _gen_request(
        properties=_gen_properties(
            dimensions={u'id': [u'b'], u'pool': [u'b']})).put()
    _gen_request(
        properties=_gen_properties(dimensions={
            u'id': [u'b'],
            u'pool': [u'b'],
            u'a.': [u'c']
        })).put()
    _gen_request(
        properties=_gen_properties(dimensions={
            u'pool': [u'b'],
            u'a.': [u'b', u'c']
        })).put() 
Example #22
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_env_prefixes(self):
    # Type error.
    with self.assertRaises(TypeError):
      _gen_request(properties=_gen_properties(env_prefixes=[]))
    with self.assertRaises(TypeError):
      _gen_request(properties=_gen_properties(env_prefixes={u'a': 1}))
    _gen_request(properties=_gen_properties(env_prefixes={})).put()
    e = {u'k': [u'v']}
    _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    # Key length.
    e = {u'k'*64: [u'v']}
    _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k'*65: [u'v']}
      _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    # # keys.
    e = {u'k%s' % i: [u'v'] for i in range(64)}
    _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k%s' % i: [u'v'] for i in range(65)}
      _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    # Value length.
    e = {u'k': [u'v'*1024]}
    _gen_request(properties=_gen_properties(env_prefixes=e)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      e = {u'k': [u'v'*1025]}
      _gen_request(properties=_gen_properties(env_prefixes=e)).put() 
Example #23
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_service_account(self):
    _gen_request(service_account=u'none').put()
    _gen_request(service_account=u'bot').put()
    _gen_request(service_account=u'joe@localhost').put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(service_account=u'joe').put()
    _gen_request(service_account=u'joe@'+u'l'*124).put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(service_account=u'joe@'+u'l'*125).put() 
Example #24
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_tags(self):
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(manual_tags=['a']).put() 
Example #25
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_tags_too_many(self):
    _gen_request(manual_tags=['a:b'] * 256).put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(manual_tags=['a:b'] * 257).put() 
Example #26
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_request_bad_realm(self):
    _gen_request(realm=None).put()
    _gen_request(realm='test:realm').put()
    with self.assertRaises(datastore_errors.BadValueError):
      _gen_request(realm='invalid_realm').put() 
Example #27
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_validate_priority(self):
    with self.assertRaises(TypeError):
      task_request.validate_priority(None)
    with self.assertRaises(TypeError):
      task_request.validate_priority('1')
    with self.assertRaises(datastore_errors.BadValueError):
      task_request.validate_priority(-1)
    with self.assertRaises(datastore_errors.BadValueError):
      task_request.validate_priority(task_request.MAXIMUM_PRIORITY + 1)
    task_request.validate_priority(0)
    task_request.validate_priority(1)
    task_request.validate_priority(task_request.MAXIMUM_PRIORITY) 
Example #28
Source File: task_request_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_secret_bytes(self):
    task_request.SecretBytes(secret_bytes='a'*(20*1024)).put()
    with self.assertRaises(datastore_errors.BadValueError):
      task_request.SecretBytes(secret_bytes='a'*(20*1024+1)).put() 
Example #29
Source File: task_queues.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _pre_put_hook(self):
    super(BotTaskDimensions, self)._pre_put_hook()
    if not self.valid_until_ts:
      raise datastore_errors.BadValueError(
          '%s.valid_until_ts is required' % self.__class__.__name__)
    _validate_dimensions_flat(self) 
Example #30
Source File: task_scheduler_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_schedule_request_id_without_pool(self):
    auth_testing.mock_is_admin(self)
    self._register_bot(0, self.bot_dimensions)
    with self.assertRaises(datastore_errors.BadValueError):
      self._quick_schedule(
          0,
          task_slices=[
              task_request.TaskSlice(
                  expiration_secs=60,
                  properties=_gen_properties(dimensions={u'id': [u'abc']}),
                  wait_for_capacity=False),
          ])