Skip to content

Commit

Permalink
Merge pull request #365 from mhjacks/test_fixlint
Browse files Browse the repository at this point in the history
Run black locally and validate that tox fix works
  • Loading branch information
mhjacks committed May 22, 2024
2 parents 838f7ce + 6ba8fd2 commit 4d532ba
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 64 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ on: [workflow_call] # allow this workflow to be called from other workflows
jobs:
linters:
uses: ./.github/workflows/tox.yml
env:
source_directory: "./source"
with:
envname: ""
labelname: "lint"
checkout_path: ${{ env.source_directory }}
checkout_ref: ${{ github.event.pull_request.head.sha }}
checkout_ref: ${{ github.event.pull_request.head.sha }}
checkout_fetch_depth: "0"
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
description: Path to pass to checkout action
required: false
type: string
default: ${{ env.source_directory }}
default: ""
checkout_ref:
description: Repository reference to pass to checkout action
required: false
Expand Down
56 changes: 44 additions & 12 deletions plugins/module_utils/service_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ def sys_id(self):


class Catalog(ServiceCatalogObject):
DISPLAY_FIELDS = ["sys_id", "description", "title",
"has_categories", "has_items", "categories", "sn_items"]
DISPLAY_FIELDS = [
"sys_id",
"description",
"title",
"has_categories",
"has_items",
"categories",
"sn_items",
]

def __init__(self, data=None):
if not data:
Expand Down Expand Up @@ -85,8 +92,13 @@ def to_ansible(self):


class Category(ServiceCatalogObject):
DISPLAY_FIELDS = ["sys_id", "description", "title",
"full_description", "subcategories"]
DISPLAY_FIELDS = [
"sys_id",
"description",
"title",
"full_description",
"subcategories",
]

def __init__(self, data=None):
if not data:
Expand All @@ -96,10 +108,22 @@ def __init__(self, data=None):


class Item(ServiceCatalogObject):
DISPLAY_FIELDS = ["sys_id", "short_description", "description",
"availability", "mandatory_attachment", "request_method",
"type", "sys_class_name", "catalogs", "name", "category", "order",
"categories", "variables"]
DISPLAY_FIELDS = [
"sys_id",
"short_description",
"description",
"availability",
"mandatory_attachment",
"request_method",
"type",
"sys_class_name",
"catalogs",
"name",
"category",
"order",
"categories",
"variables",
]

def __init__(self, data=None):
if not data:
Expand Down Expand Up @@ -127,7 +151,9 @@ def get_catalog(self, id):
"""Returns the catalog identified by id"""
if not id:
raise ValueError("catalog sys_id is missing")
record = self.generic_client.get_record_by_sys_id("/".join([SN_BASE_PATH, "catalogs"]), id)
record = self.generic_client.get_record_by_sys_id(
"/".join([SN_BASE_PATH, "catalogs"]), id
)
if record:
return Catalog(record)
return None
Expand All @@ -137,7 +163,8 @@ def get_categories(self, catalog_id):
if not id:
raise ValueError("catalog sys_id is missing")
records = self.generic_client.list_records(
"/".join([SN_BASE_PATH, "catalogs", catalog_id, "categories"]))
"/".join([SN_BASE_PATH, "catalogs", catalog_id, "categories"])
)
if records:
return [Category(record) for record in records]
return []
Expand All @@ -151,12 +178,17 @@ def get_items(self, catalog_id, query=None, batch_size=1000):
_query.update(query)
self.generic_client.batch_size = batch_size
records = self.generic_client.list_records(
"/".join([SN_BASE_PATH, "items"]), _query)
"/".join([SN_BASE_PATH, "items"]), _query
)
if records:
return [Item(record) for record in records]
return []

def get_item(self, id):
if not id:
raise ValueError("item sys_id is missing")
return Item(self.generic_client.get_record_by_sys_id("/".join([SN_BASE_PATH, "items"]), id))
return Item(
self.generic_client.get_record_by_sys_id(
"/".join([SN_BASE_PATH, "items"]), id
)
)
6 changes: 3 additions & 3 deletions plugins/modules/service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_catalog_info(sc_client, catalog, with_categories, items_config):
def run(module, sc_client):
items_config = dict(
info=ItemContent.from_str(module.params["items_info"]),
query=module.params["items_query"]
query=module.params["items_query"],
)

fetch_categories = module.params["categories"]
Expand All @@ -182,7 +182,7 @@ def run(module, sc_client):
sc_client,
sc_client.get_catalog(module.params["sys_id"]),
fetch_categories,
items_config
items_config,
)
return [catalog.to_ansible()]

Expand Down Expand Up @@ -210,7 +210,7 @@ def main():
choices=["brief", "full", "none"],
default="none",
),
items_query=dict(type="str")
items_query=dict(type="str"),
)

