def test_close_connection(self): DATABASE_URL_LOCAL = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(DATABASE_URL_LOCAL, printConn=False) self.assertEqual(conn.closed, 0) Utility.close_connection(conn) self.assertEqual(conn.closed, 1)
def test_get_seconds(self): time_dict = Utility.granularityToSeconds self.assertEqual(time_dict["S5"], Utility.getSeconds("S5")) self.assertEqual(time_dict["H1"], Utility.getSeconds("H1")) self.assertEqual([time_dict[index] for index in ["H1", "W"]], Utility.getSeconds(["H1", "W"])) # Testing if it raises the correct error with self.assertRaises(ValueError): Utility.getSeconds("OK")
def test_get_database_url(self): database_url = Utility.get_database_url() self.assertEqual(database_url[0], self.TEST_DATABASE_URL[0]) self.assertEqual(len(database_url), len(self.TEST_DATABASE_URL)) # Checking the order of appearence of all lowercase letters in url for char in ascii_lowercase: self.assertEqual(database_url.find(char), self.TEST_DATABASE_URL.find(char)) # Checking str equality of URLs self.assertEqual(Utility.get_database_url(), self.TEST_DATABASE_URL)
def test_get_all_table_names(self): conn = Utility.connectHeroku(Utility.get_database_url()) # Deleting the table vendors Utility.drop_table(conn, "vendors") # Create test tables TestTools.create_test_tables() true_names = ["vendors"] table_names = Utility.get_all_table_names(conn) for index in range(len(table_names)): self.assertEqual(table_names[index], true_names[index])
def __init__(self, name, database_info, granularity_list): assert len(granularity_list) > 0 assert self.checkGranularity(granularity_list) assert isinstance(database_info, DatabaseInfo) self.name = name self.lastUpdateTime = 0 self.database_info = database_info if isinstance(granularity_list, str): self.tools = Utility.getAssetUpdateTool(self.name, database_info, granularity_list) else: self.tools = Utility.getAssetUpdateToolDict( self.name, database_info, granularity_list)
def test_drop_table(self): conn = Utility.connectHeroku(Utility.get_database_url()) # Deleting the table vendors Utility.drop_table(conn, "vendors") # Create test tables TestTools.create_test_tables() self.assertTrue(Utility.table_exists(conn, 'vendors')) Utility.drop_table(conn, 'vendors') self.assertFalse(Utility.table_exists(conn, 'vendors')) conn.commit() Utility.close_connection(conn)
def create_test_tables(cls): # """ create tables in the PostgreSQL database""" commands = """ CREATE TABLE vendors ( vendor_id SERIAL PRIMARY KEY, vendor_name VARCHAR(255) NOT NULL ) """ conn = None try: # connect to the PostgreSQL server database_url_local = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(database_url_local) cur = conn.cursor() # create table one by one cur.execute(commands) # close communication with the PostgreSQL database server cur.close() # commit the changes conn.commit() except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close()
def create_instrument_list(): oanda = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) r = accounts.AccountInstruments(accountID=Utility.getAccountID()) name_list = [] for instru in oanda.request(r)["instruments"]: name = instru["name"] index_underscore = name.find("_") name_front = name[:index_underscore] name_back = name[(index_underscore + 1):] name_list.append(name_front) name_list.append(name_back) name_list = set(name_list[:5]) return name_list
def test_eq(self): DATABASE_URL_LOCAL = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(DATABASE_URL_LOCAL) example = DatabaseInfo("Rock", conn) example2 = DatabaseInfo("Scissors", conn) self.assertNotEqual(example, example2) self.assertEqual(example, example)
def test_get_history_from_given(self): """ Test fetching function from given date, set to be 23 March 2017: From 12:30 23/03/2017 To 18:30 23/03/2017 """ oanda = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) eurgbp = FetchInstrumentData("EUR_GBP", oanda, Utility.getAccountID(), "M5") d = date(2017, 3, 23) time_to = time(18, 30) time_from = time(12, 30) unix_timestamp_to = datetime.combine(d, time_to).timestamp() unix_timestamp_from = datetime.combine(d, time_from).timestamp() eurgbp.getHistoryFromGivenDate(5, unix_timestamp_from) eurgbp.print_data()
def __init__(self, name, conn): from tools import Utility self.name = name self.conn = conn self.tool = None if name is 'oanda': self.tool = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'})
class TestFetchInstrumentData(unittest.TestCase): NAME = "EUR_GBP" ACCOUNTID = "1337" GRAN = ["M5", "W"] API = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) def test_init_number_of_dates(self): example = FetchInstrumentData(self.NAME, self.API, self.ACCOUNTID, self.GRAN) self.assertEqual(example.getNumberOfDates(), 0)
def test_create_test_table(self): database_url_local = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(database_url_local) # Deleting the table vendors Utility.drop_table(conn, "vendors") # Creating tables TestTools.create_test_tables() self.assertTrue(Utility.table_exists(conn, 'vendors')) self.assertFalse(Utility.table_exists(conn, 'parts')) self.assertFalse(Utility.table_exists(conn, 'part_drawings')) # Closing database connection Utility.close_connection(conn)
def test_get_add_quote_query(self): # Checking the number of inputs and # number of inputs in the resulting str inputs_str = Utility.getAddQuoteQuery(self.TEST_TABLE_NAME_STR) first_str = inputs_str[inputs_str.find("("):inputs_str.find(")")] first_str = first_str.split() second_str = inputs_str[inputs_str.find("(", inputs_str.find("(") + 1):inputs_str. find(")", inputs_str.find(")") + 1)] second_str = second_str.split() self.assertEqual(len(first_str), len(second_str))
def test_get_table_col_names(self): part_drawings_col_names = ['vendor_id', 'vendor_name'] conn = Utility.connectHeroku(Utility.get_database_url()) # Deleting the table vendors Utility.drop_table(conn, "vendors") # Create test tables TestTools.create_test_tables() col_names = Utility.get_table_col_names(conn, 'vendors') for index in range(len(part_drawings_col_names)): self.assertEqual(col_names[index], part_drawings_col_names[index]) conn.commit() Utility.close_connection(conn)
def test_hash(self): DATABASE_URL_LOCAL = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(DATABASE_URL_LOCAL) example = DatabaseInfo("Rock", conn) example3 = DatabaseInfo("Rock", conn) self.assertEqual(example, example3)
import matplotlib import numpy as np import pandas as pd consumer_key = "m2rSkYpqbjXMTKZw2wznAUWXk" consumer_secret = "Prh1PCRRSurTBDKPqaUGZvKxIEqlB1rDTgAyLblNkd85To6alJ" access_token = "731414015147163648-KM4O1QkC5HxSldRCGJ71QrPfBhcLUeS" access_token_secret = "w11yNKclWFIoNXE32sfwDNieOnubCttcmViMHKSusbaDY" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) oanda = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) def create_instrument_list(): oanda = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) r = accounts.AccountInstruments(accountID=Utility.getAccountID()) name_list = [] for instru in oanda.request(r)["instruments"]: name = instru["name"] index_underscore = name.find("_") name_front = name[:index_underscore] name_back = name[(index_underscore + 1):] name_list.append(name_front)
from tools import Utility from worker import * from fetchinstrumentoanda import FetchInstrumentData import time from datetime import datetime, timedelta import psycopg2 #DATABASE_URL_LOCAL = "postgres://*****:*****@ec2-54-243-185-132.compute-1.amazonaws.com:5432/dce76cagnnlqik" config = configparser.ConfigParser() config.read('oanda.cfg') oanda = oandapy.API(environment="practice", access_token=Utility.getAccountToken(), headers={'Accept-Datetime-Format': 'UNIX'}) eurgbp = FetchInstrumentData("EUR_GBP", oanda, Utility.getAccountID(), "M5") eurgbp.getHistoryFromToday(10) eurusd = FetchInstrumentData("EUR_USD", oanda, Utility.getAccountID(), "S5") eurusd.getHistoryFromToday(15) print(eurusd.print_data()) # Creating price table try: # read the connection parameters DATABASE_URL_LOCAL = os.environ['DATABASE_URL_HEROKU'] # connect to the PostgreSQL server conn = Utility.connectHeroku(DATABASE_URL_LOCAL)
def test_create_price_table_creation_only(self): """ Test the function Utility.create_price_table on creation only """ conn = Utility.connectHeroku(Utility.get_database_url()) Utility.create_price_table(conn, 'price', True) self.assertTrue(Utility.table_exists(conn, 'price'))
def test_create_price_table_exception(self): """ Test the function Utility.create_price_table on exception only """ conn = Utility.connectHeroku(Utility.get_database_url()) Utility.create_price_table(conn, 'price', True) with self.assertRaises(Exception): Utility.create_price_table(conn, 'price')
def setUpdateTime(self): self.lastUpdateTime = Utility.getLondonUNIXDate() print("Set new update date: {}".format(self.lastUpdateTime))
def addGranularity(self, gran_list): assert isinstance(gran_list, list) self.tools = { **self.tools, **Utility.getAssetUpdateToolDict(self.name, self.database_info, gran_list) }
def test_is_list_granularity(self): self.assertTrue(Utility.is_list_in_granularity("S5")) self.assertFalse(Utility.is_list_in_granularity("TEST")) self.assertTrue(Utility.is_list_in_granularity(["S5", "M5", "W"])) self.assertFalse(Utility.is_list_in_granularity(["TEST"]))
def test_clean_database(self): conn = Utility.connectHeroku(Utility.get_database_url()) for table in Utility.get_all_table_names(conn): Utility.drop_table(conn, table) # Deleting the table vendors Utility.drop_table(conn, "vendors") # Create test tables TestTools.create_test_tables() self.assertEqual(len(Utility.get_all_table_names(conn)), 1) Utility.clean_database(conn) self.assertEqual(len(Utility.get_all_table_names(conn)), 0)
def test_connect_heroku(self): database_url_local = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(database_url_local) self.assertEqual(conn.closed, 0) # Closing database connection Utility.close_connection(conn)
def test_init_tool(self): DATABASE_URL_LOCAL = os.environ['DATABASE_URL_HEROKU'] conn = Utility.connectHeroku(DATABASE_URL_LOCAL) example = DatabaseInfo("Rock", conn) self.assertEqual(None, example.getTool())