Skip to content

Commit

Permalink
Fix typo in CLI, fix breaking config forgetfullness, add new test for… (
Browse files Browse the repository at this point in the history
#22)

* Fix typo in CLI, fix breaking config forgetfullness, add new test for this (#21)

* Bump version number
  • Loading branch information
EUdds authored Dec 22, 2023
1 parent 3123530 commit 537c10c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
6 changes: 3 additions & 3 deletions inventree_digikey_integration/Inventree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from .ConfigReader import ConfigReader


def import_digikey_part(partnum: str, prompt=False):
def import_digikey_part(partnum: str, config: ConfigReader, prompt=False):
dkpart = DigiPart.from_digikey_part_number(
partnum, injest_api_automatically=True, prompt=prompt
partnum, config, injest_api_automatically=True, prompt=prompt
)
return add_digikey_part(dkpart)
return add_digikey_part(dkpart, config)


def add_digikey_part(dkpart: DigiPart, config: ConfigReader):
Expand Down
24 changes: 15 additions & 9 deletions inventree_digikey_integration/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

from .Inventree import import_digikey_part
from .ConfigReader import ConfigReader
from pathlib import Path

DEFAULT_CONFIG_PATH = Path(__file__).resolve().parent / "config.ini"
Expand Down Expand Up @@ -30,23 +31,28 @@ def parse_args(args):

# Add the 'part_number' argument as the last item on the command line
parser.add_argument(
"query_numbers", type=str, help="Part number(s) to import", nargs="+"
"query_numbers", type=str, help="Part number(s) to import", nargs="*"
)

return parser.parse_args(args)


def main():
args = parse_args(sys.argv[1:])
def import_parts(args):
config = ConfigReader(args.config)
if len(args.query_numbers) == 0:
print("No part numbers specified")
sys.exit(1)
if len(args.query_number) == 1:
partnum = input("Enter a digikey Part Number > ")
import_digikey_part(partnum, not args.yes)
import_digikey_part(partnum, config, not args.yes)
return 1
else:
for num in args.query_number:
import_digikey_part(num, not args.yes)
for num in args.query_numbers:
import_digikey_part(num, config, not args.yes)
return len(args.query_numbers)


def main():
args = parse_args(sys.argv[1:])
num_parts = import_parts(args)
print(f"Attempted to import {num_parts} parts")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "inventree_digikey_integration"
version = "0.1.2.1"
version = "0.1.2.2"
description = "A CLI to import Digikey parts into an Inventree instance"
authors = ["Eric Udlis <[email protected]>"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_data():
config = ConfigParser()
config.read(file)
data_dict[file.stem] = config
data_dict[f"{file.stem}_path"] = file

data_dict["config_reader"] = ConfigReader(TEST_DATA_PATH / "test_config.ini")

Expand Down
19 changes: 19 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inventree_digikey_integration.__main__ as test_module
import inventree_digikey_integration.Inventree

from pathlib import Path

Expand Down Expand Up @@ -45,3 +46,21 @@ def test_argparse():
assert args.yes == True
assert args.query_numbers == ["1234", "5678"]
assert args.config == Path("test_config.ini")


def test_import_parts(monkeypatch, test_data):
# This test is to make sure that the function chooses the correct branch of the if statement
# Lets ignore the actual parsing here
monkeypatch.setattr(test_module, "import_digikey_part", lambda x, y, z: None)
# Format (args, expected_return)
test_inputs = [
(["-c", f"{test_data['test_config_path']}", "4116R-1-151LF"], 1),
]
for test_input in test_inputs:
args = test_module.parse_args(test_input[0])
res = test_module.import_parts(args)
assert (
res == test_input[1]
), "Expected return value of {} is {} but got {}".format(
test_input[0], test_input[1], res
)

0 comments on commit 537c10c

Please sign in to comment.