Пример #1
0
 def test_query_string_must_be_provided(self):
     task = MySQLFetch(db_name="test",
                       user="******",
                       password="******",
                       host="test")
     with pytest.raises(ValueError,
                        match="A query string must be provided"):
         task.run()
Пример #2
0
    def test_run_args_used_over_init_args(self, monkeypatch):
        mock_connect = MagicMock()

        monkeypatch.setattr("pymysql.connect", mock_connect)
        task = MySQLFetch(db_name="test",
                          user="******",
                          password="******",
                          host="test")
        task.run(query="select * from users", password="******")

        assert mock_connect.call_args[1]["password"] == "password_from_secret"
Пример #3
0
 def test_bad_cursor_type_param_type_raises(self):
     task = MySQLFetch(db_name="test",
                       user="******",
                       password="******",
                       host="test")
     with pytest.raises(
             TypeError,
             match=
             f"'cursor_type' should be one of \['cursor', 'dictcursor', 'sscursor', 'ssdictcursor'\] or a full cursor class, got \['cursor'\]",
     ):
         task.run(query="SELECT * FROM some_table", cursor_type=["cursor"])
Пример #4
0
 def test_bad_fetch_param_raises(self):
     task = MySQLFetch(db_name="test",
                       user="******",
                       password="******",
                       host="test")
     with pytest.raises(
             ValueError,
             match=
             "The 'fetch' parameter must be one of the following - \('one', 'many', 'all'\)",
     ):
         task.run(query="SELECT * FROM some_table",
                  fetch="not a valid parameter")
Пример #5
0
    def test_unsupported_cursor_type_str_param_raises(self):
        cursor_type = "unsupportedcursor"

        task = MySQLFetch(db_name="test",
                          user="******",
                          password="******",
                          host="test")
        with pytest.raises(
                TypeError,
                match=
                rf"'cursor_type' should be one of \['cursor', 'dictcursor', 'sscursor', 'ssdictcursor'\] or a full cursor class, got {cursor_type}",
        ):
            task.run(query="SELECT * FROM some_table", cursor_type=cursor_type)
Пример #6
0
 def test_construction_with_cursor_type_class(self):
     task = MySQLFetch(
         db_name="test",
         user="******",
         password="******",
         host="test",
         cursor_type=pymysql.cursors.DictCursor,
     )
     assert task.cursor_type == pymysql.cursors.DictCursor
Пример #7
0
 def test_construction_with_cursor_type_str(self):
     task = MySQLFetch(
         db_name="test",
         user="******",
         password="******",
         host="test",
         cursor_type="dictcursor",
     )
     assert task.cursor_type == "dictcursor"
Пример #8
0
 def test_construction(self):
     task = MySQLFetch(db_name="test",
                       user="******",
                       password="******",
                       host="test")
     assert task.fetch == "one"
Пример #9
0
from prefect.tasks.mysql.mysql import MySQLFetch, MySQLExecute
from prefect import Flow, task

EXAMPLE_TABLE = "user"
HOST = "localhost"
PORT = 3306
DB_NAME = "ext"
USER = "******"
PASSWORD = "******"

mysql_fetch = MySQLFetch(host=HOST,
                         port=PORT,
                         db_name=DB_NAME,
                         user=USER,
                         password=PASSWORD)

mysql_exec = MySQLExecute(host=HOST,
                          port=PORT,
                          db_name=DB_NAME,
                          user=USER,
                          password=PASSWORD)


@task
def print_results(x):
    print(x)


with Flow("MySQL Example") as flow:
    # fetch 3 results
    fetch_results = mysql_fetch(query=f"SELECT * FROM {EXAMPLE_TABLE}",