Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several tests write to node.open #4721

Closed
ltalirz opened this issue Feb 8, 2021 · 5 comments · Fixed by #4719
Closed

Several tests write to node.open #4721

ltalirz opened this issue Feb 8, 2021 · 5 comments · Fixed by #4719
Assignees
Labels
type/feature request status undecided
Milestone

Comments

@ltalirz
Copy link
Member

ltalirz commented Feb 8, 2021

E.g.

    def test_node_repo_cat(self):
        """Test 'verdi node repo cat' command."""
        # Test cat binary files
        with self.folder_node.open('filename.txt.gz', 'wb') as fh_out:
            fh_out.write(gzip.compress(b'COMPRESS'))

Resulting in warnings

tests/cmdline/commands/test_calcjob.py::TestVerdiCalculation::test_calcjob_inputcat
tests/cmdline/commands/test_calcjob.py::TestVerdiCalculation::test_calcjob_outputcat
tests/cmdline/commands/test_node.py::TestVerdiNode::test_node_repo_cat
tests/engine/test_calc_job.py::test_parse_not_implemented
tests/engine/test_calc_job.py::test_parse_scheduler_excepted
  /home/runner/work/aiida-core/aiida-core/aiida/orm/nodes/node.py:452: AiidaDeprecationWarning: from v2.0 only the modes 'r' and 'rb' will be accepted
    warnings.warn("from v2.0 only the modes 'r' and 'rb' will be accepted", AiidaDeprecationWarning)  # pylint: disable=no-member

How should these tests be rewritten?

@ltalirz ltalirz added the type/feature request status undecided label Feb 8, 2021
@ltalirz ltalirz added this to the v2.0.0 milestone Feb 8, 2021
@giovannipizzi giovannipizzi self-assigned this Feb 9, 2021
@giovannipizzi
Copy link
Member

The issue is that here we are hacking into an already stored node (self.folder_node) and adding a file without going via the ORM API. This is still working now (opening a file just checks if the file is there) but will not work in 2.0 (one also has to register that the file is there in the DB in 2.0).

The solution I'm implementing is the following. First, I don't have a pre-stored node self.folder_node, but a "factory" function

    @classmethod
    def get_unstored_folder_node(cls):
        """Get a "default" folder node with some data.

        The node is unstored so one can add more content to it before storing it.
        """
        folder_node = orm.FolderData()
        cls.content_file1 = 'nobody expects'
        cls.content_file2 = 'the minister of silly walks'
        cls.key_file1 = 'some/nested/folder/filename.txt'
        cls.key_file2 = 'some_other_file.txt'
        folder_node.put_object_from_filelike(io.StringIO(cls.content_file1), cls.key_file1)
        folder_node.put_object_from_filelike(io.StringIO(cls.content_file2), cls.key_file2)
        return folder_node

Then:

  1. In the tests where I just needed to use self.folder_node without modifying, I just add at the top folder_node = self.get_unstored_folder_node().store() and reference simply folder_node rather than self.folder_node
  2. In the tests where I need to add content (like the one above), I do something like this:
     def test_node_repo_cat(self):
         """Test 'verdi node repo cat' command."""
         # Test cat binary files
         folder_node = self.get_unstored_folder_node()
         folder_node.put_object_from_filelike(io.BytesIO(gzip.compress(b'COMPRESS')), 'filename.txt.gz', mode='wb')
         folder_node.store()

giovannipizzi added a commit to ltalirz/aiida-core that referenced this issue Feb 9, 2021
A few tests were writing directly to a node's repository folder, after
this got stored.
While this still works, this is now deprecated and will not work
anymore in 2.0.

Here I'm replacing all times we use `.open` with `'w'` or `'wb'` mode
with a correct call to `put_object_from_filelike`, calling it
*before* the node is stored.

In one case, the data comes from a small archive file. In this case,
I recreated the (zipped) .aiida file adding two additional (binary) files
obtained by gzipping a short string.
This was used to ensure that `inputcat` and `outputcat` work also
when binary data was requested. Actually, this is better than before,
where the actual input or output of the calculation were overwritten
and then replaced back.

This commit fixes aiidateam#4721
@ltalirz
Copy link
Member Author

ltalirz commented Sep 15, 2021

Just came back to this issue due to a from v2.0 only the modes 'r' and 'rb' will be accepted warning.

I have to say that

folder_node.put_object_from_filelike(io.StringIO('Text'), 'filename.txt', mode='w')

is quite a bit less intuitive than

with self.folder_node.open('filename.txt', 'w') as handle:
    handle.write('Text')

In retrospect it would have been useful to at least point out in the warning how users are supposed to replace existing open calls. Anyhow, I guess now it's too late - the warnings are already gone in develop.

Perhaps the example in this comment can be useful to googling AiiDA users.

@giovannipizzi
Copy link
Member

The place to add this note is here: https://github.com/aiidateam/aiida-core/wiki/AiiDA-2.0-plugin-migration-guide
Can you please write a small section there?

@giovannipizzi
Copy link
Member

(if you want you can also clarify the docstring:

.. note:: this should only be used to open a handle to read an existing file. To write a new file use the method
``put_object_from_filelike`` instead.

@ltalirz
Copy link
Member Author

ltalirz commented Sep 22, 2021

The place to add this note is here: https://github.com/aiidateam/aiida-core/wiki/AiiDA-2.0-plugin-migration-guide
Can you please write a small section there?

Thanks, done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature request status undecided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants