Skip to content

Commit

Permalink
Merge pull request #103 from jathanism/set_queries
Browse files Browse the repository at this point in the history
Updated unit tests to support assert proper support for NSoT 0.17.1.
  • Loading branch information
jathanism authored and kytrick committed Apr 8, 2016
1 parent 60aaaf4 commit d81c9ff
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 12 deletions.
15 changes: 14 additions & 1 deletion pynsot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def handle_error(self, action, data, err):
msg = str(err)

if isinstance(msg, dict):
msg = self.pretty_dict(msg, delim=':', joiner='')
msg = self.pretty_dict(msg, delim=': ', joiner='')

# If we're being verbose, print some extra context.
if self.verbose:
Expand Down Expand Up @@ -535,6 +535,19 @@ def get_single_object(self, data, resource=None, natural_keys=None):
except IndexError:
return None

def set_query(self, data):
"""
Run a set query and return the results.
:param data:
Dict of query parameters
"""
self.rebase(data)
try:
return self.resource.query.get(**data)
except HTTP_ERRORS as err:
self.handle_error('list', data, err)

def list(self, data, display_fields=None, resource=None,
verbose_fields=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion pynsot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def get_token(self, base_url, email, secret_key):
return result
else:
msg = 'Failed to fetch auth_token from %s' % base_url
err = HttpClientError(msg, response=r, content=r.content)
err = HttpClientError(msg, response=resp, content=resp.content)
self.client.error(err)

def __call__(self, r):
Expand Down
5 changes: 2 additions & 3 deletions pynsot/commands/cmd_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ def list(ctx, attributes, delimited, grep, hostname, id, limit, natural_key,
data.pop('delimited') # We don't want this going to the server.

if ctx.invoked_subcommand is None:
if query:
results = ctx.obj.api.sites(site_id).devices.query.get(
query=query, limit=limit, offset=offset)
if query is not None:
results = ctx.obj.set_query(data)
devices = get_result(results)
devices = sorted(d['hostname'] for d in devices)
joiner = ',' if delimited else '\n'
Expand Down
4 changes: 2 additions & 2 deletions pynsot/commands/cmd_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ def list(ctx, attributes, delimited, device, description, grep, id, limit,
# If we aren't passing a sub-command, just call list(), otherwise let it
# fallback to default behavior.
if ctx.invoked_subcommand is None:
if query:
results = ctx.obj.api.sites(site_id).interfaces.query.get(**data)
if query is not None:
results = ctx.obj.set_query(data)
interfaces = get_result(results)
interfaces = sorted(
(i['device'], i['name']) for i in interfaces
Expand Down
4 changes: 2 additions & 2 deletions pynsot/commands/cmd_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def list(ctx, attributes, cidr, delimited, grep, id, include_ips,
# If we aren't passing a sub-command, just call list(), otherwise let it
# fallback to default behavior.
if ctx.invoked_subcommand is None:
if query:
results = ctx.obj.api.sites(site_id).networks.query.get(**data)
if query is not None:
results = ctx.obj.set_query(data)
networks = get_result(results)
networks = sorted(
(d['network_address'], d['prefix_length']) for d in networks
Expand Down
1 change: 0 additions & 1 deletion pynsot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ def get_result(response):

# Or just return the payload... (next-gen)
return payload

2 changes: 1 addition & 1 deletion pynsot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.20.3'
__version__ = '0.20.4'
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r requirements.txt
fake-factory==0.5.0
nsot==0.17
nsot==0.17.1
py==1.4.26
pytest==2.7.0
pytest-django==2.9.1
Expand Down
38 changes: 38 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ def test_devices_list(site_client):
assert result.exit_code == 0
assert result.output == expected_output

# Now create 1 device w/ owner= w/ a space in the value
runner.run('devices add -H foo-bar3 -a owner="Jathan McCollum"')

# Test that you can query by values w/ spaces when properly quoted
result = runner.run('devices list -q \'owner="Jathan McCollum"\'')
expected_output = 'foo-bar3\n'
assert result.exit_code == 0
assert result.output == expected_output

# ... Or using backslashes works, too.
result = runner.run('devices list -q "owner=Jathan\ McCollum"')
assert result.exit_code == 0
assert result.output == expected_output

# Test that query with unbalanced quotes fails.
result = runner.run('devices list -q \'owner="Jathan McCollum\'')
assert result.exit_code == 1
assert 'No closing quotation' in result.output


def test_devices_update(site_client):
"""Test ``nsot devices update``."""
Expand Down Expand Up @@ -548,6 +567,25 @@ def test_networks_list(site_client):
assert result.exit_code == 0
assert result.output == expected_output

# Now create 1 network w/ owner= w/ a space in the value
runner.run('networks add -c 10.0.0.0/16 -a owner="Jathan McCollum"')

# Test that you can query by values w/ spaces when properly quoted
result = runner.run('networks list -q \'owner="Jathan McCollum"\'')
expected_output = '10.0.0.0/16\n'
assert result.exit_code == 0
assert result.output == expected_output

# ... Or using backslashes works, too.
result = runner.run('networks list -q "owner=Jathan\ McCollum"')
assert result.exit_code == 0
assert result.output == expected_output

# Test that query with unbalanced quotes fails.
result = runner.run('networks list -q \'owner="Jathan McCollum\'')
assert result.exit_code == 1
assert 'No closing quotation' in result.output


def test_networks_subcommands(site_client):
"""Test supernets/subnets sub-commands."""
Expand Down

0 comments on commit d81c9ff

Please sign in to comment.