-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): define the agency list command
benefits agency list -h
- Loading branch information
1 parent
78127a4
commit bf19d3d
Showing
7 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from dataclasses import dataclass | ||
|
||
from django.db.models import Q | ||
|
||
from benefits.cli.commands import BaseOptions, BenefitsCommand | ||
from benefits.core.models import TransitAgency | ||
|
||
|
||
@dataclass | ||
class Options(BaseOptions): | ||
all: bool = False | ||
name: str = None | ||
slug: str = None | ||
|
||
|
||
class List(BenefitsCommand): | ||
"""List transit agencies.""" | ||
|
||
help = __doc__ | ||
name = "list" | ||
options_cls = Options | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"-a", | ||
"--all", | ||
action="store_true", | ||
default=False, | ||
help="Show both active and inactive agencies. By default show only active agencies.", | ||
) | ||
parser.add_argument( | ||
"-n", | ||
"--name", | ||
type=str, | ||
help="Filter for agencies with matching (partial) short_name or long_name.", | ||
) | ||
parser.add_argument( | ||
"-s", | ||
"--slug", | ||
type=str, | ||
help="Filter for agencies with matching (partial) slug.", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
opts = self.parse_opts(**options) | ||
agencies = TransitAgency.objects.all() | ||
|
||
if not opts.all: | ||
agencies = agencies.filter(active=True) | ||
|
||
if opts.name: | ||
q = Q(short_name__contains=opts.name) | Q(long_name__contains=opts.name) | ||
agencies = agencies.filter(q) | ||
|
||
if opts.slug: | ||
agencies = agencies.filter(slug__contains=opts.slug) | ||
|
||
if len(agencies) > 0: | ||
if len(agencies) > 1: | ||
msg = f"{len(agencies)} agencies:" | ||
else: | ||
msg = "1 agency:" | ||
self.stdout.write(self.style.SUCCESS(msg)) | ||
|
||
active = filter(lambda a: a.active, agencies) | ||
inactive = filter(lambda a: not a.active, agencies) | ||
|
||
for agency in active: | ||
self.stdout.write(self.style.HTTP_NOT_MODIFIED(f"{agency}")) | ||
for agency in inactive: | ||
self.stdout.write(self.style.WARNING(f"[inactive] {agency}")) | ||
else: | ||
self.stdout.write(self.style.HTTP_NOT_FOUND("No matching agencies")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
|
||
from benefits.cli.agency.list import List | ||
|
||
|
||
@pytest.fixture | ||
def cmd(cmd): | ||
def call(*args, **kwargs): | ||
return cmd(List, *args, **kwargs) | ||
|
||
return call | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list(cmd): | ||
out, err = cmd() | ||
|
||
assert err == "" | ||
assert "No matching agencies" in out | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_agency(cmd, model_TransitAgency): | ||
out, err = cmd() | ||
|
||
assert err == "" | ||
assert "1 agency" in out | ||
assert str(model_TransitAgency) in out | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_agencies(cmd, model_TransitAgency): | ||
orig_agency = str(model_TransitAgency) | ||
|
||
model_TransitAgency.pk = None | ||
model_TransitAgency.long_name = "Another agency" | ||
model_TransitAgency.save() | ||
|
||
out, err = cmd() | ||
|
||
assert err == "" | ||
assert "2 agencies" in out | ||
assert orig_agency in out | ||
assert str(model_TransitAgency) in out | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_agencies_active(cmd, model_TransitAgency): | ||
orig_agency = str(model_TransitAgency) | ||
|
||
model_TransitAgency.pk = None | ||
model_TransitAgency.long_name = "Another agency" | ||
model_TransitAgency.active = False | ||
model_TransitAgency.save() | ||
|
||
out, err = cmd() | ||
|
||
assert err == "" | ||
assert "1 agency" in out | ||
assert orig_agency in out | ||
assert str(model_TransitAgency) not in out | ||
|
||
out, err = cmd("--all") | ||
|
||
assert err == "" | ||
assert "2 agencies" in out | ||
assert orig_agency in out | ||
assert f"[inactive] {model_TransitAgency}" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import pytest | ||
|
||
from django.core.management import call_command | ||
|
||
|
||
@pytest.fixture | ||
def cmd(capfd): | ||
def call(cls, *args, **kwargs): | ||
call_command(cls(), *args, **kwargs) | ||
return capfd.readouterr() | ||
|
||
return call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters