Skip to content

Commit 9bdd896

Browse files
committed
Task: Improve docs, code and tests
1 parent 3699088 commit 9bdd896

File tree

6 files changed

+107
-24
lines changed

6 files changed

+107
-24
lines changed

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: python
2+
3+
cache: pip
4+
5+
python:
6+
- "3.6"
7+
8+
install:
9+
- pip install -r dev-requirements.txt
10+
- pip install coveralls
11+
12+
script:
13+
- make tests
14+
15+
after_script:
16+
- coveralls

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
# Python database url
1+
# Py-Database-URL
2+
3+
[![Build Status](https://travis-ci.org/jairojair/py-database-url.svg?branch=master)](https://travis-ci.org/jairojair/py-database-url)
4+
[![Coverage Status](https://coveralls.io/repos/github/jairojair/py-database-url/badge.svg?branch=master)](https://coveralls.io/github/jairojair/py-database-url?branch=master)
5+
6+
## Installation
7+
8+
pip install py-database-url

docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ services:
88
- .:/code
99

1010
working_dir: /code
11+
12+
environment:
13+
- DATABASE_URL=postgres://user:password@hostname:5432/database-name

py_database_url/py_database_url.py

+54-13
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,67 @@
33
import os
44

55
try:
6-
from urlparse import urlparse
7-
except ImportError:
6+
# Python 3
87
from urllib.parse import urlparse
98

9+
except ImportError:
10+
# Python 2
11+
from urlparse import urlparse
12+
1013

11-
def config():
14+
class Config(object):
1215
"""
13-
Get database config from environment var (DATABASE_URL) automatically.
16+
Describe class
1417
"""
15-
pass
1618

19+
def __init__(self):
20+
pass
1721

18-
def parse(url=None):
19-
"""
20-
Parses a database URL.
21-
"""
22+
def __get_database_url_var(self):
23+
"""
24+
Describe
25+
"""
26+
return os.environ["DATABASE_URL"]
27+
28+
def config(self):
29+
"""
30+
Get database config from environment var (DATABASE_URL) automatically.
31+
"""
32+
pass
33+
34+
def parse(self, url=None):
35+
"""
36+
Parses a database URL.
37+
"""
38+
39+
config = {}
40+
41+
if url is None:
42+
return
43+
44+
return urlparse(url)
45+
46+
@property
47+
def orator(self):
48+
"""
49+
Method to parser for orator format.
50+
"""
51+
52+
url = self.__get_database_url_var()
53+
url = self.parse(url)
54+
55+
config = {
56+
f"{url.scheme}": {
57+
"driver": url.scheme,
58+
"host": url.hostname,
59+
"database": url.path[1:],
60+
"user": url.username,
61+
"password": url.password,
62+
"prefix": "",
63+
}
64+
}
2265

23-
config = {}
66+
return config
2467

25-
if url is None:
26-
return
2768

28-
return urlparse(url)
69+
config = Config()

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="py-database-url",
5-
description="A universal database URLS parser for Python.",
5+
description="A universal database URLs for Python applications.",
66
version="0.0.2",
77
url="https://github.com/jairojair/py-database-url",
88
license="MIT",

tests/test_parse.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
import pytest
22

3-
from py_database_url import parse
3+
from py_database_url import config
44

55

6-
def test_parse_without_url():
6+
def test_config_without_url():
77

8-
assert parse() is None
8+
assert config.parse() is None
99

1010

1111
def test_parse_database_url():
1212
"""
1313
Describe
1414
"""
1515

16-
url = "postgres://user:password@hostname:5432/database-name"
16+
url = "postgres://user:password@hostname:5432/db-name"
1717

18-
config = parse(url)
18+
ret = config.parse(url)
1919

20-
assert config.username == "user"
21-
assert config.password == "password"
22-
assert config.hostname == "hostname"
23-
assert config.port == 5432
20+
assert ret.username == "user"
21+
assert ret.password == "password"
22+
assert ret.hostname == "hostname"
23+
assert ret.port == 5432
24+
25+
26+
def test_config_database_url_orator():
27+
28+
expected = {
29+
"postgres": {
30+
"database": "database-name",
31+
"driver": "postgres",
32+
"host": "hostname",
33+
"user": "user",
34+
"password": "password",
35+
"prefix": "",
36+
}
37+
}
38+
39+
assert config.orator == expected

0 commit comments

Comments
 (0)