def connect_to_db(self): if not self.__validate_input(): return if platform.system() == 'Darwin': db_access = MySqlAccess(self._leIpAddr.text(), int(self._lePort.text())) if not db_access.connect(self._leUserName.text(), self._lePasswd.text()): self.onDbConnectedFailed.emit(self._leIpAddr.text() + self.__DELIMITER + self._lePort.text()) else: self.onDbConnected.emit(self._leIpAddr.text() + self.__DELIMITER + self._lePort.text(), db_access) else: self.onDbConnectedFailed.emit(self._leIpAddr.text() + self.__DELIMITER + self._lePort.text() + ': Not supported') self.close()
def setUp(self): self.__db_access = MySqlAccess() self.__use_mock = False
'%H:%M') record.append(endTime) startTime = record[-2][0:hyphenInd] startTime = datetime.strftime(datetime.strptime(startTime, '%I:%M %p'), '%H:%M') record[-2] = startTime #get instructor, date, and location record += scraper.data[i][16:17] record += scraper.data[i][-4:-2] if record[-3].strip() != "Instructor": # instructor field is in the format 'professor name (', reformatting it to remove the paranthesis record[-3] = record[-3][0:-2] cData.append(record) #the following code assumes that the sql table 'courses' has the same structure of this data (column count and type) sql = MySqlAccess("localhost", "root", "", "courses_db") print("The following queries could not be entered:") print("---------------------------------------------") for record in cData: if record[0] == "Subj": continue query = "INSERT INTO courses VALUES(" for item in record: query += "'" + item + "'," query = query[0:-1] + ")" try: sql.query(query) except: print(query) sql.conn.close()
class MySqlAccessTest(unittest.TestCase): def setUp(self): self.__db_access = MySqlAccess() self.__use_mock = False def tearDown(self): if not self.__use_mock: self.__db_access.disconnect() def __mock_mysql_db(self): self.__use_mock = True self.__db_access.disconnect() self.__db_access._conn = mock.Mock() def __mock_mysql_db_connect(self): self.__db_access._conn.connect = mock.Mock() def test_connect_return_none_when_user_name_is_none(self): self.assertFalse(self.__db_access.connect(user_password='******')) def test_connect_return_none_when_user_passwd_is_none(self): self.assertFalse(self.__db_access.connect(user_name='user_name')) def test_connect_return_none_when_user_name_and_passwd_is_provided(self): self.__mock_mysql_db() self.__mock_mysql_db_connect() self.assertTrue(self.__db_access.connect(user_name='user_name', user_password='******')) def test_query_to_real_db(self): """ WARNING!!! This should have MySQL DB set up """ if not self.__db_access.connect('test', 'test123') is None: select_sql = 'SHOW DATABASES' self.__db_access.query(select_sql) self.__db_access.disconnect()