def test_development_config(app): """ Test if development environment config variables are set. """ app.config.from_object('config.DevelopmentConfig') assert app.config['DEBUG'] assert not app.config['TESTING'] assert app.config['MONGODB_DATABASE'] == get_env("MONGODB_DATABASE")
def test_production_config(app): """ Test if production environment config variables are set. """ app.config.from_object('config.ProductionConfig') assert not app.config['DEBUG'] assert not app.config['TESTING'] assert app.config['MONGODB_DATABASE'] == get_env("MONGODB_DATABASE")
def test_testing_config(app): """ Test if testing environment config variables are set. """ app.config.from_object('config.TestingConfig') assert app.config['DEBUG'] assert app.config['TESTING'] assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] assert app.config['MONGODB_DATABASE'] == get_env("MONGODB_DATABASE_TEST")
class Config(): # pylint: disable=too-few-public-methods """ Main config class responsible for the main settings of the application. """ DEBUG = False TESTING = False APP_HOST = get_env("APP_HOST") APP_PORT = int(get_env("APP_PORT")) PERMITTED_REMOTES = get_env("PERMITTED_REMOTES").split(",") DATA_FILENAME = get_env("DATA_FILENAME") or "tweets.csv" MONGODB_DATABASE = get_env("MONGODB_DATABASE") SERVER_NAME = get_env("APP_HOST") + ":" + get_env("APP_PORT")
class DB: """ Database class responsible for the mongodb connection. """ URI = get_env("MONGODB_URI") @staticmethod def init(database): """ Init the database. """ client = pymongo.MongoClient(DB.URI) DB.CLIENT = client DB.DATABASE = client[database] DB.NAME = database @staticmethod def insert(collection, data): """ Insert data in a collection. """ return DB.DATABASE[collection].insert_one(data) @staticmethod def find_one(collection, query): """ Find one query inside a collection. """ return DB.DATABASE[collection].find_one(query) @staticmethod def get_last(collection): """ Return the last inserted data in collection. """ return list(DB.DATABASE[collection].find().sort([('created_at', -1) ]).limit(1))[0] @staticmethod def find_one_and_update(collection, _id, data): """ Find one record and update it. """ return DB.DATABASE[collection].find_one_and_update({"_id": _id}, {"$set": data}) @staticmethod def delete_one(collection, _id): """ Remove a specific data from a collection. """ return DB.DATABASE[collection].delete_one({"_id": _id}) @staticmethod def get_all(collection): """ Get all data in a specific collection. """ return DB.DATABASE[collection].find({}) @staticmethod def clean_data(collection): """ Clean all data in the collection. """ return DB.DATABASE[collection].delete_many({})
def test_unauthorized_remote(app): """ Checks if the unauthorized response are given for a not permitted remote address. It should check a random route and the predict route. """ test_client = app.test_client() app.config.update(PERMITTED_REMOTES="192.168.0.1") resp = test_client.post("/predict", data=json.dumps({"text": "A random text."}), content_type='application/json') data = json.loads(resp.get_data(as_text=True)) assert resp.status_code == 401 assert data['status'] == 401 assert data[ 'message'] == "Unauthorized, only permitted remotes can request." resp = test_client.get("/random_route") data = json.loads(resp.get_data(as_text=True)) assert resp.status_code == 401 assert data['status'] == 401 assert data[ 'message'] == "Unauthorized, only permitted remotes can request." app.config.update(PERMITTED_REMOTES=get_env("PERMITTED_REMOTES"))
class TestingConfig(Config): # pylint: disable=too-few-public-methods """ Config class for the testing environment. """ DEBUG = True TESTING = True MONGODB_DATABASE = get_env("MONGODB_DATABASE_TEST")
def run_seed(debug=False): """ Perform the seed of the collection in case of there is no data in dev collection. """ if not Tweet.all(): seed_collection_with_csv(utils.get_env('DATA_FILENAME'), debug=debug)
def setup_class(cls): """ Setups the TipPredictor object for using in the test methods. """ DB.init(utils.get_env('MONGODB_DATABASE_TEST')) seed_collection_with_csv(utils.get_env('DATA_FILENAME')) cls.predictor = TipPredictor(Tweet.all())
def dataset_path(cls): """ Returns the full path for the dataset file. """ basepath = os.path.dirname(__file__) filepath = os.path.abspath( os.path.join(basepath, "..", "datasets", get_env('DATA_FILENAME'))) return filepath
def setup_module(): """ Setup module seeding the MongoDB Collection. """ DB.init('tweets_test') seed_collection_with_csv(utils.get_env('DATA_FILENAME'))
def mongodb(): """ Pass a mongodb and clean data after the test is runned.""" DB.init(get_env('MONGODB_DATABASE_TEST')) yield DB DB.clean_data('tweets')