module = AnsibleModule(
Expand Down
74 changes: 40 additions & 34 deletions tests/unit/plugins/module_utils/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,44 +179,50 @@ def test_duplicate(self, tmp_path):

class TestAttachmentAreChanged:
def test_unchanged(self):
assert attachment.are_changed(
[
{"hash": "hash", "file_name": "attachment_name.txt"},
{"hash": "hash", "file_name": "another_file_name.txt"},
],
{
"attachment_name.txt": {
"path": "some/path/file_name.txt",
"type": "text/markdown",
"hash": "hash",
},
"another_file_name.txt": {
"path": "some/path/another_file_name.txt",
"type": "text/plain",
"hash": "hash",
assert (
attachment.are_changed(
[
{"hash": "hash", "file_name": "attachment_name.txt"},
{"hash": "hash", "file_name": "another_file_name.txt"},
],
{
"attachment_name.txt": {
"path": "some/path/file_name.txt",
"type": "text/markdown",
"hash": "hash",
},
"another_file_name.txt": {
"path": "some/path/another_file_name.txt",
"type": "text/plain",
"hash": "hash",
},
},
},
) == [False, False]
)
== [False, False]
)

def test_changed(self):
assert attachment.are_changed(
[
{"hash": "oldhash", "file_name": "attachment_name.txt"},
{"hash": "oldhash", "file_name": "another_file_name.txt"},
],
{
"attachment_name.txt": {
"path": "some/path/file_name.txt",
"type": "text/markdown",
"hash": "hash",
},
"another_file_name.txt": {
"path": "some/path/another_file_name.txt",
"type": "text/plain",
"hash": "hash",
assert (
attachment.are_changed(
[
{"hash": "oldhash", "file_name": "attachment_name.txt"},
{"hash": "oldhash", "file_name": "another_file_name.txt"},
],
{
"attachment_name.txt": {
"path": "some/path/file_name.txt",
"type": "text/markdown",
"hash": "hash",
},
"another_file_name.txt": {
"path": "some/path/another_file_name.txt",
"type": "text/plain",
"hash": "hash",
},
},
},
) == [True, True]
)
== [True, True]
)


class TestAttachmentListRecords:
Expand Down
37 changes: 24 additions & 13 deletions tests/unit/plugins/modules/test_service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import sys

import pytest
from ansible_collections.servicenow.itsm.plugins.module_utils import (
service_catalog
)
from ansible_collections.servicenow.itsm.plugins.module_utils import service_catalog
from ansible_collections.servicenow.itsm.plugins.modules import (
service_catalog_info,
)
Expand All @@ -33,11 +31,15 @@ def list_records(self, api, query=None):
self.call_params.append(query)
if "categories" in api:
if not isinstance(self.retured_data, tuple):
raise ValueError("expected tuple in retured_data when looking for categories")
raise ValueError(
"expected tuple in retured_data when looking for categories"
)
return self.retured_data[1]
if "items" in api:
if not isinstance(self.retured_data, tuple):
raise ValueError("expected tuple in retured_data when looking for categories")
raise ValueError(
"expected tuple in retured_data when looking for categories"
)
return self.retured_data[2]
if isinstance(self.retured_data, tuple):
return self.retured_data[0]
Expand All @@ -59,7 +61,7 @@ def test_get_without_categories(self, create_module):
),
categories=False,
items_info="none",
items_query=None
items_query=None,
)
)

Expand All @@ -85,7 +87,7 @@ def test_get_by_sys_id(self, create_module):
sys_id="catalog_sys_id",
categories=False,
items_info="none",
items_query=None
items_query=None,
)
)

Expand All @@ -110,7 +112,7 @@ def test_get_with_categories(self, create_module):
),
categories=True,
items_info="none",
items_query=None
items_query=None,
)
)

Expand All @@ -123,11 +125,16 @@ def test_get_with_categories(self, create_module):
)
category = dict(sys_id="category_sys_id")

records = service_catalog_info.run(module, self.get_sc_client(([catalog], [category])))
records = service_catalog_info.run(
module, self.get_sc_client(([catalog], [category]))
)

assert len(records) == 1
assert len(records[0]["categories"]) == 1
assert records[0]["categories"][0] == service_catalog.Category(category).to_ansible()
assert (
records[0]["categories"][0]
== service_catalog.Category(category).to_ansible()
)
assert records[0]["sys_id"] == "1"

def test_get_with_categories_and_items(self, create_module):
Expand All @@ -138,7 +145,7 @@ def test_get_with_categories_and_items(self, create_module):
),
categories=True,
items_info="brief",
items_query=None
items_query=None,
)
)

Expand All @@ -153,11 +160,15 @@ def test_get_with_categories_and_items(self, create_module):
item = dict(sys_id="item_sys_id")

records = service_catalog_info.run(
module, self.get_sc_client(([catalog], [category], [item])))
module, self.get_sc_client(([catalog], [category], [item]))
)

assert len(records) == 1
assert len(records[0]["categories"]) == 1
assert len(records[0]["sn_items"]) == 1
assert records[0]["categories"][0] == service_catalog.Category(category).to_ansible()
assert (
records[0]["categories"][0]
== service_catalog.Category(category).to_ansible()
)
assert records[0]["sn_items"][0] == service_catalog.Category(item).to_ansible()
assert records[0]["sys_id"] == "1"

0 comments on commit 4d532ba

Please sign in to comment.