def _TestQueryNotifications(tester, user_cookie, request_dict):
  """Called by the ServiceTester in order to test query_notifications
  service API call.
  """
  validator = tester.validator
  user_id, device_id = tester.GetIdsFromCookie(user_cookie)

  def _MakeNotificationDict(notification):
    """Create a viewpoint dict from the followed object plus its
    referenced viewpoint object.
    """
    notification_dict = {'notification_id': notification.notification_id,
                         'name': notification.name,
                         'sender_id': notification.sender_id,
                         'timestamp': notification.timestamp}

    util.SetIfNotNone(notification_dict, 'op_id', notification.op_id)

    if notification.update_seq is not None or notification.viewed_seq is not None:
      vp_dict = notification_dict.setdefault('inline', {}).setdefault('viewpoint', {})
      vp_dict['viewpoint_id'] = notification.viewpoint_id
      util.SetIfNotNone(vp_dict, 'update_seq', notification.update_seq)
      util.SetIfNotNone(vp_dict, 'viewed_seq', notification.viewed_seq)

    if notification.activity_id is not None:
      viewpoint_id = notification.viewpoint_id
      activity = validator.GetModelObject(Activity, DBKey(viewpoint_id, notification.activity_id))
      activity_dict = activity.MakeMetadataDict()
      notification_dict.setdefault('inline', {})['activity'] = activity_dict

      if activity.name == 'post_comment' and notification.invalidate is None:
        comment_id = activity_dict['post_comment']['comment_id']
        comment = validator.GetModelObject(Comment, DBKey(viewpoint_id, comment_id))
        notification_dict['inline']['comment'] = comment._asdict()

    invalidate = notification.GetInvalidate()
    if invalidate is not None:
      invalidate.pop('headers')
      notification_dict['invalidate'] = invalidate

    return notification_dict

  def _MakeUsageDict():
    """Lookup the user's accounting entries and build a USAGE_METADATA dict."""
    # Make sure that the accounting model is up-to-date.
    validator.ValidateAccounting()

    user_hash = '%s:%d' % (Accounting.USER_SIZE, user_id)

    def _AccountingAsDict(sort_key):
      act = validator.GetModelObject(Accounting, DBKey(user_hash, sort_key), must_exist=False)
      if act is None:
        return None
      act_dict = act._asdict()
      # GetModelObject just returns everything, with no possibility of selecting the columns.
      act_dict.pop('hash_key', None)
      act_dict.pop('sort_key', None)
      act_dict.pop('op_ids', None)
      return act_dict

    usage_dict = {}
    util.SetIfNotNone(usage_dict, 'owned_by', _AccountingAsDict(Accounting.OWNED_BY))
    util.SetIfNotNone(usage_dict, 'shared_by', _AccountingAsDict(Accounting.SHARED_BY))
    util.SetIfNotNone(usage_dict, 'visible_to', _AccountingAsDict(Accounting.VISIBLE_TO))
    if len(usage_dict.keys()) > 0:
      return usage_dict
    return None

  # Send query_notifications request.
  actual_dict = tester.SendRequest('query_notifications', user_cookie, request_dict)

  # Build expected response dict.
  start_key = int(request_dict['start_key']) if 'start_key' in request_dict else None
  limit = request_dict.get('limit', None)
  scan_forward = request_dict.get('scan_forward', True)
  notifications = validator.QueryModelObjects(Notification,
                                              user_id,
                                              start_key=start_key,
                                              limit=limit,
                                              query_forward=scan_forward)

  expected_dict = {'notifications': [_MakeNotificationDict(n) for n in notifications]}
  if len(notifications) > 0:
    expected_dict['last_key'] = '%015d' % notifications[-1].notification_id
  if len(expected_dict['notifications']) > 0:
    usage_dict = _MakeUsageDict()
    if usage_dict is not None:
      last_notification = expected_dict['notifications'][-1]
      last_notification.setdefault('inline', {})['user'] = { 'usage': usage_dict }

  # Create a clear_badges notification if badge needs to be reset.
  notifications = validator.QueryModelObjects(Notification, user_id)
  last_notification = notifications[-1] if notifications else None
  if last_notification is not None and last_notification.badge != 0 and scan_forward:
    validator.ValidateCreateDBObject(Notification,
                                     notification_id=last_notification.notification_id + 1,
                                     user_id=user_id,
                                     name='clear_badges',
                                     timestamp=util._TEST_TIME,
                                     sender_id=user_id,
                                     sender_device_id=device_id,
                                     badge=0)

  tester._CompareResponseDicts('query_notifications', user_id, request_dict, expected_dict, actual_dict)
  return actual_dict
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/test/query_notifications_test.py/_TestQueryNotifications/original