Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 16, 2024
1 parent 809b1a8 commit 0986b92
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 88 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
http://sphinx-doc.org/config.html
"""

import os
import sys

Expand Down
1 change: 1 addition & 0 deletions nailgun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
:doc:`/examples` before the documentation here.
"""

from logging import basicConfig

basicConfig()
1 change: 1 addition & 0 deletions nailgun/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
http://docs.python-requests.org/en/latest/api/#main-interface
"""

from json import dumps
import logging
from warnings import simplefilter
Expand Down
1 change: 1 addition & 0 deletions nailgun/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
presenting that information.
"""

import json
from os.path import isfile, join
from threading import Lock
Expand Down
5 changes: 3 additions & 2 deletions nailgun/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
workings of entity classes.
"""

from datetime import datetime
from functools import lru_cache
import hashlib
Expand Down Expand Up @@ -2931,7 +2932,7 @@ def read(self, entity=None, attrs=None, ignore=None, params=None):
if self._server_config:
entity._server_config = self._server_config
result = super().read(entity, attrs, ignore, params)
if 'content_view_components' in attrs and attrs['content_view_components']:
if attrs.get('content_view_components'):
result.content_view_component = [
ContentViewComponent(
server_config=self._server_config,
Expand Down Expand Up @@ -4775,7 +4776,7 @@ def read(self, entity=None, attrs=None, ignore=None, params=None):
)
else:
result.image = None
if 'interfaces' in attrs and attrs['interfaces']:
if attrs.get('interfaces'):
result.interface = [
Interface(
server_config=self._server_config,
Expand Down
1 change: 1 addition & 0 deletions nailgun/entity_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
unpleasant to work with manually.
"""

import random

