-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
72 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) | ||
import os | ||
import sys | ||
sys.path.append(os.path.dirname(os.path.realpath(__file__))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import os | ||
|
||
SCHEME = os.environ.get("API_SCHEME", "http") | ||
NETLOC = os.environ.get("API_NETLOC", "ex.com") | ||
NETLOC = os.environ.get("API_NETLOC", "ex.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) | ||
import os | ||
import sys | ||
sys.path.append(os.path.dirname(os.path.realpath(__file__))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ def db_path(): | |
def db_mock(db_path): | ||
return init_db(db_path) | ||
|
||
|
||
@pytest.fixture | ||
def argparser(): | ||
return parser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
from main import main | ||
from pytest_mock import mocker | ||
import pytest | ||
from argparse import Namespace | ||
from test_db import script | ||
from test_db import script, manager_mock | ||
from unittest import mock | ||
from app.db import get_db, close_db, init_db | ||
from argparse import ArgumentParser | ||
import re | ||
from app.db import get_db, init_db | ||
import shortuuid | ||
from urllib.parse import urlunsplit | ||
from app.config import SCHEME, NETLOC | ||
|
||
|
||
def get_short_url(url): | ||
uuid = shortuuid.uuid(name=url)[:7] | ||
return urlunsplit((SCHEME, NETLOC, uuid, '', '')) | ||
|
||
|
||
def test_main_noparams(db_path, mocker): | ||
mocker.patch('main.DB_PATH', db_path) | ||
with pytest.raises(SystemExit): | ||
main() | ||
|
||
def test_main_short_url(db_path, argparser, mocker, capsys): | ||
|
||
@pytest.mark.parametrize('args, output', | ||
[(['on.ln'], "ERROR!!! URL does not exists"), | ||
(['goo.gl'], 'https://www.google.com/'), (['https://www.google.com/', '--generate'], | ||
'goo.gl'), | ||
(['https://www.onliner.by', '--generate'], get_short_url('https://www.onliner.by')), | ||
(['https://www.onliner.by', '--generate', '--short_url', 'onl.by'], 'onl.by'), | ||
(['https://www.google.com/', '--generate', '--short_url', 'goo.gl'], | ||
"ERROR!!! This url already in database"), | ||
] | ||
) | ||
def test_main(db_path, argparser, mocker, capsys, args, output): | ||
mocker.patch('main.DB_PATH', db_path) | ||
init_db(db_path=db_path) | ||
with mock.patch('app.db.DB_PATH', db_path): | ||
db = get_db() | ||
db.executescript(script) | ||
args = argparser.parse_args(['on.ln']) | ||
parser = mocker.patch('argparse.ArgumentParser.parse_args', return_value=args) | ||
main() | ||
captured = capsys.readouterr() | ||
print(captured) | ||
assert 'URL does not exists\n' == captured.err | ||
args = argparser.parse_args(args) | ||
mocker.patch('argparse.ArgumentParser.parse_args', return_value=args) | ||
with manager_mock(db, 'db.get_db', 'db.db_manager'): | ||
main() | ||
captured = capsys.readouterr() | ||
assert output+'\n' == captured.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters