Ejemplo n.º 1
0
    def test_get_and_put(self):
        self.assertEqual(event_models.ReminderEvent.get(0), None)
        test_event = event_models.ReminderEvent.create(0)

        self.assertEqual(test_event.level, 0)  # Tests @property taken from ID.
        self.assertEqual(test_event.model, 'Device')
        self.assertEqual(test_event.name, 'reminder_level_0')
        self.assertEqual(event_models.ReminderEvent.make_name(0),
                         'reminder_level_0')

        test_event.description = 'Device is due soon.'
        tomorrow = event_models.create_timedelta(1, 'd')
        test_event.conditions = [
            event_models.CustomEventCondition(name='due_date',
                                              opsymbol='<',
                                              value=tomorrow),
            event_models.CustomEventCondition(name='locked',
                                              opsymbol='=',
                                              value=False),
            event_models.CustomEventCondition(name='lost',
                                              opsymbol='=',
                                              value=False)
        ]
        test_event.template = 'reminder_due'
        test_event.put()

        self.assertEqual(test_event, event_models.ReminderEvent.get(0))
Ejemplo n.º 2
0
  def test_match(self):
    """Tests the match function in every possible way."""
    test_shelf = shelf_model.Shelf.enroll(
        'test@{}'.format(loanertest.USER_DOMAIN), 'US-NYC', 16,
        'Statue of Liberty', 40.6892534, -74.0466891, 1.0,
        loanertest.USER_EMAIL)

    self.assertFalse(event_models.CustomEventCondition(  # 16 !< 10.
        name='capacity', opsymbol='<', value=10).match(test_shelf))

    self.assertFalse(event_models.CustomEventCondition(  # None !< timedelta.
        name='last_audit_time', opsymbol='<', value='-3d').match(test_shelf))

    self.assertTrue(event_models.CustomEventCondition(  # 16 <= 16.
        name='capacity', opsymbol='<=', value=16).match(test_shelf))

    self.assertFalse(event_models.CustomEventCondition(  # None !<= timedelta.
        name='last_audit_time', opsymbol='<=', value='-3d').match(test_shelf))

    self.assertTrue(event_models.CustomEventCondition(  # 16 == 16.
        name='capacity', opsymbol='==', value=16).match(test_shelf))

    self.assertTrue(event_models.CustomEventCondition(  # 16 != 10.
        name='capacity', opsymbol='!=', value=10).match(test_shelf))

    self.assertTrue(event_models.CustomEventCondition(  # 16 > 10.
        name='capacity', opsymbol='>', value=10).match(test_shelf))

    self.assertFalse(event_models.CustomEventCondition(  # 16 !>= 24.
        name='capacity', opsymbol='>=', value=24).match(test_shelf))
Ejemplo n.º 3
0
 def setup_events(self):
   tomorrow_delta = event_models.create_timedelta(1, 'd')
   self.reminder_due_event = event_models.ReminderEvent.create(0)
   self.reminder_due_event.description = 'Device due in less than one day.'
   self.reminder_due_event.conditions = [
       event_models.CustomEventCondition(
           name='due_date', opsymbol='<', value=tomorrow_delta),
       event_models.CustomEventCondition(
           name='due_date', opsymbol='>', value=datetime.timedelta(seconds=0)),
       event_models.CustomEventCondition(
           name='locked', opsymbol='=', value=False),
       event_models.CustomEventCondition(
           name='lost', opsymbol='=', value=False)
   ]
   self.reminder_due_event.actions = ['DO_THING1', 'DO_THING2', 'DO_THING3']
   self.reminder_due_event.put()
Ejemplo n.º 4
0
  def test_build_query_components(self):
    """Tests building a simple query and one with two inequality filters."""
    self.setup_events()

    query_components1 = self.device_event._build_query_components()
    self.assertEqual(query_components1['less_than_properties'], ['due_date'])
    self.assertEqual(query_components1['extra_inequality_conditions'], [])
    self.assertLen(
        query_components1['query'].filters._ConjunctionNode__nodes, 3)

    # Add another inequality filter that uses <. Propery name hould be added to
    # less_than_properties, condition should be added to
    # extra_inequality_filters, and the core query should be identical.
    self.device_event.conditions.append(
        event_models.CustomEventCondition(
            name='last_heartbeat', opsymbol='<',
            value=event_models.create_timedelta(-5, 'd')))
    query_components2 = self.device_event._build_query_components()
    self.assertListEqual(
        query_components2['less_than_properties'],
        ['due_date', 'last_heartbeat'])
    self.assertEqual(
        query_components2['extra_inequality_conditions'],
        [self.device_event.conditions[3]])
    self.assertLen(
        query_components2['query'].filters._ConjunctionNode__nodes, 3)
