Esempio n. 1
0
def run_tests():
    '''
    >>> print(bool(ex))
    True
    >>> print(bool(name_space))
    True

    >>> kv_obj = kv2()
    >>> print(type(kv_obj))
    <class 'seneca.smart_contract_user_libs.storage.kv2.kv2'>

    >>> _ = print(kv_obj['x'])
    None

    >>> kv_obj['x'] = 5
    >>> print(kv_obj['x'])
    5

    >>> kv_obj['x'] = 6
    >>> print(kv_obj['x'])
    6
    '''
    global name_space, ex
    import doctest, sys

    from seneca.seneca_internal.storage.mysql_executer import Executer
    import seneca.load_test_conf as lc

    name_space = 'test_tabular'
    ex = Executer(**lc.db_settings)

    return doctest.testmod(sys.modules[__name__], extraglobs={**locals()})
Esempio n. 2
0
    def __init__(self, should_reset):
        self.log = get_logger("DB")
        self.log.info(
            "Creating DB instance with should_reset={}".format(should_reset))

        self.lock = Lock()

        self.ex = Executer('root', '', '', '127.0.0.1')
        self.tables = build_tables(self.ex, should_drop=should_reset)
Esempio n. 3
0
def set_up():
    global ex_, contract_table
    import load_test_conf as lc

    this_dir = os.path.dirname(__file__)

    ex_ = Executer(**lc.db_settings)

    ## Setup steps ##
    contract_file_path = os.path.join(this_dir, 'example_contracts/')

    try:
        ex_.raw('drop database seneca_test;')
    except Exception as e:
        if e.args[0]['error_code'] == 1046:
            pass
        else:
            raise

    ex_.raw('create database seneca_test;')
    ex_.raw('use seneca_test;')

    contract_table = t.Table('smart_contracts',
                             t.Column('contract_address', t.str_len(30), True),
                             [
                                 t.Column('code_str', str),
                                 t.Column('author', t.str_len(60)),
                                 t.Column('execution_datetime', datetime),
                                 t.Column('execution_status', t.str_len(30)),
                             ])

    try:
        contract_table.drop_table().run(ex)
    except Exception as e:
        if e.args[0]['error_code'] == 1051:
            pass
        else:
            raise

    contract_table.create_table().run(ex)
Esempio n. 4
0
def run_tests():
    ## SETUP ##
    global ex
    global name_space

    import sys, json
    from os.path import abspath, dirname
    from seneca.seneca_internal.storage.mysql_executer import Executer

    name_space = 'test_kv'
    import seneca.load_test_conf as lc
    ex_ = Executer(**lc.db_settings)

    def ex__(obj):
        print('Running Query:')
        print(obj.to_sql())
        res = ex_(obj)
        print(res)
        print('\n')
        return res

    ex = ex__

    ## END SETUP ##
    print('****** STARTING TESTS ******')

    kv_name = 'policies'
    try:
        drop_kv(kv_name)
    except:
        print('No KV "{}" detected, creating...'.format(kv_name))
    try:
        get_kv(kv_name)
        raise
    except Exception as e:
        assert e.args[0][
            'error_code'] == 1146, 'KV "{}" still exist after dropping'.format(
                kv_name)
    p = create_kv(kv_name)
Esempio n. 5
0
    def setUp(self):
        super().setUp()

        self.ex = Executer('root', '', '', '127.0.0.1')
Esempio n. 6
0
def run_tests():
    # TODO: Replace with doctest
    ## SETUP ##
    global ex
    global name_space

    import sys
    from os.path import abspath, dirname
    import configparser
    from seneca.seneca_internal.storage.mysql_executer import Executer

    import seneca.load_test_conf as lc

    name_space = 'test_tabular'

    ex_ = Executer(**lc.db_settings)

    def ex__(obj):
        print('Running Query:')
        print(obj.to_sql())
        res = ex_(obj)
        print(res)
        print('\n')
        return res

    ex = ex__

    ## END SETUP ##
    print('****** STARTING TESTS******')

    try:
        print(drop_table('users'))
        print('DROPPED TABLE')
    except:
        print('Table "users" not already created skipping...')

    u = create_table('users', [('first_name', str_len(30), True),
                               ('last_name', str_len(30), True),
                               ('nick_name', str_len(30)), ('balance', int)])

    print(u.select().run())

    u.insert([
        {
            'first_name': 'Test1',
            'last_name': 'l1',
            'nick_name': '1',
            'balance': 10
        },
        {
            'first_name': 'Test2',
            'last_name': 'l2',
            'nick_name': '2',
            'balance': 20
        },
        {
            'first_name': 'Test3',
            'last_name': 'l3',
            'nick_name': '3',
            'balance': 30
        },
    ]).run()

    u2 = get_table('users')

    add_column(u2, ('address', str))
    print(dir(u2.underlying_obj))
    drop_column(u2, 'address')
    print(dir(u2.underlying_obj))

    print(u.select().where(and_(u.first_name == 'test',
                                u.last_name == 'test2')).to_sql())
