-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nside/1.1.0
1.1.0
- Loading branch information
Showing
7 changed files
with
109 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Publish Python 🐍 distributions 📦 to PyPI | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
build-n-publish: | ||
name: Build and publish Python 🐍 distributions 📦 to PyPI | ||
runs-on: ubuntu-18.04 | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.8 | ||
|
||
- name: Install pypa/build | ||
run: python -m pip install build | ||
|
||
- name: Build a binary wheel and a source tarball | ||
run: python -m build --sdist --wheel --outdir dist/ | ||
|
||
- name: Publish distribution 📦 to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
with: | ||
user: __token__ | ||
password: ${{ secrets.PYPI_API_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Run Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python 3.8 | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.8 | ||
|
||
- name: Install package | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -e . | ||
- name: Run tests | ||
run: | | ||
python -m unittest discover tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
import logging | ||
import sys | ||
from flask import Flask, g | ||
from .database import Database | ||
from .routes import setup_routes | ||
|
||
def create_app(database_uri): | ||
# Initialize Flask app | ||
app = Flask(__name__) | ||
|
||
setup_routes(app, database_uri) | ||
def get_db(): | ||
if 'db' not in g: | ||
g.db = Database(database_uri) | ||
return g.db | ||
tables = Database(database_uri).get_tables() | ||
setup_routes(app, tables, get_db) | ||
|
||
# Configure logging | ||
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) | ||
app.logger.addHandler(logging.StreamHandler(sys.stderr)) | ||
app.logger.setLevel(logging.DEBUG) | ||
|
||
@app.teardown_appcontext | ||
def teardown_db(exception): | ||
db = g.pop('db', None) | ||
|
||
if db is not None: | ||
db.close() | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,61 @@ | ||
import unittest | ||
import json | ||
import os | ||
import shutil | ||
from sqlite2rest import create_app | ||
from sqlite2rest import Database, create_app | ||
import unittest | ||
|
||
class TestRoutes(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
# Copy the database file before running the tests | ||
shutil.copyfile('data/chinook.db', 'test_chinook.db') | ||
# Use a database file for testing | ||
cls.db_uri = 'test.db' | ||
cls.db = Database(cls.db_uri) | ||
|
||
# Create a test table | ||
cls.db.cursor.execute('CREATE TABLE Artist (ArtistId INTEGER PRIMARY KEY, Name TEXT);') | ||
cls.db.conn.commit() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
# Delete the copied database file after running the tests | ||
os.remove('test_chinook.db') | ||
# Delete the database file after running the tests | ||
os.remove('test.db') | ||
|
||
def setUp(self): | ||
# Use the copied database file for testing | ||
self.app = create_app('test_chinook.db') | ||
# Create the Flask app | ||
self.app = create_app(self.db_uri) | ||
self.client = self.app.test_client() | ||
|
||
def test_get(self): | ||
def test_0get(self): | ||
response = self.client.get('/Artist') | ||
self.assertEqual(response.status_code, 200) | ||
artists = json.loads(response.data) | ||
self.assertIsInstance(artists, list) | ||
self.assertEqual(artists, []) | ||
|
||
def test_create(self): | ||
response = self.client.post('/Artist', json={'Name': 'Test Artist'}) | ||
response = self.client.post('/Artist', json={'ArtistId': 1, 'Name': 'Test Artist'}) | ||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(json.loads(response.data), {'message': 'Record created.'}) | ||
|
||
# Verify the creation by reading it back | ||
response = self.client.get('/Artist') | ||
self.assertEqual(response.status_code, 200) | ||
artists = json.loads(response.data) | ||
self.assertEqual(artists, [{'ArtistId': 1, 'Name': 'Test Artist'}]) | ||
|
||
def test_update(self): | ||
# First, create a record to update | ||
self.client.post('/Artist', json={'Name': 'Test Artist'}) | ||
self.client.post('/Artist', json={'ArtistId': 2, 'Name': 'Test Artist'}) | ||
|
||
# Then, update the record | ||
response = self.client.put('/Artist/1', json={'Name': 'Updated Artist'}) | ||
response = self.client.put('/Artist/2', json={'Name': 'Updated Artist'}) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(json.loads(response.data), {'message': 'Record updated.'}) | ||
|
||
def test_delete(self): | ||
# First, create a record to delete | ||
self.client.post('/Artist', json={'Name': 'Test Artist'}) | ||
self.client.post('/Artist', json={'ArtistId': 3, 'Name': 'Test Artist'}) | ||
|
||
# Then, delete the record | ||
response = self.client.delete('/Artist/1') | ||
response = self.client.delete('/Artist/3') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(json.loads(response.data), {'message': 'Record deleted.'}) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |