Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test to the import_db.py functions #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions app/import_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def normalize_string(serial_number, fixed_size=30):
return f"{all_alpha}{missing_zeros}{all_digit}"



def get_database_connection():
"""connects to the MySQL database and returns the connection"""
return MySQLdb.connect(host=config.MYSQL_HOST,
Expand Down Expand Up @@ -188,7 +187,7 @@ def import_database_from_excel(filepath):

db.close()

return
return serials_counter, invalid_counter


def db_check():
Expand Down Expand Up @@ -261,11 +260,11 @@ def separate(input_string):
db.commit()

db.close()
return output


filepath = sys.argv[1]

import_database_from_excel(filepath)
db_check()

os.remove(filepath)
if __name__ == '__main__':
filepath = sys.argv[1]
import_database_from_excel(filepath)
db_check()
os.remove(filepath)
11 changes: 6 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,12 @@ def process():
return jsonify(ret), 200

def log_new_sms(status, sender, message, answer, cur):
if len(message) > 40:
return;
now = time.strftime('%Y-%m-%d %H:%M:%S')
cur.execute("INSERT INTO PROCESSED_SMS (status, sender, message, answer, date) VALUES (%s, %s, %s, %s, %s)", (status, sender, message, answer, now))

if len(message) > 40:
return
now = time.strftime('%Y-%m-%d %H:%M:%S')
cur.execute("INSERT INTO PROCESSED_SMS (status, sender, message, answer, date) VALUES (%s, %s, %s, %s, %s)",(status, sender, message, answer, now, ))


@app.errorhandler(404)
def page_not_found(error):
""" returns 404 page"""
Expand Down
Binary file added app/test_excel.xlsx
Binary file not shown.
27 changes: 27 additions & 0 deletions app/test_import_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import import_db as idb


class TestImportDb(unittest.TestCase):
def test_normalize_string(self):
self.assertEqual(idb.normalize_string('abc12', 10), 'ABC0000012')
self.assertEqual(idb.normalize_string('abc00١٢', 10), 'ABC0000012')

def test_get_database_connection(self):
try:
idb.get_database_connection()
except Exception as err:
self.fail(f"Error in connect to db: {err}")

def test_import_database_from_excel(self):
self.assertEqual(
idb.import_database_from_excel('./test_excel.xlsx'),
(2, 2))

def test_db_check(self):
self.assertNotEqual(idb.db_check(), [])



if __name__ == '__main__':
unittest.main()