-
Notifications
You must be signed in to change notification settings - Fork 4
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
Publish questionnaires via PUT #1
Open
hbmaclean
wants to merge
2
commits into
ThePerlShop:main
Choose a base branch
from
hbmaclean:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -25,44 +25,106 @@ BEGIN { | |
} | ||
use Catalyst::Test 'TPS::Questionnaire'; | ||
|
||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content), | ||
{ | ||
result => { count => 0, list => [] }, | ||
status => 'ok', | ||
}, | ||
'GET /api/questionnaire -> ok', | ||
); | ||
|
||
is( | ||
request('/api/questionnaire/1')->code, | ||
404, | ||
'GET /api/questionnaire/1 -> 404', | ||
); | ||
|
||
is( | ||
decode_json(request( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was failing. I initially assumed it was me and my new environment; but removing decode_json() here fixed it. |
||
POST '/api/questionnaire', | ||
confirm_no_initial_questionnaires(); | ||
post_new_questionnaire(); | ||
publish_unpublished_questionnaire(); | ||
|
||
done_testing(); | ||
|
||
sub confirm_no_initial_questionnaires { | ||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content), | ||
{ | ||
result => { count => 0, list => [] }, | ||
status => 'ok', | ||
}, | ||
'GET /api/questionnaire -> ok', | ||
); | ||
|
||
# And thus, cannot GET id 1 | ||
is( | ||
request('/api/questionnaire/1')->code, | ||
404, | ||
'GET /api/questionnaire/1 -> 404', | ||
); | ||
} | ||
|
||
|
||
sub post_new_questionnaire { | ||
is( | ||
request( | ||
POST '/api/questionnaire', | ||
Content_Type => 'application/json', | ||
Content => encode_json({ | ||
title => 'Sample Questionnaire', | ||
is_published => \1, | ||
questions => [ 'Foo?', 'Bar?' ] | ||
}) | ||
)->code, | ||
200, | ||
'POST /api/questionnaire -> ok', | ||
); | ||
|
||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content), | ||
{ | ||
result => { count => 1, list => [ | ||
{ id => 1, title => 'Sample Questionnaire' }, | ||
] }, | ||
status => 'ok', | ||
}, | ||
'GET /api/questionnaire -> ok', | ||
); | ||
} | ||
|
||
sub publish_unpublished_questionnaire { | ||
is( | ||
request( | ||
POST '/api/questionnaire', | ||
Content_Type => 'application/json', | ||
Content => encode_json({ | ||
title => 'Sample Questionnaire', | ||
is_published => \1, | ||
questions => [ 'Foo?', 'Bar?' ], | ||
title => 'New Questionnaire', | ||
is_published => \0, | ||
questions => [ 'When?', 'Why?' ] | ||
}) | ||
)->code), | ||
200, | ||
'POST /api/questionnaire -> ok', | ||
); | ||
|
||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content), | ||
{ | ||
result => { count => 1, list => [ | ||
{ id => 1, title => 'Sample Questionnaire' }, | ||
] }, | ||
status => 'ok', | ||
}, | ||
'GET /api/questionnaire -> ok', | ||
); | ||
)->code, | ||
200, | ||
'POST /api/questionnaire (unpublished) - ok' | ||
); | ||
|
||
# Cannot see it when unpublished: | ||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content), | ||
{ | ||
result => { count => 1, list => [ | ||
{ id => 1, title => 'Sample Questionnaire' }, | ||
] }, | ||
status => 'ok', | ||
}, | ||
'Still only one questionnaire displayed' | ||
); | ||
|
||
done_testing(); | ||
is( | ||
request(PUT '/api/questionnaire/2', Content_Type => 'application/json')->code, | ||
200, | ||
'PUT /api/questionnaire -> ok (published!)' | ||
); | ||
|
||
my $r = request('/api/questionnaire'); | ||
|
||
is( | ||
decode_json(request('/api/questionnaire')->decoded_content)->{result}{count}, | ||
2, | ||
'Second questionnaire displayed' | ||
); | ||
|
||
# Confirm cannot re-publish. | ||
# Underlying carp() goes to STDERR - capture and inspect. | ||
my @err; | ||
local $SIG{__WARN__} = sub { push @err, $_[0] }; | ||
eval { request(PUT '/api/questionnaire/2', Content_Type => 'application/json') }; | ||
like($err[-1], qr/already published/, 'Cannot re-publish published questionnaire'); | ||
|
||
eval { request(PUT '/api/questionnaire/2222222222222222222', Content_Type => 'application/json') }; | ||
like($err[-1], qr/not found, cannot be published/, 'Cannot publish non-existant questionnaire'); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more: Depending on the scope and anticipated lifetime of the project, I might rather write a generic "update()" method that takes a hashref of columns and values to be updated. In fact I'd prefer that save() were generic, functioning like a merge statement (insert or update) - so that the "new vs. update" thinking is when the object is instantiated; and then the object's setters can be used; and finally saved... But again, it depends on how many data changes are likely and how long the project will remain.