1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) #
# and others #
# #
# All rights reserved. This program and the accompanying materials #
# are made available under the terms of the Apache License, Version 2.0 #
# which accompanies this distribution, and is available at #
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
from discover.fetchers.db.db_access import DbAccess
from test.fetch.test_fetch import TestFetch
from test.fetch.db_fetch.test_data.db_access import *
from unittest.mock import MagicMock, patch
from test.fetch.db_fetch.mock_cursor import MockCursor
class TestDbAccess(TestFetch):
def setUp(self):
super().setUp()
self.configure_environment()
self.fetcher = DbAccess()
@patch("mysql.connector.connect")
def test_db_connect(self, db_connect):
DbAccess.conn = None
db_conn = MagicMock()
db_conn.ping = MagicMock()
db_connect.return_value = db_conn
self.fetcher.db_connect(DB_CONFIG['host'], DB_CONFIG['port'],
DB_CONFIG['user'], DB_CONFIG['pwd'],
DB_CONFIG['schema'])
self.assertEqual(True, db_connect.called, "connect method has't been called")
db_conn.ping.assert_called_once_with(True)
def test_connect_to_db(self):
DbAccess.conn = None
self.fetcher.db_connect = MagicMock()
self.fetcher.connect_to_db()
self.assertEqual(True, self.fetcher.db_connect.called)
def test_connect_to_db_with_force(self):
DbAccess.conn = MagicMock()
self.fetcher.db_connect = MagicMock()
self.fetcher.connect_to_db(force=True)
self.assertEqual(True, self.fetcher.db_connect.called)
def test_connect_to_db_without_force(self):
DbAccess.conn = MagicMock()
self.fetcher.db_connect = MagicMock()
self.fetcher.connect_to_db()
self.assertEqual(False, self.fetcher.db_connect.called)
def test_get_objects_list_for_id_with_id(self):
# mock the db cursor
mock_cursor = MockCursor(OBJECTS_LIST)
mock_cursor.execute = MagicMock()
self.fetcher.connect_to_db = MagicMock()
DbAccess.conn.cursor = MagicMock(return_value=mock_cursor)
result = self.fetcher.get_objects_list_for_id(QUERY_WITH_ID, OBJECT_TYPE, ID)
mock_cursor.execute.assert_called_once_with(QUERY_WITH_ID, [ID])
self.assertEqual(result, OBJECTS_LIST, "Can't get objects list")
def test_get_objects_list_for_id_without_id(self):
mock_cursor = MockCursor(OBJECTS_LIST)
self.fetcher.connect_to_db = MagicMock()
DbAccess.conn.cursor = MagicMock(return_value=mock_cursor)
mock_cursor.execute = MagicMock()
result = self.fetcher.get_objects_list_for_id(QUERY_WITHOUT_ID, OBJECT_TYPE, None)
mock_cursor.execute.assert_called_once_with(QUERY_WITHOUT_ID)
self.assertEqual(result, OBJECTS_LIST, "Can't get objects list")
def test_get_objects_list_for_id_with_id_with_exception(self):
mock_cursor = MockCursor(OBJECTS_LIST)
self.fetcher.connect_to_db = MagicMock()
# mock exception
DbAccess.conn.cursor = MagicMock(return_value=mock_cursor)
mock_cursor.execute = MagicMock(side_effect=[AttributeError, ""])
result = self.fetcher.get_objects_list_for_id(QUERY_WITH_ID, OBJECT_TYPE, ID)
self.assertEqual(mock_cursor.execute.call_count, 2, "Can't invoke execute method " +
"twice when error occurs")
self.assertEqual(result, OBJECTS_LIST, "Can't get objects list")
def test_get_objects_list_for_id_without_id_with_exception(self):
mock_cursor = MockCursor(OBJECTS_LIST)
self.fetcher.connect_to_db = MagicMock()
DbAccess.conn.cursor = MagicMock(return_value=mock_cursor)
mock_cursor.execute = MagicMock(side_effect=[AttributeError, ""])
result = self.fetcher.get_objects_list_for_id(QUERY_WITHOUT_ID,
OBJECT_TYPE,
None)
self.assertEqual(mock_cursor.execute.call_count, 2, "Can't invoke execute method " +
"twice when error occurs")
self.assertEqual(result, OBJECTS_LIST, "Can't get objects list")
|