Exemple #1
0
    def test_one_is(self):
        with Assertable(self.rock_bands) as in_rock_bands:
            #in_rock_bands.one("formation").is_(1968)
            in_rock_bands.one([("band", "Pink Floyd"), "periods"]).is_({1963: "Formation",
                                                                        1968: "Fame",
                                                                        1978: "Waters leadership",
                                                                        1986: "Gilmour leadership",
                                                                        1995: "End",
                                                                        2005: "Reunion"})
            in_rock_bands.one([("band", "Cream"), "members"]).has("Clapton")

            self.assertRaises(AssertionError, in_rock_bands.one([("band", "Cream"),
                                                                 "members"]).is_, "Clapton")
            self.assertRaises(AssertionError,
                              in_rock_bands.one([("band", "Pink Floyd"),
                                                 "periods"]).is_, {1963: "Formation"})

            self.assertRaises(AssertionError,
                              in_rock_bands.one([("band",
                                                  "Pink Floyd"), "periods"]).has, {1968: "Fame",
                                                                                   2000: "!"})

            self.assertRaises(AssertionError, in_rock_bands.one("albums year").is_, 1972)
            self.assertRaises(AssertionError, in_rock_bands.one("albums year").is_, 1973)
Exemple #2
0
    def test_trivial(self):
        with Assertable([]) as empty_lst:
            empty_lst().evals_false()
            empty_lst().is_not_none()
            empty_lst.every_existent('a').is_('b')
            empty_lst.one().is_([])
            empty_lst().is_([])

        with Assertable({}) as empty_dict:
            empty_dict().evals_false()
            empty_dict().is_not_none()
            empty_lst.every_existent('a').is_('b')
            empty_dict.one().is_({})
            empty_dict().is_({})

        with Assertable('') as empty_str:
            empty_str().evals_false()
            empty_str().is_not_none()
            empty_str.every_existent('a').is_('b')
            empty_str.one().is_('')
            empty_str().is_('')

        with Assertable(None) as none:
            none().evals_false()
            none().is_none()
            none.one().is_(None)
            none().is_(None)

        with Assertable(set([1, 1])) as simple_set:
            simple_set().has_length(1)
            simple_set.one().is_(1)

        with Assertable((1, 2, 3)) as simple_tuple:
            simple_tuple().evals_true()
            simple_tuple().has_length(3)
            simple_tuple().has(2, 3)
            simple_tuple().is_((1, 3, 2))
            simple_tuple().is_ordered((1, 2, 3))
Exemple #3
0
 def test_only_prefix_as_str(self):
     with Assertable(self.rock_bands, "albums title") as in_album_titles:
         in_album_titles.some().is_("The Wall")
         in_album_titles.one().matches("Made in Ja")
         in_album_titles.no().has("Python")
