Compare commits
2 Commits
d60634dff2
...
03ecae4be5
| Author | SHA1 | Date |
|---|---|---|
|
|
03ecae4be5 | |
|
|
5201a444e9 |
|
|
@ -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
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -2,5 +2,13 @@
|
|||
requires = ["setuptools>=61.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[metadata]
|
||||
[project]
|
||||
name = "recepie_graph"
|
||||
version = "0.0.1"
|
||||
description = "mapping out recipes relations"
|
||||
dependencies = [
|
||||
"SQLAlchemy==1.4.39",
|
||||
"python-dotenv==0.20.0",
|
||||
"beautifulsoup4==4.11.1",
|
||||
"psycopg2-binary==2.9.3"
|
||||
]
|
||||
|
|
@ -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,6 +29,18 @@ def setup_logging(args: argparse.Namespace) -> logging.Logger:
|
|||
return logger
|
||||
|
||||
|
||||
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)
|
||||
|
|
@ -36,10 +48,7 @@ def main(): # pragma: no cover
|
|||
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
|
||||
|
|
|
|||
|
|
@ -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,26 +1,50 @@
|
|||
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
|
||||
|
|
@ -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,6 +65,7 @@ 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)
|
||||
|
|
@ -55,3 +79,22 @@ def test_setup_logging():
|
|||
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