Source

twoq / twoq / tests / auto / filtering.py

# -*- coding: utf-8 -*-
'''auto filtering call chain test mixins'''

from inspect import ismodule

from twoq.support import port


class ASliceQMixin(object):

    def test_first(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).first().end(), 5)

    def test_nth(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).nth(2).end(), 3)
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).nth(10, 11).end(), 11)

    def test_last(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).last().end(), 1)

    def test_initial(self):
        self.assertEqual(
            self.qclass(5, 4, 3, 2, 1).initial().end(), [5, 4, 3, 2]
        )

    def test_rest(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).rest().end(), [4, 3, 2, 1])

    def test_take(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).take(2).end(), [5, 4])

    def test_takeback(self):
        self.assertEqual(self.qclass(5, 4, 3, 2, 1).snatch(2).end(), [2, 1])

    def test_partition(self):
        self.assertEqual(
            self.qclass(1, 2, 3, 4, 5, 6).tap(
                lambda x: x % 2 == 0
            ).partition().end(), [[1, 3, 5], [2, 4, 6]]
        )


class ACollectQMixin(object):

    def test_members(self):
        from inspect import isclass
        class stooges: #@IgnorePep8
            name = 'moe'
            age = 40
        class stoog2: #@IgnorePep8
            name = 'larry'
            age = 50
        class stoog3: #@IgnorePep8
            name = 'curly'
            age = 60
            class stoog4: #@IgnorePep8
                name = 'beastly'
                age = 969
        test = lambda x: not x[0].startswith('__')
        value = self.qclass(
            stooges, stoog2, stoog3
        ).tap(test, isclass).tuple_wrap().members().untap().end(),
        self.assertEqual(
            value,
            ((('age', 40), ('name', 'moe'), ('age', 50), ('name', 'larry'),
            ('age', 60), ('name', 'curly'), ('stoog4', (('age', 969),
            ('name', 'beastly')))),),
            value,
        )

    def test_mro(self):
        from inspect import isclass
        class stooges: #@IgnorePep8
            name = 'moe'
            age = 40
        class stoog2(stooges): #@IgnorePep8
            name = 'larry'
            age = 50
        class stoog3(stoog2): #@IgnorePep8
            name = 'curly'
            age = 60
        test = lambda x: not x[0].startswith('__')
        value = self.qclass(
            stoog3
        ).tap(test, isclass).tuple_wrap().mro().members().untap().end(),
        self.assertEqual(
            value,
            ((('age', 40), ('name', 'moe'), ('age', 50), ('name', 'larry'),
            ('age', 60), ('name', 'curly'), ('stoog4', (('age', 969),
            ('name', 'beastly')))),),
            value,
        )

    def test_pick(self):
        from stuf import stuf
        stooges = [
            stuf(name='moe', age=40),
            stuf(name='larry', age=50),
            stuf(name='curly', age=60)
        ]
        self.assertEqual(
            self.qclass(*stooges).pick('name').end(),
            ['moe', 'larry', 'curly'],
        )
        self.assertEqual(
            self.qclass(*stooges).pick('name', 'age').end(),
            [('moe', 40), ('larry', 50), ('curly', 60)],
        )
        self.assertEqual(
            self.qclass(*stooges).pick('place').end(), [],
        )

    def test_pluck(self):
        from stuf import stuf
        stooges = [
            stuf(name='moe', age=40),
            stuf(name='larry', age=50),
            stuf(name='curly', age=60)
        ]
        self.assertEqual(
            self.qclass(*stooges).pluck('name').end(),
            ['moe', 'larry', 'curly'],
        )
        self.assertEqual(
            self.qclass(*stooges).pluck('name', 'age').end(),
            [('moe', 40), ('larry', 50), ('curly', 60)],
        )
        stooges = [['moe', 40], ['larry', 50], ['curly', 60]]
        self.assertEqual(
            self.qclass(*stooges).pluck(0).end(), ['moe', 'larry', 'curly'],
        )
        self.assertEqual(self.qclass(*stooges).pluck(1).end(), [40, 50, 60])
        self.assertEqual(self.qclass(*stooges).pluck('place').end(), [])


class ASetQMixin(object):

    def test_difference(self):
        self.assertEqual(
            self.qclass([1, 2, 3, 4, 5], [5, 2, 10]).difference().end(),
            [1, 3, 4],
        )

    def test_disjointed(self):
        self.assertTrue(self.qclass([1, 2, 3], [5, 4, 10]).disjointed().end())
        self.assertFalse(self.qclass([1, 2, 3], [5, 2, 10]).disjointed().end())

    def test_intersection(self):
        self.assertEqual(
            self.qclass(
                [1, 2, 3], [101, 2, 1, 10], [2, 1]
            ).intersection().end(), [1, 2],
        )

    def test_union(self):
        self.assertEqual(
            self.qclass([1, 2, 3], [101, 2, 1, 10], [2, 1]).union().end(),
            [1, 10, 3, 2, 101],
        )

    def test_unique(self):
        self.assertEqual(
            self.qclass(1, 2, 1, 3, 1, 4).unique().end(), [1, 2, 3, 4],
        )
        self.assertEqual(
            self.qclass(1, 2, 1, 3, 1, 4).tap(round).unique().end(),
            [1, 2, 3, 4],
        )


class AFilterQMixin(ACollectQMixin, ASetQMixin, ASliceQMixin):

    '''combination mixin'''

    def test_filter(self):
        self.assertEqual(
            self.qclass(1, 2, 3, 4, 5, 6).tap(
                lambda x: x % 2 == 0
            ).filter().end(), [2, 4, 6]
        )

    def test_find(self):
        self.assertEqual(
            self.qclass(1, 2, 3, 4, 5, 6).tap(
                lambda x: x % 2 == 0
            ).find().end(), 2,
        )

    def test_reject(self):
        self.assertEqual(
            self.qclass(1, 2, 3, 4, 5, 6).tap(
                lambda x: x % 2 == 0
            ).reject().end(), [1, 3, 5]
        )

    def test_compact(self):
        self.assertEqual(
            self.qclass(0, 1, False, 2, '', 3).compact().end(), [1, 2, 3],
        )

    def test_without(self):
        self.assertEqual(
            self.qclass(1, 2, 1, 0, 3, 1, 4).without(0, 1).end(), [2, 3, 4],
        )

__all__ = sorted(name for name, obj in port.items(locals()) if not any([
    name.startswith('_'), ismodule(obj), name in ['ismodule', 'port']
]))
del ismodule
del port
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.