diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 650b04b6..50d2bdc2 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -9,7 +9,7 @@ assignees: '' ### Read this first -We don't use this issue tracker to help users. If you had trouble, please ask it on some user community. +We don't use this issue tracker to help users. If you had trouble, please ask it on some user community. See [here](https://github.com/PyMySQL/mysqlclient-python#support). Please use this tracker only when you are sure about it is an issue of this software. And please provide full information from first. I don't want to ask questions like "What is your Python version?", "Do you confirm MySQL error log?". If the issue report looks incomplete, I will just close it. diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 00000000..d6aff95a --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,17 @@ +name: Lint + +on: [push, pull_request] + +jobs: + lint: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - uses: psf/black@stable + - name: Setup flake8 annotations + uses: rbialon/flake8-annotations@v1 + - name: flake8 + run: | + pip install flake8 + flake8 --ignore=E203,E501,W503 --max-line-length=88 . diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 00000000..5b69b416 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,36 @@ +name: Test + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-20.04 + strategy: + matrix: + python-version: [3.5, 3.6, 3.7, 3.8, 3.9] + services: + mysql: + image: mysql:8.0 + ports: + - 3306:3306 + env: + MYSQL_DATABASE: mysqldb_test + MYSQL_ROOT_PASSWORD: secretsecret + options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10 + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Run tests + env: + TESTDB: actions.cnf + run: | + pip install -U pip + pip install -U mock coverage pytest pytest-cov + pip install . + pytest --cov ./MySQLdb + - uses: codecov/codecov-action@v1 diff --git a/.github/workflows/windows.yaml b/.github/workflows/windows.yaml index 4b62655c..c65ce188 100644 --- a/.github/workflows/windows.yaml +++ b/.github/workflows/windows.yaml @@ -4,13 +4,13 @@ on: push: branches: - master - create: + workflow_dispatch: jobs: build: runs-on: windows-latest env: - CONNECTOR_VERSION: "3.1.9" + CONNECTOR_VERSION: "3.1.11" steps: - name: Cache Connector @@ -18,7 +18,7 @@ jobs: uses: actions/cache@v1 with: path: c:/mariadb-connector - key: mariadb-connector-${CONNECTOR_VERSION}-win + key: mariadb-connector-c-${{ env.CONNECTOR_VERSION }}-win - name: Download and Unzip Connector if: steps.cache-connector.outputs.cache-hit != 'true' @@ -40,15 +40,13 @@ jobs: cmake -DCMAKE_INSTALL_PREFIX=c:/mariadb-connector -DCMAKE_INSTALL_COMPONENT=Development -DCMAKE_BUILD_TYPE=Release -P cmake_install.cmake - name: Checkout mysqlclient - uses: actions/checkout@v1 + uses: actions/checkout@v2 with: - ref: master - fetch-depth: 10 path: mysqlclient - name: Site Config shell: bash - working-directory: ../mysqlclient + working-directory: mysqlclient run: | pwd find . @@ -61,8 +59,10 @@ jobs: - name: Build wheels shell: cmd - working-directory: ../mysqlclient + working-directory: mysqlclient run: | + py -3.9 -m pip install -U setuptools wheel pip + py -3.9 setup.py bdist_wheel py -3.8 -m pip install -U setuptools wheel pip py -3.8 setup.py bdist_wheel py -3.7 -m pip install -U setuptools wheel pip @@ -71,16 +71,18 @@ jobs: py -3.6 setup.py bdist_wheel - name: Upload Wheel - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v2 with: name: win-wheels - path: ../mysqlclient/dist + path: mysqlclient/dist/*.whl - name: Check wheels shell: bash - working-directory: ../mysqlclient/dist + working-directory: mysqlclient/dist run: | ls -la + py -3.9 -m pip install --no-index --find-links . mysqlclient + py -3.9 -c "import MySQLdb; print(MySQLdb.version_info)" py -3.8 -m pip install --no-index --find-links . mysqlclient py -3.8 -c "import MySQLdb; print(MySQLdb.version_info)" py -3.7 -m pip install --no-index --find-links . mysqlclient diff --git a/.travis.yml b/.travis.yml index ec1cd379..75c6d425 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,6 @@ language: python python: - "nightly" - "pypy3" - - "3.9-dev" - - "3.8" - - "3.7" - - "3.6" - - "3.5" cache: pip @@ -60,20 +55,6 @@ jobs: script: - cd django-${DJANGO_VERSION}/tests/ - ./runtests.py --parallel=2 --settings=test_mysql - - name: flake8 - python: "3.8" - install: - - pip install -U pip - - pip install flake8 - script: - - flake8 --ignore=E203,E501,W503 --max-line-length=88 . - - name: black - python: "3.8" - install: - - pip install -U pip - - pip install black - script: - - black --check --exclude=doc/ . #- &django_3_0 # <<: *django_2_2 # name: "Django 3.0 test (Python 3.8)" diff --git a/HISTORY.rst b/HISTORY.rst index 778b6431..6ad6e148 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,12 @@ +====================== + What's new in 2.0.2 +====================== + +Release: 2020-12-10 + +* Windows: Update MariaDB Connector/C to 3.1.11. +* Optimize fetching many rows with DictCursor. + ====================== What's new in 2.0.1 ====================== diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 62d0969d..27880ca2 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1194,7 +1194,8 @@ _mysql_field_to_python( static PyObject * _mysql_row_to_tuple( _mysql_ResultObject *self, - MYSQL_ROW row) + MYSQL_ROW row, + PyObject *unused) { unsigned int n, i; unsigned long *length; @@ -1221,7 +1222,8 @@ _mysql_row_to_tuple( static PyObject * _mysql_row_to_dict( _mysql_ResultObject *self, - MYSQL_ROW row) + MYSQL_ROW row, + PyObject *cache) { unsigned int n, i; unsigned long *length; @@ -1237,30 +1239,48 @@ _mysql_row_to_dict( c = PyTuple_GET_ITEM(self->converter, i); v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); if (!v) goto error; - if (!PyMapping_HasKeyString(r, fields[i].name)) { - PyMapping_SetItemString(r, fields[i].name, v); + + PyObject *pyname = PyUnicode_FromString(fields[i].name); + if (pyname == NULL) { + Py_DECREF(v); + goto error; + } + int err = PyDict_Contains(r, pyname); + if (err < 0) { // error + Py_DECREF(v); + goto error; + } + if (err) { // duplicate + Py_DECREF(pyname); + pyname = PyUnicode_FromFormat("%s.%s", fields[i].table, fields[i].name); + if (pyname == NULL) { + Py_DECREF(v); + goto error; + } + } + + err = PyDict_SetItem(r, pyname, v); + if (cache) { + PyTuple_SET_ITEM(cache, i, pyname); } else { - int len; - char buf[256]; - strncpy(buf, fields[i].table, 256); - len = strlen(buf); - strncat(buf, ".", 256-len); - len = strlen(buf); - strncat(buf, fields[i].name, 256-len); - PyMapping_SetItemString(r, buf, v); + Py_DECREF(pyname); } Py_DECREF(v); + if (err) { + goto error; + } } return r; - error: - Py_XDECREF(r); +error: + Py_DECREF(r); return NULL; } static PyObject * _mysql_row_to_dict_old( _mysql_ResultObject *self, - MYSQL_ROW row) + MYSQL_ROW row, + PyObject *cache) { unsigned int n, i; unsigned long *length; @@ -1275,20 +1295,26 @@ _mysql_row_to_dict_old( PyObject *v; c = PyTuple_GET_ITEM(self->converter, i); v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); - if (!v) goto error; - { - int len=0; - char buf[256]=""; - if (strlen(fields[i].table)) { - strncpy(buf, fields[i].table, 256); - len = strlen(buf); - strncat(buf, ".", 256-len); - len = strlen(buf); - } - strncat(buf, fields[i].name, 256-len); - PyMapping_SetItemString(r, buf, v); + if (!v) { + goto error; + } + + PyObject *pyname; + if (strlen(fields[i].table)) { + pyname = PyUnicode_FromFormat("%s.%s", fields[i].table, fields[i].name); + } else { + pyname = PyUnicode_FromString(fields[i].name); } + int err = PyDict_SetItem(r, pyname, v); Py_DECREF(v); + if (cache) { + PyTuple_SET_ITEM(cache, i, pyname); + } else { + Py_DECREF(pyname); + } + if (err) { + goto error; + } } return r; error: @@ -1296,15 +1322,66 @@ _mysql_row_to_dict_old( return NULL; } -typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); +static PyObject * +_mysql_row_to_dict_cached( + _mysql_ResultObject *self, + MYSQL_ROW row, + PyObject *cache) +{ + PyObject *r = PyDict_New(); + if (!r) { + return NULL; + } + + unsigned int n = mysql_num_fields(self->result); + unsigned long *length = mysql_fetch_lengths(self->result); + MYSQL_FIELD *fields = mysql_fetch_fields(self->result); + + for (unsigned int i=0; iconverter, i); + PyObject *v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); + if (!v) { + goto error; + } + + PyObject *pyname = PyTuple_GET_ITEM(cache, i); // borrowed + int err = PyDict_SetItem(r, pyname, v); + Py_DECREF(v); + if (err) { + goto error; + } + } + return r; + error: + Py_XDECREF(r); + return NULL; +} + + +typedef PyObject *_convertfunc(_mysql_ResultObject *, MYSQL_ROW, PyObject *); +static _convertfunc * const row_converters[] = { + _mysql_row_to_tuple, + _mysql_row_to_dict, + _mysql_row_to_dict_old +}; Py_ssize_t _mysql__fetch_row( _mysql_ResultObject *self, PyObject *r, /* list object */ Py_ssize_t maxrows, - _PYFUNC *convert_row) + int how) { + _convertfunc *convert_row = row_converters[how]; + + PyObject *cache = NULL; + if (maxrows > 0 && how > 0) { + cache = PyTuple_New(mysql_num_fields(self->result)); + if (!cache) { + return -1; + } + } + Py_ssize_t i; for (i = 0; i < maxrows; i++) { MYSQL_ROW row; @@ -1317,20 +1394,29 @@ _mysql__fetch_row( } if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) { _mysql_Exception((_mysql_ConnectionObject *)self->conn); - return -1; + goto error; } if (!row) { break; } - PyObject *v = convert_row(self, row); - if (!v) return -1; + PyObject *v = convert_row(self, row, cache); + if (!v) { + goto error; + } + if (cache) { + convert_row = _mysql_row_to_dict_cached; + } if (PyList_Append(r, v)) { Py_DECREF(v); - return -1; + goto error; } Py_DECREF(v); } + Py_XDECREF(cache); return i; +error: + Py_XDECREF(cache); + return -1; } static char _mysql_ResultObject_fetch_row__doc__[] = @@ -1348,15 +1434,7 @@ _mysql_ResultObject_fetch_row( PyObject *args, PyObject *kwargs) { - typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); - static char *kwlist[] = { "maxrows", "how", NULL }; - static _PYFUNC *row_converters[] = - { - _mysql_row_to_tuple, - _mysql_row_to_dict, - _mysql_row_to_dict_old - }; - _PYFUNC *convert_row; + static char *kwlist[] = {"maxrows", "how", NULL }; int maxrows=1, how=0; PyObject *r=NULL; @@ -1368,7 +1446,6 @@ _mysql_ResultObject_fetch_row( PyErr_SetString(PyExc_ValueError, "how out of range"); return NULL; } - convert_row = row_converters[how]; if (!maxrows) { if (self->use) { maxrows = INT_MAX; @@ -1378,7 +1455,7 @@ _mysql_ResultObject_fetch_row( } } if (!(r = PyList_New(0))) goto error; - Py_ssize_t rowsadded = _mysql__fetch_row(self, r, maxrows, convert_row); + Py_ssize_t rowsadded = _mysql__fetch_row(self, r, maxrows, how); if (rowsadded == -1) goto error; /* DB-API allows return rows as list. diff --git a/MySQLdb/codecov.yml b/MySQLdb/codecov.yml new file mode 100644 index 00000000..174a4994 --- /dev/null +++ b/MySQLdb/codecov.yml @@ -0,0 +1,2 @@ +ignore: + - "MySQLdb/constants/*" diff --git a/MySQLdb/times.py b/MySQLdb/times.py index f0e9384c..915d827b 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -131,7 +131,11 @@ def Time_or_None(s): def Date_or_None(s): try: - return date(int(s[:4]), int(s[5:7]), int(s[8:10]),) # year # month # day + return date( + int(s[:4]), + int(s[5:7]), + int(s[8:10]), + ) # year # month # day except ValueError: return None diff --git a/doc/conf.py b/doc/conf.py index 33f9781c..5d8cd1a0 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -18,12 +18,12 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -#sys.path.insert(0, os.path.abspath("..")) +# sys.path.insert(0, os.path.abspath("..")) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = "1.0" +# needs_sphinx = "1.0" # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. @@ -36,7 +36,7 @@ source_suffix = ".rst" # The encoding of source files. -#source_encoding = "utf-8-sig" +# source_encoding = "utf-8-sig" # The master toctree document. master_doc = "index" @@ -56,37 +56,37 @@ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. -#language = None +# language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = "" +# today = "" # Else, today_fmt is used as the format for a strftime call. -#today_fmt = "%B %d, %Y" +# today_fmt = "%B %d, %Y" # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ["_build"] # The reST default role (used for this markup: `text`) to use for all documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). -#add_module_names = True +# add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] +# modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- @@ -98,26 +98,26 @@ # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. -#html_theme_options = {} +# html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] +# html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -#html_title = None +# html_title = None # A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None +# html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. -#html_logo = None +# html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. -#html_favicon = None +# html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, @@ -126,44 +126,44 @@ # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = "%b %d, %Y" +# html_last_updated_fmt = "%b %d, %Y" # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. -#html_sidebars = {} +# html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} +# html_additional_pages = {} # If false, no module index is generated. -#html_domain_indices = True +# html_domain_indices = True # If false, no index is generated. -#html_use_index = True +# html_use_index = True # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True +# html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True +# html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True +# html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = "" +# html_use_opensearch = "" # This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None +# html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = "MySQLdbdoc" @@ -188,23 +188,23 @@ # The name of an image file (relative to this directory) to place at the top of # the title page. -#latex_logo = None +# latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # If true, show page references after internal links. -#latex_show_pagerefs = False +# latex_show_pagerefs = False # If true, show URL addresses after external links. -#latex_show_urls = False +# latex_show_urls = False # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. -#latex_domain_indices = True +# latex_domain_indices = True # -- Options for manual page output -------------------------------------------- @@ -214,7 +214,7 @@ man_pages = [("index", "mysqldb", "MySQLdb Documentation", ["Andy Dustman"], 1)] # If true, show URL addresses after external links. -#man_show_urls = False +# man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ @@ -235,10 +235,10 @@ ] # Documents to append as an appendix to all manuals. -#texinfo_appendices = [] +# texinfo_appendices = [] # If false, no module index is generated. -#texinfo_domain_indices = True +# texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' +# texinfo_show_urls = 'footnote' diff --git a/doc/user_guide.rst b/doc/user_guide.rst index e52d0f79..83b800e8 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -674,10 +674,9 @@ CursorDictRowsMixIn Cursor The default cursor class. This class is composed of - ``CursorWarningMixIn``, ``CursorStoreResultMixIn``, - ``CursorTupleRowsMixIn,`` and ``BaseCursor``, i.e. it raises - ``Warning``, uses ``mysql_store_result()``, and returns rows as - tuples. + ``CursorStoreResultMixIn``, ``CursorTupleRowsMixIn``, and + ``BaseCursor``, i.e. uses ``mysql_store_result()`` and returns + rows as tuples. DictCursor Like ``Cursor`` except it returns rows as dictionaries. diff --git a/metadata.cfg b/metadata.cfg index 527a2c7b..b0ee0db4 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,12 +1,12 @@ [metadata] -version: 2.0.1 -version_info: (2,0,1,'final',0) +version: 2.0.2 +version_info: (2,0,2,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com license: GPL platforms: ALL -url: https://github.com/PyMySQL/mysqlclient-python +url: https://github.com/PyMySQL/mysqlclient classifiers: Development Status :: 5 - Production/Stable Environment :: Other Environment @@ -20,10 +20,10 @@ classifiers: Programming Language :: C Programming Language :: Python Programming Language :: Python :: 3 - Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: diff --git a/setup_windows.py b/setup_windows.py index c374ad60..c25cc52b 100644 --- a/setup_windows.py +++ b/setup_windows.py @@ -38,7 +38,6 @@ def get_config(): libraries = ["kernel32", "advapi32", "wsock32", client] include_dirs = [os.path.join(connector, r"include")] - extra_compile_args = ["/Zl", "/D_CRT_SECURE_NO_WARNINGS"] extra_link_args = ["/MANIFEST"] name = "mysqlclient" @@ -53,7 +52,6 @@ def get_config(): ext_options = dict( library_dirs=library_dirs, libraries=libraries, - extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, include_dirs=include_dirs, extra_objects=extra_objects, diff --git a/tests/actions.cnf b/tests/actions.cnf new file mode 100644 index 00000000..8918f031 --- /dev/null +++ b/tests/actions.cnf @@ -0,0 +1,11 @@ +# To create your own custom version of this file, read +# http://dev.mysql.com/doc/refman/5.1/en/option-files.html +# and set TESTDB in your environment to the name of the file + +[MySQLdb-tests] +host = 127.0.0.1 +port = 3306 +user = root +database = mysqldb_test +password = secretsecret +default-character-set = utf8mb4 diff --git a/tests/capabilities.py b/tests/capabilities.py index cafe1e61..da753d15 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -68,12 +68,12 @@ def new_table_name(self): def create_table(self, columndefs): - """ Create a table using a list of column definitions given in - columndefs. + """Create a table using a list of column definitions given in + columndefs. - generator must be a function taking arguments (row_number, - col_number) returning a suitable data object for insertion - into the table. + generator must be a function taking arguments (row_number, + col_number) returning a suitable data object for insertion + into the table. """ self.table = self.new_table_name() diff --git a/tests/configdb.py b/tests/configdb.py index f3a56e24..c2949039 100644 --- a/tests/configdb.py +++ b/tests/configdb.py @@ -5,7 +5,10 @@ tests_path = path.dirname(__file__) conf_file = environ.get("TESTDB", "default.cnf") conf_path = path.join(tests_path, conf_file) -connect_kwargs = dict(read_default_file=conf_path, read_default_group="MySQLdb-tests",) +connect_kwargs = dict( + read_default_file=conf_path, + read_default_group="MySQLdb-tests", +) def connection_kwargs(kwargs): diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 0ca8bce6..4824d9cc 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -66,25 +66,25 @@ class DatabaseAPI20Test(unittest.TestCase): - """ Test a database self.driver for DB API 2.0 compatibility. - This implementation tests Gadfly, but the TestCase - is structured so that other self.drivers can subclass this - test case to ensure compiliance with the DB-API. It is - expected that this TestCase may be expanded in the future - if ambiguities or edge conditions are discovered. + """Test a database self.driver for DB API 2.0 compatibility. + This implementation tests Gadfly, but the TestCase + is structured so that other self.drivers can subclass this + test case to ensure compiliance with the DB-API. It is + expected that this TestCase may be expanded in the future + if ambiguities or edge conditions are discovered. - The 'Optional Extensions' are not yet being tested. + The 'Optional Extensions' are not yet being tested. - self.drivers should subclass this test, overriding setUp, tearDown, - self.driver, connect_args and connect_kw_args. Class specification - should be as follows: + self.drivers should subclass this test, overriding setUp, tearDown, + self.driver, connect_args and connect_kw_args. Class specification + should be as follows: - import dbapi20 - class mytest(dbapi20.DatabaseAPI20Test): - [...] + import dbapi20 + class mytest(dbapi20.DatabaseAPI20Test): + [...] - Don't 'import DatabaseAPI20Test from dbapi20', or you will - confuse the unit tester - just 'import dbapi20'. + Don't 'import DatabaseAPI20Test from dbapi20', or you will + confuse the unit tester - just 'import dbapi20'. """ # The self.driver module. This should be the module where the 'connect' @@ -110,15 +110,15 @@ def executeDDL2(self, cursor): cursor.execute(self.ddl2) def setUp(self): - """ self.drivers should override this method to perform required setup - if any is necessary, such as creating the database. + """self.drivers should override this method to perform required setup + if any is necessary, such as creating the database. """ pass def tearDown(self): - """ self.drivers should override this method to perform required cleanup - if any is necessary, such as deleting the test database. - The default drops the tables that may be created. + """self.drivers should override this method to perform required cleanup + if any is necessary, such as deleting the test database. + The default drops the tables that may be created. """ con = self._connect() try: @@ -521,8 +521,8 @@ def test_fetchone(self): ] def _populate(self): - """ Return a list of sql commands to setup the DB for the fetch - tests. + """Return a list of sql commands to setup the DB for the fetch + tests. """ populate = [ "insert into {}booze values ('{}')".format(self.table_prefix, s) @@ -710,9 +710,9 @@ def test_mixedfetch(self): con.close() def help_nextset_setUp(self, cur): - """ Should create a procedure called deleteme - that returns two result sets, first the - number of rows in booze then "name from booze" + """Should create a procedure called deleteme + that returns two result sets, first the + number of rows in booze then "name from booze" """ raise NotImplementedError("Helper not implemented") # sql=""" diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index fe9ef03e..0b4dd21a 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -120,12 +120,12 @@ def test_MULTIPOLYGON(self): INSERT INTO test_MULTIPOLYGON (id, border) VALUES (1, - Geomfromtext( + ST_Geomfromtext( 'MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))')) """ ) - c.execute("SELECT id, AsText(border) FROM test_MULTIPOLYGON") + c.execute("SELECT id, ST_AsText(border) FROM test_MULTIPOLYGON") row = c.fetchone() self.assertEqual(row[0], 1) self.assertEqual( @@ -133,7 +133,7 @@ def test_MULTIPOLYGON(self): "MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))", ) - c.execute("SELECT id, AsWKB(border) FROM test_MULTIPOLYGON") + c.execute("SELECT id, ST_AsWKB(border) FROM test_MULTIPOLYGON") row = c.fetchone() self.assertEqual(row[0], 1) self.assertNotEqual(len(row[1]), 0) diff --git a/tests/test_MySQLdb_dbapi20.py b/tests/test_MySQLdb_dbapi20.py index 6b3a3787..a0dd92a1 100644 --- a/tests/test_MySQLdb_dbapi20.py +++ b/tests/test_MySQLdb_dbapi20.py @@ -161,9 +161,10 @@ def test_callproc(self): pass # performed in test_MySQL_capabilities def help_nextset_setUp(self, cur): - """ Should create a procedure called deleteme - that returns two result sets, first the - number of rows in booze then "name from booze" + """ + Should create a procedure called deleteme + that returns two result sets, first the + number of rows in booze then "name from booze" """ sql = """ create procedure deleteme() diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 479f3e27..91f0323e 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -111,3 +111,42 @@ def test_pyparam(): assert cursor._executed == b"SELECT 1, 2" cursor.execute(b"SELECT %(a)s, %(b)s", {b"a": 3, b"b": 4}) assert cursor._executed == b"SELECT 3, 4" + + +def test_dictcursor(): + conn = connect() + cursor = conn.cursor(MySQLdb.cursors.DictCursor) + + cursor.execute("CREATE TABLE t1 (a int, b int, c int)") + _tables.append("t1") + cursor.execute("INSERT INTO t1 (a,b,c) VALUES (1,1,47), (2,2,47)") + + cursor.execute("CREATE TABLE t2 (b int, c int)") + _tables.append("t2") + cursor.execute("INSERT INTO t2 (b,c) VALUES (1,1), (2,2)") + + cursor.execute("SELECT * FROM t1 JOIN t2 ON t1.b=t2.b") + rows = cursor.fetchall() + + assert len(rows) == 2 + assert rows[0] == {"a": 1, "b": 1, "c": 47, "t2.b": 1, "t2.c": 1} + assert rows[1] == {"a": 2, "b": 2, "c": 47, "t2.b": 2, "t2.c": 2} + + names1 = sorted(rows[0]) + names2 = sorted(rows[1]) + for a, b in zip(names1, names2): + assert a is b + + # Old fetchtype + cursor._fetch_type = 2 + cursor.execute("SELECT * FROM t1 JOIN t2 ON t1.b=t2.b") + rows = cursor.fetchall() + + assert len(rows) == 2 + assert rows[0] == {"t1.a": 1, "t1.b": 1, "t1.c": 47, "t2.b": 1, "t2.c": 1} + assert rows[1] == {"t1.a": 2, "t1.b": 2, "t1.c": 47, "t2.b": 2, "t2.c": 2} + + names1 = sorted(rows[0]) + names2 = sorted(rows[1]) + for a, b in zip(names1, names2): + assert a is b diff --git a/tests/travis.cnf b/tests/travis.cnf index 05ff8039..5fd6f847 100644 --- a/tests/travis.cnf +++ b/tests/travis.cnf @@ -7,5 +7,4 @@ host = 127.0.0.1 port = 3306 user = root database = mysqldb_test -#password = travis default-character-set = utf8mb4