Exemple #1
0
 def test_returns_query_for_anded_conditions(self):
     cond1 = qvarn.Equal('foo1', 'bar1')
     cond2 = qvarn.NotEqual('foo2', 'bar2')
     cond = qvarn.All(cond1, cond2)
     counter = slog.Counter()
     all_cond = qvarn.All()
     query, values = qvarn.sql_select(counter, cond, all_cond, 'TRUE')
     self.assertTrue(isinstance(query, str))
     self.assertTrue(isinstance(values, dict))
Exemple #2
0
 def test_returns_query_for_simple_equal(self):
     cond = qvarn.Equal('foo', 'bar')
     counter = slog.Counter()
     all_cond = qvarn.All()
     query, values = qvarn.sql_select(counter, cond, all_cond, 'TRUE')
     self.assertTrue(isinstance(query, str))
     self.assertTrue(isinstance(values, dict))
Exemple #3
0
 def add_cond(self, cond):
     if self.cond is None:
         self.cond = cond
     elif isinstance(self.cond, qvarn.All):
         self.cond.append_subcondition(cond)
     else:
         self.cond = qvarn.All(self.cond, cond)
 def _find_notifications(self, listener_id):
     cond = qvarn.All(
         qvarn.Equal('type', 'notification'),
         qvarn.Equal('listener_id', listener_id),
     )
     obj_ids = [keys['obj_id'] for keys, _ in self._store.get_matches(cond)]
     qvarn.log.log('trace',
                   msg_text='Found notifications',
                   notifications=obj_ids)
     return obj_ids
Exemple #5
0
 def _get_resource_type_given_path(self, path):
     cond = qvarn.All(
         qvarn.Equal('path', path),
         qvarn.Equal('type', 'resource_type'),
     )
     results = self._store.get_matches(cond=cond)
     qvarn.log.log('trace',
                   msg_text='_get_resource_type_given_path',
                   results=results,
                   path=path)
     return [obj for _, obj in results]
    def _get_notifications_list(self, *args, **kwargs):
        def timestamp(pair):
            _, obj = pair
            return obj['timestamp']

        listener_id = kwargs['listener_id']
        cond = qvarn.All(qvarn.Equal('type', 'notification'),
                         qvarn.Equal('listener_id', listener_id))
        pairs = self._store.get_matches(cond)
        ordered = sorted(pairs, key=timestamp)
        body = {'resources': [{'id': keys['obj_id']} for keys, _ in ordered]}
        return qvarn.ok_response(body)
 def _get_a_notification(self, *args, **kwargs):
     listener_id = kwargs['listener_id']
     notification_id = kwargs['notification_id']
     cond = qvarn.All(
         qvarn.Equal('type', 'notification'),
         qvarn.Equal('listener_id', listener_id),
         qvarn.Equal('id', notification_id),
     )
     pairs = self._store.get_matches(cond)
     if not pairs:
         return qvarn.no_such_resource_response(notification_id)
     if len(pairs) > 1:
         raise qvarn.TooManyResources(notification_id)
     return qvarn.ok_response(pairs[0][1])
 def _delete_notification(self, *args, **kwargs):
     listener_id = kwargs['listener_id']
     notification_id = kwargs['notification_id']
     cond = qvarn.All(
         qvarn.Equal('type', 'notification'),
         qvarn.Equal('listener_id', listener_id),
         qvarn.Equal('id', notification_id),
     )
     for keys, _ in self._store.get_matches(cond):
         values = {
             key: keys[key]
             for key in keys if isinstance(keys[key], str)
         }
         self._store.remove_objects(**values)
     return qvarn.ok_response({})
Exemple #9
0
    def _get_resource_type_given_type(self, type_name):
        cond = qvarn.All(
            qvarn.Equal('id', type_name),
            qvarn.Equal('type', 'resource_type'),
        )
        results = self._store.get_matches(cond=cond)
        qvarn.log.log('trace',
                      msg_text='_get_resource_type_given_type',
                      results=results,
                      type_name=type_name)
        objs = [obj for _, obj in results]

        if not objs:  # pragma: no cover
            raise qvarn.NoSuchResourceType(type_name)
        elif len(objs) > 1:  # pragma: no cover
            raise qvarn.TooManyResourceTypes(type_name)

        rt = qvarn.ResourceType()
        rt.from_spec(objs[0]['spec'])
        return rt
Exemple #10
0
 def test_returns_true_with_true_subconditions(self):
     cond = qvarn.All()
     yes = qvarn.Yes()
     cond.append_subcondition(yes)
     self.assertTrue(cond.matches(None, None))
Exemple #11
0
 def test_returns_appended_subcondition(self):
     cond = qvarn.All()
     yes = qvarn.Yes()
     cond.append_subcondition(yes)
     self.assertEqual(cond.get_subconditions(), [yes])
Exemple #12
0
 def test_returns_false_without_false_subcondition(self):
     cond = qvarn.All(qvarn.No())
     self.assertFalse(cond.matches(None, None))
Exemple #13
0
 def test_returns_true_without_subconditions(self):
     self.assertTrue(qvarn.All().matches(None, None))