Skip to content

Commit

Permalink
Initial SonarQube
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Sep 17, 2018
1 parent 927647d commit 4b38d12
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.scannerwork/

# Translations
*.mo
Expand Down
5 changes: 1 addition & 4 deletions .green
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
verbose = 2
omit-patterns = tortoise/contrib/pylint/*,tortoise/tests/*
clear-omit = true
run-coverage = true
quiet-coverage= true
no-skip-report= true
initializer = tortoise.contrib.test.initializer
finalizer = tortoise.contrib.test.finalizer
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ env:
- TORTOISE_TEST_DB="sqlite:///tmp/test-{}.sqlite"
- TORTOISE_TEST_DB="postgres://postgres:@127.0.0.1:5432/test_{}"
- TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_{}"
matrix:
include:
- language: java
dist: trusty
addons:
sonarCloud:
organization: "tortoise"
token:
secure: ""
script:
- sonar-scanner
dist: xenial
sudo: required
addons:
Expand Down
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ help:

up:
pip-compile -o requirements-dev.txt requirements-dev.in -U
cat requirements-dev.txt | fgrep -v extra-index-url > requirements-dev.txt.tmp
mv requirements-dev.txt.tmp requirements-dev.txt

deps:
@pip install -q pip-tools
Expand All @@ -36,8 +38,17 @@ lint: deps
-python setup.py check -mrs

test: deps
green
coverage run -a -m tortoise.tests.inittest
coverage erase
coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report

testall: deps
coverage erase
TORTOISE_TEST_DB=sqlite:///tmp/test-\{\}.sqlite coverage run -p --concurrency=multiprocessing `which green`
TORTOISE_TEST_DB=postgres://postgres:@127.0.0.1:5432/test_\{\} coverage run -p --concurrency=multiprocessing `which green`
TORTOISE_TEST_DB="mysql://root:@127.0.0.1:3306/test_\{\}" coverage run -p --concurrency=multiprocessing `which green`
coverage combine
coverage report

ci: check test
Expand Down
8 changes: 8 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sonar.projectKey=tortoise-orm
sonar.projectName=Tortoise ORM

sonar.links.homepage=https://github.com/tortoise/tortoise-orm

sonar.sources=.
sonar.host.url=https://sonarcloud.io
sonar.inclusions=tortoise/**/*,examples/*
11 changes: 2 additions & 9 deletions tortoise/backends/base/config_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import urllib.parse as urlparse
import uuid
from typing import Any, Dict, List, Optional # noqa

from tortoise.exceptions import ConfigurationError
Expand Down Expand Up @@ -38,7 +37,7 @@
} # type: Dict[str, Dict[str, Any]]


def expand_db_url(db_url: str, testing: bool = False) -> dict:
def expand_db_url(db_url: str) -> dict:
url = urlparse.urlparse(db_url)
if url.scheme not in DB_LOOKUP:
raise ConfigurationError('Unknown DB scheme: {}'.format(url.scheme))
Expand All @@ -56,11 +55,6 @@ def expand_db_url(db_url: str, testing: bool = False) -> dict:
for key, val in urlparse.parse_qs(url.query).items():
params[key] = val[-1]

if testing:
params['single_connection'] = True
path = path.replace('\\{', '{').replace('\\}', '}')
path = path.format(uuid.uuid4().hex)

vars = {} # type: dict
vars.update(db['vars'])
params[vars['path']] = path
Expand All @@ -86,12 +80,11 @@ def generate_config(
db_url: str,
app_modules: Dict[str, List[str]],
connection_label: Optional[str] = None,
testing: bool = False,
) -> dict:
_connection_label = connection_label or 'default'
return {
'connections': {
_connection_label: expand_db_url(db_url, testing)
_connection_label: expand_db_url(db_url)
},
'apps': {
app_label: {
Expand Down
10 changes: 7 additions & 3 deletions tortoise/contrib/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio as _asyncio
import os as _os
import uuid as _uuid
from copy import deepcopy
from typing import List
from unittest import SkipTest, expectedFailure, skip, skipIf, skipUnless # noqa
Expand Down Expand Up @@ -31,14 +32,17 @@ def getDBConfig(app_label: str, modules: List[str]) -> dict:
"""
DB Config factory, for use in testing.
"""
return _generate_config(
_TORTOISE_TEST_DB,
path = _TORTOISE_TEST_DB.replace('\\{', '{').replace('\\}', '}').format(_uuid.uuid4().hex)
config = _generate_config(
path,
app_modules={
app_label: modules
},
testing=True,
connection_label=app_label
)
if _os.environ.get('TORTOISE_TEST_POOL', '0') == '0':
config['connections'][app_label]['credentials']['single_connection'] = True
return config


async def _init_db(config):
Expand Down
75 changes: 10 additions & 65 deletions tortoise/tests/test_db_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ def test_sqlite_basic(self):
}
})

def test_sqlite_testing(self):
res = expand_db_url(
db_url='sqlite:///some/test-{}.sqlite',
testing=True,
)
file_path = res['credentials']['file_path']
self.assertIn('/some/test-', file_path)
self.assertIn('.sqlite', file_path)
self.assertNotEqual('sqlite:///some/test-{}.sqlite', file_path)
self.assertDictEqual(res, {
'engine': 'tortoise.backends.sqlite',
'credentials': {
'file_path': file_path,
'single_connection': True,
}
})

def test_sqlite_params(self):
res = expand_db_url('sqlite:///some/test.sqlite?AHA=5&moo=yes')
self.assertDictEqual(res, {
Expand All @@ -51,12 +34,12 @@ def test_sqlite_invalid(self):
expand_db_url('sqlite://')

def test_postgres_basic(self):
res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test')
res = expand_db_url('postgres://postgres:@some.host:5432/test')
self.assertDictEqual(res, {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'database': 'test',
'host': '127.0.0.1',
'host': 'some.host',
'password': '',
'port': '5432',
'user': 'postgres',
Expand All @@ -65,35 +48,15 @@ def test_postgres_basic(self):

def test_postgres_nonint_port(self):
with self.assertRaises(ConfigurationError):
expand_db_url('postgres://postgres:@127.0.0.1:moo/test')

def test_postgres_testing(self):
res = expand_db_url(
db_url='postgres://postgres:@127.0.0.1:5432/test_\{\}',
testing=True,
)
database = res['credentials']['database']
self.assertIn('test_', database)
self.assertNotEqual('test_{}', database)
self.assertDictEqual(res, {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'database': database,
'host': '127.0.0.1',
'password': '',
'port': '5432',
'user': 'postgres',
'single_connection': True,
}
})
expand_db_url('postgres://postgres:@some.host:moo/test')

def test_postgres_params(self):
res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test?AHA=5&moo=yes')
res = expand_db_url('postgres://postgres:@some.host:5432/test?AHA=5&moo=yes')
self.assertDictEqual(res, {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'database': 'test',
'host': '127.0.0.1',
'host': 'some.host',
'password': '',
'port': '5432',
'user': 'postgres',
Expand All @@ -103,12 +66,12 @@ def test_postgres_params(self):
})

def test_mysql_basic(self):
res = expand_db_url('mysql://root:@127.0.0.1:3306/test')
res = expand_db_url('mysql://root:@some.host:3306/test')
self.assertEqual(res, {
'engine': 'tortoise.backends.mysql',
'credentials': {
'database': 'test',
'host': '127.0.0.1',
'host': 'some.host',
'password': '',
'port': '3306',
'user': 'root',
Expand All @@ -117,31 +80,15 @@ def test_mysql_basic(self):

def test_mysql_nonint_port(self):
with self.assertRaises(ConfigurationError):
expand_db_url('mysql://root:@127.0.0.1:moo/test')

def test_mysql_testing(self):
res = expand_db_url('mysql://root:@127.0.0.1:3306/test_\{\}', testing=True)
self.assertIn('test_', res['credentials']['database'])
self.assertNotEqual('test_{}', res['credentials']['database'])
self.assertEqual(res, {
'engine': 'tortoise.backends.mysql',
'credentials': {
'database': res['credentials']['database'],
'host': '127.0.0.1',
'password': '',
'port': '3306',
'user': 'root',
'single_connection': True,
}
})
expand_db_url('mysql://root:@some.host:moo/test')

def test_mysql_params(self):
res = expand_db_url('mysql://root:@127.0.0.1:3306/test?AHA=5&moo=yes')
res = expand_db_url('mysql://root:@some.host:3306/test?AHA=5&moo=yes')
self.assertEqual(res, {
'engine': 'tortoise.backends.mysql',
'credentials': {
'database': 'test',
'host': '127.0.0.1',
'host': 'some.host',
'password': '',
'port': '3306',
'user': 'root',
Expand Down Expand Up @@ -190,14 +137,12 @@ def test_generate_config_explicit(self):
]
},
connection_label='models',
testing=True
)
self.assertEqual(res, {
'connections': {
'models': {
'credentials': {
'file_path': '/some/test.sqlite',
'single_connection': True
},
'engine': 'tortoise.backends.sqlite'
}
Expand Down

0 comments on commit 4b38d12

Please sign in to comment.