def test_list_should_return_empty_list_when_folder_doesnt_exist(
         self, mock_exists):
     mock_exists.return_value = False
     with patch('infrastructure.configuration.open',
                create=True) as mock_open:
         cm = ConfigurationManager()
         self.assertEquals([], cm.list())
 def test_list_should_return_empty_list_when_folder_contains_no_configurations(
         self, mock_listdir, mock_exists):
     mock_exists.return_value = True
     mock_listdir.return_value = []
     with patch('infrastructure.configuration.open',
                create=True) as mock_open:
         cm = ConfigurationManager()
         self.assertEquals([], cm.list())
    def test_new_should_return_a_config_with_defaults_and_correct_name(self):
        name = "Apple"
        expected = self.default_config
        expected.name = name
        cm = ConfigurationManager()

        actual = cm.new(name)

        self.assertConfigurationEqual(expected, actual)
    def test_load_should_throw_exception_if_bad_data(self, mock_exists):
        mock_exists.return_value = True
        with patch('infrastructure.configuration.open',
                   create=True) as mock_open:
            manager = mock_open.return_value.__enter__.return_value
            manager.read.return_value = StringIO("ASDFASDFASD")
            cm = ConfigurationManager()

            with self.assertRaises(Exception):
                cm.load(u"Some Printer")
 def test_load_should_throw_exception_not_there(self, mock_makedirs,
                                                mock_listdir, mock_exists):
     mock_exists.return_value = False
     mocked_open = mock_open()
     with patch('infrastructure.configuration.open',
                mocked_open,
                create=True):
         cm = ConfigurationManager()
         with self.assertRaises(Exception):
             cm.load(u"Not There")
    def test_load_should_load_data(self, mock_exists):
        mock_exists.return_value = True

        expected = self.default_config
        mocked_open = mock_open(read_data=self.default_config.toJson())

        with patch('infrastructure.configuration.open',
                   mocked_open,
                   create=True):
            cm = ConfigurationManager()
            actual = cm.load('Some Printer')
            self.assertConfigurationEqual(expected, actual)
 def test_list_should_only_process_cfg_files(self, mock_listdir,
                                             mock_exists):
     mock_exists.return_value = True
     mock_listdir.return_value = ['blabla.cow']
     expected = []
     mocked_open = mock_open(read_data=self.default_config.toJson())
     with patch('infrastructure.configuration.open',
                mocked_open,
                create=True):
         cm = ConfigurationManager()
         actual = cm.list()
         self.assertEquals(expected, actual)
    def test_save_should_create_data_folder_if_it_does_not_exist(
            self, mock_makedirs, mock_exists):
        mock_exists.return_value = False
        expected_path = expected_path = os.path.join(os.path.expanduser('~'),
                                                     '.peachyprintertools')
        with patch('infrastructure.configuration.open',
                   create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=file)
            cm = ConfigurationManager()
            data = cm.new('Test1')

            cm.save(data)

        mock_makedirs.assert_called_with(expected_path)
    def test_save_printers_configuration_dictionary_to_peachyprintertools_folder_in_home(
            self, mock_makedirs, mock_exists):
        mock_exists.return_value = True
        printer_name = 'Test1'
        expected_path = os.path.join(os.path.expanduser('~'),
                                     '.peachyprintertools',
                                     printer_name + '.cfg')
        mocked_open = mock_open(read_data=self.default_config.toJson())
        with patch('infrastructure.configuration.open',
                   mocked_open,
                   create=True):
            cm = ConfigurationManager()
            data = cm.new(printer_name)
            expected_data = data.toJson()
            cm.save(data)

        self.assertFalse(mock_makedirs.called)
        mocked_open.assert_called_with(expected_path, 'w')
        mocked_open().write.assert_called_with(expected_data)
    def test_new_creates_a_new_configution_dict_with_sane_values(self):
        cm = ConfigurationManager()

        actual = cm.new('Peachy Printer')
        expected = self.default_config
        self.assertConfigurationEqual(expected, actual)