def test_filter_calls_prep_val(self, m_prep): q = Query('Host') q.filter('processor_cores', '=', '42') m_prep.assert_called_once_with('Host', 'processor_cores', '42', raise_for_error=False)
def test_filter_without_label(self): """Test filtering using default label.""" q = Query('Host') q.filter('hostname', '=', 'somehostname') expected = 'WHERE host.hostname = $filterval0' self.assertTrue(expected in str(q)) self.assertEquals(q.params['filterval0'], 'somehostname')
def test_multiple_filters(self): """Tests more than one filters.""" q = Query('Host') q.filter('hostname', '=', 'somehostname') q.filter('kernel', '=', 'somekernel') expected = ('WHERE host.hostname = $filterval0 AND ' 'host_state.kernel = $filterval1') self.assertTrue(expected in str(q))
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)
def test_filter_invalid_prop(self): """Test filtering with invalid property.""" q = Query('Host') with self.assertRaises(InvalidPropertyError): q.filter('someinvalidprop', 'someop', 'someval')
def test_filter_invalid_label(self): """Test filtering with invalid label.""" q = Query('Host') with self.assertRaises(InvalidLabelError): q.filter('someprop', 'someop', 'someval', label='InvalidLabel')
def test_filter_with_label(self): """Test filtering with a non default label.""" q = Query('Host') q.filter('account_number', '=', 'somenumber', label='Environment') expected = 'WHERE environment.account_number = $filterval0' self.assertTrue(expected in str(q))