Skip to content

Commit

Permalink
test: add initial tests
Browse files Browse the repository at this point in the history
Refs: #5
  • Loading branch information
piraz committed Mar 25, 2024
1 parent 8f08a44 commit 97c40cc
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: UTF-8 -*-
#
# Copyright 2020 Flavio Goncalves Garcia
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,8 +12,47 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from importlib import reload
import os

TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
FIXTURES_ROOT = os.path.join(TEST_ROOT, "fixtures")
PROJECT_ROOT = os.path.abspath(os.path.join(TEST_ROOT, ".."))


def chdir_fixture_app(app_name, **kwargs):
dir_name = kwargs.get("dir_name", None)
suppress_log = kwargs.get("suppress_log", False)
fixture_root = kwargs.get("fixture_root", "fixtures")
test_app_dirname = os.path.join(TEST_ROOT, fixture_root, app_name)
if dir_name is not None:
test_app_dirname = os.path.join(TEST_ROOT, fixture_root,
dir_name, app_name)
os.chdir(test_app_dirname)
if suppress_log:
import logging
for handler in logging.root.handlers[:]:
# clearing loggers, solution from: https://bit.ly/2yTchyx
logging.root.removeHandler(handler)
return test_app_dirname


def chdir_app(app_name, directory=None):
""" Change to the application directory located at the resource directory
for conf tests.
The conf resources directory is firenado/tests/resources/conf.
:param app_name: The application name
:param directory: The directory to be changed
"""
import firenado.conf

test_dirname, filename = os.path.split(os.path.abspath(__file__))
if directory:
test_app_dirname = os.path.join(test_dirname, 'resources',
directory, app_name)
else:
test_app_dirname = os.path.join(test_dirname, 'resources', app_name)
os.chdir(test_app_dirname)
reload(firenado.conf)
Empty file.
10 changes: 10 additions & 0 deletions tests/fixtures/bastiontest/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from bastiontest import handlers
from firenado import tornadoweb


class BastiontestComponent(tornadoweb.TornadoComponent):

def get_handlers(self):
return [
(r"/", handlers.IndexHandler),
]
32 changes: 32 additions & 0 deletions tests/fixtures/bastiontest/conf/firenado.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
app:
component: 'bastiontest'
pythonpath: ".."
#type: 'tornado'
port: 8888

data:
sources:
- name: session
connector: redis
# host: localhost
# port: 6379
# db: 0
components:
- id: bastiontest
class: bastiontest.app.BastiontestComponent
enabled: true
#- id: admin
# enabled: true
#- id: info
# enabled: true

# Session types could be:
# file or redis.
session:
type: redis
enabled: false
# Redis session handler configuration
data:
source: session
# File session handler related configuration
# path: /tmp
7 changes: 7 additions & 0 deletions tests/fixtures/bastiontest/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from firenado import tornadoweb


class IndexHandler(tornadoweb.TornadoHandler):

def get(self):
self.write("IndexHandler output")
33 changes: 33 additions & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
#
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# print current directory
import unittest
from tests import tornado_test


def suite():
testLoader = unittest.TestLoader()
alltests = unittest.TestSuite()
alltests.addTests(testLoader.loadTestsFromModule(tornado_test))
return alltests


if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=3)
result = runner.run(suite())
if not result.wasSuccessful():
exit(2)
38 changes: 38 additions & 0 deletions tests/tornado_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2020-2024 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from firenado.testing import TornadoAsyncTestCase
from firenado.launcher import ProcessLauncher
from tests import chdir_fixture_app, PROJECT_ROOT
from tornado.httpclient import AsyncHTTPClient
from tornado.testing import gen_test


class TornadoTransportTestCase(TornadoAsyncTestCase):
""" Tornado based client test case. """

def get_launcher(self):
application_dir = chdir_fixture_app("bastiontest")
return ProcessLauncher(
dir=application_dir, path=PROJECT_ROOT)

@gen_test
async def test_get(self):
http_client = AsyncHTTPClient()
try:
response = await http_client.fetch(
f"http://localhost:{self.http_port()}/")
except Exception as e:
raise e
self.assertEqual(response.body, b"IndexHandler output")

0 comments on commit 97c40cc

Please sign in to comment.