from fauxfactory import (
Expand Down
1 change: 1 addition & 0 deletions nailgun/entity_mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Defines a set of mixins that provide tools for interacting with entities."""

import _thread as thread
from collections.abc import Iterable
import contextlib
Expand Down
1 change: 1 addition & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unit tests for :mod:`nailgun.client`."""

import inspect
from unittest import TestCase, mock

Expand Down
1 change: 1 addition & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unit tests for :mod:`nailgun.config`."""

import builtins
import json
from unittest import TestCase
Expand Down
144 changes: 86 additions & 58 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for :mod:`nailgun.entities`."""

from datetime import date, datetime
from http.client import ACCEPTED, NO_CONTENT
import inspect
Expand Down Expand Up @@ -1293,8 +1294,9 @@ def test_ignore_arg_v1(self):
),
):
with self.subTest(entity):
with mock.patch.object(EntityReadMixin, 'read') as read, mock.patch.object(
EntityReadMixin, 'read_json'
with (
mock.patch.object(EntityReadMixin, 'read') as read,
mock.patch.object(EntityReadMixin, 'read_json'),
):
with mock.patch.object(
entities,
Expand Down Expand Up @@ -1428,10 +1430,13 @@ def test_snapshot_ignore_arg(self):
Assert that entity`s predefined values of ``ignore`` are always
correctly passed on.
"""
with mock.patch.object(EntityReadMixin, 'read') as read, mock.patch.object(
EntityReadMixin,
'read_json',
return_value={'host': 3},
with (
mock.patch.object(EntityReadMixin, 'read') as read,
mock.patch.object(
EntityReadMixin,
'read_json',
return_value={'host': 3},
),
):
entities.Snapshot(self.cfg, id=2, host=3).read()
# `call_args` is a two-tuple of (positional, keyword) args.
Expand All @@ -1443,22 +1448,26 @@ def test_host_with_interface(self):
Assert that host will have interfaces initialized and assigned
correctly.
"""
with mock.patch.object(
EntityReadMixin,
'read',
return_value=entities.Host(self.cfg, id=2),
), mock.patch.object(
EntityReadMixin,
'read_json',
return_value={
'interfaces': [{'id': 2}, {'id': 3}],
'parameters': None,
'puppet_proxy': None,
},
), mock.patch.object(
entities,
'_feature_list',
return_value={'Puppet'},
with (
mock.patch.object(
EntityReadMixin,
'read',
return_value=entities.Host(self.cfg, id=2),
),
mock.patch.object(
EntityReadMixin,
'read_json',
return_value={
'interfaces': [{'id': 2}, {'id': 3}],
'parameters': None,
'puppet_proxy': None,
},
),
mock.patch.object(
entities,
'_feature_list',
return_value={'Puppet'},
),
):
host = entities.Host(self.cfg, id=2).read()
self.assertTrue(hasattr(host, 'interface'))
Expand Down Expand Up @@ -2419,14 +2428,17 @@ def test_content_upload_upload(self):
"""
filename = gen_string('alpha')
filepath = os.path.join(gen_string('alpha'), filename)
with mock.patch.object(
entities.ContentUpload,
'create',
) as create, mock.patch.object(
entities.Repository,
'import_uploads',
return_value={'status': 'success'},
) as import_uploads:
with (
mock.patch.object(
entities.ContentUpload,
'create',
) as create,
mock.patch.object(
entities.Repository,
'import_uploads',
return_value={'status': 'success'},
) as import_uploads,
):
mock_open = mock.mock_open(read_data=gen_string('alpha').encode('ascii'))
with mock.patch(_BUILTIN_OPEN, mock_open, create=True):
response = self.content_upload.upload(filepath, filename)
Expand All @@ -2443,14 +2455,17 @@ def test_content_upload_no_filename(self):
"""
filename = gen_string('alpha')
filepath = os.path.join(gen_string('alpha'), filename)
with mock.patch.object(
entities.ContentUpload,
'create',
) as create, mock.patch.object(
entities.Repository,
'import_uploads',
return_value={'status': 'success'},
) as import_uploads:
with (
mock.patch.object(
entities.ContentUpload,
'create',
) as create,
mock.patch.object(
entities.Repository,
'import_uploads',
return_value={'status': 'success'},
) as import_uploads,
):
mock_open = mock.mock_open(read_data=gen_string('alpha').encode('ascii'))
with mock.patch(_BUILTIN_OPEN, mock_open, create=True):
response = self.content_upload.upload(filepath)
Expand Down Expand Up @@ -3269,11 +3284,14 @@ def test_upload_content_v1(self):
:meth:`tests.test_entities.GenericTestCase.test_generic`.
"""
kwargs = {'kwarg': gen_integer()}
with mock.patch.object(client, 'post') as post, mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler:
with (
mock.patch.object(client, 'post') as post,
mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler,
):
response = self.repo.upload_content(**kwargs)
self.assertEqual(post.call_count, 1)
self.assertEqual(len(post.call_args[0]), 1)
Expand All @@ -3288,11 +3306,15 @@ def test_upload_content_v2(self):
the (mock) server fails to return a "success" status.
"""
kwargs = {'kwarg': gen_integer()}
with mock.patch.object(client, 'post') as post, mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'failure'},
) as handler, self.assertRaises(entities.APIResponseError):
with (
mock.patch.object(client, 'post') as post,
mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'failure'},
) as handler,
self.assertRaises(entities.APIResponseError),
):
self.repo.upload_content(**kwargs)
self.assertEqual(post.call_count, 1)
self.assertEqual(len(post.call_args[0]), 1)
Expand All @@ -3315,11 +3337,14 @@ def test_import_uploads_uploads(self):
'checksum': gen_string('numeric'),
}
]
with mock.patch.object(client, 'put') as put, mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler:
with (
mock.patch.object(client, 'put') as put,
mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler,
):
response = self.repo.import_uploads(uploads=uploads, **kwargs)
self.assertEqual(put.call_count, 1)
self.assertEqual(len(put.call_args[0]), 2)
Expand All @@ -3336,11 +3361,14 @@ def test_import_uploads_upload_ids(self):
"""
kwargs = {'kwarg': gen_integer()}
upload_ids = [gen_string('numeric')]
with mock.patch.object(client, 'put') as put, mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler:
with (
mock.patch.object(client, 'put') as put,
mock.patch.object(
entities,
'_handle_response',
return_value={'status': 'success'},
) as handler,
):
response = self.repo.import_uploads(upload_ids=upload_ids, **kwargs)
self.assertEqual(put.call_count, 1)
self.assertEqual(len(put.call_args[0]), 2)
Expand Down
1 change: 1 addition & 0 deletions tests/test_entity_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unit tests for :mod:`nailgun.entity_fields`."""

import datetime
from random import randint
import socket
Expand Down
Loading

0 comments on commit 0986b92

Please sign in to comment.