Ejemplo n.º 1
0
 def test_save_creates_new_file_when_path_originally_dne(self):
     self.assertFalse(os.path.isfile(self.tempfile))
     cfbs = ConfigFileBackingStore(self.tempfile)
     cfbs.add_identifier("Iron Man")
     cfbs.set("Iron Man", "identity", "Tony Stark")
     cfbs.save()
     self.assertTrue(os.path.isfile(self.tempfile))
 def test_save_raises_ioerror_when_cant_make_new_file(self):
   temppath = os.path.join(self.tempdir, "extra", "directories", "test.ini")
   cfbs = ConfigFileBackingStore(temppath)
   cfbs.add_identifier("Iron Man")
   cfbs.set("Iron Man", "identity", "Tony Stark")
   with self.assertRaises(IOError):
     cfbs.save()
 def test_save_creates_new_file_when_path_originally_dne(self):
   self.assertFalse(os.path.isfile(self.tempfile))
   cfbs = ConfigFileBackingStore(self.tempfile)
   cfbs.add_identifier("Iron Man")
   cfbs.set("Iron Man", "identity", "Tony Stark")
   cfbs.save()
   self.assertTrue(os.path.isfile(self.tempfile))
Ejemplo n.º 4
0
 def test_save_raises_ioerror_when_cant_make_new_file(self):
     temppath = os.path.join(self.tempdir, "extra", "directories",
                             "test.ini")
     cfbs = ConfigFileBackingStore(temppath)
     cfbs.add_identifier("Iron Man")
     cfbs.set("Iron Man", "identity", "Tony Stark")
     with self.assertRaises(IOError):
         cfbs.save()
 def test_raises_ioerror_when_permission_denied(self):
   self.create_config_file(self.tempfile, {})
   mode = os.stat(self.tempfile)[stat.ST_MODE]
   new_mode = mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH
   os.chmod(self.tempfile, new_mode)
   cfbs = ConfigFileBackingStore(self.tempfile)
   cfbs.add_identifier("Iron Man")
   cfbs.set("Iron Man", "identity", "Tony Stark")
   with self.assertRaises(IOError):
     cfbs.save()
Ejemplo n.º 6
0
 def create_backing_store(self, data):
   bs = ConfigFileBackingStore(self.tempfile)
   for ident in data.keys():
     bs.add_identifier(ident)
     ident_data = data[ident]
     for key in ident_data.keys():
       value = ident_data[key]
       bs.set(ident, key, value)
   bs.save()
   return bs
Ejemplo n.º 7
0
 def test_raises_ioerror_when_permission_denied(self):
     self.create_config_file(self.tempfile, {})
     mode = os.stat(self.tempfile)[stat.ST_MODE]
     new_mode = mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH
     os.chmod(self.tempfile, new_mode)
     cfbs = ConfigFileBackingStore(self.tempfile)
     cfbs.add_identifier("Iron Man")
     cfbs.set("Iron Man", "identity", "Tony Stark")
     with self.assertRaises(IOError):
         cfbs.save()
 def test_config_file_backing_store_can_read_saved_file(self):
   cfbs = ConfigFileBackingStore(self.tempfile)
   cfbs.add_identifier("Iron Man")
   cfbs.set("Iron Man", "alignment", "good")
   cfbs.add_identifier("Whiplash")
   cfbs.set("Whiplash", "alignment", "evil")
   cfbs.save()
   new_cfbs = ConfigFileBackingStore(self.tempfile)
   self.assertIn("Iron Man", new_cfbs.identifiers())
   self.assertIn("Whiplash", new_cfbs.identifiers())
   self.assertEquals("good", new_cfbs.get("Iron Man", "alignment"))
   self.assertEquals("evil", new_cfbs.get("Whiplash", "alignment"))
Ejemplo n.º 9
0
 def test_config_file_backing_store_can_read_saved_file(self):
     cfbs = ConfigFileBackingStore(self.tempfile)
     cfbs.add_identifier("Iron Man")
     cfbs.set("Iron Man", "alignment", "good")
     cfbs.add_identifier("Whiplash")
     cfbs.set("Whiplash", "alignment", "evil")
     cfbs.save()
     new_cfbs = ConfigFileBackingStore(self.tempfile)
     self.assertIn("Iron Man", new_cfbs.identifiers())
     self.assertIn("Whiplash", new_cfbs.identifiers())
     self.assertEqual("good", new_cfbs.get("Iron Man", "alignment"))
     self.assertEqual("evil", new_cfbs.get("Whiplash", "alignment"))
 def test_save_modifies_contents_of_file(self):
   self.create_config_file(self.tempfile, {
       "Iron Man": {"identity": "Tony Stark"}
   })
   old_contents = self.file_contents(self.tempfile)
   cfbs = ConfigFileBackingStore(self.tempfile)
   cfbs.set("Iron Man", "alignment", "good")
   cfbs.add_identifier("Whiplash")
   cfbs.set("Whiplash", "alignment", "evil")
   cfbs.save()
   new_contents = self.file_contents(self.tempfile)
   self.assertNotIn("Whiplash", old_contents)
   self.assertIn("Whiplash", new_contents)
   self.assertNotIn("alignment", old_contents)
   self.assertIn("alignment", new_contents)
   self.assertIn("good", new_contents)
   self.assertIn("evil", new_contents)
