Skip to content

Commit 581662f

Browse files
authored
Add some testing infrastructure (#13)
This includes only basic infra
1 parent 232c395 commit 581662f

File tree

11 files changed

+165
-0
lines changed

11 files changed

+165
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
build/
2+
__pycache__
3+
*.py[cod]
4+
*~
5+
@*
6+
/build
7+
/env
8+
docs/build/
9+
*.iml
10+
/out/
11+
.venv/
12+
.mypy_cache/
13+
.incremental_checker_cache.json
14+
.cache
15+
dmypy.json
16+
.dmypy.json
17+
18+
# Packages
19+
*.egg
20+
*.egg-info
21+
*.eggs
22+
23+
# IDEs
24+
.idea
25+
*.swp
26+
.vscode
27+
28+
# Operating Systems
29+
.DS_Store
30+
31+
# Coverage Files
32+
htmlcov
33+
.coverage*
34+
35+
# pytest cache
36+
.pytest_cache/
37+
38+
.tox
39+

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "external/mypy"]
2+
path = external/mypy
3+
url = https://github.com/python/mypy.git

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: python
2+
# cache package wheels (1 cache per python version)
3+
cache: pip
4+
# newer python versions are available only on xenial (while some older only on trusty) Ubuntu distribution
5+
dist: xenial
6+
sudo: required
7+
8+
jobs:
9+
include:
10+
- name: "run test suite with python 3.6"
11+
python: 3.6
12+
13+
before_install: |
14+
# Upgrade pip, setuptools, and wheel
15+
pip install -U pip setuptools wheel
16+
17+
install:
18+
- pip install -r external/mypy/test-requirements.txt
19+
20+
script: |
21+
set -e
22+
export PYTHONPATH=`pwd`/external/mypy
23+
pytest
24+

conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest_plugins = [
2+
'mypy.test.data',
3+
]
4+

external/mypy

Submodule mypy added at d77e1c2

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
testpaths = test
3+
python_files = test*.py
4+
addopts = -nauto
5+

sqlalchemy-plugin/sqlmypy.py

Whitespace-only changes.

sqlalchemy-typing/sqltyping.py

Whitespace-only changes.

test/__init__.py

Whitespace-only changes.

test/test-data/sqlalchemy-basics.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[case testColumnFieldsInferred]
2+
from sqlalchemy.ext.declarative import declarative_base
3+
from sqlalchemy import Column, Integer, String
4+
5+
Base = declarative_base()
6+
7+
class User(Base):
8+
__tablename__ = 'users'
9+
id = Column(Integer, primary_key=True)
10+
name = Column(String)
11+
12+
user = None # type: User
13+
reveal_type(user.id)
14+
reveal_type(User.name)
15+
[out]
16+
17+
[case testColumnFieldsInferredInstance]
18+
from sqlalchemy.ext.declarative import declarative_base
19+
from sqlalchemy import Column, Integer, String
20+
21+
Base = declarative_base()
22+
23+
class User(Base):
24+
__tablename__ = 'users'
25+
id = Column(Integer(), primary_key=True)
26+
name = Column(String())
27+
28+
user = None # type: User
29+
reveal_type(user.id)
30+
reveal_type(User.name)
31+
[out]

test/testsql.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Mypy style test cases for SQLAlchemy stubs and plugin."""
2+
3+
import os
4+
import os.path
5+
import sys
6+
7+
import pytest # type: ignore # no pytest in typeshed
8+
9+
from mypy.test.config import test_temp_dir
10+
from mypy.test.data import DataDrivenTestCase, DataSuite
11+
from mypy.test.helpers import assert_string_arrays_equal
12+
from mypy import api
13+
14+
this_file_dir = os.path.dirname(os.path.realpath(__file__))
15+
prefix = os.path.dirname(this_file_dir)
16+
17+
# Locations of test data files such as test case descriptions (.test).
18+
test_data_prefix = os.path.join(prefix, 'test', 'test-data')
19+
20+
21+
class SQLDataSuite(DataSuite):
22+
files = ['sqlalchemy-basics.test']
23+
data_prefix = test_data_prefix
24+
25+
def run_case(self, testcase: DataDrivenTestCase) -> None:
26+
27+
assert testcase.old_cwd is not None, "test was not properly set up"
28+
mypy_cmdline = [
29+
'--show-traceback',
30+
'--no-silence-site-packages',
31+
]
32+
py2 = testcase.name.lower().endswith('python2')
33+
if py2:
34+
mypy_cmdline.append('--py2')
35+
else:
36+
mypy_cmdline.append('--python-version={}'.format('.'.join(map(str,
37+
sys.version_info[:2]))))
38+
39+
# Write the program to a file.
40+
program_path = os.path.join(test_temp_dir, 'main.py')
41+
mypy_cmdline.append(program_path)
42+
with open(program_path, 'w') as file:
43+
for s in testcase.input:
44+
file.write('{}\n'.format(s))
45+
output = []
46+
# Type check the program.
47+
out, err, returncode = api.run(mypy_cmdline)
48+
# split lines, remove newlines, and remove directory of test case
49+
for line in (out + err).splitlines():
50+
if line.startswith(test_temp_dir + os.sep):
51+
output.append(line[len(test_temp_dir + os.sep):].rstrip("\r\n"))
52+
else:
53+
output.append(line.rstrip("\r\n"))
54+
# Remove temp file.
55+
os.remove(program_path)
56+
assert_string_arrays_equal(testcase.output, output,
57+
'Invalid output ({}, line {})'.format(
58+
testcase.file, testcase.line))

0 commit comments

Comments
 (0)