def test_transaction(self):
     """Test passing params to transaction."""
     params = Params({Param.Autobegin: 1})
     # Create RAM-only transaction
     tr = Transaction(params=params)
     tr.put(b'key', b'value')  # Must not fail
     params.free()
    def test_commit(self):
        """Test RAM transaction commit, must be empty after."""
        # Test with statement
        with Transaction() as tr:
            tr.put(b'put-commit-1', b'value-1')
            tr.put(b'put-commit-2', b'value-2')
            tr.commit()

        # RAM transaction is cleared on commit/rollback
        with Transaction() as tr:
            with self.assertRaises(EmptyError):
                self.assertEqual(tr.getvalue(b'put-commit-1'), b'value-1')
                self.assertEqual(tr.getvalue(b'put-commit-2'), b'value-2')
 def test_delete(self):
     """Test RAM transaction delete."""
     with Transaction() as tr:
         tr.put(b'to-delete', b'value')
         self.assertEqual(tr.getvalue(b'to-delete'), b'value')
         tr.delete(b'to-delete')
         with self.assertRaises(NotFoundError):
             tr.getvalue(b'to-delete')
 def test_transaction_begin(self):
     """Test transaction with and without begin."""
     transaction = Transaction()
     self.assertTrue(transaction.is_initialized)
     self.assertTrue(transaction.ram_only)
     self.assertFalse(transaction.is_started)
     with self.assertRaises(NotStartedError):
         transaction.put(b'key', b'value')
     transaction.begin()
     self.assertTrue(transaction.is_started)
     self.assertTrue(transaction.is_initialized)
    def test_db(self):
        """Test passing params to database."""
        params = Params({Param.Autobegin: 1})
        path = tempfile.NamedTemporaryFile(delete=False).name
        with Tkvdb(path, params) as db:
            tr = Transaction(db=db, ram_only=False)
            tr.put(b'key', b'value')  # Must not fail

            # Now override params
            params = Params({Param.Autobegin: 0})
            tr = Transaction(db=db, ram_only=False, params=params)
            with self.assertRaises(NotStartedError):
                tr.put(b'key', b'value')  # Must fail

            # Test passing params through db handle
            with db.transaction(params) as tr:
                tr.put(b'key', b'value')  # Not failing

        # Test again
        with Tkvdb(path) as db:
            params = Params({Param.Autobegin: 1})
            with db.transaction(params) as tr:
                tr.put(b'key', b'value')
    def test_get_error(self):
        """Test RAM transaction get/put."""
        tr = Transaction()
        tr.begin()

        with self.assertRaises(EmptyError):
            tr.getvalue(b'key1')

        # Now put something and check different error
        tr.put(b'key2', b'value2')
        with self.assertRaises(NotFoundError):
            tr.getvalue(b'key1')

        tr.rollback()
        tr.free()
 def test_get_put_with_stmt(self):
     """Test RAM transaction get/put."""
     with Transaction() as tr:
         tr.put(b'put-1', b'value-1')
         self.assertEqual(tr.getvalue(b'put-1'), b'value-1')
    def test_get_put(self):
        """Test transaction get/put with rollbacks."""
        tr = Transaction()
        tr.begin()

        tr.put(b'put-1', b'value-1')
        self.assertEqual(tr.getvalue(b'put-1'), b'value-1')

        tr.rollback()
        tr.free()
 def test_transaction_free(self):
     """Test transaction free method."""
     transaction = Transaction()
     transaction.free()
     self.assertFalse(transaction.is_started)
     self.assertFalse(transaction.is_initialized)