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.
|
Integration tests to come.
|
||||||
|
|
||||||
To run test use:
|
To run test use:
|
||||||
```
|
```sh
|
||||||
pytest --cov=src/recipe_graph --cov-report lcov --cov-report html
|
pytest --cov=src/recipe_graph --cov-report lcov --cov-report html
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from pydoc import apropos
|
from pydoc import apropos
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import Session
|
||||||
from recipe_graph import db
|
from recipe_graph import db
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
|
|
@ -7,7 +7,7 @@ import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def load_file(f_name: str):
|
def load_file(f_name: str) -> list[dict[str, any]]:
|
||||||
with open(f_name) as f:
|
with open(f_name) as f:
|
||||||
sites = json.load(f)
|
sites = json.load(f)
|
||||||
return sites
|
return sites
|
||||||
|
|
@ -29,6 +29,18 @@ def setup_logging(args: argparse.Namespace) -> logging.Logger:
|
||||||
return 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
|
def main(): # pragma: no cover
|
||||||
args = setup_argparser(sys.argv[1:])
|
args = setup_argparser(sys.argv[1:])
|
||||||
logger = setup_logging(args)
|
logger = setup_logging(args)
|
||||||
|
|
@ -36,10 +48,7 @@ def main(): # pragma: no cover
|
||||||
S = db.get_session()
|
S = db.get_session()
|
||||||
sites = load_file(args.file)
|
sites = load_file(args.file)
|
||||||
|
|
||||||
with S.begin() as session:
|
add_sites(S, sites, logger)
|
||||||
for site in sites:
|
|
||||||
logger.info(f"Adding {site}")
|
|
||||||
session.add(db.RecipeSite(**site))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": # pragma: no cover
|
if __name__ == "__main__": # pragma: no cover
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import sqlalchemy
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture()
|
||||||
def engine() -> sqlalchemy.engine.Engine:
|
def engine() -> sqlalchemy.engine.Engine:
|
||||||
engine = db.get_engine()
|
engine = db.get_engine()
|
||||||
# make sure db is empty otherwise might be testing a live db
|
# make sure db is empty otherwise might be testing a live db
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,50 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from recipe_graph import insert_sites
|
from recipe_graph import insert_sites, db
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
import sqlalchemy
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from test_db import engine, init_db
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def json_data() -> list[dict]:
|
def json_data() -> list[dict[str, any]]:
|
||||||
return [{"key": "value"}, {"test": "value1", "test2": "value2"}]
|
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
|
@pytest.fixture
|
||||||
def json_file(json_data: list[dict]) -> str:
|
def json_file(json_data: list[dict]) -> str:
|
||||||
f_path = "test.json"
|
f_path = "test.json"
|
||||||
with open(f_path, 'w') as f:
|
with open(f_path, "w") as f:
|
||||||
json.dump(json_data, f)
|
json.dump(json_data, f)
|
||||||
yield f_path
|
yield f_path
|
||||||
if os.path.exists(f_path):
|
if os.path.exists(f_path):
|
||||||
os.remove(f_path)
|
os.remove(f_path)
|
||||||
|
|
||||||
|
|
||||||
def test_load_file(json_file: str, json_data):
|
def test_load_file(json_file: str, json_data):
|
||||||
test_data = insert_sites.load_file(json_file)
|
test_data = insert_sites.load_file(json_file)
|
||||||
assert test_data == json_data
|
assert test_data == json_data
|
||||||
|
|
@ -33,7 +57,6 @@ def test_setup_argparser():
|
||||||
assert args.file == file_name
|
assert args.file == file_name
|
||||||
assert args.verbose == False
|
assert args.verbose == False
|
||||||
|
|
||||||
|
|
||||||
args = insert_sites.setup_argparser([file_name, "-v"])
|
args = insert_sites.setup_argparser([file_name, "-v"])
|
||||||
assert args.file == file_name
|
assert args.file == file_name
|
||||||
assert args.verbose == True
|
assert args.verbose == True
|
||||||
|
|
@ -42,6 +65,7 @@ def test_setup_argparser():
|
||||||
assert args.file == file_name
|
assert args.file == file_name
|
||||||
assert args.verbose == True
|
assert args.verbose == True
|
||||||
|
|
||||||
|
|
||||||
def test_setup_logging():
|
def test_setup_logging():
|
||||||
args = insert_sites.setup_argparser(["test"])
|
args = insert_sites.setup_argparser(["test"])
|
||||||
logger = insert_sites.setup_logging(args)
|
logger = insert_sites.setup_logging(args)
|
||||||
|
|
@ -55,3 +79,22 @@ def test_setup_logging():
|
||||||
logger = insert_sites.setup_logging(args)
|
logger = insert_sites.setup_logging(args)
|
||||||
assert logger.level == logging.INFO
|
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