insert_sites tests done
This commit is contained in:
parent
5201a444e9
commit
03ecae4be5
|
|
@ -106,7 +106,7 @@ Test are written in pytest framework. Currently focused on unittest.
|
|||
Integration tests to come.
|
||||
|
||||
To run test use:
|
||||
```
|
||||
```sh
|
||||
pytest --cov=src/recipe_graph --cov-report lcov --cov-report html
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from pydoc import apropos
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import Session
|
||||
from recipe_graph import db
|
||||
import json
|
||||
import argparse
|
||||
|
|
@ -7,7 +7,7 @@ import logging
|
|||
import sys
|
||||
|
||||
|
||||
def load_file(f_name: str):
|
||||
def load_file(f_name: str) -> list[dict[str, any]]:
|
||||
with open(f_name) as f:
|
||||
sites = json.load(f)
|
||||
return sites
|
||||
|
|
@ -29,18 +29,27 @@ def setup_logging(args: argparse.Namespace) -> logging.Logger:
|
|||
return logger
|
||||
|
||||
|
||||
def main(): # pragma: no cover
|
||||
def add_sites(
|
||||
S: Session,
|
||||
sites: list[dict[str, any]],
|
||||
logger: logging.Logger = None,
|
||||
):
|
||||
with S.begin() as session:
|
||||
for site in sites:
|
||||
if logger: # pragma: no cover
|
||||
logger.info(f"Adding {site}")
|
||||
session.add(db.RecipeSite(**site))
|
||||
|
||||
|
||||
def main(): # pragma: no cover
|
||||
args = setup_argparser(sys.argv[1:])
|
||||
logger = setup_logging(args)
|
||||
|
||||
S = db.get_session()
|
||||
sites = load_file(args.file)
|
||||
|
||||
with S.begin() as session:
|
||||
for site in sites:
|
||||
logger.info(f"Adding {site}")
|
||||
session.add(db.RecipeSite(**site))
|
||||
add_sites(S, sites, logger)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import sqlalchemy
|
|||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture()
|
||||
def engine() -> sqlalchemy.engine.Engine:
|
||||
engine = db.get_engine()
|
||||
# make sure db is empty otherwise might be testing a live db
|
||||
|
|
|
|||
|
|
@ -1,30 +1,54 @@
|
|||
import json
|
||||
import os
|
||||
from recipe_graph import insert_sites
|
||||
from recipe_graph import insert_sites, db
|
||||
from sqlalchemy import select
|
||||
import sqlalchemy
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
from test_db import engine, init_db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def json_data() -> list[dict]:
|
||||
def json_data() -> list[dict[str, any]]:
|
||||
return [{"key": "value"}, {"test": "value1", "test2": "value2"}]
|
||||
|
||||
@pytest.fixture
|
||||
def db_initialized(engine) -> sqlalchemy.engine.Engine:
|
||||
init_db(engine)
|
||||
return engine
|
||||
|
||||
@pytest.fixture
|
||||
def mock_sites() -> list[dict[str, any]]:
|
||||
return [
|
||||
{
|
||||
"name": "example-site",
|
||||
"ingredient_class": "example-item-name",
|
||||
"name_class": "example-content",
|
||||
"base_url": "https://www.example.com/recipe/",
|
||||
},
|
||||
{
|
||||
"name": "test-site",
|
||||
"ingredient_class": "test-item-name",
|
||||
"name_class": "test-content",
|
||||
"base_url": "https://www.test.com/recipe/",
|
||||
},
|
||||
]
|
||||
|
||||
@pytest.fixture
|
||||
def json_file(json_data: list[dict]) -> str:
|
||||
f_path = "test.json"
|
||||
with open(f_path, 'w') as f:
|
||||
with open(f_path, "w") as f:
|
||||
json.dump(json_data, f)
|
||||
yield f_path
|
||||
if os.path.exists(f_path):
|
||||
os.remove(f_path)
|
||||
|
||||
|
||||
def test_load_file(json_file: str, json_data):
|
||||
test_data = insert_sites.load_file(json_file)
|
||||
assert test_data == json_data
|
||||
|
||||
|
||||
|
||||
def test_setup_argparser():
|
||||
file_name = "test"
|
||||
|
|
@ -33,7 +57,6 @@ def test_setup_argparser():
|
|||
assert args.file == file_name
|
||||
assert args.verbose == False
|
||||
|
||||
|
||||
args = insert_sites.setup_argparser([file_name, "-v"])
|
||||
assert args.file == file_name
|
||||
assert args.verbose == True
|
||||
|
|
@ -42,16 +65,36 @@ def test_setup_argparser():
|
|||
assert args.file == file_name
|
||||
assert args.verbose == True
|
||||
|
||||
|
||||
def test_setup_logging():
|
||||
args = insert_sites.setup_argparser(["test"])
|
||||
logger = insert_sites.setup_logging(args)
|
||||
assert logger.level == logging.WARNING
|
||||
|
||||
args = insert_sites.setup_argparser(["test", "-v"])
|
||||
logger = insert_sites.setup_logging(args)
|
||||
logger = insert_sites.setup_logging(args)
|
||||
assert logger.level == logging.INFO
|
||||
|
||||
args = insert_sites.setup_argparser(["test", "--verbose"])
|
||||
logger = insert_sites.setup_logging(args)
|
||||
logger = insert_sites.setup_logging(args)
|
||||
assert logger.level == logging.INFO
|
||||
|
||||
|
||||
|
||||
def test_add_sites(mock_sites, db_initialized):
|
||||
db_session = db.get_session()
|
||||
insert_sites.add_sites(db_session, mock_sites)
|
||||
|
||||
results = []
|
||||
with db_session.begin() as session:
|
||||
results = session.execute(select(db.RecipeSite)).all()
|
||||
|
||||
assert len(results) > 0
|
||||
assert len(results) == 2
|
||||
print(db.RecipeSite(name="a"))
|
||||
for i, (site,) in enumerate(results):
|
||||
site.name == mock_sites[i]["name"]
|
||||
site.ingredient_class == mock_sites[i]["ingredient_class"]
|
||||
site.name_class == mock_sites[i]["name_class"]
|
||||
site.base_url == mock_sites[i]["base_url"]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue