rdbunit

Unit testing for SQL queries

https://github.com/dspinellis/rdbunit

Science Score: 57.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 5 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.3%) to scientific vocabulary
Last synced: 7 months ago · JSON representation ·

Repository

Unit testing for SQL queries

Basic Info
  • Host: GitHub
  • Owner: dspinellis
  • License: apache-2.0
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 116 KB
Statistics
  • Stars: 24
  • Watchers: 2
  • Forks: 4
  • Open Issues: 5
  • Releases: 0
Created about 9 years ago · Last pushed over 1 year ago
Metadata Files
Readme License Citation

README.md

RDBUnit: Unit testing for SQL queries

rdbunit CI

RDBUnit is a unit testing framework for relational database SQL queries. It allows you to express in a simple way the setup prior to a query, the query, and the expected results. RDBUnit can test SELECT queries as well as queries that are used for creating tables and views. All types of queries can be either embedded into the test script, or they can be included from an external file. The tables for the input and the expected results can be created with minimal ceremony: RDBUnit will automatically infer the types of the tables' fields.

For complex relational OLAP queries RDBUnit can be combined particularly effectively with the simple-rolap relational online analytical processing framework. You can find a complete tutorial on using RDBUnit with simple-rolap for mining Git repositories in a technical briefing presented at the 2017 International Conference on Software Engineering.

You can cite this work as follows.

  • Diomidis Spinellis. Unit Tests for SQL. IEEE Software, vol. 41, no. 1, pp. 31–34, Jan.-Feb. 2024. doi: 10.1109/MS.2023.3328788.

Installation

Using Pip

sh pip install rdbunit

From source

  • Clone this repository git clone --depth=1 https://github.com/dspinellis/rdbunit.git cd rdbunit pipenv shell pip install .

Test specification

For every SQL query you want to test, create an RDBUnit file that specifies the query's input, execution, and expected result. The setup-query-results sequence can be specified multiple times within a test file.

Simple example

The following example illustrates this concept.

Step 1: Create the unit test specification

Create a file named max_revenue.rdbu with the following contents.

``` BEGIN SETUP sales: month revenue March 130 April 50 END

BEGIN SELECT SELECT MAX(revenue) as max_revenue FROM sales; END

BEGIN RESULT max_revenue 130 END ```

Step 2: Run the unit test

Run the unit test to see its result as follows.

$ rdbunit --database=sqlite max_revenue.rdbu | sqlite3 ok 1 - max_revenue.rdbu: test_select_result 1..1

Table data details

The input and output are specified as table contents. The input starts with a line containing the words BEGIN SETUP, while the results start with a line containing the words BEGIN RESULTS. The input and output are specified by first giving a table's name, followed by a colon. The name may be prefixed by the name of a database where the table is to reside, followed by a dot. The next row contains the names of table's fields, separated by spaces. Then come the table's data, which are terminated by a blank line, or by the word END. The table data automatically derive the fields' data types from those of the first row. (For this reason avoid specifying NULL values in the first row.)

More than one table can be specified in the setup. In the results the table name is not specified, if the tested query is a selection statement, rather than a table or view creation.

Setup example

BEGIN SETUP contacts: name registered value reg_date John true 12 '2015-03-02' Mary false 10 '2012-03-02' END

Results example (named table)

Named table results are used with CREATE queries. BEGIN RESULT leadership.nl_commits_leader_comments: project_id n 1 3 END

Results example (unnamed set)

Unnamed set results are used with SELECT queries. BEGIN RESULT name registered value reg_date a John True 12 '2015-03-02' Null END

Tested query

The query to test is either specified inline with a BEGIN SELECT (for selection queries) or with BEGIN CREATE (for creation and insert or update queries) statement, or by specifying a file through the corresponding INCLUDE SELECT or INCLUDE CREATE statement. An INCLUDE SELECT or INCLUDE CREATE statement can be combined with a corresponding BEGIN SELECT or BEGIN CREATE statement to customize the query or database for the testing environment. For example, this may be needed to add a unique table index to simulate a corresponding primary key or to delete an input table row to obtain an empty table.

Inline example

BEGIN SELECT SELECT *, NULL AS a FROM contacts WHERE registered; END

External query example

INCLUDE CREATE nl_commits_leader_comments.sql

Unit testing

To run the tests run RDBUnit piping its output to one of the supported relational database systems (current MySQL, PostgreSQL, and sqLite). A number of command-line flags allow you to tailor the operation of RDBUnit. When running, RDBUnit will report on its output something like ok 1 - recent_commit_projects.rdbu: test_stratsel.recent_commit_projects for each succeeding test case and not ok 1 - project_stars.rdbu: test_stratsel.project_stars for each failing test case. A final line will list the number of succeeding and failing test cases. By default RDBUnit creates and operates on temporary databases, whose name is prefixed with the word test_.

By specifying the --results option (or the equivalent -r short option) you can direct rdbunit to display the table generated for each test. This is useful for debugging test failures or for generating reference data (after suitable manual verification).

Execution example (SQLite)

sh $ rdbunit --database=sqlite recent_commit_projects.rdbu | sqlite3 ok 1 - recent_commit_projects.rdbu: test_stratsel.recent_commit_projects

Execution example (MySQL)

