Conversation
www/app.py
Outdated
| print "rovercode-web id is " + str(web_id) | ||
| return r | ||
|
|
||
| def heartbeat_thread(): |
There was a problem hiding this comment.
I'm open to ideas about how to unit test this function.
README.md
Outdated
| $ py.test | ||
| ``` | ||
| We're having some trouble running pytest-cov inside the Docker container. If you are using Docker for development and want to run test, you'll need to `pip install pytest pytest-cov` outside and run the `py.test` outside of Docker for now. | ||
| Or, if your using Docker, make sure the container is running (the `sudo docker run ...` command above), then do: |
www/app.py
Outdated
| def register_with_web(): | ||
| global web_id | ||
| global payload | ||
| payload = {'name': 'Chipy', 'owner': 'Mr. Hurlburt', 'local_ip': get_local_ip()} |
There was a problem hiding this comment.
The name and owner will eventually come from some kind of configuration?
www/app.py
Outdated
| global payload | ||
| payload = {'name': 'Chipy', 'owner': 'Mr. Hurlburt', 'local_ip': get_local_ip()} | ||
| print "Registering with rovercode-web" | ||
| r = requests.post("https://rovercode.com/mission-control/rovers/", payload) |
There was a problem hiding this comment.
I would suggest moving the base URL somewhere that can be accessed by everything needing it so you only will have to change it in one place in the future.
There was a problem hiding this comment.
Good call. Will do.
| def test_sensor_init(): | ||
| assert len(app.binary_sensors) > 0 | ||
|
|
||
| def test_get_local_ip(): |
There was a problem hiding this comment.
To test all of these effectively, you'll need to use something like responses. It's a way to mock up the response from the server so that you don't actually need a server for the tests. Then you can check through all of the error cases as well. I can help you with it at some point if you run into issues. Basically, you setup a response to a particular request and see how the function behaves.
There was a problem hiding this comment.
Yeah, I'll try it out!
The other challenge with heartbeat_thread is that it is an infinite loop. Any ideas on how to get around that problem?
There was a problem hiding this comment.
I think you'll need a way to exit the loop. A shutdown variable or something like it that it's checking every time through the loop. If it were part of a class, you could have a class method to start and stop the thread. I'm not sure if the socketio background task will accept a class member or not.
There was a problem hiding this comment.
I wrapped it in a class with a stop function that disabled the shutdown variable. And socketio has not problem accepting a class member.
But, in my test function, how do I get the result of the thread function? If I start it in the background using socketio, I don't know where the return goes to. If I start it in the foreground, it blocks and I can't call the .stopThread function.
There was a problem hiding this comment.
Maybe a better way is to add an optional parameter (run_once) that is false by default, but that the test can set to true so that it just runs through once. That won't give 100% test coverage, but it will test most of it. I think you'd have to spin up a task in your test in order to stop the task in the infinite loop case.
There was a problem hiding this comment.
Sounds good -- I'll go with the run_once strategy for now.
www/app.py
Outdated
| print "rovercode-web id is " + str(web_id) | ||
| return r | ||
|
|
||
| def heartbeat_thread(): |
…rnetof/rovercode into feature/Rover_self_register
This reverts commit f9514a1.
|
Also added prospector and fixed the lint warning. |
.travis.yml
Outdated
| - pip install coveralls | ||
| script: | ||
| - py.test | ||
| - pwd && prospector |
There was a problem hiding this comment.
Do you still need to print the directory here or was that for debugging?
There was a problem hiding this comment.
Nope, I'll remove it!
www/app.py
Outdated
| print "Registering rover!" | ||
| ws_thread = None | ||
| hb_thread = None | ||
| payload = None; |
www/tests/test_app.py
Outdated
| json=response_payload, status=200, | ||
| content_type='application/json') | ||
| result = heartbeat_manager.register() | ||
| assert result.status_code in [200, 201] |
There was a problem hiding this comment.
You set the response to 200 so you should assert that it is exactly 200.
There was a problem hiding this comment.
Good point. Will change.
www/tests/test_app.py
Outdated
| web_id = 333 | ||
| heartbeat_manager = app.HeartBeatManager(payload=payload) | ||
| response_payload = payload.copy() | ||
| response_payload['id'] = 333 |
There was a problem hiding this comment.
You can use the web_id variable here.
There was a problem hiding this comment.
Good catch, will do.
www/app.py
Outdated
| payload = None; | ||
|
|
||
| pwm = pwmLib.get_platform_pwm(pwmtype="softpwm") | ||
| gpio = gpioLib.get_platform_gpio(); |
There was a problem hiding this comment.
Whoops, will fix all of these.
www/app.py
Outdated
| self.run = True; | ||
| self.thread = None; | ||
| self.web_id = id; | ||
| self.payload = payload; |
www/tests/test_app.py
Outdated
| content_type='application/json') | ||
|
|
||
| result = heartbeat_manager.thread_func(run_once=True) | ||
| assert result.status_code in [200, 201] |
www/tests/test_app.py
Outdated
| assert len(responses.calls) == 1 | ||
| assert responses.calls[0].request.url == app.ROVERCODE_WEB_REG_URL+str(web_id)+"/" | ||
|
|
||
| # Test for a rover who has been forgotten by the server |
There was a problem hiding this comment.
Good point. I'll separate it out.
www/tests/test_app.py
Outdated
|
|
||
| heartbeat_manager.web_id = web_id_new | ||
| result = heartbeat_manager.thread_func(run_once=True) | ||
| assert result.status_code in [200, 201] |
|
Looks like most of the missing test coverage is in the API handlers for the stored block diagrams. All of that will be going away since they will be stored in the cloud right? |
|
If you hadn't already found it, there's a visual representation of the test coverage output in |
|
I hadn't found the visual representation. That's awesome. |
|
Agreed -- a lot of the uncovered code deals with saving block diagrams. We can delete that stuff. Maybe in a separate PR? |
|
Separate PR is fine with me. There is one missing change for asserting |
Makes rover periodically check in with rovercode-web.
Also, fixes test coverage when in Docker.