Esempio n. 7
0
def run_tests():
    # TODO: make these into real tests with this format:
    #def run_tests():
    #    '''
    #    '''
    #    import doctest, sys, ast
    #    return doctest.testmod(sys.modules[__name__], extraglobs={**locals()})
    from seneca.seneca_internal.storage.mysql_executer import Executer
    import seneca.load_test_conf as lc

    ### Unit Tests ###
    import unittest
    import sys

    u = Table('users', AutoIncrementColumn('id'), [
        Column('first_name', str),
        Column('last_name', str),
        Column('balance', int),
        Column('creation_date', datetime)
    ])

    class TestQueries(unittest.TestCase):
        def test_create(self):
            self.assertEqual(u.select().to_sql(), 'SELECT *\nFROM users;')

    suite = unittest.TestSuite()
    # TODO: discover and add all tests
    suite.addTest(unittest.makeSuite(TestQueries))
    unittest.TextTestRunner(verbosity=1).run(suite)

    ### End to End tests ###
    import os

    ex_ = Executer(**lc.db_settings)

    def ex(obj):
        print('Running Query:')
        print(obj.to_sql())
        res = ex_(obj)
        print(res)
        print('\n')
        return res

    u = Table('users', AutoIncrementColumn('id'), [
        Column('first_name', str),
        Column('last_name', str),
        Column('balance', int),
        Column('creation_date', datetime)
    ])

    try:
        u.drop_table().run(ex)
    except Exception as e:
        if not e.args[0][
                'error_message'] == "Unknown table 'seneca_test.users'":
            raise

    u.create_table(if_not_exists=True).run(ex)
    u.select().where(u.first_name != None).run(ex)

    u.insert([
        {
            'first_name': 'Test',
            'last_name': 'User',
            'balance': 0
        },
        {
            'first_name': 'Test2',
            'last_name': 'User',
            'balance': 0
        },
        {
            'first_name': 'Test3',
            'last_name': 'User',
            'balance': 0
        },
    ]).run(ex)
    print(u.select().run(ex))

    u.update({'balance': 1000}).run(ex)
    print(u.select().run(ex))

    print(u.select('id', 'first_name').order_by('id', desc=True).run(ex))

    u.delete().where(
        or_(u.first_name == 'Test',
            Column('first_name') == 'Test3')).run(ex)

    print(u.select().run(ex))

    u.insert([
        {
            'first_name': 'Test1',
            'last_name': 'User',
            'balance': 0
        },
        {
            'first_name': 'Test2',
            'last_name': 'User',
            'balance': 10
        },
    ]).run(ex)

    print(u.count().run(ex))
    print(u.count().where(u.first_name == 'Test2').run(ex))

    print(u.select().run(ex))
    print(u.count_unique('first_name').run(ex))
    print(u.count_unique('first_name').where(u.balance >= 10).run(ex))

    print(u.add_column('nick_name', str).run(ex))
    print(u.add_column('unique_nick_name', str_len(30), True).run(ex))
    print(u.select().run(ex))
    print(u.drop_column('nick_name').run(ex))
    print(u.select().run(ex))
    print(u.select().where(u.first_name == 'Test2').order_by(
        'balance', desc=False).run(ex))
    print(u.select().where(u.first_name == 'Test2').order_by(
        'balance', desc=True).run(ex))

    t2 = Table.from_existing('users').run(ex)
    u.add_column('nick_name', str).run(ex)
    t2.drop_column('nick_name').run(ex)
    t2.add_column('nick_name', str).run(ex)
 def setUp(self):
     super().setUp()
     self.ex = Executer('root', '', '', '127.0.0.1')
     self.tables = build_tables(self.ex, should_drop=True)
Esempio n. 9
0
    def __init__(self, reset_db=True):
        super().__init__()

        self.ex = Executer.init_local_noauth_dev()
        self.tables = build_tables(self.ex, should_drop=reset_db)