Пример #1
0
def p_apply_python_function(t):
    """
    sexpr : LPAREN python_function sexprs RPAREN
    sexpr : LPAREN python_function sexpr RPAREN
    sexpr : LPAREN python_function RPAREN
    """
    python_function = t[2]
    if len(t) > 4:
        args = t[3]
        if not isinstance(args, list):
            args = (args,)
    else:
        args = ()
    python_function = python_function.split(".")
    # Import as part of package or module
    if len(python_function) > 1:
        module = import_module(python_function[0])
        if module is None:
            raise Exception("Could not import modules for expression: '%s'" % t[2])
        function = module
        for module_element in python_function[1:]:
            function = getattr(function, module_element)
    else:
        function_name = python_function[0]
        try:
            function = globals()["__builtins__"][function_name]
        except AttributeError:
            raise AttributeError("Function does not exist in builtins: {}".format(function_name))
    result = function(*args)
    if function.__name__ == "range":
        t[0] = create_array(result)
    else:
        t[0] = shell_quote("'%s'" % unicode(function(*args)))
Пример #2
0
def get_test_classes(module_name):
    module = import_test_module(module_name)
    module_members = OrderedDict(inspect.getmembers(module))
    classes = (value for name, value in module_members.items()
               if inspect.isclass(value) and issubclass(
                   value, unittest.TestCase) and not value == ModuleTestCase)
    for cls in classes:
        if issubclass(cls, ModuleTestCase):
            try:
                module = import_module(cls.module_path)
            except BaseException:
                module = type(cls.module_path, (), {})
            thing = module
        else:
            thing_name = re.search(r' (\w+)\.$', cls.__doc__).group(1)
            thing = module_members[thing_name]
        class_path = ".".join([module_name, cls.__name__])
        yield (thing, class_path)
Пример #3
0
def p_apply_python_function(t):
    """
    sexpr : LPAREN python_function sexprs RPAREN
    sexpr : LPAREN python_function sexpr RPAREN
    sexpr : LPAREN python_function RPAREN
    """
    python_function = t[2]
    if len(t) > 4:
        args = t[3]
        if not isinstance(args, list):
            args = (args,)
    else:
        args = ()
    python_function = python_function.split('.')
    # Import as part of package or module
    if len(python_function) > 1:
        module = import_module(python_function[0])
        if module is None:
            raise Exception(
                "Could not import modules for expression: '%s'" % t[2]
            )
        function = module
        for module_element in python_function[1:]:
            function = getattr(function, module_element)
    else:
        function_name = python_function[0]
        try:
            function = globals()['__builtins__'][function_name]
        except AttributeError:
            raise AttributeError(
                "Function does not exist in builtins: {}".format(
                    function_name
                )
            )
    result = function(*args)
    if function.__name__ == "range":
        t[0] = create_array(result)
    else:
        t[0] = shell_quote("'%s'" % unicode(function(*args)))
Пример #4
0
 def test_exception_on_import(self):
     with self.assertRaises(ImportError) as cm:
         import_module('hello')
     self.assertIn("command-line", str(cm.exception))
Пример #5
0
 def test_import(self):
     with redirect_stdout(StringIO()) as output:
         import_module('hello')
     self.assertIn("command-line", output.getvalue())
Пример #6
0
import unittest
import os
import sys

from helpers import import_module
lib = import_module(
    'lib',
    '%s/../1_get_deck_names.py' % os.path.dirname(os.path.realpath(__file__)))


class TestMethods(unittest.TestCase):
    def test_get_deck_names(self):
        self.assertEqual(
            sorted(lib.get_deck_names()),
            ['Default', 'anki-code-second-deck', 'anki-code-test-deck'])


if __name__ == '__main__':
    unittest.main()
import unittest
import os
import sys

from helpers import import_module
lib = import_module(
    'lib',
    '%s/../3_search_cards.py' % os.path.dirname(os.path.realpath(__file__)))


class TestMethods(unittest.TestCase):
    def test_search_cards(self):
        # https://apps.ankiweb.net/docs/manual.html#searching
        self.assertSequenceEqual(lib.search_cards('*'),
                                 [1521410434570, 1521410453807])
        self.assertSequenceEqual(lib.search_cards('test'), [1521410453807])
        self.assertSequenceEqual(
            lib.search_cards('deck:anki-code-second-deck'), [1521410434570])

        deck_name = 'anki-code-second-deck'
        card_by_deck_id = lib.search_cards('deck:%s' % deck_name)[0]
        card_by_deck = lib.collection.getCard(card_by_deck_id)
        deck_id = lib.collection.decks.id(deck_name)
        self.assertEqual(card_by_deck.did, deck_id)


if __name__ == '__main__':
    unittest.main()
import unittest
import os
import sys

from helpers import import_module
lib = import_module(
    'lib', '%s/../0_create_deck_note_card.py' %
    os.path.dirname(os.path.realpath(__file__)))

# run `$ ./scripts/copy-profile.sh` after running to restore state


class TestMethods(unittest.TestCase):
    def test_create_deck_note_card(self):
        deck_name = 'test-deck'
        deck = lib.create_deck(deck_name)
        self.assertTrue(deck['id'] > 0)
        self.assertEqual(deck['name'], deck_name)

        note, cards = lib.create_note(deck['id'], 'front', 'back')
        self.assertSequenceEqual(note.fields, ['front', 'back'])
        self.assertTrue(len(cards), 1)


if __name__ == '__main__':
    unittest.main()
import unittest
import os
import sys

from helpers import import_module
lib = import_module(
    'lib',
    '%s/../4_change_deck.py' % os.path.dirname(os.path.realpath(__file__)))

# run `$ ./scripts/copy-profile.sh` after running to restore state


class TestMethods(unittest.TestCase):
    def test_change_deck(self):
        search_results = lib.search_cards('*')
        self.assertEqual(len(search_results), 2)

        deck_name_source = 'anki-code-test-deck'
        deck_name_target = 'anki-code-second-deck'
        search_results = lib.search_cards('deck:%s' % deck_name_target)
        self.assertEqual(len(search_results), 1)
        search_results = lib.search_cards('deck:%s' % deck_name_source)
        self.assertEqual(len(search_results), 1)

        card_id = search_results[0]
        card = lib.collection.getCard(card_id)
        deck_source_id = lib.collection.decks.id(deck_name_source)
        deck_target_id = lib.collection.decks.id(deck_name_target)
        self.assertEqual(card.did, deck_source_id)
        self.assertNotEqual(deck_source_id, deck_target_id)
Пример #10
0
import unittest
import os
import sys

from helpers import import_module
lib = import_module('lib', '%s/../2_get_cards_from_deck.py' %
                    os.path.dirname(os.path.realpath(__file__)))


class TestMethods(unittest.TestCase):

    def test_get_deck_names(self):
        deck_name = 'anki-code-second-deck'

        ret = lib.get_cards_from_deck('anki-code-second-deck')
        self.assertEqual(sorted(list(ret.keys())), ['card', 'note'])

        self.assertDictEqual(
            ret['card']._getQA(False, False),
            {'a': 'card-1\n\n<hr id=answer>\n\nback-1',
                'id': 1521410434570, 'q': 'card-1'}
        )
        self.assertSequenceEqual(ret['note'].tags, [])
        self.assertEqual(ret['note'].model()['type'], 0)


if __name__ == '__main__':
    unittest.main()