Commit 7d787538 authored by Lucas Kanashiro's avatar Lucas Kanashiro

Merge branch 'feature/16-continuous-integration' into 'development'

Adds Continuous Integration Pipeline

See merge request !10
parents 4abcb429 62ee6521
Pipeline #52465 passed with stages
in 5 minutes and 19 seconds
stages:
- Static Analysis
- Test
lint-flake8:
image: python:3.7
stage: Static Analysis
before_script:
- python3 -m pip install flake8
script:
- flake8 --ignore F401,E402
unit-test:
services:
- postgres:11.3
image: python:3.7
stage: Test
variables:
POSTGRES_USER: finder
POSTGRES_PASSWORD: password
POSTGRES_DB: finder_test
APP_SETTINGS: config.DevelopmentConfig
DATABASE_TEST_URL: postgresql://finder:password@postgres:5432/finder_test
script:
- pip3 install -r requirements.txt
- python3 manage.py test
\ No newline at end of file
from flask import render_template
from app import app
from app.models import Provider, ProviderSchema, provider_schema, providers_schema
from app.models import Image, ImageSchema, image_schema, images_schema
from app.models import Provider
from app.models import Image
@app.route("/")
def index():
all_providers = Provider.query.all()
return render_template('home.html', len = len(all_providers), Providers = all_providers)
return render_template('home.html',
len=len(all_providers),
Providers=all_providers)
@app.route("/provider/<id>")
def provider(id):
provider = Provider.query.get(id)
all_images_from_providers = Image.query.filter_by(provider_id=id).all()
return render_template('provider.html', Provider = provider,
len = len(all_images_from_providers),
Images = all_images_from_providers)
return render_template('provider.html',
Provider=provider,
len=len(all_images_from_providers),
Images=all_images_from_providers)
@app.route("/image/<id>")
def image(id):
image = Image.query.get(id)
return render_template('image.html', Image = image, Provider = image.provider)
\ No newline at end of file
return render_template('image.html',
Image=image,
Provider=image.provider)
from app import app
from app import db
from flask import Flask, request, jsonify
from app.models import Image, ImageSchema, image_schema, images_schema
from flask import request, jsonify
from app.models import Image, image_schema, images_schema
# endpoint to create new image
......@@ -18,16 +18,16 @@ def add_image():
created_at = request.json['created_at']
provider_id = request.json['provider_id']
new_image = Image( name,
arch,
release,
img_type,
vendor,
version,
job_url,
package,
created_at,
provider_id)
new_image = Image(name,
arch,
release,
img_type,
vendor,
version,
job_url,
package,
created_at,
provider_id)
db.session.add(new_image)
db.session.commit()
......@@ -85,4 +85,4 @@ def image_delete(id):
db.session.delete(image)
db.session.commit()
return image_schema.jsonify(image)
\ No newline at end of file
return image_schema.jsonify(image)
from app import app
from app import db
from flask import Flask, request, jsonify
from app.models import Provider, ProviderSchema, provider_schema, providers_schema
from flask import request, jsonify
from app.models import Provider, provider_schema, providers_schema
# endpoint to create new provider
......@@ -9,7 +9,6 @@ from app.models import Provider, ProviderSchema, provider_schema, providers_sche
def add_provider():
name = request.json['name']
body = request.json['body']
new_provider = Provider(name, body)
db.session.add(new_provider)
......@@ -54,4 +53,4 @@ def provider_delete(id):
db.session.delete(provider)
db.session.commit()
return provider_schema.jsonify(provider)
\ No newline at end of file
return provider_schema.jsonify(provider)
from app import db
from app import ma
class Provider(db.Model):
__tablename__ = "providers"
id = db.Column(db.Integer, primary_key=True)
......@@ -15,11 +16,13 @@ class Provider(db.Model):
def __repr__(self):
return '<Provider %r>' % self.name
class ProviderSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ('name', 'body')
provider_schema = ProviderSchema()
providers_schema = ProviderSchema(many=True)
......@@ -41,17 +44,17 @@ class Image(db.Model):
provider = db.relationship('Provider', foreign_keys=provider_id)
def __init__( self,
name,
arch,
release,
img_type,
vendor,
version,
job_url,
package,
created_at,
provider_id):
def __init__(self,
name,
arch,
release,
img_type,
vendor,
version,
job_url,
package,
created_at,
provider_id):
self.name = name
self.arch = arch
self.release = release
......@@ -66,19 +69,21 @@ class Image(db.Model):
def __repr__(self):
return '<Image %r>' % self.name
class ImageSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ( 'name',
'arch',
'release',
'img_type',
'vendor',
'version',
'job_url',
'package',
'created_at',
'provider_id')
fields = ('name',
'arch',
'release',
'img_type',
'vendor',
'version',
'job_url',
'package',
'created_at',
'provider_id')
image_schema = ImageSchema()
images_schema = ImageSchema(many=True)
\ No newline at end of file
images_schema = ImageSchema(many=True)
from flask_testing import TestCase
from app import app, db
import os
class BaseTestCase(TestCase):
......
......@@ -18,11 +18,10 @@ class TestConfig(TestCase):
self.assertTrue(app.config['DEBUG'])
self.assertTrue(app.config['DEVELOPMENT'])
self.assertFalse(app.config['SQLALCHEMY_TRACK_MODIFICATIONS'])
self.assertTrue(
app.config['SQLALCHEMY_DATABASE_URI'] ==
os.environ.get('DATABASE_URL')
)
sqlalchemy_db_uri = app.config['SQLALCHEMY_DATABASE_URI']
database_url = os.environ.get('DATABASE_URL')
self.assertTrue(sqlalchemy_db_uri == database_url)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
unittest.main()
......@@ -27,7 +27,6 @@ class TestImageService(BaseTestCase):
"created_at": "2017-08-09",
"provider_id": 1
}
headers = {'Content-Type': 'application/json'}
response = self.client.post("/api/image",
......@@ -46,7 +45,6 @@ class TestImageService(BaseTestCase):
self.assertIn('2017-08-09T00:00:00+00:00', data['created_at'])
self.assertEqual(1, data['provider_id'])
def test_get_image(self):
"""Ensure the /image route behaves correctly."""
......@@ -54,16 +52,16 @@ class TestImageService(BaseTestCase):
db.session.add(provider)
db.session.commit()
image = Image( name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
image = Image(name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
db.session.add(image)
db.session.commit()
......@@ -82,7 +80,6 @@ class TestImageService(BaseTestCase):
self.assertIn('2017-08-09T00:00:00+00:00', data['created_at'])
self.assertEqual(1, data['provider_id'])
def test_put_image(self):
"""Ensure the /image route behaves correctly."""
......@@ -90,20 +87,19 @@ class TestImageService(BaseTestCase):
db.session.add(provider)
db.session.commit()
image = Image( name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
image = Image(name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
db.session.add(image)
db.session.commit()
args = {
"name": "name_change",
"arch": "arch_change",
......@@ -120,8 +116,8 @@ class TestImageService(BaseTestCase):
headers = {'Content-Type': 'application/json'}
response = self.client.put("/api/image/1",
data=json.dumps(args),
headers=headers)
data=json.dumps(args),
headers=headers)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertIn('name_change', data['name'])
......@@ -135,7 +131,6 @@ class TestImageService(BaseTestCase):
self.assertIn('2018-08-09T00:00:00+00:00', data['created_at'])
self.assertEqual(1, data['provider_id'])
def test_delete_image(self):
"""Ensure the /image route behaves correctly."""
......@@ -143,16 +138,16 @@ class TestImageService(BaseTestCase):
db.session.add(provider)
db.session.commit()
image = Image( name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
image = Image(name='name',
arch='arch',
release='release',
img_type='img_type',
vendor='vendor',
version='version',
job_url='job_url',
package='package',
created_at='2017-08-09',
provider_id=1)
db.session.add(image)
db.session.commit()
......@@ -173,4 +168,4 @@ class TestImageService(BaseTestCase):
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
unittest.main()
......@@ -24,7 +24,6 @@ class TestProviderService(BaseTestCase):
self.assertIn('provider', data['name'])
self.assertIn('test', data['body'])
def test_get_provider(self):
"""Ensure the /provider route behaves correctly."""
......@@ -38,7 +37,6 @@ class TestProviderService(BaseTestCase):
self.assertIn('provider', data['name'])
self.assertIn('test', data['body'])
def test_put_provider(self):
"""Ensure the /provider route behaves correctly."""
......@@ -53,14 +51,13 @@ class TestProviderService(BaseTestCase):
headers = {'Content-Type': 'application/json'}
response = self.client.put("/api/provider/1",
data=json.dumps(args),
headers=headers)
data=json.dumps(args),
headers=headers)
data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertIn('provider_change', data['name'])
self.assertIn('test_change', data['body'])
def test_delete_provider(self):
"""Ensure the /provider route behaves correctly."""
......@@ -74,5 +71,6 @@ class TestProviderService(BaseTestCase):
self.assertIn('provider', data['name'])
self.assertIn('test', data['body'])
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
unittest.main()
......@@ -19,4 +19,4 @@ class TestingConfig(BaseConfig):
"""Testing configuration"""
TESTING = True
PRESERVE_CONTEXT_ON_EXCEPTION = False
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_TEST_URL")
\ No newline at end of file
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_TEST_URL")
from app import manager, app, db
from app import app, db
from flask.cli import FlaskGroup
import sys
import unittest
cli = FlaskGroup(app)
@cli.command()
def test():
"""Runs the tests without code coverage"""
......@@ -14,6 +15,7 @@ def test():
return 0
sys.exit(result)
@cli.command('recreate_db')
def recreate_db():
db.drop_all()
......
from app import app
if __name__ == "__main__":
app.run()
\ No newline at end of file
app.run()
......@@ -8,7 +8,7 @@ data = json.load(json_file)
for provider in data['providers']:
print('Name: ' + provider['name'])
print('Body: ' + provider['body'])
print('')
print('')
db.session.add(Provider(name=provider['name'], body=provider['body']))
for image in data['images']:
......@@ -22,16 +22,16 @@ for image in data['images']:
print('package: ' + image['package'])
print('created_at: ' + image['created_at'])
print('provider_id: ' + image['provider_id'])
print('')
db.session.add(Image(name=image['name'],
arch=image['arch'],
release=image['release'],
img_type=image['img_type'],
vendor=image['vendor'],
version=image['version'],
job_url=image['job_url'],
package=image['package'],
created_at=image['created_at'],
provider_id=image['provider_id']))
print('')
db.session.add(Image(name=image['name'],
arch=image['arch'],
release=image['release'],
img_type=image['img_type'],
vendor=image['vendor'],
version=image['version'],
job_url=image['job_url'],
package=image['package'],
created_at=image['created_at'],
provider_id=image['provider_id']))
db.session.commit()
\ No newline at end of file
db.session.commit()
......@@ -5,4 +5,4 @@ POSTGRES_HOST=db
DB_PORT=5432
APP_SETTINGS=config.DevelopmentConfig
DATABASE_URL=postgresql://finder:password@db:5432/finder
DATABASE_TEST_URL=postgresql://finder:password@db:5432/finder_test
DATABASE_TEST_URL=postgresql://finder:password@db:5432/finder_test
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment