Exemple #1
0
    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.fromEngine(
            SqliteDatabase.makeEngine(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.makeEngine(
                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.fromEngine(
                SqliteDatabase.makeEngine(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))
Exemple #2
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)