Falcon, wsgiref : Unit test cases - python

I have the below code:
master.py
def create():
master = falcon.API(middleware=Auth())
msg = Message()
master.add_route('/message', msg)
master = create()
if __name__ == '__main__':
httpd = simple_server.make_server("127.0.0.1", 8989, master)
process = Thread(target=httpd.serve_forever, name="master_process")
process.start()
#Some logic happens here
s_process = Thread(target=httpd.shutdown(), name="shut_process")
s_process.start()
s.join()
I tried to create the below test case for the below:
from falcon import testing
from master import create
#pytest.fixture(scope='module')
def client():
return testing.TestClient(create())
def test_post_message(client):
result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, body='{"message": "I'm here!"}') --> This line throws the error
assert result.status_code == 200
I tried running the above but get the below error:
TypeError: 'NoneType' object is not callable
I actually am unable to figure out how I should go about writing the test case for this.

Based on what #hoefling said, the below fixed it:
master.py
def create():
master = falcon.API(middleware=Auth())
msg = Message()
master.add_route('/message', msg)
return master
master = create()
if __name__ == '__main__':
httpd = simple_server.make_server("127.0.0.1", 8989, master)
process = Thread(target=httpd.serve_forever, name="master_process")
process.start()
#Some logic happens here
s_process = Thread(target=httpd.shutdown(), name="shut_process")
s_process.start()
s.join()
And then the test case works:
from falcon import testing
from master import create
#pytest.fixture(scope='module')
def client():
return testing.TestClient(create())
def test_post_message(client):
result = client.simulate_post('/message', headers={'token': "ubxuybcwe"},
body='{"message": "I'm here!"}')
assert result.status_code == 200
Thanks a lot #hoefling!

Related

Flask API returns {"locations":null} instead returning of locations

I have attached 2 separate codes one is for the flask app server.py and the second is for loading pickles files: util.py.
util.py has 3 functions,
First hello() returning message successfully
and the second get_location_names() not returning the location, it is supposed to return the location
This is my server.py which is a flask app.
from flask import Flask, request, jsonify
app = Flask(__name__)
import util
#app.route('/')
def hello():
response = jsonify({
'mesg' : util.hello()
})
response.headers.add('Access-Control-Allow-Origin','*')
return response
#app.route('/get_location_names')
def get_location_names():
response = jsonify({
'locations' : util.get_location_names()
})
response.headers.add('Access-Control-Allow-Origin','*')
return response
#app.route('/predict_home_price',methods=['POST'])
def predict_home_price():
total_sqft = float(request.form['total_sqft'])
location = request.form['location']
bed = int(request.form['bed'])
bath = int(request.form['bath'])
response = jsonify({
'estimated_price' : utils.get_estimated_price(location, total_sqft, bed, bath)
})
response.headers.add('Access-Control-Allow-Origin','*')
return response
if __name__ == '__main__':
print('Starting Python Flask Server')
app.run()
And here is my utils for loading pickles files
import json
import pickle
import numpy as np
__location = None
__data_columns = None
__model = None
def hello():
return "Hello World! This is Python Flask Server...........!"
def get_estimated_price(location,sqft,bed,bath):
try:
loc_index = __data_columns.index(location.lower())
except:
loc_index = -1
x = np.zeros(len(__data_columns))
x[0] = sqft
x[1] = bath
x[2] = bed
if loc_index >= 0:
x[loc_index] = 1
return round(__model.predict([x])[0],2)
def get_location_names():
global __location
return __location
def load_saved_artifacts():
print('Loading Saved Artifacts....Start')
global __data_columns
global __location
with open('./artifacts/columns.json','r') as col:
__data_columns = json.load(col)['my_data_columns']
__location = __data_columns[3:]
global __model
with open('./artifacts/House_Prediction_Price.pickle','rb') as my_model:
__model = pickle.load(my_model)
print('Loading saved artifacts is done...!')
if __name__ == '__main__':
load_saved_artifacts()
print(get_location_names())
print(get_estimated_price('1st block jayanagar',1000,3,3))
print(get_estimated_price('1st block jayanagar',1000,2,2))
print(get_estimated_price('kodihalli',1000,2,2))
print(get_estimated_price('Ejipura',1000,2,2))
The code is importing the module utils.py from another module server.py, then the __name__ variable will be set to that module’s name, and hence your load_saved_artifacts() in if block won't execute.
Try adding load_saved_artifacts() outside of if block, and check for the _location in get_location_names()
In your get_location_names() check the value for __location
#.... <utils.py> other lines
def get_location_names():
global __location
if __location is None:
load_saved_artifacts() # Doing this will make first request dependent on the execution time of this function call
return __location
load_saved_artifacts() # you can call your function here so that it will execute when your module is imported
if __name__ == "__main__":
#....
#other lines

click python Cli.testing TypeError

I have 2 files:
click_example.py
import click
#click.group(invoke_without_command=True)
#click.option('--apikey', default='My key',
help="API key to use.")
#click.pass_context
def main(ctx, apikey):
"""
A CLI for live and past football scores from various football leagues.
resources are given as commands and subresources and their filters as options
"""
headers = {'X-Auth-Token': apikey}
ctx.obj['headers'] = headers
#main.command()
#click.option('--limit', '-l', help='limit number of records')
def scorers(limit):
click.echo('limit is : %s' % limit)
if __name__ == '__main__':
main(obj={})
and a test file:
test_cs.py
from click.testing import CliRunner
import unittest
from .clicksample import main
class Sample(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
def tearDown(self):
pass
def test_sample_command(self):
result = self.runner.invoke(main)
self.assertEqual(0, result.exit_code)
if __name__ == '__main__':
unittest.main()
I want to know why the test does not pass. When I run the clicksample script from the command line it works as expected but for some reason it does not exit as expected in the test. When I poke the result using pdb I get the following stats:
(Pdb) result
<Result TypeError("'NoneType' object does not support item assignment",)>
(Pdb) result.exit_code
-1
(Pdb)
You never set ctx.obj. This can fix that for you:
def main(ctx, apikey):
if ctx.obj is None:
ctx.obj = {}
...

Lauch function with arguments as a thread in Python

Im trying to lauch this programm as a infinite thread in my program
https://github.com/qLethon/bitmex_simple_websocket
from bitmex_simple_websocket import BitMEXWebSocket
import json
class MyBitMEXWebsocket(BitMEXWebSocket):
def on_message(self, ws, message):
data = json.loads(message)
if 'table' in data and data['table'] == 'tradeBin1m':
print(data['data'][0])
bitmex = MyBitMEXWebsocket(endpoint='wss://www.bitmex.com/realtime?subscribe=tradeBin1m:XBTUSD')
how launch it in
if __name__ == '__main__':
as a thread correctly
In my current code, only first thread myBitMEXWebsocket function starts, but tradingview and trader wont
if __name__ == '__main__':
Thread(target = MyBitMEXWebsocket(endpoint='wss://www.bitmex.com/realtime?subscribe=quote:XBTUSD')).start()
Thread(target = tradingview).start()
for count_var_short, count_var_long in tradingview():
trader(count_var_short,count_var_long)
Solved, i asked github author, he gives this answer
class MyBitMEXWebsocket(BitMEXWebSocket):
def on_message(self, ws, message):
data = json.loads(message)
if 'table' in data and data['table'] == 'quote':
print(data['data'][0])
WS_BestBid = (data['data'][0]["bidPrice"])
WS_BestAsk = (data['data'][0]["askPrice"])
#print('bid:',WS_BestBid)
#print('ask:',WS_BestAsk)
#print('bid:',data['data'][0]["bidPrice"])
#print('ask:',data['data'][0]["askPrice"])
def f():
MyBitMEXWebsocket(endpoint='wss://www.bitmex.com/realtime?subscribe=quote:XBTUSD,order:XBTUSD')
th1 = threading.Thread(target=f)
th1.start()
if __name__ == '__main__':
Thread(target = f).start()
Thread(target = tradingview).start()
for count_var_short, count_var_long in tradingview():
trader(count_var_short,count_var_long)

Unable to stop running Python thread

I have an application listening on a specific TCP port to handle received requests (listen.py). After that, I have another one (trigger.py) that depending on the requested parameters triggers the respective operation.
Now, lets say the operation A was triggered (opA.py). Operation A uses a worker thread to start (worker.py). When the user request listen.py to stop operation A, the started thread is supposed to stop.
UPDATED:
The problem is that the thread is never stopped since the problem lies in trigger.py. The OperationA instance is lost once the code exits. So, I can never call stopOperation since it show me AttributeError: 'NoneType' object has no attribute 'stopOperation'
Any ideas of How to solve this?
listen.py
from trigger import Trigger
'''
code to handle requests here:
1st: param -> 'start'
2nd: param -> 'stop'
'''
t = Trigger()
t.execute(param)
trigger.py
from opA import OperationA
class Trigger():
def execute(param):
opA = OperationA()
if param == 'start':
opA.startOperation()
elif param == 'stop':
opA.stopOperation()
opA.py
from worker import ThreadParam
class OperationThread(ThreadParam):
def run(self):
while (self.running == False):
'''
do something here
'''
class OperationA():
def _init__(self):
listenThread = OperationThread(self)
def startOperation(self):
self.listenThread.start()
def stopOperation(self):
if self.listenThread.isAlive() == True:
print 'Thread is alive'
self.listenThread.killSignal()
else:
print 'Thread is dead'
worker.py
from threading import Thread
class ThreadParam(Thread):
def __init__(self, _parent):
Thread.__init__(self)
self.parent = _parent
self.running = False;
def killSignal(self):
self.running = True;
A minimal useful Trigger might look like this:
class Trigger(object):
def __init__(self):
self.operation = None
def execute(self, command):
if command == 'start':
assert self.operation is None
self.operation = OperationA()
self.operation.start_operation()
elif command == 'stop':
self.operation.stop_operation()
self.operation = None
else:
print 'Unknown command', repr(command)

Python, Call a class function within another class

Can you anyone please help me call the broadcast function from class BroadcastServerFactory in class test, as per attached code
I have tried so many methods of call a function from another class, but no solution
import sys
from twisted.internet import reactor
from twisted.python import log
from twisted.web.server import Site
from twisted.web.static import File
from autobahn.websocket import WebSocketServerFactory, \
WebSocketServerProtocol, \
listenWS
class test():
//call broadcast function from here
class BroadcastServerProtocol(WebSocketServerProtocol):
def onOpen(self):
self.factory.register(self)
def onMessage(self, msg, binary):
if not binary:
self.factory.broadcast("'%s' from %s" % (msg, self.peerstr))
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
self.factory.unregister(self)
class BroadcastServerFactory(WebSocketServerFactory):
"""
Simple broadcast server broadcasting any message it receives to all
currently connected clients.
"""
def __init__(self, url, debug = False, debugCodePaths = False):
WebSocketServerFactory.__init__(self, url, debug = debug, debugCodePaths = debugCodePaths)
self.clients = []
self.tickcount = 0
self.tick()
def tick(self):
self.tickcount += 1
self.broadcast("'tick %d' from server" % self.tickcount)
reactor.callLater(1, self.tick)
def register(self, client):
if not client in self.clients:
print "registered client " + client.peerstr
self.clients.append(client)
def unregister(self, client):
if client in self.clients:
print "unregistered client " + client.peerstr
self.clients.remove(client)
def broadcast(self, msg):
print "broadcasting message '%s' .." % msg
for c in self.clients:
c.sendMessage(msg)
print "message sent to " + c.peerstr
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
log.startLogging(sys.stdout)
debug = True
else:
debug = False
ServerFactory = BroadcastServerFactory
#ServerFactory = BroadcastPreparedServerFactory
factory = ServerFactory("ws://localhost:9000",
debug = debug,
debugCodePaths = debug)
factory.protocol = BroadcastServerProtocol
factory.setProtocolOptions(allowHixie76 = True)
listenWS(factory)
webdir = File(".")
web = Site(webdir)
reactor.listenTCP(8080, web)
reactor.run()
class test():
def __init__(self, factory):
factory.broadcast("I don't know what I'm doing!")
Meanwhile, in main...
factory = ServerFactory("ws://localhost:9000",
debug = debug,
debugCodePaths = debug)
test(factory)
This will do what you want, but it seems you're missing some core concepts about classes and instances. For the test class to call anything on another class, there needs to be an instance of it first (bar the case of static methods).

Categories