Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for filtering pd_workshops #13663

Merged
merged 7 commits into from
Mar 15, 2017
Merged

Add API for filtering pd_workshops #13663

merged 7 commits into from
Mar 15, 2017

Conversation

aoby
Copy link
Contributor

@aoby aoby commented Mar 8, 2017

No description provided.

@aoby aoby requested a review from mehalshah March 8, 2017 20:10
@aoby aoby changed the title Add api for filtering pd_workshops Add API for filtering pd_workshops Mar 8, 2017
workshops = workshops.start_on_or_after(ensure_date(params[:start])) if params[:start]
workshops = workshops.start_on_or_before(ensure_date(params[:end])) if params[:end]
workshops = workshops.where(course: params[:course]) if params[:course]
workshops = workshops.where(organizer: params[:organizer]) if params[:organizer]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be organizer_id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we might as well be explicit. Will update

# returns [String] the original value
def ensure_date(date_str)
# will raise ArgumentError if it's not a valid date string
DateTime.parse(date_str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to online this
DateTime.parse(date_str) && date_str

@@ -192,7 +192,8 @@ def self.in_state(state)
when STATE_ENDED
where.not(started_at: nil).where.not(ended_at: nil)
else
raise "Unrecognized state: #{state}"
raise "Unrecognized state: #{state}" if error_on_bad_state
none
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all workshops should be in a good state I'd suggest logging something that should be picked up by HoneyBadger. It might be enough to just log an ERROR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a query argument, not the workshop state. The workshop itself is always in a valid state

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to know if someone passes a bogus argument to the API

@@ -103,6 +103,71 @@ class Api::V1::Pd::WorkshopsControllerTest < ::ActionController::TestCase
assert_equal workshop_in_progress.id, response[0]['id']
end

# Action: filter
test 'admins can filter' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Teachers and students cannot filter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's included in the all_forbidden (see line 526 below). Do you want an explicit test case for this too?

FakeController.any_instance.stubs(params: @params)

@user = mock
@user.expects(admin?: true)
@user.stubs(admin?: true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can always just update this attribute instead of stub it. Doesn't really matter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@user is a mock (see previous line), not a real user.

test 'filter_workshops with state' do
expects(:in_state).with('Not Started', error_on_bad_state: false)
params state: 'Not Started'
@controller.filter_workshops @workshop_query
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert that you get stuff back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is stubbed here so nothing useful is returned. These test cases are verifying that the expected queries are made via. the expects

Or do you mean for the default test on line 74? In that case, with no expectation, any filter will fail the test. I can add a comment explaining this, and/or do some more explicit validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments, and rearranged the test expectations. PTAL


# Orders by the workshop state, in order: Not Started, In Progress, Ended
# @param :desc [Boolean] optional - when true, sort descending
def self.order_by_state(desc: false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little twitchy at all of this raw SQL - I feel like there is a way to write this in ActiveRecord

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retracting - you can't get around CASE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't feel great that this logic is duplicated in SQL and in the model, but I couldn't come up with a better solution short of saving the state in the DB and I didn't like that either. This logic is pretty small and unlikely to change so I think it's ok.

@@ -0,0 +1,33 @@
class Api::V1::Pd::WorkshopDownloadSerializer < ActiveModel::Serializer
attributes :id, :status, :dates, :organizer_name, :organizer_email, :location_address,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

location_name ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -68,10 +66,12 @@ class FakeController < ::ApplicationController
expects(:not).with(course: Pd::Workshop::COURSE_CSF)

params course: '-csf'
@controller.load_filtered_ended_workshops
load_filtered_ended_workshops
end

test 'filter_workshops default' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general you're verifying that the filters you expect to be applied are actually being applied, but not asserting on the final output. Expected?

@aoby aoby merged commit a607b7e into staging Mar 15, 2017
@aoby aoby deleted the pd-workshop-filters-api branch March 15, 2017 17:51
@aoby aoby restored the pd-workshop-filters-api branch March 15, 2017 18:17
@aoby aoby deleted the pd-workshop-filters-api branch March 15, 2017 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants