Example #1
0
    def test_query_select_field_string_key(self, user):
        user.du1 = DummyModel('a')
        user.du2 = DummyModel('b')
        user.du3 = DummyModel('c')
        user.du4 = DummyModel('d')
        query = lambda: [DummyModel(ch) for ch in 'abc']

        field = fields.QuerySelect(query, 'foo')
        assert field.serialize('du1', user) == 'a'
        assert field.serialize('du2', user) == 'b'
        assert field.serialize('du3', user) == 'c'
        with pytest.raises(ValidationError):
            field.serialize('du4', user)
Example #2
0
    def test_query_select_list_field_string_key_deserialization(self):
        query = lambda: [DummyModel(ch) for ch in 'abecde']

        field = fields.QuerySelectList(query, 'foo')
        assert field.deserialize(['a', 'c', 'b']) == \
               [DummyModel('a'), DummyModel('c'), DummyModel('b')]
        assert field.deserialize(['d', 'e', 'e']) == \
               [DummyModel('d'), DummyModel('e'), DummyModel('e')]
        assert field.deserialize([]) == []
        with pytest.raises(ValidationError):
            field.deserialize(['a', 'b', 'f'])
        with pytest.raises(ValidationError):
            field.deserialize(['a', 'b', 'b'])
Example #3
0
    def test_query_select_field_string_key_deserialization(self):
        query = lambda: [DummyModel(ch) for ch in 'abc']

        field = fields.QuerySelect(query, 'foo')
        assert field.deserialize('a') == DummyModel('a')
        assert field.deserialize('b') == DummyModel('b')
        assert field.deserialize('c') == DummyModel('c')
        with pytest.raises(ValidationError):
            field.deserialize('d')
        with pytest.raises(ValidationError):
            field.deserialize('bar d')
        assert list(field.keys()) == [ch for ch in 'abc']
        assert list(field.results()) == [DummyModel(ch) for ch in 'abc']
        assert list(field.pairs()) == [(ch, DummyModel(ch)) for ch in 'abc']
        assert list(field.labels()) == [(ch, 'bar ' + ch) for ch in 'abc']
        assert list(field.labels('foo')) == [(ch, ch) for ch in 'abc']
        assert list(field.labels(str)) == [(ch, 'bar ' + ch) for ch in 'abc']
Example #4
0
    def test_query_select_list_field_string_key(self, user):
        user.du1 = [DummyModel('a'), DummyModel('c'), DummyModel('b')]
        user.du2 = [DummyModel('d'), DummyModel('e'), DummyModel('e')]
        user.du3 = [DummyModel('a'), DummyModel('b'), DummyModel('f')]
        user.du4 = [DummyModel('a'), DummyModel('b'), DummyModel('b')]
        user.du5 = []
        query = lambda: [DummyModel(ch) for ch in 'abecde']

        field = fields.QuerySelectList(query, 'foo')
        assert field.serialize('du1', user) == ['a', 'c', 'b']
        assert field.serialize('du2', user) == ['d', 'e', 'e']
        assert field.serialize('du5', user) == []
        with pytest.raises(ValidationError):
            field.serialize('du3', user)
        with pytest.raises(ValidationError):
            field.serialize('du4', user)