def test_raises_value_error_with_key_fieldname_empty(self):
        file_path = self.create_tempfile()
        os.remove(file_path)

        with self.assertRaises(ValueError):
            file_release_manager.CSVFileReleaseManager(file_path=file_path,
                                                       key_fieldname='')
    async def test_removes_values_from_existing_file(self, key):
        file_path = self.create_tempfile()
        existing_fieldnames = ['key', 'a', 'b']
        existing_values = [
            {
                'key': 1,
                'a': 10,
                'b': 20
            },
            {
                'key': 2,
                'a': 11,
                'b': 21
            },
        ]
        _write_values_to_csv(file_path=file_path,
                             fieldnames=existing_fieldnames,
                             values=existing_values)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        await release_mngr._remove_values_greater_than(key)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        if key == 0:
            expected_fieldnames = ['key']
        else:
            expected_fieldnames = existing_fieldnames
        self.assertEqual(actual_fieldnames, expected_fieldnames)
        expected_values = existing_values[0:key]
        expected_values = tree.map_structure(str, expected_values)
        self.assertEqual(actual_values, expected_values)
    def test_raises_type_error_with_save_mode(self, save_mode):
        file_path = self.create_tempfile()
        os.remove(file_path)

        with self.assertRaises(TypeError):
            file_release_manager.CSVFileReleaseManager(file_path=file_path,
                                                       save_mode=save_mode)
    def test_raises_type_error_with_key_fieldname(self, key_fieldname):
        file_path = self.create_tempfile()
        os.remove(file_path)

        with self.assertRaises(TypeError):
            file_release_manager.CSVFileReleaseManager(
                file_path=file_path, key_fieldname=key_fieldname)
    async def test_appends_value_to_existing_file(self, value):
        file_path = self.create_tempfile()
        existing_fieldnames = ['key', 'a', 'b']
        existing_value = {'key': 1, 'a': 10, 'b': 20}
        _write_values_to_csv(file_path=file_path,
                             fieldnames=existing_fieldnames,
                             values=[existing_value])
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path,
            save_mode=file_release_manager.CSVSaveMode.APPEND)

        await release_mngr._append_value(value)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        expected_fieldnames = existing_fieldnames.copy()
        expected_fieldnames.extend(
            [x for x in value.keys() if x not in expected_fieldnames])
        self.assertEqual(actual_fieldnames, expected_fieldnames)
        expected_value1 = {name: '' for name in expected_fieldnames}
        expected_value1.update(existing_value)
        expected_value2 = {name: '' for name in expected_fieldnames}
        expected_value2.update(value)
        expected_values = [expected_value1, expected_value2]
        expected_values = tree.map_structure(str, expected_values)
        self.assertEqual(actual_values, expected_values)
    def test_creates_new_file_with_file_path_path_like(self):
        file_path = self.create_tempfile()
        os.remove(file_path)
        self.assertFalse(os.path.exists(file_path))

        file_release_manager.CSVFileReleaseManager(file_path=file_path)

        self.assertTrue(os.path.exists(file_path))
    async def test_raises_type_error_with_key(self, key):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        with self.assertRaises(TypeError):
            await release_mngr._remove_values_greater_than(key)
    def test_does_not_raise_type_error_with_key_fieldname(self):
        file_path = self.create_tempfile()
        os.remove(file_path)

        try:
            file_release_manager.CSVFileReleaseManager(file_path=file_path,
                                                       key_fieldname='z')
        except TypeError:
            self.fail('Raised TypeError unexpectedly.')
    def test_creates_new_dir_with_file_path_path_like(self):
        file_path = self.create_tempfile()
        root_dir = os.path.dirname(file_path)
        shutil.rmtree(root_dir)
        self.assertFalse(os.path.exists(root_dir))

        file_release_manager.CSVFileReleaseManager(file_path=file_path)

        self.assertTrue(os.path.exists(file_path))
    async def test_raises_type_error_with_key(self, key):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)
        value = 1
        type_signature = computation_types.TensorType(tf.int32)

        with self.assertRaises(TypeError):
            await release_mngr.release(value, type_signature, key)
    def test_does_not_raise_type_error_with_save_mode(self):
        file_path = self.create_tempfile()
        os.remove(file_path)

        try:
            file_release_manager.CSVFileReleaseManager(
                file_path=file_path,
                save_mode=file_release_manager.CSVSaveMode.APPEND)
        except TypeError:
            self.fail('Raised TypeError unexpectedly.')
    def test_returns_values_from_empty_file(self):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        fieldnames, values = release_mngr._read_values()

        self.assertEqual(fieldnames, ['key'])
        self.assertEqual(values, [])
    async def test_writes_value(self, value, type_signature, expected_value):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        await release_mngr.release(value, type_signature, 1)

        _, actual_value = _read_values_from_csv(file_path)
        self.assertEqual(actual_value, expected_value)
    def test_initializes_with_empty_file(self):
        file_path = self.create_tempfile()
        _write_values_to_csv(file_path=file_path,
                             fieldnames=['key'],
                             values=[])
        self.assertTrue(os.path.exists(file_path))

        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        self.assertIsNone(release_mngr._latest_key)
    async def test_removes_values_from_empty_file(self, key):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        await release_mngr._remove_values_greater_than(key)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        self.assertEqual(actual_fieldnames, ['key'])
        self.assertEqual(actual_values, [])
    async def test_does_not_raise_type_error_with_key(self, key):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)
        value = 1
        type_signature = computation_types.TensorType(tf.int32)

        try:
            await release_mngr.release(value, type_signature, key)
        except TypeError:
            self.fail('Raised TypeError unexpectedly.')
    def test_writes_values_to_empty_file(self, fieldnames, values):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        release_mngr._write_values(fieldnames=fieldnames, values=values)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        self.assertEqual(actual_fieldnames, fieldnames)
        expected_values = tree.map_structure(str, values)
        self.assertEqual(actual_values, expected_values)
    def test_raises_incompatible_file_error_with_unknown_key_fieldname(self):
        file_path = self.create_tempfile()
        _write_values_to_csv(file_path=file_path,
                             fieldnames=['z', 'a', 'b'],
                             values=[{
                                 'z': 1,
                                 'a': 10,
                                 'b': 20
                             }])

        with self.assertRaises(
                file_release_manager.FileReleaseManagerIncompatibleFileError):
            file_release_manager.CSVFileReleaseManager(file_path=file_path)
    async def test_raises_permission_denied_error(self):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path,
            save_mode=file_release_manager.CSVSaveMode.APPEND)

        with mock.patch.object(csv.DictWriter, 'writerow') as mock_writerow:
            mock_writerow.side_effect = csv.Error()

            with self.assertRaises(file_release_manager.
                                   FileReleaseManagerPermissionDeniedError):
                await release_mngr._append_value({})
    def test_returns_values_from_existing_file(self, fieldnames, values):
        file_path = self.create_tempfile()
        _write_values_to_csv(file_path=file_path,
                             fieldnames=fieldnames,
                             values=values)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        actual_fieldnames, actual_values = release_mngr._read_values()

        self.assertEqual(actual_fieldnames, fieldnames)
        expected_values = tree.map_structure(str, values)
        self.assertEqual(actual_values, expected_values)
    def test_initializes_with_existing_file(self):
        file_path = self.create_tempfile()
        _write_values_to_csv(file_path=file_path,
                             fieldnames=['key', 'a', 'b'],
                             values=[{
                                 'key': 1,
                                 'a': 10,
                                 'b': 20
                             }])
        self.assertTrue(os.path.exists(file_path))

        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        self.assertEqual(release_mngr._latest_key, 1)
    def test_writes_values_to_existing_file(self, fieldnames, values):
        file_path = self.create_tempfile()
        _write_values_to_csv(file_path=file_path,
                             fieldnames=['key', 'a', 'b'],
                             values=[{
                                 'key': 1,
                                 'a': 10,
                                 'b': 20
                             }])
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)

        release_mngr._write_values(fieldnames=fieldnames, values=values)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        self.assertEqual(actual_fieldnames, fieldnames)
        expected_values = tree.map_structure(str, values)
        self.assertEqual(actual_values, expected_values)
    async def test_calls_remove_values_greater_than_with_empty_file(self):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path)
        value = {'a': 11, 'b': 21}
        type_signature = computation_types.StructType([
            ('a', tf.int32),
            ('b', tf.int32),
        ])

        with mock.patch.object(release_mngr, '_remove_values_greater_than'
                               ) as mock_remove_values_greater_than:
            await release_mngr.release(value, type_signature, 1)

            mock_remove_values_greater_than.assert_called_once_with(0)

        self.assertEqual(release_mngr._latest_key, 1)
    async def test_appends_value_to_empty_file(self, value):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path,
            save_mode=file_release_manager.CSVSaveMode.APPEND)

        await release_mngr._append_value(value)

        actual_fieldnames, actual_values = _read_values_from_csv(file_path)
        expected_fieldnames = ['key']
        expected_fieldnames.extend(
            [x for x in value.keys() if x not in expected_fieldnames])
        self.assertEqual(actual_fieldnames, expected_fieldnames)
        expected_value = {name: '' for name in expected_fieldnames}
        expected_value.update(value)
        expected_values = [expected_value]
        expected_values = tree.map_structure(str, expected_values)
        self.assertEqual(actual_values, expected_values)
    async def test_calls_write_value(self):
        file_path = self.create_tempfile()
        os.remove(file_path)
        release_mngr = file_release_manager.CSVFileReleaseManager(
            file_path=file_path,
            save_mode=file_release_manager.CSVSaveMode.WRITE)
        value = {'a': 11, 'b': 21}
        type_signature = computation_types.StructType([
            ('a', tf.int32),
            ('b', tf.int32),
        ])

        with mock.patch.object(release_mngr,
                               '_write_value') as mock_write_value:
            await release_mngr.release(value, type_signature, 1)

            mock_write_value.assert_called_once_with({
                'key': 1,
                'a': 11,
                'b': 21
            })

        self.assertEqual(release_mngr._latest_key, 1)
 def test_raises_value_error_with_file_path_empty(self):
     with self.assertRaises(ValueError):
         file_release_manager.CSVFileReleaseManager(file_path='')
    def test_raises_incompatible_file_error_with_unknown_file(self):
        file_path = self.create_tempfile()

        with self.assertRaises(
                file_release_manager.FileReleaseManagerIncompatibleFileError):
            file_release_manager.CSVFileReleaseManager(file_path=file_path)
 def test_raises_type_error_with_file_path(self, file_path):
     with self.assertRaises(TypeError):
         file_release_manager.CSVFileReleaseManager(file_path=file_path)