def test_data_persistence(self):
        app_state = AppState()

        new_conf = DownloadConfiguration(number_of_images=9309,
                                         images_per_category=83,
                                         download_destination='481516')

        last_result = Result(failed_urls=['1', 'one'], succeeded_urls=['x'])
        progress_info = ProgressInfo(total_downloaded=192,
                                     total_failed=38,
                                     finished=False,
                                     last_result=last_result)

        position = Position(3, 1)
        counts = {'wnid1': 29, 'wnid10': 3}
        internal = InternalState(iterator_position=position,
                                 category_counts=counts,
                                 file_index=322)

        app_state.set_configuration(new_conf)
        app_state.set_progress_info(progress_info)
        app_state.set_internal_state(internal)
        app_state.save()

        app_state = AppState()

        conf = app_state.download_configuration
        self.assertEqual(conf.download_destination, '481516')
        self.assertEqual(conf.number_of_images, 9309)
        self.assertEqual(conf.images_per_category, 83)

        progress_info = app_state.progress_info
        self.assertEqual(progress_info.total_downloaded, 192)
        self.assertEqual(progress_info.total_failed, 38)
        self.assertEqual(progress_info.finished, False)
        self.assertEqual(progress_info.last_result.failed_urls, ['1', 'one'])

        self.assertEqual(progress_info.last_result.succeeded_urls, ['x'])

        internal = app_state.internal_state
        self.assertEqual(internal.iterator_position.word_id_offset, 3)
        self.assertEqual(internal.iterator_position.url_offset, 1)
        self.assertEqual(internal.category_counts, counts)
        self.assertEqual(internal.file_index, 322)
    def test_to_json(self):
        app_state = AppState()

        new_conf = DownloadConfiguration(number_of_images=9309,
                                         images_per_category=83,
                                         download_destination='481516')

        last_result = Result(failed_urls=['1', 'one'], succeeded_urls=['x'])
        progress_info = ProgressInfo(total_downloaded=192,
                                     total_failed=38,
                                     finished=False,
                                     last_result=last_result)

        position = Position(3, 1)
        counts = {'wnid1': 29, 'wnid10': 3}
        internal = InternalState(iterator_position=position,
                                 category_counts=counts,
                                 file_index=322)

        app_state.set_configuration(new_conf)
        app_state.set_progress_info(progress_info)
        app_state.set_internal_state(internal)

        app_state.add_error('Some error')

        state_data = json.loads(app_state.to_json())

        self.assertEqual(state_data['downloadPath'], '481516')
        self.assertEqual(state_data['numberOfImages'], 9309)
        self.assertEqual(state_data['imagesPerCategory'], 83)
        self.assertNotEqual(state_data['timeLeft'], '')
        self.assertEqual(state_data['imagesLoaded'], 192)
        self.assertEqual(state_data['failures'], 38)
        self.assertEqual(state_data['failedUrls'], ['1', 'one'])
        self.assertEqual(state_data['succeededUrls'], ['x'])

        self.assertEqual(state_data['errors'], ['Some error'])

        self.assertAlmostEqual(state_data['progress'], 192.0 / 9309)