Skip to content

Commit

Permalink
test: module 7 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
0xp3p3x0 committed Aug 24, 2018
1 parent 81dc52e commit ca9e1b2
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 42 deletions.
28 changes: 26 additions & 2 deletions jobs/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite3
import datetime

from flask import g, Flask, render_template
from flask import g, Flask, render_template, redirect, url_for

app = Flask(__name__)

Expand Down Expand Up @@ -35,4 +36,27 @@ def jobs():
@app.route('/job/<job_id>')
def job(job_id):
job = query_db('SELECT job.id, job.title, job.description, job.salary, employer.id as employer_id, employer.name as employer_name FROM job JOIN employer ON employer.id = job.employer_id WHERE job.id = ?', [job_id], True)
return render_template('job.html', job=job)
return render_template('job.html', job=job)

@app.route('/employer/<employer_id>')
def employer(employer_id):
employer = query_db('SELECT * FROM employer WHERE id=?', [employer_id], True)
jobs = query_db('SELECT job.id, job.title, job.description, job.salary FROM job JOIN employer ON employer.id = job.employer_id WHERE employer.id = ?', [employer_id])
reviews = query_db('SELECT review, rating, title, date, status FROM review JOIN employer ON employer.id = review.employer_id WHERE employer.id = ?', [employer_id])
return render_template('employer.html', employer=employer, jobs=jobs, reviews=reviews)

@app.route('/employer/<employer_id>/review', methods=('GET', 'POST'))
def review(employer_id):
if request.method == 'POST':
review = request.form['review']
rating = request.form['rating']
title = request.form['title']
status = request.form['status']
date = datetime.datetime.now().strftime("%m/%d/%Y")

db = get_db()
db.excute('INSERT INTO review (review, rating, title, date, status, employer_id) VALUES (?, ?, ?, ?, ?, ?)', (review, rating, title, date, status, employer_id))
db.commit()
return redirect(url_for('employer', employer_id=employer_id))

return render_template('review.html', employer_id=employer_id)
8 changes: 6 additions & 2 deletions jobs/templates/_macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
{% endmacro %}

{% macro show_jobs(jobs) %}
{% for job in jobs %}
{{ show_job(job) }}
<div class="columns is-multiline">
{% for job in jobs %}
<div class="column is-half">
{{ show_job(job) }}
</div>
{% endfor %}
</div>
{% endmacro %}
40 changes: 40 additions & 0 deletions jobs/templates/employer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends "layout.html" %}

{% block content %}
<div>
<h1>{{ employer['name'] }}</h1>
<div class="description">
<p>{{ employer['description'] }}</p>
</div>
</div>
<h2>Jobs</h2>
{{ show_jobs(jobs) }}

<h2>Reviews</h2>
{% for review in reviews %}
<div class="box">
<article class="media">
<div class="media-left">
{% for _ in range(1, review['rating']): %}
<span class="fa fa_star checked"></span>
{% endfor %}
</div>
<div class="media-content">
<div class="content">
<p>
<strong>{{ review['title'] }}</strong>
<small>{{ review['status'] }}</small>
<small>{{ review['date'] }}</small>
{{ review['review'] }}
</p>
</div>
</div>
</article>
</div>
{% endfor %}
<div class="columns">
<div class="column">
<a href="{{ url_for('review', employer_id=employer['id']) }}" class="button is-info is-pulled-right">Create Review</a>
</div>
</div>
{% endblock %}
4 changes: 3 additions & 1 deletion jobs/templates/job.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% extends "layout.html" %}

{{ show_job(job) }}
{% block content %}
{{ show_job(job) }}
{% endblock %}
52 changes: 52 additions & 0 deletions jobs/templates/review.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% extends "layout.html" %}

{% block content %}
<h1>New Review</h1>
<form method="post">
<div class="field">
<label class="label">Job Title</label>
<div class="control">
<input class="input" type="text" name="title" id="title" required>
</div>
</div>
<div class="field">
<label class="label">Rating</label>
<div class="control">
<div class="select">
<select name="rating" id="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
</div>
<div class="field">
<label class="label">Review</label>
<div class="control">
<textarea class="textarea" name="review" id="review" required></textarea>
</div>
</div>
<div class="field">
<label class="label">Employment Status</label>
<div class="control">
<div class="select">
<select name="status" id="status">
<option value="former">Former</option>
<option value="current">Current</option>
</select>
</div>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-link">Submit</button>
</div>
<div class="control">
<a href="{{ url_for('employer', employer_id=employer_id) }}" class="button is-text">Cancel</a>
</div>
</div>
</form>
{% endblock %}
31 changes: 19 additions & 12 deletions tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ In `<p>` tag add the following:

@pytest.mark.show_jobs_macro_for_loop Still in `_macros.html` and in the body of the `show_jobs` macro add the following HTML:

- Add a `<div>` with two classes `columns` and `is_multiline`.
- Add a `<div>` with two classes `columns` and `is-multiline`.
- In this `<div>` add a `for in` loop that loops through all jobs. **Note: Use the `{% %}` template syntax, don’t forget about ending the `for` loop.**

## 4.8 - Show Jobs Macro For Loop Body
Expand Down Expand Up @@ -303,7 +303,7 @@ At this point you can see all jobs on the homepage:

In the file use an `extends` template tag to inherit `layout.html`.

After the `extends` tag add a call to the `show_job` macro passing in `job`. **Note: Use the `{{}}` for the macro call.**
After the `extends` tag add a template `block` called `content`. In the block call the `show_job` macro passing in `job`. **Note: Use the `{{}}` for the macro call.**

## 5.2 - Job Route Function

Expand Down Expand Up @@ -363,7 +363,7 @@ To the top of the file inherit from the `layout.html` template by using an `exte

## 6.4 - Employer Template Reviews

@pytest.mark.employer_template_reviews Open `employer.html` and find the review `<h2>`, remove the comment surrounding the empty `{% %}` template tag. To this tag add a `for in` loop to loop through all `reviews`. Add the `endfor` directive to the second empty `{% %}` template tag, don't forget to the remove the comment.
@pytest.mark.employer_template_reviews Still in `employer.html` find the review `<h2>`, remove the comment surrounding the empty `{% %}` template tag. To this tag add a `for in` loop to loop through all `reviews`. Add the `endfor` directive to the second empty `{% %}` template tag, don't forget to the remove the comment.

## 6.5 - Employer Template Review Stars

Expand Down Expand Up @@ -432,31 +432,38 @@ At this point you can see an individual employer:

# Module 07 - Employer Reviews

## 7.1 - Review Route
## 7.1 - Review Template

@pytest.mark.review_template To display a review form, create a new file called `review.html` in the templates folder. Open `templates.html`, find the appropriate block of HTML and copy and paste it to `review.html`.

Inherit from the `layout.html` template by using an `extends` template tag.

Find the cancel anchor tag. Add an `href` attribute with a value of `{{ url_for('employer', employer_id=employer_id) }}`.

## 7.2 - Review Route

@pytest.mark.app_review_route In `app.py` below the `employer` function create a new function called `review`. Add `employer_id` to the parameter list.

Add a route decorator with a URL pattern of `/employer/<employer_id>/review`. Also add a keyword argument `methods` set to a tuple with two values: `'GET'` and `'POST'`.

In the body of the function return the `render_template` function passing in the `review.html` template and a keyword argument of `employer_id=employer_id`.

## 7.2 - POST Request Check
## 7.3 - POST Request Check

@pytest.mark.app_review_post_request_check In the body of the `review` above the render_template function call, create an `if` statement that checks if `request.method` is `'POST'`.
@pytest.mark.app_review_post_request_check In the body of the `review` above the `render_template` function call, create an `if` statement that checks if `request.method` is equal to `'POST'`.

- In the `if` statement create four variables `review`, `rating`, `title`, and `status`. Set them equal to their respective `request.form` values i.e. `request.form['review']`.
- Create a `date` variable assign it todays date formatted like '08/10/2018'. **Hint: Use `now()` and `strftime("%m/%d/%Y")`. If you use `now()` add an `import datetime` statement to the top of `app.py`.**

## 7.3 - Insert Review
## 7.4 - Insert Review

@pytest.mark.app_review_insert_review Still in the `review` function below the variables in the `if` statement connect to the database, insert the form values, and commit the changes. Follow these steps:
@pytest.mark.app_review_insert_review Still in the `review` function below the variables in the `if` statement, connect to the database, insert the form values, and commit the changes. Follow these steps:

- Assign a `db` variable to a call to `get_db()`
- `execute` the following SQL statement: `'INSERT INTO review (review, rating, title, date, status, employer_id) VALUES (?, ?, ?, ?, ?, ?)'` passing the values: `(review, rating, title, date, status, employer_id)`
- `execute` the following SQL statement on `db`: `'INSERT INTO review (review, rating, title, date, status, employer_id) VALUES (?, ?, ?, ?, ?, ?)'` passing the values: `(review, rating, title, date, status, employer_id)`
- `commit` the changes to the database.
- Return a redirect taking the user back to the employer page. **Hint: use `redirect()` and `url_for()` (pass a keyword argument of `employer_id=employer_id`) both of which need to be imported from flask.**

## 7.4 - Review Form Cancel

@pytest.mark.review_form_cancel Open `review.html` and find the cancel anchor tag. Add an `href` attribute with a value of `{{ url_for('employer', employer_id=employer_id) }}`.
## 7.5 - Employer Review Button

@pytest.mark.employer_review_button Open the `employer.html` template and find the anchor tag to create a review. Add an `href` attribute with a value of `{{ url_for('review', employer_id=employer['id']) }}`.
14 changes: 10 additions & 4 deletions tests/test_module4.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_show_job_macro_html_module4():
def test_show_job_macro_header_module4():
assert template_exists('_macros'), 'The `_macros.html` template does not exist in the `templates` folder.'
assert 'job:job_id:job:id' in template_functions('_macros', 'url_for'), 'Looks like the job title link `href` is incorrect.'
assert 'job:title' in template_macro_variables('_macros', 'show_job'), 'Looks like the job title link does not have content.'
assert 'job:title' in template_variables('_macros'), 'Looks like the job title link does not have content.'

@pytest.mark.show_job_macro_body
def test_show_job_macro_body_module4():
assert template_exists('_macros'), 'The `_macros.html` template does not exist in the `templates` folder.'
assert 'employer:employer_id:job:employer_id' in template_functions('_macros', 'url_for'), 'Looks like the job title link `href` is incorrect.'
assert 'job:employer_name' in template_macro_variables('_macros', 'show_job'), 'Are you showing the employer name?'
assert 'job:salary' in template_macro_variables('_macros', 'show_job'), 'Are you showing the job salary?'
assert 'job:description' in template_macro_variables('_macros', 'show_job'), 'Are you showing the job description?'
assert 'job:employer_name' in template_variables('_macros'), 'Are you showing the employer name?'
assert 'job:salary' in template_variables('_macros'), 'Are you showing the job salary?'
assert 'job:description' in template_variables('_macros'), 'Are you showing the job description?'

@pytest.mark.show_jobs_macro_definition
def test_show_jobs_macro_definition_module4():
Expand All @@ -43,11 +43,17 @@ def test_show_jobs_macro_definition_module4():
@pytest.mark.show_jobs_macro_for_loop
def test_show_jobs_macro_for_loop_module4():
assert template_exists('_macros'), 'The `_macros.html` template does not exist in the `templates` folder.'
html = template_macro_soup('_macros', 'show_jobs')
div = html.select('div.columns.is-multiline')
assert len(div) == 1, 'Has a `<div>` with classes of `columns` and `is-multiline` been added to the `show_jobs` macro?'
assert 'job:jobs' in show_jobs_for(), 'Does the `show_jobs` macro contain a `for` loop?'

@pytest.mark.show_jobs_macro_for_loop_body
def test_show_jobs_macro_for_loop_body_module4():
assert template_exists('_macros'), 'The `_macros.html` template does not exist in the `templates` folder.'
html = template_macro_soup('_macros', 'show_jobs')
div = html.select('div.column.is-half')
assert len(div) == 1, 'Has a `<div>` with classes of `column` and `is-half` been added to the `show_jobs` macro `for` loop body?'
assert 'show_job:job' in show_jobs_for(), 'Does the `show_jobs` macro call `show_job`?'

@pytest.mark.import_macros
Expand Down
2 changes: 2 additions & 0 deletions tests/test_module5.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
def test_app_job_template_module5():
assert template_exists('job'), 'The `job.html` template does not exist in the `templates` folder.'
assert 'layout.html' in template_extends('job'), 'The `job.html` template does not extend `layout.html`.'
assert 'content' in template_block('job'), 'Have you added a template `block` called `content`?'
assert 'show_job:job' in template_functions('job', 'show_job'), 'Have you call the `show_job` macro in the `job.html` file?'
assert False

@pytest.mark.app_job_route
def test_app_job_route_module5():
Expand Down
49 changes: 39 additions & 10 deletions tests/test_module6.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,72 @@
import sys

from jobs import app
from .utils import *

@pytest.mark.employer_template
def test_employer_template_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
el = template_data('employer').select('.box .media .media-content .content')
assert len(el) == 1, 'Has the `HTML` from `templates.html` been copied to the `employer.html` template?'
assert 'layout.html' in template_extends('employer'), 'The `employer.html` template does not extend `layout.html`.'

@pytest.mark.employer_template_details
def test_employer_template_details_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
assert 'employer:name' in template_variables('employer'), "Looks like the `employer['name']` is not present in the template."
assert 'employer:description' in template_variables('employer'), "Looks like the `employer['description']` is not present in the template."

@pytest.mark.employer_template_all_jobs
def test_employer_template_all_jobs_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
assert 'show_jobs:jobs' in template_functions('employer', 'show_jobs'), 'Have you called the `show_jobs` macro in the `employer.html` file?'

@pytest.mark.employer_template_reviews
def test_employer_template_reviews_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
assert 'review:reviews' in employer_for(), 'Have you created a `for` loop that cycles through `reviews`?'

@pytest.mark.employer_template_review_stars
def test_employer_template_review_stars_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
assert '_:range:1:review:rating' in employer_for(), 'Have you created a `for` loop that cycles through `reviews`?'
el = template_data('employer').select('.fa.fa_star.checked')
assert len(el) == 1, 'Has the star `<span>` been added to the `employer.html` template?'

@pytest.mark.employer_template_review_details
def test_employer_template_review_details_module6():
pass
assert template_exists('employer'), 'The `employer.html` template does not exist in the `templates` folder.'
assert 'review:title' in template_variables('employer'), "Looks like the `review['title']` is not present in the template."
assert 'review:status' in template_variables('employer'), "Looks like the `review['status']` is not present in the template."
assert 'review:date' in template_variables('employer'), "Looks like the `review['date']` is not present in the template."
assert 'review:review' in template_variables('employer'), "Looks like the `review['review']` is not present in the template."

@pytest.mark.app_employer_route
def test_app_employer_route_module6():
pass
assert 'employer' in dir(app), 'Have you created the `employer` function?'
assert 'route:/employer/<employer_id>' in get_functions(app.employer)
result = [item for item in get_functions(app.employer) if item.startswith('render_template:employer.html')]
assert len(result) == 1, 'Have you called the `render_template` function.'

@pytest.mark.app_employer_route_employers
def test_app_employer_route_employers_module6():
pass
assert 'employer' in dir(app), 'Have you created the `employer` function?'
assert 'employer_id' in inspect.getfullargspec(app.employer).args, 'Have you added the correct parameters to the `employer` function parameter list?'
query_db = 'query_db:SELECT * FROM employer WHERE id=?:employer_id:True'
assert query_db in get_functions(app.employer), '`query_db` has not been called or has the wrong parameters.'
result = [item for item in get_functions(app.employer) if item.startswith('render_template:employer.html:employer:employer')]
assert len(result) == 1, 'Have you added `employer` to the `render_template` call.'

@pytest.mark.app_employer_route_jobs
def test_app_employer_route_jobs_module6():
pass
query_db = 'query_db:SELECT job.id, job.title, job.description, job.salary FROM job JOIN employer ON employer.id = job.employer_id WHERE employer.id = ?:employer_id'
assert query_db in get_functions(app.employer), '`query_db` has not been called or has the wrong parameters.'
result = [item for item in get_functions(app.employer) if item.startswith('render_template:employer.html:employer:employer:jobs:jobs')]
assert len(result) == 1, 'Have you added `jobs` to the `render_template` call.'

@pytest.mark.app_employer_route_reviews
def test_app_employer_route_reviews_module6():
pass
query_db = 'query_db:SELECT review, rating, title, date, status FROM review JOIN employer ON employer.id = review.employer_id WHERE employer.id = ?:employer_id'
assert query_db in get_functions(app.employer), '`query_db` has not been called or has the wrong parameters.'
result = [item for item in get_functions(app.employer) if item.startswith('render_template:employer.html:employer:employer:jobs:jobs:reviews:reviews')]
assert len(result) == 1, 'Have you added `reviews` to the `render_template` call.'
Loading

0 comments on commit ca9e1b2

Please sign in to comment.