Ejemplo n.º 5
0
  def test_get_filter(self):
    condition = event_models.CustomEventCondition(
        name='foo', opsymbol='<', value=42)
    test_filter = condition.get_filter()

    self.assertIsInstance(test_filter, ndb.query.FilterNode)
    self.assertEqual(test_filter._FilterNode__name, condition.name)
    self.assertEqual(test_filter._FilterNode__opsymbol, condition.opsymbol)
    self.assertEqual(test_filter._FilterNode__value, condition.value)
Ejemplo n.º 6
0
    def setup_events(self):
        """Creates test events."""
        self.device_event = event_models.CustomEvent.create('device_event')
        self.device_event.description = 'This is the device event.'
        self.device_event.model = 'Device'
        self.device_event.conditions = [
            event_models.CustomEventCondition(
                name='due_date',
                opsymbol='<',
                value=event_models.create_timedelta(1, 'd')),
            event_models.CustomEventCondition(name='locked',
                                              opsymbol='=',
                                              value=False),
            event_models.CustomEventCondition(name='lost',
                                              opsymbol='=',
                                              value=False)
        ]
        self.device_event.actions = ['DO_THING1', 'DO_THING2', 'DO_THING3']
        self.device_event.put()

        self.shelf_event1 = event_models.CustomEvent.create('shelf_event_1')
        self.shelf_event1.description = 'This is the first shelf event.'
        self.shelf_event1.model = 'Shelf'
        self.shelf_event1.conditions = [
            event_models.CustomEventCondition(name='last_audit_time',
                                              opsymbol='<',
                                              value=_THREE_DAYS_AGO_DELTA),
            event_models.CustomEventCondition(name='enabled',
                                              opsymbol='=',
                                              value=True)
        ]
        self.shelf_event1.actions = ['DO_THING4', 'DO_THING5', 'DO_THING6']
        self.shelf_event1.put()

        self.shelf_event2 = event_models.CustomEvent.create('shelf_event_2')
        self.shelf_event2.description = 'This is the second shelf event.'
        self.shelf_event2.model = 'Shelf'
        self.shelf_event2.conditions = [
            event_models.CustomEventCondition(name='last_audit_time',
                                              opsymbol='<',
                                              value=_THREE_DAYS_AGO_DELTA),
            event_models.CustomEventCondition(name='enabled',
                                              opsymbol='=',
                                              value=True),
            event_models.CustomEventCondition(name='capacity',
                                              opsymbol='=',
                                              value=24),
        ]
        self.shelf_event2.actions = ['DO_THING7', 'DO_THING8', 'DO_THING9']
        self.shelf_event2.put()
Ejemplo n.º 7
0
def _import_event_dict(model, event_dict):
  """Imports a YAML-derived event dictionary into a model, and put.

  Args:
    model: event_models.CoreEvent, A datastore NDB model of this class or a
        subclass.
    event_dict: dict, A configuration derived from YAML.
  """
  for prop, value in event_dict.iteritems():
    if prop not in ['actions', 'conditions', 'level', 'name']:
      setattr(model, prop, value)
    elif prop == 'actions':
      for action in value:
        model.actions.append(action)
    elif prop == 'conditions':
      for condition in value:
        condition['value'] = _interpret_time(condition['value'])
        model.conditions.append(event_models.CustomEventCondition(**condition))
  model.put()
Ejemplo n.º 8
0
  def test_null_due_dates(self, mock_loginfo):
    """Tests with events and devices, but due_date for both devices are null."""
    self.setup_devices()  # pylint: disable=no-value-for-parameter
    tomorrow_delta = event_models.create_timedelta(1, 'd')
    simple_reminder_due_event = event_models.ReminderEvent.create(0)
    simple_reminder_due_event.description = 'Due date-based event.'
    simple_reminder_due_event.conditions = [
        event_models.CustomEventCondition(
            name='due_date', opsymbol='<', value=tomorrow_delta)]
    simple_reminder_due_event.put()
    self.testbed.mock_raiseevent.reset_mock()

    response = self.testapp.get(
        r'/_cron/run_reminder_events?find_remindable_devices=true')

    self.assertEqual(response.status_int, 200)
    retrieved_device1 = self.device1.key.get()
    retrieved_device2 = self.device2.key.get()

    self.assertIsNone(retrieved_device1.next_reminder)
    self.assertIsNone(retrieved_device2.next_reminder)