sh $ rdbunit commits_comments.rdbu | mysql -u root -p -N Enter password: ok 1 - commits_comments.rdbu: test_leadership.nl_commits_leader_comments ok 2 - commits_comments.rdbu: test_leadership.leader_commits_nl_comments ok 3 - commits_comments.rdbu: test_leadership.commits_with_comments 1..3

Execution example (PostgreSQL)

```sh $ rdbunit --database=postgresql commitscomments.rdbu | psql -U ght -h 127.0.0.1 -t -q ghtorrent ok 1 - commitscomments.rdbu: testleadership.nlcommitsleadercomments

ok 2 - commitscomments.rdbu: testleadership.leadercommitsnl_comments

ok 3 - commitscomments.rdbu: testleadership.commitswithcomments

1..3 ```

Troubleshooting

When unit tests fail you can troubleshoot them as follows.

Test fails due to a syntax error

Example: $ rdbunit --database=sqlite mytest.rdbu | sqlite3 Error: near line 26: near ".": syntax error not ok 1 - mytest.rdbu: mytest 1..1 Pipe the output of rdbunit to cat -n to see the offending line. $ rdbunit --database=sqlite mytest.rdbu | cat -n

Test fails due to incorrect results

Example: $ rdbunit --database=sqlite fileid_to_global_map.rdbu | sqlite3 not ok 1 - fileid_to_global_map.rdbu: fileid_to_global_map 1..1 Re-run the specific test with the --results option to see the generated output or with the --compare option to see the difference in the obtained result sets. ``` $ rdbunit --database=sqlite --results fileidtoglobalmap.rdbu | sqlite3 not ok 1 - fileidtoglobalmap.rdbu: fileidtoglobal_map Result set: 2|3|1 2|5|2 5|1|3 5|2|1 5|3|4 1..1

$ rdbunit --database=sqlite fileidtoglobalmap.rdbu --compare | sqlite3 not ok 1 - fileidtoglobalmap.rdbu: fileidtoglobal_map Non expected records in result set: 5|3|4 Missing records in result set: 4|3|4 1..1

```

Development

Contributions via GitHub pull requests are welcomed. Each contribution passes through continuous integration, which verifies the code's style (pycodestyle) and checks for errors (pylint). It also tests the input and output of RDBunit and its operation on the three supported relational database systems. On a local host, after creating a virtual environment (pipenv), entering it (pipenv shell), and installing the required development dependencies (pipenv install --dev), you can run the following commands.

  • pycodestyle src/rdbunit/__main__.py
  • pylint src/rdbunit/__main__.py
  • tests/test-parts.sh
  • tests/test-sqlite.sh

Owner

  • Name: Diomidis Spinellis
  • Login: dspinellis
  • Kind: user
  • Location: Athens, Greece
  • Company: Athens University of Economics and Business & Delft University of Technology

Professor of Software Engineering at AUEB and of Software Analytics at TU Delft, programmer, and technology author.

Citation (CITATION.cff)

cff-version: 1.2.0
title: RDBUnit
message: >-
  If you use this software, please cite, using the metadata from this file,
  both the article from preferred-citation and the software itself.
preferred-citation:
  date-released: "2024-01-01"
  doi: "10.1109/MS.2023.3328788"
  title: "Unit Tests for SQL"
  authors:
    - family-names: "Spinellis"
      given-names: "Diomidis"
  type: "article"
  volume: "41"
  journal: "IEEE Software"
  publisher: "IEEE"
  start: "22"
  end: "26"
type: software
authors:
  - given-names: Diomidis
    family-names: Spinellis
    email: dds@aueb.gr
    affiliation: Athens University of Economics and Business
    orcid: 'https://orcid.org/0000-0003-4231-1897'
repository-code: 'https://github.com/dspinellis/rdbunit/'
keywords:
  - Unit testing
  - SQL
  - Relational query
  - SQLite
  - PostgreSQL
  - MariaDB
  - mySQL
license: Apache-2.0

GitHub Events

Total
  • Watch event: 3
  • Fork event: 1
Last Year
  • Watch event: 3
  • Fork event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 99
  • Total Committers: 2
  • Avg Commits per committer: 49.5
  • Development Distribution Score (DDS): 0.01
Past Year
  • Commits: 8
  • Committers: 2
  • Avg Commits per committer: 4.0
  • Development Distribution Score (DDS): 0.125
Top Committers
Name Email Commits
Diomidis Spinellis d****s@a****r 98
e-panourgia t****0@a****r 1
Committer Domains (Top 20 + Academic)
aueb.gr: 2

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 9
  • Total pull requests: 3
  • Average time to close issues: 4 days
  • Average time to close pull requests: 1 day
  • Total issue authors: 1
  • Total pull request authors: 1
  • Average comments per issue: 0.44
  • Average comments per pull request: 0.33
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 3
  • Average time to close issues: N/A
  • Average time to close pull requests: 1 day
  • Issue authors: 1
  • Pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.33
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • dspinellis (9)
Pull Request Authors
  • e-panourgia (6)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 30 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
pypi.org: rdbunit

RDBUnit is a unit testing framework for relational database queries.

  • Homepage: https://github.com/dspinellis/rdbunit
  • Documentation: https://github.com/dspinellis/rdbunit
  • License: Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Latest release: 1.1.4
    published about 2 years ago
  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 30 Last month
Rankings
Dependent packages count: 7.3%
Stargazers count: 14.9%
Forks count: 19.2%
Average: 27.5%
Dependent repos count: 68.5%
Maintainers (1)
Last synced: 8 months ago