Skip to content

Commit c0f87c6

Browse files
committed
fixes - Fix SonarCube smells and bugs
1 parent bae6ea9 commit c0f87c6

6 files changed

Lines changed: 36 additions & 29 deletions

File tree

codeinsight_sdk/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def __init__(self, response: requests.Response):
2323
super().__init__("Error: %s - %s" % (self.code, self.message))
2424

2525
except KeyError:
26-
raise Exception(f"Error parsing response: {resp}")
26+
raise ValueError(f"Error parsing response: {resp}")
2727
except json.decoder.JSONDecodeError:
28-
raise Exception(f"Error decoding response: {resp}")
28+
raise ValueError(f"Error decoding response: {resp}")

codeinsight_sdk/handlers.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def create(client, cls):
1717
}
1818
handler = handlers.get(k)
1919
if handler is None:
20-
raise Exception(f"Handler not found for class '{k}'")
20+
raise ValueError(f"Handler not found for class '{k}'")
2121
return handler(client, cls)
2222

2323
@abc.abstractmethod
@@ -56,7 +56,7 @@ def get(self, id:int) -> Project:
5656
project_data = resp.json()['data']
5757
return self.cls.from_dict(project_data)
5858

59-
def get_id(self, projectName:str) -> int:
59+
def get_id(self, project_name:str) -> int:
6060
"""
6161
Retrieves the ID of a project based on its name.
6262
@@ -66,15 +66,14 @@ def get_id(self, projectName:str) -> int:
6666
Returns:
6767
int: The ID of the project.
6868
"""
69-
path = f"project/id"
70-
params = {"projectName": projectName}
69+
path = "project/id"
70+
params = {"projectName": project_name}
7171
resp = self.client.request("GET", url_part=path, params=params)
7272
try:
73-
projectId = resp.json()['Content: '] # Yes, the key is called 'Content: ' ...
73+
project_id = resp.json()['Content: '] # Yes, the key is called 'Content: ' ...
7474
except KeyError:
7575
raise CodeInsightError(resp)
76-
#raise Exception(f"Content key not found in response: {resp.json()}")
77-
return projectId
76+
return project_id
7877

7978
def get_inventory_summary(self, project_id:int) -> List[ProjectInventoryItem]:
8079
"""

tests/test_client.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,24 @@ def test_get_all_projects(self, client):
3434
projects = client.projects.all()
3535
assert len(projects) > 0
3636

37-
def test_get_project_id(self,client):
38-
projectName = "Test"
37+
def test_get_project_id(self, client):
38+
project_name = "Test"
3939
with requests_mock.Mocker() as m:
4040
m.get(f"{TEST_URL}/codeinsight/api/project/id", text='{ "Content: ": 1 }') # Yes, the key is called 'Content: ' ...
41-
project_id = client.projects.get_id(projectName)
41+
project_id = client.projects.get_id(project_name)
4242
assert project_id == 1
4343

4444
def test_get_project_id_invalid(self,client):
45-
projectName = "Invalid_Project"
45+
project_name = "Invalid_Project"
4646
fake_response_json = """{ "Arguments: " : ["",""],
4747
"Key: ": " InvalidProjectNameParm",
4848
"Error: ": "The project name entered was not found" }
4949
"""
5050
with requests_mock.Mocker() as m:
51-
# Note, the key names end with a colon and space '...: '
51+
# Note, the key names end with a colon and space '...: '
5252
m.get(f"{TEST_URL}/codeinsight/api/project/id", text=fake_response_json, status_code=400)
5353
with pytest.raises(CodeInsightError):
54-
project_id = client.projects.get_id(projectName)
55-
assert project_id != 1
54+
client.projects.get_id(project_name)
5655

5756
def test_get_project(self,client):
5857
project_id = 1

tests/test_handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pytest
22

33
from codeinsight_sdk import CodeInsightClient
4-
from codeinsight_sdk.handlers import *
5-
from codeinsight_sdk.models import *
4+
from codeinsight_sdk.handlers import Handler, ProjectHandler, ReportHandler
5+
from codeinsight_sdk.models import Project, Report
66

77
class TestHandlers(object):
88
@pytest.fixture

tests/test_models.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ def project(self):
1010
def test_project(self, project):
1111
assert project.id == 1
1212
assert project.name == "Test"
13+
assert isinstance(project, Project)
1314

14-
15-
@pytest.mark.skip(reason="Not implemented")
1615
class TestReport(object):
1716
@pytest.fixture
1817
def report(self):
19-
return Report(id=1)
18+
return Report(id=1,
19+
name="Test",
20+
path="path/to/report",
21+
default=True,
22+
enabled=True,
23+
enableProjectPicker=True,
24+
order=1,
25+
createdDateTime="Today",
26+
updatedDateTime="Tomorrow")
2027

2128
def test_report(self, report):
2229
assert report.id == 1
30+
assert report.enabled == True
31+
assert isinstance(report, Report)

tests/test_not_implemented.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ def client(self):
1111
## Coming soon features ##
1212
def test_inventories(self, client):
1313
with pytest.raises(NotImplementedError):
14-
client.inventories() != None
14+
client.inventories()
1515

1616
def test_vulnerabilities(self, client):
1717
with pytest.raises(NotImplementedError):
18-
client.vulnerabilites() != None
18+
client.vulnerabilites()
1919

2020
def test_users(self, client):
2121
with pytest.raises(NotImplementedError):
22-
client.users() != None
22+
client.users()
2323

2424
def test_licenses(self, client):
2525
with pytest.raises(NotImplementedError):
26-
client.licenses() != None
26+
client.licenses()
2727

2828
def test_tasks(self, client):
2929
with pytest.raises(NotImplementedError):
30-
client.tasks() != None
30+
client.tasks()
3131

3232
def test_rules(self, client):
3333
with pytest.raises(NotImplementedError):
34-
client.rules() != None
34+
client.rules()
3535

3636
def test_files(self, client):
3737
with pytest.raises(NotImplementedError):
38-
client.files() != None
38+
client.files()
3939

4040
def test_folders(self, client):
4141
with pytest.raises(NotImplementedError):
42-
client.folders() != None
42+
client.folders()

0 commit comments

Comments
 (0)