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

Examples of use #351

Closed
sylvain-ri opened this issue Feb 9, 2022 · 10 comments
Closed

Examples of use #351

sylvain-ri opened this issue Feb 9, 2022 · 10 comments

Comments

@sylvain-ri
Copy link

I would like to have full working examples

Based on the documentation I found on pythonhosted.org, this library seems very easy to use. But I haven't managed to actually create a fixture and use it for my tests.

Something is missing for me to know how the factories, and connect to the fixture database. It looks promising though, so i would really like to use your library.

@fizyk
Copy link
Member

fizyk commented Feb 9, 2022

@sylvain-ri maybe you don't have to use factories at the beginning? What are you trying to achieve?

Hmmm.... simple example a'la docker without the use of factories might be helpful though anyway 🤔

@sylvain-ri
Copy link
Author

I would like to add unittests to a python module with SQL queries.
So i thought the best way would be to create a fixture or mock of the database, so that the unittests can run the query functions against that test database.

Is pytest-mysql designed for this goal ?

@fizyk
Copy link
Member

fizyk commented Feb 10, 2022

@sylvain-ri yes
If you'd look at these tests:
https://github.com/ClearcodeHQ/pytest-mysql/blob/main/tests/test_mysql.py
The first three tests there are the most basic ones that would allow you to run MySQL queries to the database and require no configuration or factories to be used for pytest-mysql. You'll need to install MySQL/MariaDB though.

Also, bear in mind, that when you connect to an external service like a database in your tests, these are no longer unit tests, but integration tests.

@sylvain-ri
Copy link
Author

@fizyk Thank you for your answer.
I'm quite new to this database testing, so I can't figure out how to make those tests work. In the example file you give, when I run it, it hangs there with no test running. When using the factories, I face a FileNotFound error. So i'm probably using it wrong, somewhere, somehow.

Is there an example that i can "just run" ? It's easier (for me) to start from a template and then modify it to my need than starting from a blank page :)

@fizyk
Copy link
Member

fizyk commented Feb 10, 2022

@sylvain-ri what system are you running the tests on? And have you got the MySQL installed? Plus, could you share the full FileNotFound error? I'd like to see what exactly could not be found.

@sylvain-ri
Copy link
Author

sylvain-ri commented Feb 11, 2022

I tried to run your file example https://github.com/ClearcodeHQ/pytest-mysql/blob/main/tests/test_mysql.py
I have mysql python package, but MySQL itself, not sure. Got MySQL workbench.
I'm developping an app in the cloud, with a database attached. I thought your library would allow to have a temporary or in memory database for the purpose of testing ?

Tests results

================================================================== short test summary info ==================================================================
ERROR ai_utils/db/tests/test_query.py::test_proc - FileNotFoundError: [WinError 2] The system cannot find the file specified
ERROR ai_utils/db/tests/test_query.py::test_mysql - FileNotFoundError: [WinError 2] The system cannot find the file specified
ERROR ai_utils/db/tests/test_query.py::test_mysql_test_without_cursor - FileNotFoundError: [WinError 2] The system cannot find the file specified
ERROR ai_utils/db/tests/test_query.py::test_mysql_newfixture - FileNotFoundError: [WinError 2] The system cannot find the file specified
ERROR ai_utils/db/tests/test_query.py::test_random_port

Logs

The error report is extremely verbose, I cut some parts.

$ python -m pytest ai_utils/ --mysql-port=3306
====================================================== test session starts =======================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\work\ai_engine
plugins: cov-3.0.0, mysql-2.2.0
collected 6 items

ai_utils\db\tests\test_query.py .EEEEE                                                                                      [100%]

============================================================= ERRORS =============================================================
__________________________________________________ ERROR at setup of test_proc ___________________________________________________

