-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separate contributor's guide; Document reports api endpoints * Write test * Re-write test * Add jbuilder json views * Give credit h/t SO * Prevent data leakage by cleaning the db before this test. * Test reports view * Change search endpoint to regular index endpoint, clean code, refactor * Re-write, and can now filter on one or both params, which is actually good - more robust
- Loading branch information
Showing
16 changed files
with
212 additions
and
45 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,54 @@ | ||
# Contributor's Guide | ||
|
||
## Installation | ||
|
||
Install Ruby 2.2.5, probably using rbenv. | ||
|
||
Install PostgreSQL, probably using homebrew. | ||
|
||
Download the source code: | ||
|
||
```sh | ||
git clone [email protected]:data-creative/law-schools-reporter.git | ||
cd law-schools-reporter/ | ||
bin/setup # installs package dependencies and preps the database | ||
``` | ||
|
||
## Setup | ||
|
||
Seed the database with schools and employment data. | ||
|
||
```sh | ||
bundle exec rake db:seed | ||
``` | ||
|
||
## Developing | ||
|
||
Run interactive development console: | ||
|
||
```shell | ||
bin/rails c | ||
``` | ||
|
||
Serve locally: | ||
|
||
```shell | ||
bin/rails s | ||
``` | ||
|
||
## Testing | ||
|
||
Run tests: | ||
|
||
```shell | ||
bundle exec rspec spec/ | ||
``` | ||
|
||
## Deploying | ||
|
||
Gain access to the `law-school-reporter` heroku application. Then deploy: | ||
|
||
```sh | ||
git push heroku master # or ... | ||
git push heroku mybranch:master | ||
``` |
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,6 +1,13 @@ | ||
# Credits, Notes, and Reference | ||
|
||
### Previous Projects | ||
|
||
+ [ToneBase API](https://github.com/data-creative/tonebase-api/) | ||
|
||
### Rails | ||
|
||
+ [Routing via non-id attribute](https://stackoverflow.com/a/20773186) | ||
+ [Linking to show page and passing params](https://stackoverflow.com/a/8477394) | ||
+ [Linking to show page and passing the resource](https://stackoverflow.com/a/21708346/) | ||
+ [Rendering JSON within a controller spec](https://stackoverflow.com/a/10038777/670433) | ||
+ [Preventing data leakage across tests](https://stackoverflow.com/questions/9927671/rspec-database-cleaner-not-cleaning-correctly#comment15607311_9927671) |
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,48 +1,15 @@ | ||
# Law Schools Reporter | ||
|
||
## Installation | ||
## API Endpoints | ||
|
||
Install Ruby 2.2.5, probably using rbenv. | ||
### Schools | ||
|
||
Install PostgreSQL, probably using homebrew. | ||
+ GET `api/v1/schools` | ||
+ GET `api/v1/schools/:uuid` | ||
|
||
Download the source code: | ||
### Employment Reports | ||
|
||
```sh | ||
git clone [email protected]:data-creative/law-schools-reporter.git | ||
cd law-schools-reporter/ | ||
bin/setup # installs package dependencies and preps the database | ||
``` | ||
|
||
## Usage | ||
|
||
Seed the database with schools and employment data. | ||
|
||
```sh | ||
bundle exec rake db:seed | ||
``` | ||
|
||
## Developing | ||
|
||
Serve locally: | ||
|
||
```shell | ||
rails s | ||
``` | ||
|
||
Run tests: | ||
|
||
```shell | ||
bundle exec rspec spec/ | ||
``` | ||
|
||
## Deploying | ||
|
||
Gain access to the `law-school-reporter` heroku application. Then deploy: | ||
|
||
```sh | ||
git push heroku master # or ... | ||
git push heroku mybranch:master | ||
``` | ||
+ GET `api/v1/employment_reports` | ||
+ GET `api/v1/employment_reports?years[]=2016&years[]=2017&schools[]=25&schools[]=36` | ||
|
||
## [License](/LICENSE.md) |
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,22 @@ | ||
class Api::V1::EmploymentReportsController < ApplicationController | ||
|
||
# GET /api/v1/employment_reports | ||
# GET /api/v1/employment_reports.json | ||
# | ||
# GET /api/v1/employment_reports?years[]=2016&years[]=2017&schools[]=96&schools[]=132 | ||
def index | ||
@reports = EmploymentReport.all | ||
@reports = @reports.where(year: years) if params[:years] | ||
@reports = @reports.where(school_uuid: school_uuids) if params[:schools] | ||
end | ||
|
||
private | ||
|
||
def school_uuids | ||
params.require(:schools) | ||
end | ||
|
||
def years | ||
params.require(:years) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
json.extract! report, | ||
:school_uuid, | ||
:reported_school_name, | ||
:year, | ||
:total_grads, | ||
:total_employed, | ||
:employment_types, | ||
:employment_statuses, | ||
:employment_locations |
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 @@ | ||
json.array! @reports, partial: 'api/v1/employment_reports/report', as: :report |
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
49 changes: 49 additions & 0 deletions
49
spec/controllers/api/v1/employment_reports_controller_spec.rb
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,49 @@ | ||
require 'rails_helper' | ||
require_relative "../../../support/api/v1/response" | ||
|
||
RSpec.describe Api::V1::EmploymentReportsController, type: :controller do | ||
let(:valid_session){ {} } | ||
|
||
before(:all) do | ||
create(:school, :with_reports, uuid: 98, report_years: [2015, 2016, 2017]) # 2 matches | ||
create(:school, :with_reports, uuid: 76, report_years: [2011, 2012, 2016]) # 1 match | ||
create(:school, :with_reports, uuid: 23, report_years: [2015, 2016, 2017]) # 0 matches | ||
end | ||
|
||
describe "GET #index", "when not given any search parameters" do | ||
let(:response){ get(:index, params: {format: 'json'}, session: valid_session) } | ||
|
||
render_views # avoids JSON parsing error | ||
|
||
it "returns a success response" do | ||
expect(response).to be_success | ||
end | ||
|
||
it "should exclude non-matching resources" do # ensures proper configuration of this test :-) | ||
expect(EmploymentReport.count).to eql(3 + 3 + 3) | ||
end | ||
|
||
it "should include all resources" do | ||
expect(parsed_response.count).to eql(3 + 3 + 3) | ||
end | ||
end | ||
|
||
describe "GET #index", "when given both search parameters" do | ||
let(:search_params){ {years: ["2016", "2017"], schools: ["98", "76"]} } | ||
let(:response){ get(:index, params: search_params.merge(format: 'json'), session: valid_session) } | ||
|
||
render_views # avoids JSON parsing error | ||
|
||
it "returns a success response" do | ||
expect(response).to be_success | ||
end | ||
|
||
it "should exclude non-matching resources" do # ensures proper configuration of this test :-) | ||
expect(EmploymentReport.count).to eql(3 + 3 + 3) | ||
end | ||
|
||
it "should include matching resources" do | ||
expect(parsed_response.count).to eql(2 + 1 + 0) | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Api::V1::Response | ||
def parsed_response | ||
JSON.parse(response.body) | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include Api::V1::Response | ||
end |
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,5 +1,5 @@ | ||
module Api::V1::View | ||
def parsed_response | ||
def parsed_view | ||
JSON.parse(rendered) | ||
end | ||
end | ||
|
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,25 @@ | ||
require 'rails_helper' | ||
require_relative "../../../../support/api/v1/view" | ||
|
||
RSpec.describe "api/v1/employment_reports/index", type: :view do | ||
before(:each) do | ||
@reports = assign(:reports, [ create(:employment_report), create(:employment_report) ] ) | ||
render | ||
end | ||
|
||
it "renders a list of reports" do | ||
expect(parsed_view.count).to eql(@reports.count) | ||
expect(parsed_view.first.keys).to match_array( | ||
[ | ||
"school_uuid", | ||
"reported_school_name", | ||
"year", | ||
"total_grads", | ||
"total_employed", | ||
"employment_types", | ||
"employment_statuses", | ||
"employment_locations" | ||
] | ||
) | ||
end | ||
end |
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