<s> import datetime <EOL> import re <EOL> from dateutil import parser <EOL> from nose . tools import * <EOL> from rest_framework import serializers as ser <EOL> from tests . base import ApiTestCase <EOL> from api . base . filters import FilterMixin <EOL> from api . base . exceptions import ( <EOL> InvalidFilterError , <EOL> InvalidFilterOperator , <EOL> InvalidFilterComparisonType , <EOL> InvalidFilterMatchType , <EOL> ) <EOL> class FakeSerializer ( ser . Serializer ) : <EOL> filterable_fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> string_field = ser . CharField ( ) <EOL> list_field = ser . ListField ( ) <EOL> date_field = ser . DateField ( ) <EOL> datetime_field = ser . DateTimeField ( ) <EOL> int_field = ser . IntegerField ( ) <EOL> float_field = ser . FloatField ( ) <EOL> bool_field = ser . BooleanField ( source = '<STR_LIT>' ) <EOL> class FakeView ( FilterMixin ) : <EOL> serializer_class = FakeSerializer <EOL> class TestFilterMixin ( ApiTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestFilterMixin , self ) . setUp ( ) <EOL> self . view = FakeView ( ) <EOL> def test_parse_query_params_default_operators ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_parse_query_params_casts_values ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , '<STR_LIT:foo>' ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , '<STR_LIT:bar>' ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <NUM_LIT> ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , False ) <EOL> def test_parse_query_params_uses_field_source_attribute ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , False ) <EOL> assert_equal ( fields [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_parse_query_params_generalizes_dates ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> start = parser . parse ( '<STR_LIT>' ) <EOL> stop = start + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> for match in fields [ '<STR_LIT>' ] : <EOL> if match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , start ) <EOL> elif match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , stop ) <EOL> else : <EOL> self . fail ( ) <EOL> def test_parse_query_params_comparable_field ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> for match in fields [ '<STR_LIT>' ] : <EOL> if match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , <NUM_LIT> ) <EOL> elif match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , <NUM_LIT> ) <EOL> else : <EOL> self . fail ( ) <EOL> def test_parse_query_params_matchable_field ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> for match in fields [ '<STR_LIT>' ] : <EOL> if match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , '<STR_LIT:foo>' ) <EOL> elif match [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> assert_equal ( match [ '<STR_LIT:value>' ] , '<STR_LIT:bar>' ) <EOL> else : <EOL> self . fail ( ) <EOL> def test_parse_query_params_raises_InvalidFilterError_bad_field ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' <EOL> } <EOL> with assert_raises ( InvalidFilterError ) : <EOL> self . view . parse_query_params ( query_params ) <EOL> def test_parse_query_params_raises_InvalidFilterComparisonType ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' <EOL> } <EOL> with assert_raises ( InvalidFilterComparisonType ) : <EOL> self . view . parse_query_params ( query_params ) <EOL> def test_parse_query_params_raises_InvalidFilterMatchType ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> with assert_raises ( InvalidFilterMatchType ) : <EOL> self . view . parse_query_params ( query_params ) <EOL> def test_parse_query_params_raises_InvalidFilterOperator ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> with assert_raises ( InvalidFilterOperator ) : <EOL> self . view . parse_query_params ( query_params ) <EOL> def test_InvalidFilterOperator_parameterizes_valid_operators ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> try : <EOL> self . view . parse_query_params ( query_params ) <EOL> except InvalidFilterOperator as err : <EOL> ops = re . search ( r'<STR_LIT>' , err . detail ) . groupdict ( ) [ '<STR_LIT>' ] <EOL> assert_equal ( ops , "<STR_LIT>" ) <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' <EOL> } <EOL> try : <EOL> self . view . parse_query_params ( query_params ) <EOL> except InvalidFilterOperator as err : <EOL> ops = re . search ( r'<STR_LIT>' , err . detail ) . groupdict ( ) [ '<STR_LIT>' ] <EOL> assert_equal ( ops , "<STR_LIT>" ) <EOL> def test_parse_query_params_supports_multiple_filters ( self ) : <EOL> query_params = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> } <EOL> fields = self . view . parse_query_params ( query_params ) <EOL> assert_in ( '<STR_LIT>' , fields ) <EOL> for match in fields [ '<STR_LIT>' ] : <EOL> assert_in ( match [ '<STR_LIT:value>' ] , ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) ) <EOL> def test_convert_value_bool ( self ) : <EOL> value = '<STR_LIT:true>' <EOL> field = FakeSerializer . _declared_fields [ '<STR_LIT>' ] <EOL> value = self . view . convert_value ( value , field ) <EOL> assert_true ( isinstance ( value , bool ) ) <EOL> assert_true ( value ) <EOL> def test_convert_value_date ( self ) : <EOL> value = '<STR_LIT>' <EOL> field = FakeSerializer . _declared_fields [ '<STR_LIT>' ] <EOL> value = self . view . convert_value ( value , field ) <EOL> assert_true ( isinstance ( value , datetime . datetime ) ) <EOL> assert_equal ( value , parser . parse ( '<STR_LIT>' ) ) <EOL> def test_convert_value_int ( self ) : <EOL> value = '<STR_LIT>' <EOL> field = FakeSerializer . _declared_fields [ '<STR_LIT>' ] <EOL> value = self . view . convert_value ( value , field ) <EOL> assert_equal ( value , <NUM_LIT> ) <EOL> def test_convert_value_float ( self ) : <EOL> value = '<STR_LIT>' <EOL> orig_type = type ( value ) <EOL> field = FakeSerializer . _declared_fields [ '<STR_LIT>' ] <EOL> value = self . view . convert_value ( value , field ) <EOL> assert_equal ( value , <NUM_LIT> ) </s>
