def test_create_stock(self): """test_create_stock""" with transaction() as tx: tx.add(Stock(exchange="ex", symbol="sym")) with transaction() as tx: stock_from_db = tx.query(Stock).first() assert "ex" == stock_from_db.exchange assert "sym" == stock_from_db.symbol
def test_query_quote_failed(self): with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="XXXXX")) query_quote_op = QueryQuoteOperation(stock_id=1, logger=self.logger) with pytest.raises(Exception): query_quote_op.run() with transaction() as tx: quote = tx.query(Quote).first() assert not quote
def test_query_quote_failed(self): with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="XXXXX")) query_quote_op = QueryQuoteOperation(stock_id=1, logger=self.logger) error = query_quote_op.run() assert "Bad Request" in error with transaction() as tx: quote = tx.query(Quote).first() assert not quote
def test_create_scheduled_action(self): """test_create_scheduled_action""" with transaction() as tx: scheduled_action = UpdateQuoteAction(action_date=self.act_time, interval_in_second=3600) tx.add(scheduled_action) with transaction() as tx: update_quote_action = tx.query(UpdateQuoteAction).first() assert update_quote_action.action_date == self.act_time assert update_quote_action.interval_in_second == 3600
def test_create_quote(self): """test_create_quote""" with transaction() as tx: stock = Stock(exchange="ex", symbol="sym") stock.quotes.append( Quote(price="10.10", change="-1.1", change_percentage="-10.02", date="1986-01-01")) tx.add(stock) with transaction() as tx: quote = tx.query(Quote).first() assert quote.price == 10.10 assert quote.stock.symbol == "sym"
def execute(self): """execute""" cutoff_date = (datetime.datetime.now() - datetime.timedelta(days=self.days)) \ .strftime("%Y-%m-%d") responce = {} with transaction() as tx: query = tx.query(Quote).filter(Quote.date > cutoff_date) if self.stock_id: query = query.filter(Quote.stock_id == self.stock_id) for quote in query.order_by(Quote.date): if quote.stock_id not in responce: responce[quote.stock_id] = {} responce[quote.stock_id]['exchange'] = quote.stock.exchange responce[quote.stock_id]['symbol'] = quote.stock.symbol responce[quote.stock_id]['quotes'] = [] quote_dict = { 'date': str(quote.date), 'price': quote.price, 'change': quote.change, 'change_percentage': quote.change_percentage } responce[quote.stock_id]['quotes'].append(quote_dict) self.reply = responce
def complete(self): """mark_in_process""" self.logger.info("Finish executing:{0}".format(self)) with transaction() as tx: tx.add(self) self.action_date += timedelta(seconds=self.interval_in_second) self.in_progress = False
def setup_method(self, method): """setup_method :param method: """ super(TestListQuotesOperation, self).setup_method(method) today = datetime.datetime.now() with transaction() as tx: stock = Stock(exchange="NASDAQ", symbol="AAPL") tx.add(stock) stock.quotes.append( Quote(price=1, change=0.1, change_percentage=10, date=today.strftime("%Y-%m-%d"))) stock.quotes.append( Quote(price=2, change=0.2, change_percentage=20, date=(today - datetime.timedelta(days=1)).strftime("%Y-%m-%d"))) stock = Stock(exchange="NASDAQ", symbol="MSFT") tx.add(stock) stock.quotes.append( Quote(price=1, change=0.1, change_percentage=10, date=today.strftime("%Y-%m-%d")))
def test_worker_run_scheduled_task(self): """test_worker_run""" worker = Worker() worker.execute_scheduled_task() with transaction() as tx: quote = tx.query(Quote).first() action = tx.query(UpdateQuoteAction).first() assert quote assert action.action_date == (self.act_time + timedelta(seconds=7200)) # run it again. Nothing changes worker.execute_scheduled_task() with transaction() as tx: action = tx.query(UpdateQuoteAction).first() assert action.action_date == (self.act_time + timedelta(seconds=7200))
def test_worker_run_scheduled_task(self, stock, update_quote_action): """test_worker_run""" worker = Worker() worker.execute_scheduled_task() with transaction() as tx: quote = tx.query(Quote).first() action = tx.query(UpdateQuoteAction).first() assert quote assert action.action_date == (update_quote_action.action_date + timedelta(seconds=update_quote_action.interval_in_second)) # run it again. Nothing changes worker.execute_scheduled_task() with transaction() as tx: action = tx.query(UpdateQuoteAction).first() assert action.action_date == (update_quote_action.action_date + timedelta(seconds=update_quote_action.interval_in_second))
def complete(self): """complete_operation""" with transaction() as tx: tx.add(self) self.action_date += timedelta(seconds=self.interval_in_second) self.in_progress = False self.last_update_time = datetime.utcnow() self.logger.info("Finish executing:{0}".format(self))
def execute(self): """execute""" with transaction() as tx: action = tx.query(UpdateQuoteAction).first() if not action: self.reply = {"error": "update_quote_action is not found."} action.run()
def execute(self): """execute the operation""" with transaction() as tx: stock = Stock(exchange=self.exchange, symbol=self.symbol) self.logger.info("Adding stock {0}".format(stock)) tx.add(stock) tx.flush() self.reply = str(stock)
def test_query_quote_succeed(self): """test_query_quote_succeed""" with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="AAPL")) query_quote_op = QueryQuoteOperation(stock_id=1, logger=self.logger) query_quote_op.run() with transaction() as tx: quote = tx.query(Quote).first() assert quote # rerun doesn't create more entries query_quote_op.run() with transaction() as tx: quote = tx.query(Quote).all() assert len(quote) == 1
def test_add_stock_duplicated(self): """test_add_stock_duplicated""" with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="AAPL")) add_stock_op = AddStockOperation(exchange="NASDAQ", symbol="AAPL", logger=self.logger) reply = add_stock_op.run() assert "Duplicate" in reply
def execute(self, tx=None): """execute""" with transaction(tx=tx) as tx: stocks = tx.query(Stock).all() self.logger.info("Update quotes for stocks:{0}".format(len(stocks))) for stock in stocks: op = QueryQuoteOperation(stock_id=stock.stock_id) op.run()
def test_update_quote_action(self): """test_update_quote_action""" with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="AAPL")) scheduled_action = UpdateQuoteAction(action_date=self.act_time, interval_in_second=3600) tx.add(scheduled_action) with transaction() as tx: action = tx.query(ScheduledAction).first() action.run() with transaction() as tx: tx.expire_all() quote = tx.query(Quote).first() action = tx.query(ScheduledAction).first() assert quote assert action.action_date == (self.act_time + timedelta(seconds=3600))
def execute(self): """execute""" with transaction(tx=self.tx) as tx: scheduled_action = ScheduledAction(action_date=datetime.utcnow(), type=self.action_type, interval_in_second=self.interval_in_second) self.logger.info("Adding scheduled action: {0}".format(scheduled_action)) tx.add(scheduled_action) tx.flush() self.reply = str(scheduled_action)
def execute(self): """execute :param tx: """ """execute""" with transaction(tx=self.tx) as tx: stock = tx.query(Stock).filter( Stock.stock_id == self.stock_id).first() if not stock: raise Exception("stock with id {} is not found".format( self.stock_id)) url = self.BASE_QUERY_URL.format(stock.exchange, stock.symbol) request = None try: request = urllib2.urlopen(url) content = request.read() quote_json = json.loads(content[3:])[0] price = quote_json['l'] change = quote_json['c'] change_percentage = quote_json['cp'] if change == 0: self.logger.warning( 'Hit response with invalid date:{}'.format(content)) return quote_date = datetime \ .strptime(quote_json['lt_dts'], '%Y-%m-%dT%H:%M:%SZ')\ .strftime("%Y-%m-%d") quote = tx.query(Quote) \ .filter(Quote.stock_id == self.stock_id) \ .filter(Quote.date == quote_date) \ .first() if quote: quote.price = price quote.change = change quote.change_percentage = change_percentage self.logger.info("Update quote: {0}".format(quote)) else: quote = Quote(price=price, change=change, change_percentage=change_percentage, date=quote_date) self.logger.info("Add quote: {0}".format(quote)) stock.quotes.append(quote) except urllib2.HTTPError: self.logger.error("invalid url {0}".format(url)) raise finally: if request: request.close()
def test_service_run(self): """test_service_run""" request = '{"action":"add_stock", "payload":{"exchange":"NASDAQ","symbol":"AAPL"}}' service = StockTracerService(mq_name="ut_mq") service.dispatch_request(request) with transaction() as tx: stock = tx.query(Stock).first() assert stock.symbol == "AAPL" assert stock.exchange == "NASDAQ"
def update_quote_action(): utc_now = datetime.utcnow() act_time = utc_now - timedelta(hours=1) act_time -= timedelta(microseconds=act_time.microsecond) with transaction() as tx: scheduled_action = UpdateQuoteAction(action_date=act_time, interval_in_second=7200) tx.add(scheduled_action) return scheduled_action
def test_add_scheduled_action_succeed(self): """test_add_scheduled_action_succeed""" add_scheduled_action_op = AddScheduledAction(action_type='update_quote', interval_in_second=10) result = add_scheduled_action_op.run() result_json = json.loads(result) assert result_json['type'] == 'update_quote' assert result_json['interval_in_second'] == '10' with transaction() as tx: action = tx.query(UpdateQuoteAction).first() assert action
def execute(self): """execute :param tx: """ """execute""" with transaction(tx=self.tx) as tx: stock = tx.query(Stock).filter(Stock.stock_id == self.stock_id).first() if not stock: raise Exception("stock with id {} is not found".format(self.stock_id)) url = self.BASE_QUERY_URL.format(stock.exchange, stock.symbol) request = None try: request = urllib2.urlopen(url) content = request.read() quote_json = json.loads(content[3:])[0] price = quote_json['l'] change = quote_json['c'] change_percentage = quote_json['cp'] if not change or float(change) == 0: self.logger.warning('Hit response with invalid date:{}'.format(content)) return quote_date = datetime \ .strptime(quote_json['lt_dts'], '%Y-%m-%dT%H:%M:%SZ')\ .strftime("%Y-%m-%d") quote = tx.query(Quote) \ .filter(Quote.stock_id == self.stock_id) \ .filter(Quote.date == quote_date) \ .first() if quote: quote.price = price quote.change = change quote.change_percentage = change_percentage self.logger.info("Update quote: {0}".format(quote)) else: quote = Quote( price=price, change=change, change_percentage=change_percentage, date=quote_date) self.logger.info("Add quote: {0}".format(quote)) stock.quotes.append(quote) except urllib2.HTTPError: self.logger.error("invalid url {0}".format(url)) raise finally: if request: request.close()
def mark_in_process(self): """mark_in_process""" self.logger.info("Start executing:{0}".format(self)) with transaction() as tx: tx.expire_all() tx.add(self) tx.query(ScheduledAction).filter(ScheduledAction.id == self.id).with_for_update().first() if self.in_progress: raise Exception("Action:{0} is already been executed on another worker".format(self.id)) self.in_progress = True
def execute(self): """execute""" with transaction(tx=self.tx) as tx: scheduled_action = ScheduledAction( action_date=datetime.utcnow(), type=self.action_type, interval_in_second=self.interval_in_second) self.logger.info( "Adding scheduled action: {0}".format(scheduled_action)) tx.add(scheduled_action) tx.flush() self.reply = str(scheduled_action)
def test_add_stock_succeed(self): """test_add_stock_succeed""" add_stock_op = AddStockOperation(exchange="NASDAQ", symbol="AAPL", logger=self.logger) result = add_stock_op.run() json_result = json.loads(result) assert json_result['stock_id'] == '1' assert json_result['symbol'] == 'AAPL' with transaction() as tx: stock = tx.query(Stock).first() assert stock.symbol == "AAPL" assert stock.exchange == "NASDAQ"
def execute_scheduled_task(self): utc_now = datetime.utcnow() with transaction() as tx: actions = tx.query(ScheduledAction) \ .filter(ScheduledAction.action_date < utc_now) \ .all() self.logger.info("Find scheduled actions: {0}".format(len(actions))) for action in actions: try: action.run() except Exception as e: print(e.message)
def mark_in_process(self): """mark_in_process""" self.logger.info("Start executing:{0}".format(self)) with transaction() as tx: tx.expire_all() tx.add(self) tx.query(ScheduledAction).filter( ScheduledAction.id == self.id).with_for_update().first() if self.in_progress: raise Exception( "Action:{0} is already been executed on another worker". format(self.id)) self.in_progress = True
def setup_method(self, method): """setup_method :param method: """ super(TestWorker, self).setup_method(method) utc_now = datetime.utcnow() act_time = utc_now - timedelta(hours=1) self.act_time = act_time - timedelta(microseconds=act_time.microsecond) with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="AAPL")) scheduled_action = UpdateQuoteAction(action_date=self.act_time, interval_in_second=7200) tx.add(scheduled_action)
def setup_method(self, method): """setup_method :param method: """ super(TestListQuotesOperation, self).setup_method(method) today = datetime.datetime.now() with transaction() as tx: stock = Stock(exchange="NASDAQ", symbol="AAPL") tx.add(stock) stock.quotes.append(Quote(price=1, change=0.1, change_percentage=10, date=today.strftime("%Y-%m-%d"))) stock.quotes.append(Quote(price=2, change=0.2, change_percentage=20, date=(today - datetime.timedelta(days=1)).strftime("%Y-%m-%d"))) stock = Stock(exchange="NASDAQ", symbol="MSFT") tx.add(stock) stock.quotes.append(Quote(price=1, change=0.1, change_percentage=10, date=today.strftime("%Y-%m-%d")))
def stock(): with transaction() as tx: tx.add(Stock(exchange="NASDAQ", symbol="AAPL"))