Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #35 from eccentricOrange:eccentricOrange/issue32
Browse files Browse the repository at this point in the history
EccentricOrange/issue32
  • Loading branch information
eccentricOrange authored May 28, 2022
2 parents 106fcdc + 9fb3eb7 commit 124a27a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
11 changes: 6 additions & 5 deletions npbc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from argparse import ArgumentParser
from argparse import Namespace as ArgNamespace
from datetime import datetime
from sys import argv
from typing import Generator

from colorama import Fore, Style
Expand All @@ -21,7 +22,7 @@
from npbc_regex import DELIVERY_MATCH_REGEX


def define_and_read_args() -> ArgNamespace:
def define_and_read_args(arguments: list[str]) -> ArgNamespace:
"""configure parsers
- define the main parser for the application executable
- define subparsers (one for each functionality)
Expand Down Expand Up @@ -157,7 +158,7 @@ def define_and_read_args() -> ArgNamespace:
update_parser.set_defaults(func=update)


return main_parser.parse_args()
return main_parser.parse_args(arguments)


def status_print(status: bool, message: str) -> None:
Expand Down Expand Up @@ -696,7 +697,7 @@ def update(args: ArgNamespace) -> None:
status_print(False, "Update failed.")


def main() -> None:
def main(arguments: list[str]) -> None:
"""main function
- initialize the database
- parses the command line arguments
Expand All @@ -712,11 +713,11 @@ def main() -> None:
return

# parse the command line arguments
parsed = define_and_read_args()
parsed = define_and_read_args(arguments)

# execute the appropriate function
parsed.func(parsed)


if __name__ == "__main__":
main()
main(argv[1:])
2 changes: 1 addition & 1 deletion npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
SCHEMA_PATH = Path(__file__).parent / 'schema.sql'

# if in a development environment, set the paths to the data folder
if str(environ.get('NPBC_DEVELOPMENT')) == "1" or str(environ.get('CI')) == "true":
if environ.get('NPBC_DEVELOPMENT') or environ.get('CI'):
DATABASE_DIR = Path('data')
SCHEMA_PATH = Path('data') / 'schema.sql'

Expand Down
4 changes: 2 additions & 2 deletions npbc_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
RANGE_MATCH_REGEX = compile_regex(r'^\d{1,2} *- *\d{1,2}$')

# match for weekday name. day must appear as "daynames" (example = "mondays"). all lowercase.
DAYS_MATCH_REGEX = compile_regex(f"^{'|'.join([day_name.lower() + 's' for day_name in WEEKDAY_NAMES_ITERABLE])}$")
DAYS_MATCH_REGEX = compile_regex(f"^{'|'.join(map(lambda x: x.lower() + 's', WEEKDAY_NAMES_ITERABLE))}$")

# match for nth weekday name. day must appear as "n-dayname" (example = "1-monday"). all lowercase. must be one digit.
N_DAY_MATCH_REGEX = compile_regex(f"^\\d *- *({'|'.join([day_name.lower() for day_name in WEEKDAY_NAMES_ITERABLE])})$")
N_DAY_MATCH_REGEX = compile_regex(f"^\\d *- *({'|'.join(map(lambda x: x.lower(), WEEKDAY_NAMES_ITERABLE))})$")

# match for the text "all" in any case.
ALL_MATCH_REGEX = compile_regex(r'^[aA][lL]{2}$')
Expand Down
54 changes: 34 additions & 20 deletions test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,42 @@

from pytest import raises

import npbc_cli
import npbc_core
import npbc_exceptions

DATABASE_PATH = Path("data") / "npbc.db"
SCHEMA_PATH = Path("data") / "schema.sql"
TEST_SQL = Path("data") / "test.sql"
ACTIVE_DIRECTORY = Path("data")
DATABASE_PATH = ACTIVE_DIRECTORY / "npbc.db"
SCHEMA_PATH = ACTIVE_DIRECTORY / "schema.sql"
TEST_SQL = ACTIVE_DIRECTORY / "test.sql"


def setup_db(database_path: Path, schema_path: Path, test_sql: Path):
database_path.unlink(missing_ok=True)

def setup_db():
DATABASE_PATH.unlink(missing_ok=True)

with connect(database_path) as connection:
connection.executescript(schema_path.read_text())
with connect(DATABASE_PATH) as connection:
connection.executescript(SCHEMA_PATH.read_text())
connection.commit()
connection.executescript(test_sql.read_text())
connection.executescript(TEST_SQL.read_text())

connection.close()


def test_db_creation():
DATABASE_PATH.unlink(missing_ok=True)
assert not DATABASE_PATH.exists()

try:
npbc_cli.main([])

except SystemExit:
pass

assert DATABASE_PATH.exists()


def test_get_papers():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 'paper1', 0, 0, 0),
Expand Down Expand Up @@ -65,7 +79,7 @@ def test_get_papers():


def test_get_undelivered_strings():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 1, 2020, 11, '5'),
Expand All @@ -86,7 +100,7 @@ def test_get_undelivered_strings():


def test_delete_paper():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

npbc_core.delete_existing_paper(2)

Expand Down Expand Up @@ -115,7 +129,7 @@ def test_delete_paper():


def test_add_paper():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 'paper1', 0, 0, 0),
Expand Down Expand Up @@ -165,7 +179,7 @@ def test_add_paper():


def test_edit_paper():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 'paper1', 0, 0, 0),
Expand Down Expand Up @@ -235,19 +249,19 @@ def test_delete_string():
(5, 3, 2020, 10, 'all')
]

setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()
npbc_core.delete_undelivered_string(string='all')
assert Counter(npbc_core.get_undelivered_strings()) == Counter(known_data[:4])

setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()
npbc_core.delete_undelivered_string(month=11)
assert Counter(npbc_core.get_undelivered_strings()) == Counter([known_data[4]])

setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()
npbc_core.delete_undelivered_string(paper_id=1)
assert Counter(npbc_core.get_undelivered_strings()) == Counter(known_data[2:])

setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

with raises(npbc_exceptions.StringNotExists):
npbc_core.delete_undelivered_string(string='not exists')
Expand All @@ -257,7 +271,7 @@ def test_delete_string():


def test_add_string():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 1, 2020, 11, '5'),
Expand All @@ -279,7 +293,7 @@ def test_add_string():


def test_save_results():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
setup_db()

known_data = [
(1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01'),
Expand Down

0 comments on commit 124a27a

Please sign in to comment.