Ejemplo n.º 11
0
 def test_save_modifies_contents_of_file(self):
     self.create_config_file(self.tempfile,
                             {"Iron Man": {
                                 "identity": "Tony Stark"
                             }})
     old_contents = self.file_contents(self.tempfile)
     cfbs = ConfigFileBackingStore(self.tempfile)
     cfbs.set("Iron Man", "alignment", "good")
     cfbs.add_identifier("Whiplash")
     cfbs.set("Whiplash", "alignment", "evil")
     cfbs.save()
     new_contents = self.file_contents(self.tempfile)
     self.assertNotIn("Whiplash", old_contents)
     self.assertIn("Whiplash", new_contents)
     self.assertNotIn("alignment", old_contents)
     self.assertIn("alignment", new_contents)
     self.assertIn("good", new_contents)
     self.assertIn("evil", new_contents)
Ejemplo n.º 12
0
 def test_add_identifier_raises_valueerror_when_identifier_exists(self):
     self.create_config_file(self.tempfile, {"Iron Man": {}})
     cfbs = ConfigFileBackingStore(self.tempfile)
     with self.assertRaises(ValueError):
         cfbs.add_identifier("Iron Man")
Ejemplo n.º 13
0
class BackedObjectManagerTests(unittest.TestCase):
    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.tempfile = os.path.join(self.tempdir, "test.ini")
        self.backing_store = ConfigFileBackingStore(self.tempfile)
        self.manager = BackedObjectManager(self.backing_store,
                                           BackedObjectBackedObjectAdapter())

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def populate_backing_store(self, data):
        for ident in data.keys():
            self.backing_store.add_identifier(ident)
            ident_data = data[ident]
            for key in ident_data.keys():
                value = ident_data[key]
                self.backing_store.set(ident, key, value)
        self.backing_store.save()

    def test_reading_data_from_store(self):
        self.populate_backing_store({
            "Iron Man": {
                "identity": "Tony Stark",
            },
            "War Machine": {
                "identity": "James Rhodes",
                "alias": "Rhodey",
            },
        })
        iron_man = self.manager.find("Iron Man")
        war_machine = self.manager.find("War Machine")

        self.assertIsNotNone(iron_man)
        self.assertEquals(iron_man.identifier, "Iron Man")
        self.assertEquals(iron_man.backed_value("identity"), "Tony Stark")

        self.assertIsNotNone(war_machine)
        self.assertEquals(war_machine.identifier, "War Machine")
        self.assertEquals(war_machine.backed_value("identity"), "James Rhodes")
        self.assertEquals(war_machine.backed_value("alias"), "Rhodey")

    def test_all(self):
        self.populate_backing_store({
            "Iron Man": {},
            "War Machine": {},
        })
        all_objects = self.manager.all()
        self.assertIs(len(all_objects), 2)
        obj1 = all_objects[0]
        obj2 = all_objects[1]
        self.assertFalse(obj1.identifier == obj2.identifier)
        self.assertIn(obj1.identifier, ["Iron Man", "War Machine"])
        self.assertIn(obj2.identifier, ["Iron Man", "War Machine"])

    def test_find_returns_none_with_invalid_identifier(self):
        im = self.manager.find("Iron Man")
        self.assertIsNone(self.manager.find("Iron Man"))

    def test_find_returns_non_none_with_valid_identifier(self):
        self.populate_backing_store({"Iron Man": {}})
        self.assertIsNotNone(self.manager.find("Iron Man"))

    def test_find_returns_same_object_between_calls(self):
        self.populate_backing_store({"Iron Man": {}})
        first_result = self.manager.find("Iron Man")
        self.assertIsNotNone(first_result)
        second_result = self.manager.find("Iron Man")
        self.assertIsNotNone(second_result)
        self.assertIs(first_result, second_result)

    def test_find_after_set_object_for_identifier_returns_same_object_that_was_set(
            self):
        war_machine = BackedObject(self.backing_store, "War Machine")
        self.manager.set_object_for_identifier(war_machine, "War Machine")
        self.assertEquals(self.manager.find("War Machine"), war_machine)
 def test_added_identifiers_show_up_in_subsequent_calls(self):
   self.create_config_file(self.tempfile, {})
   cfbs = ConfigFileBackingStore(self.tempfile)
   cfbs.add_identifier("Iron Man")
   self.assertIn("Iron Man", cfbs.identifiers())
