Exemplo n.º 1
0
def test_delete_raises_without_name():

    ''' can't delete a value from obj dict without a name as a key '''
    f = Field()
    obj = object()
    with pytest.raises(AttributeError):
        f.delete(obj)
Exemplo n.º 2
0
class Torrent(Item):
    @prepare
    def prep(self, soup):
        cats, links, seeders, leechers = soup.findAll('td')
        soup._links = links.findAll('a')

        soup._match = re.search('Uploaded (.*), Size (.*), ULed by (.*)',
                                links.find('font').get_text()).groups(0)

        soup._seeders = seeders.get_text()
        soup._leechers = leechers.get_text()

    name = S._links[0].get_text()
    link = S._links[0]['href']
    magnet = S._links[1]['href']

    @Field
    def torrent(soup):
        try:
            return soup._links[2]['href']
        except IndexError:
            return None

    created = S._match[0]
    size = S._match[1]
    user = S._match[2]

    seeders = Field(lambda s: int(s._seeders))
    leechers = Field(lambda s: int(s._leechers))
Exemplo n.º 3
0
def test_get_raises_without_name():

    ''' can't get a value in obj dict without a name as a key '''
    f = Field()
    obj = object()
    with pytest.raises(AttributeError):
        f.get(obj)
Exemplo n.º 4
0
def test_set_model_name_twice():
    ''' fields can be set to a model by name only once '''
    f = Field()
    f.model_name = "model1"

    with pytest.raises(AttributeError):
        f.model_name = "model2"
    assert f.model_name == "model1"
Exemplo n.º 5
0
    def test_basic_field(self):
        from declare import Field
        constructor = Mock(spec=[])
        field = Field(constructor)

        value = field._compute(sentinel.soup)

        constructor.assert_called_once_with(sentinel.soup)
        self.assertEqual(value, constructor())
Exemplo n.º 6
0
def test_set_model_name_twice():

    ''' fields can be set to a model by name only once '''
    f = Field()
    f.model_name = "model1"

    with pytest.raises(AttributeError):
        f.model_name = "model2"
    assert f.model_name == "model1"
Exemplo n.º 7
0
def test_wrong_typedef_type():
    ''' typedef must be instanceof or subclassof TypeDefinition or None '''
    # None is allowed
    Field(typedef=None)

    with pytest.raises(TypeError):
        Field(typedef=5)

    with pytest.raises(TypeError):
        Field(typedef=int)
Exemplo n.º 8
0
def test_field_mixin_finds_fields_and_subclasses():
    ''' All instances of ``field.Field`` and its subclasses are added to
    ``Meta['fields']``. '''
    class Subclass(Field):
        pass

    f = Field()
    g = Subclass(typedef=TypeDefinition())

    class Model(metaclass=ModelMetaclass):
        model_f = f
        model_g = g
        other = list()

    fields = Model.Meta.fields
    fields_by_name = Model.Meta.fields_by_model_name

    # Keyed by the class's attr keys
    assert 'model_f' in fields_by_name
    assert 'model_g' in fields_by_name

    # Objects are not wrapped at instantiation
    assert fields_by_name['model_f'] is f
    assert fields_by_name['model_g'] is g

    # Order and count
    assert len(fields) == 2
    assert fields == [f, g]

    # Non-fields are not included
    assert 'other' not in fields
Exemplo n.º 9
0
    def test_with_field(self):
        from declare import Field, MapField
        constructor = Mock(spec=[], return_value=[1, 2, 3])
        argument_field = Field(constructor)
        mapped_function = lambda x: x * 2
        field = MapField(mapped_function, argument_field)

        value = field._compute(sentinel.soup)

        constructor.assert_called_once_with(sentinel.soup)
        self.assertEqual(value, [2, 4, 6])
Exemplo n.º 10
0
 class Point(Item):
     x = Field(lambda s: s['x'])
     y = MAGIC['y']
Exemplo n.º 11
0
 class Model(metaclass=ModelMetaclass):
     f = Field()
Exemplo n.º 12
0
def test_typedef_class():
    ''' passing a typedef class will store an instance of the class '''
    field = Field(typedef=TypeDefinition)
    assert field.typedef is not TypeDefinition
Exemplo n.º 13
0
class Container(object):
    ''' Subclass object so instances have a __dict__ '''
    field = Field()
Exemplo n.º 14
0
def test_delete_raises_without_name():
    ''' can't delete a value from obj dict without a name as a key '''
    f = Field()
    obj = object()
    with pytest.raises(AttributeError):
        f.delete(obj)
Exemplo n.º 15
0
def test_get_raises_without_name():
    ''' can't get a value in obj dict without a name as a key '''
    f = Field()
    obj = object()
    with pytest.raises(AttributeError):
        f.get(obj)