def getNewConnection(self, database: SqliteDatabase, *,
                      writeable: bool) -> SqliteDatabase:
     connection = SqliteDatabase.connect(filename=database.filename,
                                         writeable=writeable)
     return SqliteDatabase.fromConnection(origin=database.origin,
                                          connection=connection,
                                          writeable=writeable)
    def testConnection(self):
        """Test that different ways of connecting to a SQLite database
        are equivalent.
        """
        _, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
        # Create a read-write database by passing in the filename.
        rwFromFilename = SqliteDatabase.fromConnection(
            SqliteDatabase.connect(filename=filename), origin=0)
        self.assertEqual(rwFromFilename.filename, filename)
        self.assertEqual(rwFromFilename.origin, 0)
        self.assertTrue(rwFromFilename.isWriteable())
        self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromFilename))
        # Create a read-write database via a URI.
        rwFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}", origin=0)
        self.assertEqual(rwFromUri.filename, filename)
        self.assertEqual(rwFromUri.origin, 0)
        self.assertTrue(rwFromUri.isWriteable())
        self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromUri))
        # We don't support SQLite URIs inside SQLAlchemy URIs.
        with self.assertRaises(NotImplementedError):
            SqliteDatabase.connect(uri=f"sqlite:///file:{filename}?uri=true")

        # Test read-only connections against a read-only file.
        with removeWritePermission(filename):
            # Create a read-only database by passing in the filename.
            roFromFilename = SqliteDatabase.fromConnection(
                SqliteDatabase.connect(filename=filename),
                origin=0,
                writeable=False)
            self.assertEqual(roFromFilename.filename, filename)
            self.assertEqual(roFromFilename.origin, 0)
            self.assertFalse(roFromFilename.isWriteable())
            self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromFilename))
            # Create a read-write database via a URI.
            roFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}",
                                               origin=0,
                                               writeable=False)
            self.assertEqual(roFromUri.filename, filename)
            self.assertEqual(roFromUri.origin, 0)
            self.assertFalse(roFromUri.isWriteable())
            self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromUri))
Example #3
0
 def testConnection(self):
     """Test that different ways of connecting to a SQLite database
     are equivalent.
     """
     # Create an in-memory database by passing filename=None.
     memFromFilename = SqliteDatabase.fromConnection(SqliteDatabase.connect(filename=None), origin=0)
     self.assertIsNone(memFromFilename.filename)
     self.assertEqual(memFromFilename.origin, 0)
     self.assertTrue(memFromFilename.isWriteable())
     self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromFilename))
     # Create an in-memory database via a URI.
     memFromUri = SqliteDatabase.fromUri("sqlite://", origin=0)
     self.assertIsNone(memFromUri.filename)
     self.assertEqual(memFromUri.origin, 0)
     self.assertTrue(memFromUri.isWriteable())
     self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromUri))
     # We don't support SQLite URIs inside SQLAlchemy URIs.
     with self.assertRaises(NotImplementedError):
         SqliteDatabase.connect(uri="sqlite:///:memory:?uri=true")
     # We don't support read-only in-memory databases.
     with self.assertRaises(NotImplementedError):
         SqliteDatabase.connect(filename=None, writeable=False)
Example #4
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     _, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
     connection = SqliteDatabase.connect(filename=filename)
     return SqliteDatabase.fromConnection(connection=connection, origin=origin)
Example #5
0
 def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
     connection = SqliteDatabase.connect(filename=None)
     return SqliteDatabase.fromConnection(connection=connection, origin=origin)