Skip to content

Commit

Permalink
CHORE: adding better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidawad committed May 7, 2018
1 parent cf2954c commit 9ebc7d3
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .coveralls
@@ -0,0 +1,4 @@
repo_token: hJJWJuaBh8mU4adgwkZLh8gGkQYIxRbbo
service_name: travis-ci
# parallel: true # if the CI is running your build in parallel

23 changes: 23 additions & 0 deletions .travis.yml
@@ -0,0 +1,23 @@
language: python
python:
# - "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
- "3.6-dev" # 3.6 development branch
- "3.7-dev" # 3.7 development branch


# command to install dependencies
install:
- pip install -r requirements.txt

- pip install coveralls

- coverage run tests.py

script:
- pytest tests.py # or py.test for Python versions 3.5 and below

after_success: coveralls
2 changes: 1 addition & 1 deletion Procfile
@@ -1 +1 @@
web: gunicorn messenger:app --log-file=-
web: gunicorn --chdir app/ messenger:app --log-file=-
13 changes: 12 additions & 1 deletion README.md
@@ -1,8 +1,15 @@
# lobe

[![Build Status](https://travis-ci.org/davidawad/lobe.svg?branch=master)](https://travis-ci.org/davidawad/lobe) [![Coverage Status](https://coveralls.io/repos/github/davidawad/lobe/badge.svg?branch=master)](https://coveralls.io/github/davidawad/lobe?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/9b6d24df1f5e2f05226a/maintainability)](https://codeclimate.com/github/davidawad/lobe/maintainability)


To provide for the common defense, and promote the general welfare.

lobe is powered by a few different things.

## Getting Started


### messenger platform
Set up messenger.py on heroku or some other platform. Make sure that facebook has subscribed your page to messenger events

Expand Down Expand Up @@ -55,7 +62,7 @@ curl --request POST --header 'content-type: application/json' -d@- --url 'localh
}
```

It will take a while for the model to be trained so just wait on it.
It will take a while for the model to be trained so just wait on it.


Then make requests using the name of the model you just created and the name of the project that will use that new model
Expand All @@ -64,3 +71,7 @@ curl -X POST localhost:5000/parse \
-d '{"q":"what is court like?", "model":"model_20180302-170041", "project":"lobe"}'
```


## Testing

All test cases are contained inside the `test` folder.
Empty file added app/__init__.py
Empty file.
File renamed without changes.
28 changes: 7 additions & 21 deletions messenger.py → app/messenger.py
@@ -1,22 +1,6 @@
#!/usr/bin/env python
# coding:utf-8

# Messenger API integration example
# We assume you have:
# * a Wit.ai bot setup (https://wit.ai/docs/quickstart)
# * a Messenger Platform setup (https://developers.facebook.com/docs/messenger-platform/quickstart)
# You need to `pip install the following dependencies: requests, bottle.
#
# 1. pip install requests bottle
# 2. You can run this example on a cloud service provider like Heroku, Google Cloud Platform or AWS.
# Note that webhooks must have a valid SSL certificate, signed by a certificate authority and won't work on your localhost.
# 3. Set your environment variables e.g. WIT_TOKEN=your_wit_token
# FB_PAGE_TOKEN=your_page_token
# FB_VERIFY_TOKEN=your_verify_token
# 4. Run your server e.g. python messenger.py {PORT}
# 5. Subscribe your page to the Webhooks using verify_token and `https://<your_host>/webhook` as callback URL.
# 6. Talk to your bot on Messenger!

# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
Expand Down Expand Up @@ -105,18 +89,19 @@ def messenger_webhook():
# Let's forward the message to Wit /message
# and customize our response to the message in handle_message
response = client.message(msg=text, context={'session_id':fb_id})

handle_message(response=response, fb_id=fb_id)
# TODO should this function return anything
else:
# Returned another event
log('Received an invalid message on the facebook endpoint: ' + data)
return 'OK', 200

# TODO return a different error code on error

return 'OK', 200



#TODO write something to break apart shorter replies

def fb_message(sender_id, text):
"""
Function for returning response to messenger
Expand All @@ -134,13 +119,15 @@ def fb_message(sender_id, text):


def send_content(recipient_id, content):

log("sending message to {recipient}: {content}".format(
recipient=recipient_id,
content=str(content)))

params = {
"access_token": FB_PAGE_TOKEN
}

headers = {
"Content-Type": "application/json"
}
Expand Down Expand Up @@ -171,7 +158,7 @@ def handle_parsed_intent(parsed_intent):
ret_text, ret_replies, ret_buttons = 'DEFAULT MESSAGE', [], []

# TODO look at intent to determine if more work is necessary
log('RECEIVED PARSED INTENT: ' + str(parsed_intent))
log('RECEIVED PARSED INTENT: ' + str(parsed_intent))

# handle greeting routine
if parsed_intent == 'greetings':
Expand All @@ -189,7 +176,6 @@ def handle_parsed_intent(parsed_intent):
# TODO cleaner way to do this setting?
ret_obj["text"] = ret_text


if ret_replies:
ret_obj["quick_replies"] = ret_replies

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added test/functional/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions test/functional/test_smoke.py
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
import pytest

# add parent folders to system path to import classes to testing
import sys
sys.path.append('../')

from test_conf import *



# smoke tests
class TestForSmoke(object):

def test_something_basic(self):
pass
Empty file added test/pytest.ini
Empty file.
58 changes: 58 additions & 0 deletions test/test_conf.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
import pytest

# add parent folders to system path to import classes to testing
import sys
sys.path.append('../app')

from messenger import *
from constants import *

# from project import create_app, db
# from project.models import User


@pytest.fixture(scope='module')
def new_user():
# user = User('patkennedy79@gmail.com', 'FlaskIsAwesome')
return
# return user


@pytest.fixture(scope='module')
def test_client():
# flask_app = create_app('flask_test.cfg')

# # Flask provides a way to test your application by exposing the Werkzeug test Client
# # and handling the context locals for you.
# testing_client = flask_app.test_client()

# # Establish an application context before running the tests.
# ctx = flask_app.app_context()
# ctx.push()

# yield testing_client # this is where the testing happens!

# ctx.pop()
return


@pytest.fixture(scope='module')
def init_database():
# Create the database and the database table
# db.create_all()

# # Insert user data
# user1 = User(email='patkennedy79@gmail.com', plaintext_password='FlaskIsAwesome')
# user2 = User(email='kennedyfamilyrecipes@gmail.com', plaintext_password='PaSsWoRd')
# db.session.add(user1)
# db.session.add(user2)

# # Commit the changes for the users
# db.session.commit()

# yield db # this is where the testing happens!

# db.drop_all()
return

Empty file added test/unit/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions test/unit/test_base.py
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-

import pytest

# add parent folders to system path to import classes to testing
import sys
sys.path.append('../')

from test_conf import *



# FIXME this shouldn't exist, we should have separate functions to do these things
def return_response_object(text, quick_replies=None, buttons=None):
ret_obj = {}
ret_obj['text'] = text
ret_obj['quick_replies'] = quick_replies
ret_obj['buttons'] = buttons
return ret_obj


class TestBotResponses(object):

def test_intro_message_response(self):
reply = bot_response("Hello")
response_obj = return_response_object("Hello! I’m Sloan, your automated guide for advice on your student loans brought to you by TISLA. This is not legal advice but simple guidance to help you manage your student loan debt. Send 'restart' at any time to restart. Ready? Simply type your question and I’ll try and guide you to the best way to manage your student debt!", raw_response_data['replies']['intro'])

assert reply.get('text', None) == response_obj.get('text')
assert reply.get('buttons', None) == response_obj.get('buttons')
assert reply.get('quick_replies', None) == response_obj.get('quick_replies')

0 comments on commit 9ebc7d3

Please sign in to comment.