Skip to content

load username and email #63

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

Closed
wants to merge 7 commits into from
Closed

Conversation

yucongalicechen
Copy link
Collaborator

@yucongalicechen yucongalicechen commented May 20, 2024

  • closes load the username into the metadata #30, closes load user email into metadata #31.
  • Initial commit on UC1 and UC4 in load the username into the metadata #30. Changed the pseudocode structued in that issue to add the functionality of overwriting config file using user inputs. For tests I added two json config files in conftest.py.
  • Added user_config.py to manage user configuration.
  • Added load_user_info function in labpdfprocapp.py right after get_args().
  • I'll address UC5 later. For the case of multiple usernames and emails, I haven't thought of a great way of doing it now..

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this @yucongalicechen .

When you write hte tests I will review those and then I will look at the code.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see inline

@@ -44,4 +47,10 @@ def user_filesystem(tmp_path):
f.write("good_data.xy \n")
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")

user_config_data = {"username": "good_username", "email": "[email protected]"}
with open(conf_dir / "test.json", "w") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to have the correct name here and below... diffpyconf.json

user_config_data = {"username": "good_username", "email": "[email protected]"}
with open(conf_dir / "test.json", "w") as f:
json.dump(user_config_data, f)
with open(conf_dir / "test2.json", "w") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not allowed. We will only check for diffpyconf.json.

However, we will check for it here, but also in cwd where the programming is running. We would like to test both situations so we can't write that second file in conftest.py but will have to write it in a bespoke test.

def test_load_user_info(monkeypatch, inputs, expected, user_filesystem):
os.chdir(user_filesystem / "conf_dir")
expected_username, expected_email = expected
input_username, input_email, input_config_file = inputs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not inputting a config file. options are:

  1. find diffpyconfig.json in cwd
  2. failing that, find diffpyconfig.json in ~
  3. failing that, check if username and email are provided in the input line
  4. failing that, trigger the workflow to create diffpyconfig.json in ~

Then the workflow is:

  1. If 1 or 3, check that those files contain username/email. If they do continue, if they don't trigger workflow to update diffpyconfig.json
  2. If continue above, then check if username/email are provided at the command line. If yes, use them (but don't update config). If not, use the values in config.
  3. if (4.) above, then use these values to create the diffpyconfig.json

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edited the tests. I included "paths" in @pytest.mark.parametrize because it helps manage different cwd and home directories, not sure if this is the best approach though.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testing structure is hard for me to understand so I am not sure if it is capturing the right information. It may be ok, but not easy to follow.

I was thinking something more like

def test_load_user_info_configfile(monkeypatch, inputs, expected, user_filesystem):
    os.chdir(user_filesystem)

    # test it works when config file is in home directory
    cli_inputs = ["2.5", "data.xy"]
    actual_args = get_args(cli_inputs)
    actual_args = load_user_info(actual_args)
    assert actual_args.username == expected_args_username
    assert actual_args.email == expected_args_email

    # test it works when config file is in current directory
    with open(user_filesystem / 'diffpyconfig.json', "w") as f:
        config_data = json.dump(user_data f)
    cli_inputs = ["2.5", "data.xy"]
    actual_args = get_args(cli_inputs)
    actual_args = load_user_info(actual_args)
    assert actual_args.username == expected_args_username
    assert actual_args.email == expected_args_email

then other tests for other cases

this seems more understandable to me, though your tests may also be working, I just can't follow them too easily.

@yucongalicechen
Copy link
Collaborator Author

Okay, yes I also think this is more readable. But this is not testing the config file? I was using different paths because I wanted to test if the config file was modified or not by the inputs.

@sbillinge
Copy link
Contributor

sbillinge commented May 21, 2024 via email

@sbillinge
Copy link
Contributor

sbillinge commented May 21, 2024 via email

@yucongalicechen
Copy link
Collaborator Author

yucongalicechen commented May 21, 2024

Yes I get it. It's just my previous codes updated the config files, so I wanted to correct that. The tests are a bit similar so I was thinking of ways to put them together. I now separate the tests into no config files, config file in home directory, and config file in cwd.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can test everything (except the missing config file) in one test thinking about it. We just have to test the username and email come out right when the right flow is followed, then we read the config file in conf_dir and make sure it has the right thing in it.

For the missing config file we will have to delete the config file in home before running the same test. We can call the user home_dir_user, cwd_user, cli_user, input_user and same for the emails and make sure the right user shows up in args and in the home_dir/diffpyconf.json These can be handed in in params

What do you think?

@sbillinge
Copy link
Contributor

def test_load_user_info_configfile(monkeypatch, inputs, expected, user_filesystem):
    os.chdir(user_filesystem)

    # test it works when config file is in home directory
    cli_inputs = ["2.5", "data.xy"]
    actual_args = get_args(cli_inputs)
    actual_args = load_user_info(actual_args)
    assert actual_args.username == expected_args_username
    assert actual_args.email == expected_args_email

    # test it works when config file is in current directory
    with open(user_filesystem / 'diffpyconfig.json', "w") as f:
        config_data = json.dump(user_data f)
    cli_inputs = ["2.5", "data.xy"]
    actual_args = get_args(cli_inputs)
    actual_args = load_user_info(actual_args)
    assert actual_args.username == expected_args_username
    assert actual_args.email == expected_args_email

@sbillinge sbillinge closed this May 22, 2024
@yucongalicechen
Copy link
Collaborator Author

Yes I think we can combine these. Since this PR is closed, shall I make another PR for the updates?

@sbillinge
Copy link
Contributor

sbillinge commented May 22, 2024 via email

@sbillinge
Copy link
Contributor

I see I closed it, but that was in error, I thought it was a stale PR from earlier. My apologies.

I think writing a little test private function that asserts that the name and email are correct in args and also that they are correct in the home_dir/diffpyconfig.json will do it, then this function can be called with all the different arrangements of things on disc (config files present/absent/valid/invalid in different locations).

Actually, I thought more about this and I think that this is a capability that we may want to have working across many our diffpy programs (which is why I chose the name of the config file with diffpy not labpdfproc), but from this point of view, having this capability in diffpy.utils rather than labpdfproc actually makes sense to me. We can maybe finish the reviewing process here on this PR but not merge it, then copy the code over to diffpy.utils and merge it over there?

I think this is also true for the time-stamp and package name-version capabilities. Everywhere we write .chi we would like them to look like this.

@yucongalicechen
Copy link
Collaborator Author

Ah, okay, got it. I'll push my latest changes here and copy over the codes to diffpy.utils.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of quick comments. But are you then going to combine tests into one or two test functions as we discussed? Please reach out if you have questions.

@@ -8,6 +9,8 @@ def user_filesystem(tmp_path):
base_dir = Path(tmp_path)
input_dir = base_dir / "input_dir"
input_dir.mkdir(parents=True, exist_ok=True)
conf_dir = base_dir / "conf_dir"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call it home_dir instead of conf_dir to make the intent clearer.

@@ -44,4 +47,8 @@ def user_filesystem(tmp_path):
f.write("good_data.xy \n")
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")

user_config_data = {"username": "good_username", "email": "[email protected]"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be home_user and [email protected]

@yucongalicechen
Copy link
Collaborator Author

replaced by #77

@yucongalicechen yucongalicechen deleted the user branch June 14, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

load user email into metadata load the username into the metadata
2 participants