Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fbef9e

Browse files
Add unittesting
1 parent 1be2ee0 commit 1fbef9e

File tree

5 files changed

+125
-17
lines changed

5 files changed

+125
-17
lines changed

‎.vscode/settings.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"python.linting.pylintEnabled": false,
44
"python.pythonPath": "C:\\Users\\Alex\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
55
"python.linting.flake8Enabled": true,
6-
"python.linting.flake8Args": ["--max-line-length=140", "--ignore=E402"]
6+
"python.linting.flake8Args": ["--max-line-length=140", "--ignore=E402"],
7+
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "test_*.py"],
8+
"python.testing.pytestEnabled": false,
9+
"python.testing.nosetestsEnabled": false,
10+
"python.testing.unittestEnabled": true
711
}

‎ms_graph/workbooks.py‎ renamed to ‎ms_graph/_workbooks.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ def __init__(self, session: object) -> None:
2828

2929
# Set the endpoint.
3030
self.endpoint = 'workbook'
31+
32+
def create_session(self) -> Dict:
33+
pass

‎ms_graph/session.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import json as json_lib
12
import requests
3+
import logging
24

3-
from pprint import pprint
45
from typing import Dict
56

67

@@ -28,7 +29,16 @@ def __init__(self, client: object) -> None:
2829

2930
from ms_graph.client import MicrosoftGraphClient
3031

32+
# We can also add custom formatting to our log messages.
33+
log_format = '%(asctime)-15s|%(filename)s|%(message)s'
34+
3135
self.client: MicrosoftGraphClient = client
36+
logging.basicConfig(
37+
filename="logs/log_file_custom.log",
38+
level=logging.INFO,
39+
encoding="utf-8",
40+
format=log_format
41+
)
3242

3343
def build_headers(self, mode: str = 'json') -> Dict:
3444
"""Used to build the headers needed to make the request.
@@ -106,7 +116,9 @@ def make_request(self, method: str, endpoint: str, mode: str = None, params: dic
106116
# Define the headers.
107117
headers = self.build_headers(mode='json')
108118

109-
print(url)
119+
logging.info(
120+
"URL: {url}".format(url=url)
121+
)
110122

111123
# Define a new session.
112124
request_session = requests.Session()
@@ -139,5 +151,19 @@ def make_request(self, method: str, endpoint: str, mode: str = None, params: dic
139151
'status_code': response.status_code
140152
}
141153
elif not response.ok:
142-
pprint(response.json())
154+
155+
# Define the error dict.
156+
error_dict = {
157+
'error_code': response.status_code,
158+
'response_url': response.url,
159+
'response_body': json_lib.loads(response.content.decode('ascii')),
160+
'response_request': dict(response.request.headers),
161+
'response_method': response.request.method,
162+
}
163+
164+
# Log the error.
165+
logging.error(
166+
msg=json_lib.dumps(obj=error_dict, indent=4)
167+
)
168+
143169
raise requests.HTTPError()

‎samples/use_client.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
'Directory.ReadWrite.All',
1111
'User.Read.All',
1212
'Directory.Read.All',
13-
'Directory.ReadWrite.All',
14-
# 'offline_access',
15-
# 'openid',
16-
# 'profile'
13+
'Directory.ReadWrite.All'
1714
]
1815

1916
# Initialize the Parser.

‎tests/test_client.py‎

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,109 @@
22

33
from unittest import TestCase
44
from configparser import ConfigParser
5+
from ms_graph.client import MicrosoftGraphClient
56

7+
from ms_graph.mail import Mail
8+
from ms_graph.notes import Notes
9+
from ms_graph.users import Users
10+
from ms_graph.search import Search
11+
from ms_graph.drives import Drives
12+
from ms_graph.groups import Groups
13+
from ms_graph.session import GraphSession
14+
from ms_graph.drive_items import DriveItems
15+
from ms_graph.personal_contacts import PersonalContacts
616

7-
class MySession(TestCase):
817

9-
"""Will perform a unit test for the <PLACEHOLDER> session."""
18+
class MicrosoftGraphSessionTest(TestCase):
19+
20+
"""Will perform a unit test for the `MicrosoftGraphClient` session."""
1021

1122
def setUp(self) -> None:
12-
"""Set up the <PLACEHOLDER> Client."""
23+
"""Set up the `MicrosoftGraphClient` Client."""
24+
25+
scopes = [
26+
'Calendars.ReadWrite',
27+
'Files.ReadWrite.All',
28+
'User.ReadWrite.All',
29+
'Notes.ReadWrite.All',
30+
'Directory.ReadWrite.All',
31+
'User.Read.All',
32+
'Directory.Read.All',
33+
'Directory.ReadWrite.All'
34+
]
1335

1436
# Initialize the Parser.
1537
config = ConfigParser()
1638

1739
# Read the file.
18-
config.read('configs/config.ini')
40+
config.read('config/config.ini')
1941

2042
# Get the specified credentials.
21-
config.get('main', '')
43+
client_id = config.get('graph_api', 'client_id')
44+
client_secret = config.get('graph_api', 'client_secret')
45+
redirect_uri = config.get('graph_api', 'redirect_uri')
46+
47+
# Initialize the Client.
48+
graph_client = MicrosoftGraphClient(
49+
client_id=client_id,
50+
client_secret=client_secret,
51+
redirect_uri=redirect_uri,
52+
scope=scopes,
53+
credentials='config/ms_graph_state.jsonc'
54+
)
55+
56+
self.client = graph_client
2257

2358
def test_creates_instance_of_session(self):
24-
"""Create an instance and make sure it's a <PLACEHOLDER>."""
25-
pass
59+
"""Create an instance and make sure it's a `MicrosoftGraphClient`."""
60+
61+
self.assertIsInstance(self.client, MicrosoftGraphClient)
62+
63+
def test_creates_instance_of_mail(self):
64+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Mail`."""
65+
66+
self.assertIsInstance(self.client.mail(), Mail)
67+
68+
def test_creates_instance_of_drive_items(self):
69+
"""Create an instance and make sure it's a `MicrosoftGraphClient.DriveItems`."""
70+
71+
self.assertIsInstance(self.client.drive_item(), DriveItems)
72+
73+
def test_creates_instance_of_drives(self):
74+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Drives`."""
75+
76+
self.assertIsInstance(self.client.drives(), Drives)
77+
78+
def test_creates_instance_of_users(self):
79+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Users`."""
80+
81+
self.assertIsInstance(self.client.users(), Users)
82+
83+
def test_creates_instance_of_groups(self):
84+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Groups`."""
85+
86+
self.assertIsInstance(self.client.groups(), Groups)
87+
88+
def test_creates_instance_of_notes(self):
89+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Notes`."""
90+
91+
self.assertIsInstance(self.client.notes(), Notes)
92+
93+
def test_creates_instance_of_search(self):
94+
"""Create an instance and make sure it's a `MicrosoftGraphClient.Search`."""
95+
96+
self.assertIsInstance(self.client.search(), Search)
97+
98+
def test_creates_instance_of_personal_contacts(self):
99+
"""Create an instance and make sure it's a `MicrosoftGraphClient.PersonalContacts`."""
100+
101+
self.assertIsInstance(
102+
self.client.personal_contacts(), PersonalContacts)
26103

27104
def tearDown(self) -> None:
28-
"""Teardown the <PLACEHOLDER> Client."""
29-
pass
105+
"""Teardown the `MicrosoftGraphClient` Client."""
106+
107+
del self.client
30108

31109

32110
if __name__ == '__main__':

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /