Exemple #1
0
    def test_insert_and_insert(self):
        s = datetime.datetime.now()
        d = {}
        d['id'] = 0
        d['val_null'] = 1
        d['val_integer'] = 2
        d['val_real'] = 3
        d['val_text'] = 4
        d['val_blob'] = 5
        d['now'] = 6
        d['elapsed_time'] = 7

        sql = SQL(db_path=test_db_path, schema_path=test_schema_path)
        sql.create_db()
        sql.create_table('test_table')

        d_insert = {}
        d_insert['id'] = None  # integer primary key
        # autoincrement unique not null
        d_insert['val_null'] = None
        d_insert['val_integer'] = 3
        d_insert['val_real'] = 10.0
        d_insert['val_text'] = 'text'
        d_insert['val_blob'] = b'bytes'
        d_insert['now'] = lib.current_y15sformat_time()
        e = datetime.datetime.now()
        d_insert['elapsed_time'] = (e - s).total_seconds()
        sql.insert('test_table', d_insert)
        sql.commit()

        d_selected = sql.select('test_table')

        d_insert['id'] = 1
        for column, index in d.items():
            self.assertEqual(d_insert[column], d_selected[0][index])

        d_update = {}
        d_update['val_null'] = 'None None'
        d_update['val_integer'] = 30
        d_update['val_real'] = 100.0
        d_update['val_text'] = 'text text'
        d_update['val_blob'] = b'bytes bytes'
        now = datetime.datetime.now()
        t = now + datetime.timedelta(0, 1000, 0)
        d_update['now'] = lib.y15sformat_time(t)
        e = datetime.datetime.now()
        d_update['elapsed_time'] = (e - s).total_seconds()

        d_update['id'] = 1
        with self.assertRaises(sqlite3.IntegrityError) as raiz:
            sql.insert('test_table', d_update)
        args = raiz.exception.args
        self.assertEqual('UNIQUE constraint failed: test_table.id', args[0])
Exemple #2
0
    def setUp(self):
        self.watson_office_addr = ('localhost', 65530)
        simulation_dir = os.path.join(".", SIMULATION_DIR)
        self.log_level = 'INFO'
        self.start_up_orig = lib.make_start_up_orig()
        start_up_time = lib.y15sformat_time(self.start_up_orig)
        dir_name = os.path.join(simulation_dir, start_up_time)
        self.dir_name = dir_name

        td = D_TIMEDELTA.get(self._testMethodName, TD_ZERO)
        self.watson = Watson(self.watson_office_addr, SIMULATION_SECONDS,
                             self.start_up_orig - td, self.dir_name,
                             self.log_level)
Exemple #3
0
    def setUp(self):
        watson_office_addr = ('localhost', 0)
        simulation_seconds = 3
        log_level = 'DEBUG'

        simulation_dir = SIMULATION_DIR
        start_up_orig = lib.make_start_up_orig()
        start_up_time = lib.y15sformat_time(start_up_orig)

        dir_name = os.path.join(simulation_dir, start_up_time)

        if not os.path.isdir(dir_name):
            print(f"os.makedirs(dir_name={dir_name}, exist_ok=True)")
            os.makedirs(dir_name, exist_ok=True)

        self.watson = Watson(watson_office_addr, simulation_seconds,
                             start_up_orig, dir_name, log_level)
Exemple #4
0
    def test_update_and_select(self):
        s = datetime.datetime.now()
        d = {}
        d['id'] = 0
        d['val_null'] = 1
        d['val_integer'] = 2
        d['val_real'] = 3
        d['val_text'] = 4
        d['val_blob'] = 5
        d['now'] = 6
        d['elapsed_time'] = 7

        sql = SQL(db_path=test_db_path, schema_path=test_schema_path)
        sql.create_db()
        sql.create_table('test_table')

        d_insert = {}
        d_insert['id'] = None  # integer primary key
        # autoincrement unique not null
        d_insert['val_null'] = None
        d_insert['val_integer'] = 3
        d_insert['val_real'] = 10.0
        d_insert['val_text'] = 'text'
        d_insert['val_blob'] = b'bytes'
        s = datetime.datetime.now()
        now = s
        d_insert['now'] = lib.y15sformat_time(now)
        e = s + datetime.timedelta(0, 100, 0)
        d_insert['elapsed_time'] = (e - s).total_seconds()
        sql.insert('test_table', d_insert)
        sql.commit()

        d_selected = sql.select('test_table')

        d_insert['id'] = 1
        for column, index in d.items():
            self.assertEqual(d_insert[column], d_selected[0][index])

        d_update = {}
        d_update['val_null'] = 'None None'
        d_update['val_integer'] = 30
        d_update['val_real'] = 100.0
        d_update['val_text'] = 'text text'
        d_update['val_blob'] = b'bytes bytes'
        d_update['now'] = lib.y15sformat_time(now + datetime.timedelta(0, 200))
        e = s + datetime.timedelta(0, 200, 0)
        d_update['elapsed_time'] = (e - s).total_seconds()

        d_where = {'id': 1}
        sql.update('test_table', d_update, d_where)

        d_selected = sql.select('test_table', conditions='where id = 1')

        self.assertNotEqual(d_update, d_selected)
        for column, index in d.items():
            if column == 'id':
                self.assertEqual(d_insert[column], d_selected[0][index])
                continue
        # print(d_update[column], ',', d_selected[0][index])
        # print(d_insert[column], ',', d_selected[0][index])
        # print()
            self.assertEqual(d_update[column], d_selected[0][index])
            self.assertNotEqual(d_insert[column], d_selected[0][index])
Exemple #5
0
 def test_y15sformat_time(self):
     start_up_orig = lib.make_start_up_orig()
     # Y15S_FORMAT='%Y-%m-%dT%H%M%S'
     y15s = lib.y15sformat_time(start_up_orig)
     self.assertIsInstance(y15s, str)
     self.assertRegex(y15s, r"\A\d{4}-\d{2}-\d{2}T\d{6}\Z")