Ejemplo n.º 15
0
 def test_added_identifiers_show_up_in_subsequent_calls(self):
     self.create_config_file(self.tempfile, {})
     cfbs = ConfigFileBackingStore(self.tempfile)
     cfbs.add_identifier("Iron Man")
     self.assertIn("Iron Man", cfbs.identifiers())
 def test_add_identifier_raises_valueerror_when_identifier_exists(self):
   self.create_config_file(self.tempfile, {"Iron Man": {}})
   cfbs = ConfigFileBackingStore(self.tempfile)
   with self.assertRaises(ValueError):
     cfbs.add_identifier("Iron Man")
class BackedObjectManagerTests(unittest.TestCase):

  def setUp(self):
    self.tempdir = tempfile.mkdtemp()
    self.tempfile = os.path.join(self.tempdir, "test.ini")
    self.backing_store = ConfigFileBackingStore(self.tempfile)
    self.adapter = BackedObjectBackedObjectAdapter()
    self.manager = BackedObjectManager(self.backing_store, self.adapter)

  def tearDown(self):
    shutil.rmtree(self.tempdir)

  def populate_backing_store(self, data):
    for ident in data.keys():
      self.backing_store.add_identifier(ident)
      ident_data = data[ident]
      for key in ident_data.keys():
        value = ident_data[key]
        self.backing_store.set(ident, key, value)
    self.backing_store.save()

  def test_reading_data_from_store(self):
    self.populate_backing_store({
        "Iron Man": {
            "identity": "Tony Stark",
        },
        "War Machine": {
            "identity": "James Rhodes",
            "alias": "Rhodey",
        },
    })
    iron_man = self.manager.find("Iron Man")
    war_machine = self.manager.find("War Machine")

    self.assertIsNotNone(iron_man)
    self.assertEquals(iron_man.identifier, "Iron Man")
    self.assertEquals(iron_man.backed_value("identity"), "Tony Stark")

    self.assertIsNotNone(war_machine)
    self.assertEquals(war_machine.identifier, "War Machine")
    self.assertEquals(war_machine.backed_value("identity"), "James Rhodes")
    self.assertEquals(war_machine.backed_value("alias"), "Rhodey")

  def test_all(self):
    self.populate_backing_store({
        "Iron Man": {},
        "War Machine": {},
    })
    all_objects = self.manager.all()
    self.assertIs(len(all_objects), 2)
    obj1 = all_objects[0]
    obj2 = all_objects[1]
    self.assertFalse(obj1.identifier == obj2.identifier)
    self.assertIn(obj1.identifier, ["Iron Man", "War Machine"])
    self.assertIn(obj2.identifier, ["Iron Man", "War Machine"])

  def test_find_returns_none_with_invalid_identifier(self):
    im = self.manager.find("Iron Man")
    self.assertIsNone(self.manager.find("Iron Man"))

  def test_find_returns_non_none_with_valid_identifier(self):
    self.populate_backing_store({"Iron Man": {}})
    self.assertIsNotNone(self.manager.find("Iron Man"))

  def test_find_returns_same_object_between_calls(self):
    self.populate_backing_store({"Iron Man": {}})
    first_result = self.manager.find("Iron Man")
    self.assertIsNotNone(first_result)
    second_result = self.manager.find("Iron Man")
    self.assertIsNotNone(second_result)
    self.assertIs(first_result, second_result)

  def test_find_after_set_object_for_identifier_returns_same_object_that_was_set(self):
    war_machine = BackedObject(self.backing_store, "War Machine")
    self.manager.set_object_for_identifier(war_machine, "War Machine")
    self.assertEquals(self.manager.find("War Machine"), war_machine)

  def test_all_doesnt_include_objects_where_verification_fails(self):
    self.populate_backing_store({
        "Iron Man":     {},
        "War Machine":  {},
        "Whiplash":     {},
    })
    self.adapter.verifier = lambda obj: obj.identifier.startswith("W")
    identifiers = map(lambda obj: obj.identifier, self.manager.all())
    self.assertEqual(len(identifiers), 2)
    self.assertIn("War Machine", identifiers)
    self.assertIn("Whiplash", identifiers)

  def test_find_returns_none_for_object_where_verification_fails(self):
    self.populate_backing_store({"Iron Man": {}})
    self.adapter.verifier = lambda obj: False
    self.assertIsNone(self.manager.find("Iron Man"))