Skip to content

Commit

Permalink
Fixed the current unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drkostas committed May 15, 2020
1 parent 42b48f3 commit 06850dc
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [X] Integrate old config into the Configuration class
- [X] Add ColorLogging
- [X] Update requirements
- [X] Check that current unit tests are working
- [ ] Fix README
- [ ] Check that current unit tests are working
- [ ] Create unit tests for the HGN code
- [ ] Add Metadata Class that stores step times into a database
11 changes: 6 additions & 5 deletions datastore/mysql_datastore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from typing import List, Tuple, Dict

from mysql import connector as mysql_connector
from mysql import connector
import mysql.connector.connection_cext

from .abstract_datastore import AbstractDatastore

Expand All @@ -11,8 +12,8 @@
class MySqlDatastore(AbstractDatastore):
__slots__ = ('_connection', '_cursor')

_connection: mysql_connector.connection_cext.CMySQLConnection
_cursor: mysql_connector.connection_cext.CMySQLCursor
_connection: connector.connection_cext.CMySQLConnection
_cursor: connector.connection_cext.CMySQLCursor

def __init__(self, config: Dict) -> None:
"""
Expand All @@ -25,7 +26,7 @@ def __init__(self, config: Dict) -> None:

@staticmethod
def get_connection(username: str, password: str, hostname: str, db_name: str, port: int = 3306) \
-> Tuple[mysql_connector.connection_cext.CMySQLConnection, mysql_connector.connection_cext.CMySQLCursor]:
-> Tuple[connector.connection_cext.CMySQLConnection, connector.connection_cext.CMySQLCursor]:
"""
Creates and returns a connection and a cursor/session to the MySQL DB
Expand All @@ -37,7 +38,7 @@ def get_connection(username: str, password: str, hostname: str, db_name: str, po
:return:
"""

connection = mysql_connector.connect(
connection = connector.connect(
host=hostname,
user=username,
passwd=password,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ graphframes==0.6
importlib-metadata==1.6.0
joblib==0.15.0
jsonschema==3.2.0
mysql-connector==2.2.9
mysql-connector-python==8.0.20
mysql-connector==2.2.9
networkx==2.4
nose==1.3.7
numpy==1.18.4
Expand Down
13 changes: 3 additions & 10 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def test_to_json(self):
'password': 'pass2',
'db_name': 'db3',
'port': 3306},
'type': 'mysql'}],
'cloudstore': [{'config':
{'api_key': 'apiqwerty'},
'type': 'dropbox'}]}
'type': 'mysql'}]}
# Compare
logger.info('Comparing the results..')
self.assertDictEqual(self._sort_dict(expected_json), self._sort_dict(configuration.to_json()))
Expand All @@ -53,9 +50,8 @@ def test_to_yaml(self):
# Modify and export yml
logger.info('Changed the host and the api_key..')
configuration.datastore[0]['config']['hostname'] = 'changedhost'
configuration.cloudstore[0]['config']['api_key'] = 'changed_api'
logger.info('Exporting to yaml..')
configuration.to_yaml('test_data/test_configuration/actual_output_to_yaml.yml', include_tag=True)
configuration.to_yaml('test_data/test_configuration/actual_output_to_yaml.yml')
# Load the modified yml
logger.info('Loading the exported yaml..')
modified_configuration = Configuration(
Expand All @@ -69,10 +65,7 @@ def test_to_yaml(self):
'password': 'pass2',
'db_name': 'db3',
'port': 3306},
'type': 'mysql'}],
'cloudstore': [{'config':
{'api_key': 'changed_api'},
'type': 'dropbox'}]}
'type': 'mysql'}]}
self.assertDictEqual(self._sort_dict(expected_json), self._sort_dict(modified_configuration.to_json()))

@classmethod
Expand Down
4 changes: 0 additions & 4 deletions tests/test_data/test_configuration/actual_output_to_yaml.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
cloudstore:
- config:
api_key: changed_api
type: dropbox
datastore:
- config:
db_name: db3
Expand Down
10 changes: 5 additions & 5 deletions tests/test_mysql_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ class TestMysqlDatastore(unittest.TestCase):
def test_connect(self):
# Test the connection with the correct api key
try:
MySqlDatastore(config=self.configuration.get_datastores()[0])
MySqlDatastore(config=self.configuration.get_datastore_configs()[0])
except MsqlProgrammingError as e:
logger.error('Error connecting with the correct credentials: %s', e)
self.fail('Error connecting with the correct credentials')
else:
logger.info('Connected with the correct credentials successfully.')
# Test that the connection is failed with the wrong credentials
with self.assertRaises(MsqlProgrammingError):
datastore_conf_copy = copy.deepcopy(self.configuration.get_datastores()[0])
datastore_conf_copy = copy.deepcopy(self.configuration.get_datastore_configs()[0])
datastore_conf_copy['password'] = 'wrong_password'
MySqlDatastore(config=datastore_conf_copy)
logger.info("Loading Mysql with wrong credentials failed successfully.")

def test_create_drop(self):
data_store = MySqlDatastore(config=self.configuration.get_datastores()[0])
data_store = MySqlDatastore(config=self.configuration.get_datastore_configs()[0])
# Create table
logger.info('Creating table..')
data_store.create_table(self.table_name, self.test_table_schema)
Expand All @@ -50,7 +50,7 @@ def test_create_drop(self):
self.assertNotIn(self.table_name, data_store.show_tables())

def test_insert_update_delete(self):
data_store = MySqlDatastore(config=self.configuration.get_datastores()[0])
data_store = MySqlDatastore(config=self.configuration.get_datastore_configs()[0])
# Create table
logger.info('Creating table..')
data_store.create_table(self.table_name, self.test_table_schema)
Expand Down Expand Up @@ -110,7 +110,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
data_store = MySqlDatastore(config=cls.configuration.get_datastores()[0])
data_store = MySqlDatastore(config=cls.configuration.get_datastore_configs()[0])
for table in cls.generated_table_names:
logger.info('Dropping table {0}'.format(table))
data_store.drop_table(table=table)
Expand Down

0 comments on commit 06850dc

Please sign in to comment.