示例#1
0
 def test_orderby_multiple(self):
     """Test multiple orderby statements."""
     q = Query('Environment')
     q.orderby('account_number', 'ASC', label='Environment')
     q.orderby('name', 'DESC', label='Environment')
     expected = ("ORDER BY environment.account_number ASC, "
                 "environment.name DESC")
     self.assertTrue(expected in str(q))
示例#2
0
    def search(self, request):
        """Search objects by type, identity, and property filters."""
        search = SearchSerializer(data=request.data)
        if not search.is_valid():
            raise ValidationError(search.errors)

        vd = search.validated_data
        query = Query(vd.get('model')) \
            .identity(vd.get('identity')) \
            .time(vd.get('time'))

        for f in vd.get('filters', []):
            query.filter(f['prop'],
                         f['operator'],
                         f['value'],
                         label=f['model'])

        for o in vd.get('orders', []):
            query.orderby(o['prop'], o['direction'], label=o['model'])

        count = query.count()

        records = query.page(page=vd['page'],
                             pagesize=vd['pagesize'],
                             index=vd.get('index'))

        serializer = GenericSerializer({
            'query': str(query),
            'data': vd,
            'params': query.params,
            'count': count,
            'pagesize': vd['pagesize'],
            'page': vd['page'],
            'records': records
        })
        return Response(serializer.data)
示例#3
0
 def test_orderby_state_property(self):
     """Test orderby on a state property."""
     q = Query('Host')
     q.orderby('kernel', 'DESC', label='Host')
     expected = 'ORDER BY host_state.kernel DESC'
     self.assertTrue(expected in str(q))
示例#4
0
 def test_orderby_invalid_label(self):
     """Test that orderby raises InvalidLabelError"""
     q = Query('Environment')
     with self.assertRaises(InvalidLabelError):
         q.orderby('aproperty', 'DESC', label='invalidlabel')
示例#5
0
 def test_orderby_invalid_property(self):
     """Test that orderby raises InvalidPropertyError"""
     q = Query('Environment')
     with self.assertRaises(InvalidPropertyError):
         q.orderby('invalidproperty', 'ASC')
示例#6
0
 def test_orderby_no_label(self):
     """Test a single nondefault orderby"""
     q = Query('Environment')
     q.orderby('account_number', 'DESC')
     expected = "ORDER BY environment.account_number DESC"
     self.assertTrue(expected in str(q))