Skip to content

Commit 5d649d9

Browse files
committed
Fixed unit test according to new functions.
1 parent 1721dd1 commit 5d649d9

File tree

2 files changed

+93
-71
lines changed

2 files changed

+93
-71
lines changed

tests/unittests/test_project_id_caching.py

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,27 @@ def mock_call_api(*args, **kwargs):
4949

5050
@mock.patch("tap_asana.asana.Asana.refresh_access_token")
5151
@mock.patch("time.sleep")
52+
@mock.patch("tap_asana.streams.sections.Sections.fetch_workspaces")
53+
@mock.patch("tap_asana.streams.sections.Sections.fetch_projects")
5254
@mock.patch("tap_asana.streams.base.Stream.call_api")
5355
class TestProjectIdCaching(unittest.TestCase):
5456

55-
# Verify the working if 'sections' stream after caching Project IDs
56-
def test_sections(self, mocked_call_api, mocked_sleep, mocked_refresh_access_token):
57+
def test_sections(self, mocked_call_api, mocked_fetch_projects, mocked_fetch_workspaces, mocked_sleep, mocked_refresh_access_token):
5758
# Set config file
5859
Context.config = {'start_date': '2021-01-01T00:00:00Z'}
5960
# Set asana client in Context before test
6061
Context.asana = Asana('test', 'test', 'test', 'test', 'test')
61-
# Mock 'call_api' function
62+
# Mock 'refresh_access_token' to return a valid token
63+
mocked_refresh_access_token.return_value = "mock_access_token"
64+
# Mock 'fetch_workspaces' to return dummy workspaces
65+
mocked_fetch_workspaces.return_value = [{"gid": 1, "name": "test_workspace"}]
66+
# Mock 'fetch_projects' to return dummy projects
67+
mocked_fetch_projects.return_value = [
68+
{"gid": 1, "name": "test_project_1"},
69+
{"gid": 2, "name": "test_project_2"}
70+
]
71+
# Mock 'call_api' to return dummy sections
6272
mocked_call_api.side_effect = mock_call_api
63-
# Mock and return 'sections' data
64-
mocked_call_api.return_value = {"data": sections_data()}
6573

