# Path: ice/history.py
# class ManagedROMArchive(object):
#   def __init__(self, archive_path):
#     self.archive_path = archive_path
# 
#     self.archive = self.load_archive(archive_path)
# 
#   def load_archive(self, path):
#     if not os.path.exists(path):
#       return None
# 
#     with open(path) as archive_file:
#       return json.load(archive_file)
# 
#   def archive_key(self, user):
#     return str(user.user_id)
# 
#   def previous_managed_ids(self, user):
#     if self.archive is None:
#       return None
# 
#     key = self.archive_key(user)
#     return self.archive[key] if key in self.archive else []
# 
#   def set_managed_ids(self, user, managed_ids):
#     # `dict` makes a copy of `archive` so I can modify it freely
#     new_archive = dict(self.archive) if self.archive else {}
#     # Overwrite the old data
#     new_archive[self.archive_key(user)] = managed_ids
#     # Save the data to disk
#     archive_json = json.dumps(new_archive)
#     with open(self.archive_path, "w+") as f:
#       f.write(archive_json)
#     # At this point if an exception wasnt thrown then we know the data
#     # save successfully, so overwrite our local data with what we just saved
#     self.archive = new_archive


import json
import os
import shutil
import tempfile
import unittest
from mockito import *
from ice.history import ManagedROMArchive

    archive = ManagedROMArchive(missing_path)
    self.assertIsNone(archive.previous_managed_ids(self.mock_user))

  def test_previous_managed_ids_raises_exception_for_malformed_json(self):
    with open(self.temppath, "w+") as f:
      f.write("notrealjson")

    with self.assertRaises(ValueError):
      archive = ManagedROMArchive(self.temppath)

  def test_previous_managed_ids_returns_empty_list_for_missing_user(self):
    data = {
      "1337": []
    }
    with open(self.temppath, "w+") as f:
      f.write(json.dumps(data))
    archive = ManagedROMArchive(self.temppath)

    self.assertEquals(archive.previous_managed_ids(self.mock_user), [])

  def test_previous_managed_ids_returns_list_from_json(self):
    data = {
      "1234": [
        "1234567890",
        "0987654321",
      ]
    }
    with open(self.temppath, "w+") as f:
      f.write(json.dumps(data))

# Path: ice/history.py
# class ManagedROMArchive(object):
#   def __init__(self, archive_path):
#     self.archive_path = archive_path
# 
#     self.archive = self.load_archive(archive_path)
# 
#   def load_archive(self, path):
#     if not os.path.exists(path):
#       return None
# 
#     with open(path) as archive_file:
#       return json.load(archive_file)
# 
#   def archive_key(self, user):
#     return str(user.user_id)
# 
#   def previous_managed_ids(self, user):
#     if self.archive is None:
#       return None
# 
#     key = self.archive_key(user)
#     return self.archive[key] if key in self.archive else []
# 
#   def set_managed_ids(self, user, managed_ids):
#     # `dict` makes a copy of `archive` so I can modify it freely
#     new_archive = dict(self.archive) if self.archive else {}
#     # Overwrite the old data
#     new_archive[self.archive_key(user)] = managed_ids
#     # Save the data to disk
#     archive_json = json.dumps(new_archive)
#     with open(self.archive_path, "w+") as f:
#       f.write(archive_json)
#     # At this point if an exception wasnt thrown then we know the data
#     # save successfully, so overwrite our local data with what we just saved
#     self.archive = new_archive


# Path: tests/managed_rom_archive_tests.py
import json
import os
import shutil
import tempfile
import unittest
from mockito import *
from ice.history import ManagedROMArchive


    archive = ManagedROMArchive(missing_path)
    self.assertIsNone(archive.previous_managed_ids(self.mock_user))

  def test_previous_managed_ids_raises_exception_for_malformed_json(self):
    with open(self.temppath, "w+") as f:
      f.write("notrealjson")

    with self.assertRaises(ValueError):
      archive = ManagedROMArchive(self.temppath)

  def test_previous_managed_ids_returns_empty_list_for_missing_user(self):
    data = {
      "1337": []
    }
    with open(self.temppath, "w+") as f:
      f.write(json.dumps(data))
    archive = ManagedROMArchive(self.temppath)

    self.assertEquals(archive.previous_managed_ids(self.mock_user), [])

  def test_previous_managed_ids_returns_list_from_json(self):
    data = {
      "1234": [
        "1234567890",
        "0987654321",
      ]
    }
    with open(self.temppath, "w+") as f:
      f.write(json.dumps(data))

    archive = ManagedROMArchive(self.temppath)