# Path: ice/persistence/backed_object.py
# class BackedObject(object):
# 
#   def __init__(self, backing_store, identifier):
#     self.backing_store = backing_store
#     self.identifier = identifier
# 
#   def backed_value(self, key, default=None):
#     return self.backing_store.get(self.identifier, key, default)
# 
# Path: ice/persistence/backed_object_manager.py
# class BackedObjectManager(object):
# 
#   def __init__(self, backing_store, model_adapter):
#     self.backing_store = backing_store
#     self.adapter = model_adapter
#     self.managed_objects = {}
# 
#   def __iter__(self):
#     return iter(self.all())
# 
#   def all(self):
#     # Since not all identifiers are guaranteed to return full objects, we
#     # filter out any `None` elements before returning
#     return filter(None, map(self.find, self.backing_store.identifiers()));
# 
#   def new(self, identifier):
#     obj = self.adapter.new(self.backing_store, identifier)
#     if self.adapter.verify(obj):
#       return obj
# 
#   def find(self, identifier):
#     if identifier not in self.backing_store.identifiers():
#       return None
# 
#     # See if we have a cached version from before
#     if identifier in self.managed_objects:
#       return self.managed_objects[identifier]
# 
#     # If not, create it lazily
#     obj = self.new(identifier)
#     self.managed_objects[identifier] = obj
#     return obj
# 
#   def set_object_for_identifier(self, obj, identifier):
#     self.managed_objects[identifier] = obj
#     # Ensure that the identifier exists in the backing store before we ask
#     # the adapter to save it
#     if not self.backing_store.has_identifier(identifier):
#       self.backing_store.add_identifier(identifier)
#     # Make the adapter do the actual saving
#     self.adapter.save_in_store(self.backing_store, identifier, obj)
# 
# Path: ice/persistence/config_file_backing_store.py
# class ConfigFileBackingStore(backing_store.BackingStore):
# 
#   def __init__(self, path):
#     super(ConfigFileBackingStore, self).__init__(path)
#     self.configParser = ConfigParser.RawConfigParser()
#     self.configParser.read(self.path)
# 
#   def identifiers(self):
#     return self.configParser.sections()
# 
#   def add_identifier(self, ident):
#     try:
#       self.configParser.add_section(ident)
#     except ConfigParser.DuplicateSectionError:
#       raise ValueError("The identifier `%s` already exists" % str(ident))
# 
#   def remove_identifier(self, ident):
#     self.configParser.remove_section(ident)
# 
#   def keys(self, ident):
#     try:
#       return self.configParser.options(ident)
#     except ConfigParser.NoSectionError:
#       raise ValueError("No identifier named `%s` exists" % str(ident))
# 
#   def get(self, ident, key, default=None):
#     try:
#       val = self.configParser.get(ident, key.lower())
#       return val
#     except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
#       return default
# 
#   def set(self, ident, key, value):
#     self.configParser.set(ident, key.lower(), value)
# 
#   def save(self):
#     try:
#       with open(self.path, "w") as configFile:
#         self.configParser.write(configFile)
#     except IOError:
#       raise IOError("Cannot save data to `%s`. Permission Denied")


from mockito import *
from ice.persistence.backed_object import BackedObject
from ice.persistence.backed_object_manager import BackedObjectManager
from ice.persistence.config_file_backing_store import ConfigFileBackingStore
import os
import shutil
import tempfile
import unittest
  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):

# Path: ice/persistence/backed_object.py
# class BackedObject(object):
# 
#   def __init__(self, backing_store, identifier):
#     self.backing_store = backing_store
#     self.identifier = identifier
# 
#   def backed_value(self, key, default=None):
#     return self.backing_store.get(self.identifier, key, default)
# 
# Path: ice/persistence/backed_object_manager.py
# class BackedObjectManager(object):
# 
#   def __init__(self, backing_store, model_adapter):
#     self.backing_store = backing_store
#     self.adapter = model_adapter
#     self.managed_objects = {}
# 
#   def __iter__(self):
#     return iter(self.all())
# 
#   def all(self):
#     # Since not all identifiers are guaranteed to return full objects, we
#     # filter out any `None` elements before returning
#     return filter(None, map(self.find, self.backing_store.identifiers()));
# 
#   def new(self, identifier):
#     obj = self.adapter.new(self.backing_store, identifier)
#     if self.adapter.verify(obj):
#       return obj
# 
#   def find(self, identifier):
#     if identifier not in self.backing_store.identifiers():
#       return None
# 
#     # See if we have a cached version from before
#     if identifier in self.managed_objects:
#       return self.managed_objects[identifier]
# 
#     # If not, create it lazily
#     obj = self.new(identifier)
#     self.managed_objects[identifier] = obj
#     return obj
# 
#   def set_object_for_identifier(self, obj, identifier):
#     self.managed_objects[identifier] = obj
#     # Ensure that the identifier exists in the backing store before we ask
#     # the adapter to save it
#     if not self.backing_store.has_identifier(identifier):
#       self.backing_store.add_identifier(identifier)
#     # Make the adapter do the actual saving
#     self.adapter.save_in_store(self.backing_store, identifier, obj)
# 
# Path: ice/persistence/config_file_backing_store.py
# class ConfigFileBackingStore(backing_store.BackingStore):
# 
#   def __init__(self, path):
#     super(ConfigFileBackingStore, self).__init__(path)
#     self.configParser = ConfigParser.RawConfigParser()
#     self.configParser.read(self.path)
# 
#   def identifiers(self):
#     return self.configParser.sections()
# 
#   def add_identifier(self, ident):
#     try:
#       self.configParser.add_section(ident)
#     except ConfigParser.DuplicateSectionError:
#       raise ValueError("The identifier `%s` already exists" % str(ident))
# 
#   def remove_identifier(self, ident):
#     self.configParser.remove_section(ident)
# 
#   def keys(self, ident):
#     try:
#       return self.configParser.options(ident)
#     except ConfigParser.NoSectionError:
#       raise ValueError("No identifier named `%s` exists" % str(ident))
# 
#   def get(self, ident, key, default=None):
#     try:
#       val = self.configParser.get(ident, key.lower())
#       return val
#     except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
#       return default
# 
#   def set(self, ident, key, value):
#     self.configParser.set(ident, key.lower(), value)
# 
#   def save(self):
#     try:
#       with open(self.path, "w") as configFile:
#         self.configParser.write(configFile)
#     except IOError:
#       raise IOError("Cannot save data to `%s`. Permission Denied")


# Path: tests/backed_object_manager_tests.py
from mockito import *
from ice.persistence.backed_object import BackedObject
from ice.persistence.backed_object_manager import BackedObjectManager
from ice.persistence.config_file_backing_store import ConfigFileBackingStore
import os
import shutil
import tempfile
import unittest

  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")