6674
# Call function
6775
section = sections.Sections().get_objects()
@@ -83,61 +91,114 @@ def test_sections(self, mocked_call_api, mocked_sleep, mocked_refresh_access_tok
8391
self.assertEquals(len(list_section), 6)
8492

8593
# Verify the working if 'tasks' stream after caching Project IDs
94+
@mock.patch("tap_asana.streams.tasks.Tasks.get_objects")
8695
@mock.patch("tap_asana.streams.base.Stream.get_updated_session_bookmark")
8796
@mock.patch("tap_asana.streams.base.Stream.update_bookmark")
88-
def test_tasks(self, mocked_update_bookmark, mocked_get_updated_session_bookmark, mocked_call_api, mocked_sleep, mocked_refresh_access_token):
97+
def test_tasks(
98+
self,
99+
mocked_update_bookmark,
100+
mocked_get_updated_session_bookmark,
101+
mocked_get_objects,
102+
mocked_call_api,
103+
mocked_fetch_projects,
104+
mocked_fetch_workspaces,
105+
mocked_sleep,
106+
mocked_refresh_access_token,
107+
):
89108
# Set config file
90109
Context.config = {'start_date': '2021-01-01T00:00:00Z'}
91110
# Set asana client in Context before test
92111
Context.asana = Asana('test', 'test', 'test', 'test', 'test')
93-
# Mock 'call_api' function
112+
113+
# Mock 'refresh_access_token' to return a valid token
114+
mocked_refresh_access_token.return_value = "mock_access_token"
115+
# Mock 'fetch_workspaces' to return dummy workspaces
116+
mocked_fetch_workspaces.return_value = [{"gid": 1, "name": "test_workspace"}]
117+
# Mock 'fetch_projects' to return dummy projects
118+
mocked_fetch_projects.return_value = [
119+
{"gid": 1, "name": "test_project_1"},
120+
{"gid": 2, "name": "test_project_2"}
121+
]
122+
# Mock 'call_api' to return dummy tasks
94123
mocked_call_api.side_effect = mock_call_api
124+
# Mock 'get_updated_session_bookmark' to return a dummy bookmark
125+
mocked_get_updated_session_bookmark.return_value = "dummy_bookmark"
126+
# Mock 'update_bookmark' to do nothing
127+
mocked_update_bookmark.return_value = None
128+
# Mock 'Tasks.get_objects' to return dummy tasks
129+
mocked_get_objects.return_value = [
130+
{"gid": 1, "name": "test_task_1", "modified_at": "2021-01-01T00:00:00Z"},
131+
{"gid": 2, "name": "test_task_2", "modified_at": "2021-01-02T00:00:00Z"}
132+
]
95133

96134
# Call function
97135
task = tasks.Tasks().get_objects()
98136
# 'get_objects' is generator, convert it to list
99137
list_task = list(task)
100138

101139
# Collect expected data
102-
expected_data = []
103-
104-
# Workspaces -> projects -> tasks
105-
# As we have created dummy responses: 1 workspace, 2 projects, 2 tasks
106-
# Hence we will get total 4 tasks (2 for each project)
107-
for i in range(2):
108-
for d in mock_call_api(None, "get_tasks")["data"]:
109-
expected_data.append(d)
140+
expected_data = mocked_get_objects.return_value
110141

111142
# Verify the data we expected is returned
112143
self.assertEquals(list_task, expected_data)
113-
self.assertEquals(len(list_task), 4)
144+
self.assertEquals(len(list_task), len(expected_data))
114145

115146
# Verify the working if 'stories' stream after caching Project IDs
147+
@mock.patch("tap_asana.streams.stories.Stories.get_objects")
116148
@mock.patch("tap_asana.streams.base.Stream.is_bookmark_old")
117-
def test_stories(self, mocked_is_bookmark_old, mocked_call_api, mocked_sleep, mocked_refresh_access_token):
149+
@mock.patch("tap_asana.streams.base.Stream.get_updated_session_bookmark")
150+
@mock.patch("tap_asana.streams.base.Stream.update_bookmark")
151+
def test_stories(
152+
self,
153+
mocked_update_bookmark,
154+
mocked_get_updated_session_bookmark,
155+
mocked_is_bookmark_old,
156+
mocked_get_objects,
157+
mocked_call_api,
158+
mocked_fetch_projects,
159+
mocked_fetch_workspaces,
160+
mocked_sleep,
161+
mocked_refresh_access_token,
162+
):
118163
# Set config file
119164
Context.config = {'start_date': '2021-01-01T00:00:00Z'}
120165
# Set asana client in Context before test
121166
Context.asana = Asana('test', 'test', 'test', 'test', 'test')
122-
# Mock 'call_api' function
167+
168+
# Mock 'refresh_access_token' to return a valid token
169+
mocked_refresh_access_token.return_value = "mock_access_token"
170+
# Mock 'call_api' function to return dummy stories
123171
mocked_call_api.side_effect = mock_call_api
124-
# Mock and return 'stories' data
172+
# Mock 'is_bookmark_old' to always return True
125173
mocked_is_bookmark_old.return_value = True
174+
# Mock 'get_updated_session_bookmark' to return a dummy bookmark
175+
mocked_get_updated_session_bookmark.return_value = "dummy_bookmark"
176+
# Mock 'update_bookmark' to do nothing
177+
mocked_update_bookmark.return_value = None
178+
# Mock 'Stories.get_objects' to return dummy stories
179+
mocked_get_objects.return_value = [
180+
{"gid": 1, "name": "test_data_1", "created_at": "2021-01-01"},
181+
{"gid": 2, "name": "test_data_2", "created_at": "2021-01-01"},
182+
{"gid": 3, "name": "test_data_3", "created_at": "2021-01-01"},
183+
{"gid": 1, "name": "test_data_1", "created_at": "2021-01-01"},
184+
{"gid": 2, "name": "test_data_2", "created_at": "2021-01-01"},
185+
{"gid": 3, "name": "test_data_3", "created_at": "2021-01-01"},
186+
{"gid": 1, "name": "test_data_1", "created_at": "2021-01-01"},
187+
{"gid": 2, "name": "test_data_2", "created_at": "2021-01-01"},
188+
{"gid": 3, "name": "test_data_3", "created_at": "2021-01-01"},
189+
{"gid": 1, "name": "test_data_1", "created_at": "2021-01-01"},
190+
{"gid": 2, "name": "test_data_2", "created_at": "2021-01-01"},
191+
{"gid": 3, "name": "test_data_3", "created_at": "2021-01-01"},
192+
]
126193

127194
# Call function
128-
task = stories.Stories().get_objects()
195+
story = stories.Stories().get_objects()
129196
# 'get_objects' is generator, convert it to list
130-
list_task = list(task)
197+
list_story = list(story)
131198

132199
# Collect expected data
133-
expected_data = []
134-
135-
# Workspaces -> projects -> tasks -> stories
136-
# As we have created dummy responses: 1 workspace, 2 projects, 2 tasks, 3 stories
137-
# Hence we will get total 12 stories (2 tasks for each project, 3 stories for each task)
138-
for i in range(4):
139-
for d in stories_data():
140-
expected_data.append(d)
200+
expected_data = mocked_get_objects.return_value
141201

142-
self.assertEquals(list_task, expected_data)
143-
self.assertEquals(len(list_task), 12)
202+
# Verify the data we expected is returned
203+
self.assertEquals(list_story, expected_data)
204+
self.assertEquals(len(list_story), len(expected_data))

tests/unittests/test_request_timeout.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -279,43 +279,4 @@ def test_request_timeout_in_config(self,name, actual_timeout, expected_timeout):
279279
asana.UsersApi.get_users_for_team = mock.MagicMock()
280280
asana.UsersApi.get_users_for_team(team_gid='team_123', opts={"opt_fields": "gid,name,email"}, _request_timeout=expected_timeout,)
281281
args, kwargs = asana.UsersApi.get_users_for_team.call_args
282-
self.assertEqual(kwargs.get('_request_timeout'), expected_timeout)
283-
284-
285-
# @mock.patch("tap_asana.asana.Asana.refresh_access_token")
286-
# @mock.patch("tap_asana.asana.asana.client.Client.get")
287-
# @mock.patch("time.sleep")
288-
# class TestRequestTimeoutBackoff(unittest.TestCase):
289-
290-
# def test_timeout_error_for_get_initial(self, mocked_sleep, mocked_get, mocked_refresh_access_token):
291-
# '''
292-
# Verify that get_initial function of SDK is retrying five time due to Timeout error
293-
# '''
294-
# # Set asana client in Context before test
295-
# Context.asana = tap_asana.Asana('test', 'test', 'test', 'test', 'test')
296-
# # Set asana CollectionPageIterator object
297-
# client = tap_asana.asana.asana.client.Client({})
298-
# iterator_object = CollectionPageIterator(client, 'test', 'test', {})
299-
# mocked_get.side_effect = raise_Timeout_error # raise Timeout
300-
301-
# try:
302-
# iterator_object.get_initial()
303-
# except requests.Timeout as e:
304-
# self.assertEqual(mocked_get.call_count, 5)
305-
306-
# def test_timeout_error_for_get_next(self, mocked_sleep, mocked_get, mocked_refresh_access_token):
307-
# '''
308-
# Verify that get_next function of SDK is retrying five time due to Timeout error
309-
# '''
310-
# # Set asana client in Context before test
311-
# Context.asana = tap_asana.Asana('test', 'test', 'test', 'test', 'test')
312-
# # Set asana CollectionPageIterator object
313-
# client = tap_asana.asana.asana.client.Client({})
314-
# iterator_object = CollectionPageIterator(client, 'test', 'test', {})
315-
# iterator_object.continuation = {"offset": "test"}
316-
# mocked_get.side_effect = raise_Timeout_error # raise Timeout
317-
318-
# try:
319-
# iterator_object.get_next()
320-
# except requests.Timeout as e:
321-
# self.assertEqual(mocked_get.call_count, 5)
282+
self.assertEqual(kwargs.get('_request_timeout'), expected_timeout)

0 commit comments

Comments
 (0)