def test_opened_database_commits_on_exit(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") with ConfigurationDatabase.open_for_update(config_file) as config: config[config_key] = config_value with ConfigurationDatabase.open(config_file) as config: self.assertEqual(config_value, config[config_key])
def test_init(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database) with config.cursor() as cursor: # The "configuration" table has been created. self.assertEqual( cursor.execute("SELECT COUNT(*) FROM sqlite_master" " WHERE type = 'table'" " AND name = 'configuration'").fetchone(), (1, ))
def test_opened_database_rolls_back_on_unclean_exit(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") exception_type = factory.make_exception_type() # Set a configuration option, then crash. with ExpectedException(exception_type): with ConfigurationDatabase.open_for_update(config_file) as config: config[config_key] = config_value raise exception_type() # No value has been saved for `config_key`. with ConfigurationDatabase.open(config_file) as config: self.assertRaises(KeyError, lambda: config[config_key])
def test_replacing_configuration_option(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database, mutable=True) config["alice"] = {"abc": 123} config["alice"] = {"def": 456} self.assertEqual({"alice"}, set(config)) self.assertEqual({"def": 456}, config["alice"])
def test_open_permissions_new_database(self): # ConfigurationDatabase.open() applies restrictive file permissions to # newly created configuration databases. config_file = os.path.join(self.make_dir(), "config") with ConfigurationDatabase.open(config_file): perms = FilePath(config_file).getPermissions() self.assertEqual("rw-r-----", perms.shorthand())
def test_open_yields_immutable_backend(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") with ConfigurationDatabase.open(config_file) as config: with ExpectedException(ConfigurationImmutable): config[config_key] = factory.make_name("value") with ExpectedException(ConfigurationImmutable): del config[config_key]
def test_open_permissions_existing_database(self): # ConfigurationDatabase.open() leaves the file permissions of existing # configuration databases. config_file = os.path.join(self.make_dir(), "config") open(config_file, "wb").close() # touch. os.chmod(config_file, 0o644) # u=rw,go=r with ConfigurationDatabase.open(config_file): perms = FilePath(config_file).getPermissions() self.assertEqual("rw-r--r--", perms.shorthand())
def test_open_and_close(self): # ConfigurationDatabase.open() returns a context manager that closes # the database on exit. config_file = os.path.join(self.make_dir(), "config") config = ConfigurationDatabase.open_for_update(config_file) self.assertIsInstance(config, contextlib._GeneratorContextManager) with config as config: self.assertIsInstance(config, ConfigurationDatabase) with config.cursor() as cursor: self.assertEqual((1, ), cursor.execute("SELECT 1").fetchone()) self.assertRaises(sqlite3.ProgrammingError, config.cursor)
def test_mutable(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database, mutable=True) config["alice"] = 1234 del config["alice"]
def test_immutable(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database, mutable=False) self.assertRaises(ConfigurationImmutable, setitem, config, "alice", 1) self.assertRaises(ConfigurationImmutable, delitem, config, "alice")
def test_as_string(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database) self.assertThat(str(config), Equals("ConfigurationDatabase(main=:memory:)"))
def test_removing_configuration_option(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database, mutable=True) config["alice"] = {"abc": 123} del config["alice"] self.assertEqual(set(), set(config))
def test_getting_non_existent_configuration_option(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database) self.assertRaises(KeyError, lambda: config["alice"])
def test_getting_configuration_option(self): database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database, mutable=True) config["alice"] = {"abc": 123} self.assertEqual({"abc": 123}, config["alice"])
def test_configuration_pristine(self): # A pristine configuration has no entries. database = sqlite3.connect(":memory:") config = ConfigurationDatabase(database) self.assertSetEqual(set(), set(config))
def test_open_for_update_yields_mutable_backend(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") with ConfigurationDatabase.open_for_update(config_file) as config: config[config_key] = factory.make_name("value") del config[config_key]
def make_database_store(self): database = sqlite3.connect(":memory:") self.addCleanup(database.close) return ConfigurationDatabase(database, mutable=True)