request = <SubRequest 'mysql_proc' for <Function test_proc>>
tmpdir_factory = TempdirFactory(_tmppath_factory=TempPathFactory(_given_basetemp=None, _trace=<pluggy._tracing.TagTracerSub object at 0x000001700E952910>, _basetemp=WindowsPath('C:/Users/Sylvain/AppData/Local/Temp/pytest-of-Sylvain/pytest-9')))

    @pytest.fixture(scope="session")
    def mysql_proc_fixture(
        request: FixtureRequest, tmpdir_factory: TempdirFactory
    ):
        """
        Process fixture for MySQL server.

        #. Get config.
        #. Initialize MySQL data directory
        #. `Start a mysqld server
            <https://dev.mysql.com/doc/refman/5.0/en/mysqld-safe.html>`_
        #. Stop server and remove directory after tests.
            `See <https://dev.mysql.com/doc/refman/5.6/en/mysqladmin.html>`_

        :param FixtureRequest request: fixture request object
        :param tmpdir_factory: pytest fixture for temporary directories
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor

        """
        config = get_config(request)

...


        mysql_executor = MySQLExecutor(
            mysqld_safe=mysql_mysqld_safe,
            mysqld=mysql_mysqld,
            admin_exec=mysql_admin_exec,
            logfile_path=logfile_path,
            base_directory=tmpdir,
            params=mysql_params,
            user=user or config["user"] or "root",
            host=mysql_host,
            port=mysql_port,
            install_db=mysql_install_db,
        )
>       with mysql_executor:

C:\Softwares\Python39\lib\site-packages\pytest_mysql\factories\process.py:133:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\Softwares\Python39\lib\site-packages\mirakuru\base.py:181: in __enter__
    return self.start()
C:\Softwares\Python39\lib\site-packages\pytest_mysql\executor.py:136: in start
    implementation = self.implementation()
C:\Softwares\Python39\lib\site-packages\pytest_mysql\executor.py:82: in implementation
    version_output = subprocess.check_output(
C:\Softwares\Python39\lib\subprocess.py:424: in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
C:\Softwares\Python39\lib\subprocess.py:505: in run
    with Popen(*popenargs, **kwargs) as process:
C:\Softwares\Python39\lib\subprocess.py:951: in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Popen: returncode: None args: ['mysqld', '--version']>, args = 'mysqld --version', executable = None, preexec_fn = None
close_fds = False, pass_fds = (), cwd = None, env = None, startupinfo = <subprocess.STARTUPINFO object at 0x0000017051DC48B0>
creationflags = 0, shell = False, p2cread = Handle(1036), p2cwrite = -1, c2pread = 14, c2pwrite = Handle(1044), errread = -1
errwrite = Handle(1040), unused_restore_signals = True, unused_gid = None, unused_gids = None, unused_uid = None, unused_umask = -1

unused_start_new_session = False

    def _execute_child(self, args, executable, preexec_fn, close_fds,```
...
    if shell:
        startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = _winapi.SW_HIDE
        comspec = os.environ.get("COMSPEC", "cmd.exe")
        args = '{} /c "{}"'.format (comspec, args)

    if cwd is not None:
        cwd = os.fsdecode(cwd)

    sys.audit("subprocess.Popen", executable, args, cwd, env)

    # Start the process
    try:
      hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                                 # no special security
                                 None, None,
                                 int(not close_fds),
                                 creationflags,
                                 env,
                                 cwd,
                                 startupinfo)

E FileNotFoundError: [WinError 2] The system cannot find the file specified

C:\Softwares\Python39\lib\subprocess.py:1420: FileNotFoundError```

@fizyk
Copy link
Member

fizyk commented Feb 11, 2022

Windows... Sorry, It doesn't run on windows. I'd need to first adjust the underlying mirakuru ( ClearcodeHQ/mirakuru#392 ) library and then figure out the way to run MySQL on windows.

@sylvain-ri
Copy link
Author

Okay... Thanks for the information. It would help other to states this somewhere ;)

@fizyk
Copy link
Member

fizyk commented Feb 11, 2022

@sylvain-ri in the meantime, you could try running on docker containers or within a VM with installed Linux. Also, I haven't tried WSL but I think it should handle it as well 🤔

@sylvain-ri
Copy link
Author

sylvain-ri commented Feb 11, 2022

of course i can :) but it's good to know before hand to not waste time 😁
I'm surprised your library is not more used.

Feel free to reopen the issue. I think adding the information about the requirements would be enough as of now (Linux needed, MySQL, maybe other dependencies)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants