Skip to content

Commit

Permalink
linting and formatting done according to .travis.yml (vishakha-lall#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
chttrjeankr authored Apr 4, 2020
1 parent f9e7ed3 commit 226ce31
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"mapsstatic": "https://maps.googleapis.com/maps/api/staticmap",
"elevation": "https://maps.googleapis.com/maps/api/elevation/json",
"places": "https://maps.googleapis.com/maps/api/place",
} # noqa: E501
}
6 changes: 3 additions & 3 deletions databaseconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def add_learnt_statement_to_database(subject, root, verb):
db.commit()
return db


@logger_config.logger
def learn_question_response(H):
db = connection_to_database()
Expand Down Expand Up @@ -229,7 +229,7 @@ def clear_table(table_name):
db.commit()
else:
print("Table cleaning skipped.")

return db


Expand All @@ -247,4 +247,4 @@ def describe_table(cur, table_name):
print("Number of existing records:", records_no)
print()

return records_no
return records_no
18 changes: 8 additions & 10 deletions googleMapsApiModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def get_timestamp(date_time):

@logger_config.logger
def get_lat_lng(place):
response = requests.get(
f'{BASE_URL["latlng"]}?address={place}&key={config.key}'
) # noqa: E501
response = requests.get(f'{BASE_URL["latlng"]}?address={place}&key={config.key}')
resp_json_payload = response.json()
lat_lng = resp_json_payload["results"][0]["geometry"]["location"]
return lat_lng
Expand All @@ -51,7 +49,7 @@ def timezone(place, date_time):
timestamp = get_timestamp(date_time)
response = requests.get(
f'{BASE_URL["timezone"]}?location={lat_lng["lat"]},{lat_lng["lng"]}&timestamp={timestamp}&key={config.key}'
) # noqa: E501
)
resp_dict = response.json()
for key in resp_dict:
print(f"{key} : {resp_dict[key]}")
Expand Down Expand Up @@ -97,8 +95,8 @@ def mapsstatic(search_location):
def elevation(search_location):
result = gmaps.geocode(search_location)
json = requests.get(
f'{BASE_URL["elevation"]}?locations={result[0]["geometry"]["location"]["lat"]},{result[0]["geometry"]["location"]["lng"]}&key={config.key}'
).json() # noqa: E501
f'{BASE_URL["elevation"]}?locations={result[0]["geometry"]["location"]["lat"]},{result[0]["geometry"]["location"]["lng"]}&key={config.key}' # noqa: E501
).json()
result_value = json["results"][0]["elevation"]
position = "above" if result_value > 0 else "below"
print(f"{search_location} is {round(result_value,2)} metres {position} sea level")
Expand All @@ -109,12 +107,12 @@ def elevation(search_location):
def places(search_location):
address = search_location
json = requests.get(
f'{BASE_URL["places"]}/findplacefromtext/json?input={address.lower().replace(" ", "+")}&inputtype=textquery&fields=photos,formatted_address,place_id&key={config.key}'
).json() # noqa: E501
f'{BASE_URL["places"]}/findplacefromtext/json?input={address.lower().replace(" ", "+")}&inputtype=textquery&fields=photos,formatted_address,place_id&key={config.key}' # noqa: E501
).json()
logging.debug("Address:" + json["candidates"][0]["formatted_address"])
details = requests.get(
f'{BASE_URL["places"]}/details/json?place_id={json["candidates"][0]["place_id"]}&fields=rating,formatted_phone_number&key={config.key}'
).json() # noqa: E501
f'{BASE_URL["places"]}/details/json?place_id={json["candidates"][0]["place_id"]}&fields=rating,formatted_phone_number&key={config.key}' # noqa: E501
).json()
logging.debug("Rating:" + str(details["result"]["rating"]))
logging.debug("Phone:" + details["result"]["formatted_phone_number"])
photo = f'{BASE_URL["places"]}/photo?maxwidth=400&photoreference={json["candidates"][0]["photos"][0]["photo_reference"]}&key={config.key}' # noqa: E501
Expand Down
63 changes: 34 additions & 29 deletions test_databaseconnect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import databaseconnect
import config
import pytest
import os
import mysql.connector as mysql
import mysql.connector as mysql


class TestClass:

@pytest.fixture(scope="session", autouse=True)
def setup_init(self):
# Will be executed before the first test
Expand All @@ -16,15 +15,15 @@ def setup_init(self):
host=config.host,
user=config.user,
passwd=config.password,
database=config.database
database=config.database,
)

cursor = test_db.cursor()
cursor.execute("CREATE DATABASE {}".format(config.database))
print("test database created")

except Exception:
print ("Failed to create test database")
print("Failed to create test database")
# rolling back to main db
config.database = main_database
pytest.exit("Exiting test!")
Expand All @@ -39,72 +38,78 @@ def setup_init(self):
print("test database deleted.")

except Exception:
print ("Failed to delete test database.")
print("Failed to delete test database.")

config.database = main_database


def test_setup_database(self):
db = databaseconnect.setup_database()
cursor = db.cursor()
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
expected_tables = [("chat_table"), ("statement_table"), ("question_table"), ("directions_table"),]
assert tables.sort() == expected_tables.sort()

expected_tables = [
("chat_table"),
("statement_table"),
("question_table"),
("directions_table"),
]
assert tables.sort() == expected_tables.sort()

def test_add_to_database_of_chat_table(self):
db = databaseconnect.add_to_database("C", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute("select * from chat_table where root_word='root' and verb='verb' and sentence='H'")
cursor.execute(
"select * from chat_table where root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1

assert res == 1

def test_add_to_database_of_question_table(self):
db = databaseconnect.add_to_database("Q", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute("select * from question_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'")
cursor.execute(
"select * from question_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1
assert res == 1

def test_add_to_database_of_statement_table(self):
db = databaseconnect.add_to_database("O", "subject", "root", "verb", "H")
cursor = db.cursor()
cursor.execute("select * from statement_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'")
cursor.execute(
"select * from statement_table where subject='subject' and root_word='root' and verb='verb' and sentence='H'"
)
res = cursor.fetchone()[0]
assert res == 1
assert res == 1

def test_get_chat_response(self):
response = databaseconnect.get_chat_response()
assert type(response) is str


def test_get_question_response_without_subject(self):
response = databaseconnect.get_question_response("[]", "root", "verb")
assert type(response) is tuple



def test_get_question_response_with_subject(self):
response = databaseconnect.get_question_response("subject", "root", "verb")
assert type(response) is tuple


def test_add_learnt_statement_to_database(self):
db = databaseconnect.add_learnt_statement_to_database('subject', 'root', 'verb')
db = databaseconnect.add_learnt_statement_to_database("subject", "root", "verb")
cursor = db.cursor()
cursor.execute("select * from question_table where subject='subject' and root_word='root' and verb='verb'")
cursor.execute(
"select * from question_table where subject='subject' and root_word='root' and verb='verb'"
)
res = cursor.fetchone()[0]
assert res == 1

assert res == 1

def test_learn_question_response(self):
response = databaseconnect.learn_question_response("H")
assert type(response) is tuple


def test_clear_table_with_chat_table(self, monkeypatch):
from io import StringIO

yes = StringIO("y\n")
monkeypatch.setattr("sys.stdin", yes)

Expand All @@ -115,9 +120,9 @@ def test_clear_table_with_chat_table(self, monkeypatch):
entries = cursor.fetchone()
assert entries is None


def test_clear_table_with_statement_or_question_table(self, monkeypatch):
from io import StringIO

yes = StringIO("y\n")
monkeypatch.setattr("sys.stdin", yes)

Expand All @@ -126,7 +131,7 @@ def test_clear_table_with_statement_or_question_table(self, monkeypatch):
cursor = db.cursor()
cursor.execute("select * from statement_table")
entries_1 = cursor.fetchone()

cursor.execute("select * from question_table")
entries_2 = cursor.fetchone()

Expand Down
16 changes: 10 additions & 6 deletions test_googleMapsApiModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ def test_elevation_with_invalid_input(self):
assert type(result) is float

def test_places_with_valid_input(self):
result = googleMapsApiModule.places('princeton university')
result = googleMapsApiModule.places("princeton university")
assert result == "ChIJ6baYzdjmw4kRTwKQ-tZ-ugI"

def test_places_with_invalid_input(self):
with pytest.raises(IndexError):
result = googleMapsApiModule.places('esffsf')
assert result == "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRaAAAA8o1VGVvds8zkqh745Pa6t2KcBbMA&key="+config.key
result = googleMapsApiModule.places("esffsf")
assert (
result
== "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRaAAAA8o1VGVvds8zkqh745Pa6t2KcBbMA&key=" # noqa: E501
+ config.key
)

def test_timezone_with_valid_input(self):
result = googleMapsApiModule.timezone('ohio','2000 11 21 11 41')
result = googleMapsApiModule.timezone("ohio", "2000 11 21 11 41")
assert result == "America/New_York"

def test_timezone_with_invalid_input(self):
with pytest.raises(ValueError):
result = googleMapsApiModule.timezone('wijd..','2000 18 21 11 41')
assert result == "America/New_York"
result = googleMapsApiModule.timezone("wijd..", "2000 18 21 11 41")
assert result == "America/New_York"

0 comments on commit 226ce31

Please sign in to comment.