Exemple #4
0
 def test_strictly_every(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.every("albums title").is_not_none()
         self.assertRaises(AssertionError, in_rock_bands.every, "albums songs")
Exemple #5
0
 def test_has_not(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.every_existent("albums year").has_not(1984)
         in_rock_bands.every("genre").has_not("Rap")
         in_rock_bands.every_existent("periods").has_not({1984: "101"})
         in_rock_bands.every_existent("nonsense").has_not("more nonsense")
Exemple #6
0
 def test_keys_are(self):
     with Assertable(self.programming_languages) as programming_languages:
         programming_languages().keys_are(["functional", "procedural", "object oriented"])
         self.assertRaises(AssertionError,  programming_languages().keys_are, ["functional", "procedural"])
Exemple #7
0
 def test_is_not(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.one(["albums", ("title", "The Wall"), "year"]).is_not(1981)
         in_rock_bands.one(["albums", ("title", "The Wall"), "title"]).is_not("Other")
Exemple #8
0
 def test_is_nones(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.every("albums title").is_not_none()
         in_rock_bands.no("albums title").is_none()
         in_rock_bands.some(["albums", "uk chart"]).is_not_none()
         in_rock_bands.some(["albums", "uk chart"]).is_none()
Exemple #9
0
 def test_has_keys(self):
     with Assertable(self.programming_languages) as programming_languages:
         programming_languages().has_keys("functional", "procedural")
         self.assertRaises(AssertionError,  programming_languages().has_keys, "literate")
Exemple #10
0
 def test_no_has(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.no("albums songs").has("Stairway to heaven")
         in_rock_bands.no("albums songs").has(["Stairway to heaven", "Smoke on the water"])
         self.assertRaises(AssertionError, in_rock_bands.no("albums songs").has,
                           "Stairway to heaven", "Smoke on the water")
Exemple #11
0
 def test_every_has(self):
     with Assertable(self.numbers) as in_numbers:
         in_numbers.every("sequences *").has(5)
         in_numbers.every("sequences *").has(2, 5)
         in_numbers.every("sequences *").has([2, 5])
         self.assertRaises(AssertionError,  in_numbers.every("sequences *").has, 0)
Exemple #12
0
 def test_deeply_nested_has(self):
     with Assertable({"n": {"e": {"s": {"t": {"e": "D"}}}}}) as in_deeply_nested:
         in_deeply_nested.one("n e s t e").has("D")
Exemple #13
0
    def test_basic_lists(self):
        with Assertable([1, 2, 3]) as in_list:
            in_list.one().is_(1)
            in_list.one().has(2, 3)

        with Assertable([1, 1, 1]) as in_list:
            in_list.every().is_(1)
            in_list.every().is_not(2)
            in_list.no().is_(2)

        with Assertable([[1, 2, 3], [4, 5, 6]]) as in_list:
            in_list.some().has(1, 4)
            in_list.some().has([1, 2])
            in_list.no().has([1, 4])
            in_list.some().has([1, 2], [4, 5])

            in_list.no().is_([[5, 4, 6], [3, 2, 1]])
            in_list.no().is_([[4, 5, 6], [1, 2, 3]])
            in_list.one().is_([1, 2, 3])
            in_list.one().is_([6, 4, 5])

            in_list().is_([[4, 5, 6], [1, 2, 3]])
            self.assertRaises(AssertionError, in_list().is_, [[5, 4, 6], [3, 2, 1]])

        with Assertable({"x": [1, 2, 3]}) as in_list:
            in_list.one("x").is_(2, 3)
            in_list.one("x").has(1, 2)
            in_list.no("x").has([1, 2])

        with Assertable({"x": [1, 2, 3]}) as in_list:
            in_list.one("x").is_(3)

        with Assertable([{"x": [1, 2, 3]}]) as in_list:
            in_list.one("x").is_([1, 2, 3])
            in_list.one("x").has([1, 2, 3])
            in_list.one("x").has([3, 2])
            in_list.one("x").has(1, 2)

        with Assertable({"x": [1, 2, 3], "y": [4, 5, 6]}) as in_list:
            in_list.some("*").is_([3, 2, 1])
            in_list.some("*").is_([5, 4, 6])
            in_list.some("*").has([1, 2, 3])
            in_list.some("*").has([1, 2])
            in_list.some("*").has([1, 2], [5, 4])
            in_list.no("*").has([1, 2, 3, 4])
            in_list.one("*").has(1, 2)
            in_list.one("*").has(6)

        with Assertable({"x": ["flat", "nested"]}) as in_str_list:
            in_str_list.one("x").has("fl", "ed")
            in_str_list.one("x").has("f", "d")  # catch!
            in_str_list.one("x").has(["f", "l"])
            in_str_list.one("x").has(["fl", "at"], ["nest"])
            in_str_list.no("x").has(["f", "d"])

        with Assertable([{"x": [1, 2, 3]}]) as in_list:
            in_list.one("x").is_([1, 2, 3])
            in_list.one("*").is_([[1, 2, 3]])
            in_list.one("x").has([1, 2, 3])
            in_list.one("x").has([3, 2])
            in_list.one("x").has(1, 2)

        with Assertable([[1, 2, 3], [4, 5, 6]]) as in_list:
            in_list.one().is_([1, 2, 3])
Exemple #14
0
 def test_prefix_as_lst(self):
     with Assertable(self.programming_languages, ["functional", "Lisp"]) as in_lisp:
         in_lisp.one("year").is_(58)
         in_lisp.one(["designer", "name"]).is_("John McCarthy")
Exemple #15
0
 def test_every_is_a(self):
     with Assertable(self.rock_bands) as in_rock_bands:
         in_rock_bands.every("members").is_a(list)
         in_rock_bands.every_existent("albums year").is_a(int)
         in_rock_bands.every("genre").is_a(str)
Exemple #16
0
 def test_booleans(self):
     with Assertable({'a': False, 'b': [], 'c': True, 'd': 1}) as in_tricky_booleans:
         in_tricky_booleans.one('a').is_false()
         in_tricky_booleans.no('b').is_false()
         in_tricky_booleans.one('c').is_true()
         in_tricky_booleans.no('d').is_true()
Exemple #17
0
class TestDemo(TestCase):

    def setUp(self):
        self.programming_languages = \
[{'languages': {'academical': {'functional': [{'name': 'Lisp',
                                               'designer': {'name': 'John McCarthy',
                                                            'quote': """

                                                                 We understand human mental processes only slightly
                                                                        better than a fish understands swimming.

                                                                 """},
                                               'features': ['macros',
                                                            'recursion',
                                                            'garbage collection'],
                                               'type system': 'dynamic',
                                               'year': 1958},

                                              {'name': 'Haskell',
                                               'designer': {'name': 'Simon Peyton Jones',
                                                            'quote': """

                                                                    You'd like to be able to write a contract for
                                                                                    a function like:
                                                                 'You give me arguments that are bigger than zero and
                                                                   I'll give you a result that is smaller than zero'.

                                                                 """},
                                               'features': ['lazy evaluation',
                                                            'monads'],
                                               'type system': 'static',
                                               'year': 1990}]},

                'practical': {'functional': [{'name': 'Clojure',
                                              'designer': {'name': 'Rich Hickey',
                                                                       'quote': """

                                                                 Programming is not about typing... it's about thinking.

                                                                 """},
                                                          'features': ['software transactional memory',
                                                                       'multimethods',
                                                                       'protocols'],
                                                          'type system': 'dynamic',
                                                          'year': 2007}],

                              'object oriented': [{'name': 'Python',
                                                   'designer': {'name': 'Guido van Rossum',
                                                                'quote': """

                                                                 filter(P, S) is almost always written clearer as
                                                                               [x for x in S if P(x)]

                                                                 """},
                                                   'features': ['duck typing',
                                                                'GIL',
                                                                'multiple inheritance'],
                                                   'type system': 'dynamic',
                                                   'year': 1991},

                                                  {'name': 'Java',
                                                   'designer': {'name': 'James Gosling',
                                                                'quote': """

                                                                 I think everybody has a different answer for
                                                                             what Web services are.


                                                                 """},
                                                   'features': ['JIT compiler',
                                                                'generics'],
                                                   'type system': 'static',
                                                   'year': 1995}],

                              'procedural': [{'name': 'Fortran',
                                              'designer': {'name': 'John Backus',
                                                           'quote': """

                                                                 Much of my work has come from being lazy.

                                                                 """},
                                              'features': ['oldest high-level programming language',
                                                           'high-performance computing'],
                                              'type system': 'static',
                                              'year': 1957},

                                             {'name': 'C',
                                              'designer': {'name': 'Dennis Ritchie',
                                                           'quote': """

                                                                 UNIX is basically a simple operating system,
                                                                  but you have to be a genius to understand
                                                                                the simplicity.

                                                                 """},
                                              'features': ['weakly typing',
                                                           'low level'],
                                              'type system': 'static',
                                              'year': 1972}]}}}]

    def test_programming(self):
        with Assertable(self.programming_languages) as in_programming:
            # Something equals something
            in_programming.one(["languages", "academical", "functional", ("name", "Lisp"), "year"]).is_(1958)
            in_programming.some("languages academical functional year").is_(1990)
            self.assertRaises(AssertionError,
                              in_programming.every(["languages", "*", "functional", "type system"]).is_, "static")
            # Throws:
            #         AssertionError:
            #         Selection on the object under test with path ['languages', '*', 'functional', 'type system'] --->
            #
            #                 ['dynamic', 'static', 'dynamic']
            #
            #         Compared with assertion input --->
            #
            #                 'static'
            #
            #         Not verified (expected = 3, got = 1)
            in_programming.at_least(2, ["languages", "*", "functional", "type system"]).is_("dynamic")

            # Something contains something
            object_oriented_features = ["languages", "*", "object oriented", "features"]
            in_programming.some(object_oriented_features).has("duck typing")
            in_programming.some(object_oriented_features).has("duck typing", "generics")
            in_programming.some(object_oriented_features).has(["duck typing", "GIL"])
            self.assertRaises(AssertionError,
                              in_programming.some(object_oriented_features).has, ["duck typing", "generics"])
            #Throws:
            #       AssertionError:
            #       Selection on the object under test with path ['languages', '*', 'object oriented', 'features'] --->
            #
            #                 [['duck typing', 'GIL', 'multiple inheritance'], ['JIT compiler', 'generics']]
            #
            #       Compared with assertion input --->
            #
            #                 ['duck typing', 'generics']
            #
            #       Not verified (expected = 1, got = 0)
            in_programming.every(["languages", "*", "*", "type system"]).has("ic")
            in_programming.one(["languages", "practical", "procedural", ("name", "Fortran")]).has(
                                                                                            {"year": 1957,
                                                                                             "type system": "static"})
            in_programming.one(["languages", "practical", "procedural", ("name", "Fortran")]).has_some_of(
                                                                                    {"year": 2042,
                                                                                     "type system": "static"})

            # More on something equals something
            in_programming.some(object_oriented_features).is_(["JIT compiler", "generics"])
            in_programming.some(object_oriented_features).is_(["generics", "JIT compiler"])
            in_programming.some(object_oriented_features).is_ordered(["JIT compiler", "generics"])
            self.assertRaises(AssertionError,
                              in_programming.some(object_oriented_features).is_ordered, ["generics", "JIT compiler"])

            # Something compares somehow with some property of something else
            in_programming.exactly(2, "languages * * year").has(1960, cmp=operator.lt)
            in_programming("languages * * year").has(1957, cmp=operator.eq, property=min)
            in_programming.every("languages * * features").has(2, cmp=operator.ge, property=len)

            in_programming.some("languages practical functional").is_not_none()
            in_programming.every("languages **").is_not_none()

    def test_convenient_methods(self):
        with Assertable(self.programming_languages, "languages * *") as in_languages:
            in_languages.every("features").has_no_nones()
            in_languages.every("features").has_not("can fly")
            in_languages.every("features").has_not("can fly", "can drive")
            in_languages.every("year").has_not(datetime.now().year, cmp=operator.gt)
            in_languages.every("year").is_a(int)
            in_languages.no("designer name").is_("Jim Morrison")
            in_languages.at_most(1, "designer name").is_("Denis Ritchie")
            in_languages.at_most(1, "designer name").is_("Denis Ritchie", "James Gosling")
            in_languages.some("designer quote").matches("(?i)uNiX")
            in_languages.some("designer quote").matches("(?i)uNiX", "work.*lazy")
            in_languages.some(["designer"], "quote").matches("(?i)uNiX", "work.*lazy")

    def test_others(self):

        with Assertable([[1, 2, 3]]) as in_numbers:
            self.assertRaises(AssertionError, in_numbers.one().is_, [[1, 2, 3]])
            in_numbers.one().is_([1, 2, 3])
            in_numbers().is_([[1, 2, 3]])

        with Assertable([2, 3, 5, 7, 11, 13, 17, 19]) as in_primes:
            in_primes().has(True, cmp=operator.eq, property=lambda x: x == sorted(x))

            all_modulos = lambda x: [(n, x % n) for n in xrange(1, x + 1)]
            all_divisibles = lambda x: ([x for (x, m) in all_modulos(x) if m == 0], x)
            is_prime = lambda (div_set, x), _: len(div_set) == 2 and 1 in div_set and x in div_set
            in_primes.every().has("ignore this attribute", cmp=is_prime, property=all_divisibles)

        with Assertable([1, 2, 3, 3]) as in_some_duplicates:
            in_some_duplicates.at_least(3).has_no_duplicates()
            self.assertRaises(AssertionError,
                              in_some_duplicates.every().has_no_duplicates)
            self.assertRaises(AssertionError,
                in_some_duplicates().has, [1, 2, 3, 3], cmp=operator.eq, property=lambda x: list(set(x)))

        with Assertable({"programmers": [{"name": "Alice",
                                          "says": "I love functional programming!",
                                          "profession": "poet"},
                                         {"name": "Bob",
                                          "says": "I love monkey patching!",
                                          "profession": "plumber"},
                                         {"name": "Podio",
                                          "says": "I love agile!"}]}) as in_programmers:
            in_programmers.every_existent("programmers says").has("love")
            in_programmers.every("programmers says").has("love")
            self.assertRaises(AssertionError,
                              in_programmers.every, "programmers profession")
            # Throws:
            #   AssertionError: Attribute profession not found in path ['programmers', 'profession']
            in_programmers.every_existent("programmers profession").is_not_none()

            in_programmers.one(["programmers", ("name", "Bob"), "profession"]).is_("plumber")

            # This doesn't work:
            #in_programmers.one(["programmers", ("profession", "plumber"), "says"]).has("monkey")

    def test_users(self):
        with Assertable({'users': [
                {'name': 'Alice',
                 'mails': ['*****@*****.**'],
                 'country': 'UK',
                 'knows_python': False,
                 'birth_date': datetime(1987, 01, 06),
                 'favourite': {'color': 'Blue',
                               'number': 1}},
                {'name': 'Bob',
                 'mails': ['*****@*****.**', '*****@*****.**'],
                 'knows_python': True,
                 'birth_date': datetime(1982, 04, 22),
                 'favourite': {'color': 'Black',
                               'number': 42}},
                {'name': 'Mette',
                 'mails': [],
                 'country': 'DK',
                 'knows_python': True,
                 'birth_date': datetime(1980, 11, 11),
                 'favourite': {'color': 'Green',
                               'number': 7}}]}) as users:
            users('users').has_length(3)  # there are 3 users
            users.one(['users', 'name']).is_('Alice')  # exactly one user is named Alice
            users.one('users name').is_('Alice', 'Bob')  # exactly one user is named Alice and another one is Bob
            users.one('users').has({'name': 'Alice', 'country': 'UK', 'favourite': {'color': 'Blue'}})  # exactly one user has these properties...
            users.one('users').has_some_of({'name': 'Alice', 'country': 'IE'})  # exactly one user is either named Alice or comes from Ireland
            users.one(['users', ('name', 'Alice'), 'knows_python']).is_false()  # exactly one user named Alice does not know Python
            users.one('users mails').has('*****@*****.**')  # exactly one user has this mail...
            users.one('users mails').has('*****@*****.**', '*****@*****.**')  # different users has these 2 mails...
            users.one('users mails').has(['*****@*****.**', '*****@*****.**'])  # the same user has these 2 mails
            users.one('users mails').has(['*****@*****.**', '*****@*****.**'], '*****@*****.**')  # voila!
            users.one(['users', ('name', 'Bob'), 'mails']).is_(['*****@*****.**', '*****@*****.**'])  # Bob has exactly these 2 mails
            users.one(['users', ('name', 'Bob'), 'mails']).is_ordered(['*****@*****.**', '*****@*****.**'])  # Bob has exactly these 2 mails in that order
            users.every('users favourite number').is_a(int)  # every user's favourite number is an int
            users.every(['users', 'favourite'], 'number').is_a(int)  # the same - arguments values are concatenated
            users.every('users favourite *').evals_true()  # every user's favourite thing is logically true (not None, 0, empty...)
            users.every('**').is_not_none()  # every value of any property of any user is not none

        with Assertable({'users': [
                {'name': 'Alice',
                 'mails': ['*****@*****.**'],
                 'country': 'UK',
                 'knows_python': False,
                 'birth_date': datetime(1987, 01, 06),
                 'favourite': {'color': 'Blue',
                               'number': 1}},
                {'name': 'Bob',
                 'mails': ['*****@*****.**', '*****@*****.**'],
                 'knows_python': True,
                 'birth_date': datetime(1982, 04, 22),
                 'favourite': {'color': 'Black',
                               'number': 42}},
                {'name': 'Mette',
                 'mails': [],
                 'country': 'DK',
                 'knows_python': True,
                 'birth_date': datetime(1980, 11, 11),
                 'favourite': {'color': 'Green',
                               'number': 7}}]}, 'users') as users:
            users().has_length(3)  # there are 3 users
            users().has(3, cmp=operator.eq, property=len)  # there are 3 users, the long way
            users().has_length(1, cmp=operator.gt)  # there are more than 1 user
            users().has(1, cmp=operator.gt, property=len)  # there are more than 1 user, the long way
            users.some('knows_python').is_true()  # some users know Python
            users.no('favourite color').is_('Yellow')  # no users have yellow as favourite color
            users.every([('name', 'Bob'), 'mails']).matches("[^@]+@[^@]+\.[^@]+")  # every Bob's mail matches the regex
            users.every('mails').has_no_duplicates()  # there are no duplicate mails
            users.every_existent('country').evals_true()  # in every user that has a country, that country is logically true
            users.every('birth_date').has(20, cmp=operator.gt,   # all users are more than 20 years old
                                          property=lambda birth_date: (datetime.now() - birth_date).days / 365)
            users.some('country').is_not('DK')  # not all countries are DK

            # users.every('name').is_('Alice')
            # AssertionError:
            # Selection on the object under test with path ['users', 'name'] --->
            #
            #        ['Alice', 'Bob', 'Mette']
            #
            # Compared with assertion input --->
            #
            #        'Alice'
            #
            # Not verified (expected = 3, got = 1)

            with Assertable({'name': 'Alice',
                             'country': 'UK',
                             'knows_python': False}) as alice:
                alice().has_keys('name', 'country')  # these are keys present in the dict
                alice().keys_are(['name', 'country', 'knows_python'])  # these are *all* the keys in the dict