pytest-postgresql icon indicating copy to clipboard operation
pytest-postgresql copied to clipboard

postgresql_proc file loader and relative paths?

Open jaskij opened this issue 9 months ago • 6 comments

I am trying to use postgresql_proc with loading external SQL files, using relative paths, similarly to what is shown in your README. It seems to me that pytest-postgresql does not do anything about the paths, instead using the current working directory as it was when calling pytest. This is quite confusing.

My expectation was for the paths to the scripts to be relative to the file in which postgresql_proc is called.

One thing that could have caused an issue is that the files are outside the Python package. It's a monorepo with the SQL elsewhere.

I have found two workarounds:

  • call os.chdir(Path(__file__).parent) at the top of conftest.py
  • prepend Path(__file__).parent to the paths for all the SQL files

jaskij avatar Mar 13 '25 22:03 jaskij

The expectation here is primarily about the state of my terminal when calling pytest not influencing the tests, less so about which value is chosen as the default.

jaskij avatar Mar 13 '25 22:03 jaskij

@jaskij the thing is that it's the Python's behaviour. any relative path you use for constructing Path object is relative to cwd, not the module you defined that variable in.

I think that it would be misleading to import that Path object into other modules, and this at least is consistent.

Actually the 2nd workaround is what you're looking for here.

See here: https://github.com/dbfixtures/pytest-postgresql/blob/main/tests/conftest.py#L13

Other approach is to build path relative to cwd: https://github.com/dbfixtures/pytest-postgresql/blob/main/tests/docker/test_noproc_docker.py#L13

fizyk avatar Mar 15 '25 16:03 fizyk

Ah, so my lack of familiarity with Python shows itself. Fair enough. Prepending the path works well enough, so there's that. What do you think about adding a sentence or two about this behavior to the README?

jaskij avatar Mar 17 '25 18:03 jaskij

@jaskij worth a try, Would you like to add it?

fizyk avatar Mar 19 '25 13:03 fizyk

Sure, why not. One question though: I'm a huge fan of admonitions, what GitHub calls alerts but they can stand out a bit too much when used in only one place in the document.

So: use them or not?

Another question is, which version to use: os.path.dirname(os.path.abspath(__file__)) or Path(__file__).absolute().parent?

jaskij avatar Mar 19 '25 14:03 jaskij

I don't think they'll work with restructured text I use for README. And for that, you'd rather have to use .. note:: I think.

And use the latter one, as that looks more pythonic.

fizyk avatar Mar 19 '25 20:03 fizyk