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 tests for controllers #615

Merged
merged 19 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.histogram_cm_per_pixel

def self.histogram_cm_per_pixel_in_tens
e = Export.where('cm_per_pixel != "" AND cm_per_pixel < 500')
.order(cm_per_pixel: 'desc')
.order('cm_per_pixel desc')

Choose a reason for hiding this comment

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

I would prefer cm_per_pixel: :desc because it is rails way.
Would love to hear from you 😄 .

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I think this is related to our MySQL adapter gem version.

hist = []
(0..e.first.cm_per_pixel / 10.to_i).each do |bin|
hist[bin] = 0
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@
end

end

9 changes: 9 additions & 0 deletions test/fixtures/annotations.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
one:
id: 3
map_id: 1
user_id: 1
annotation_type: polygon
coordinates: [12,77]
text: This is an annotation
created_at: <%= Time.now %>

lorem:
id: 1
map_id: 1
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/exports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
bands_string: ''
cm_per_pixel: 10

two:
two:
map_id: 2
user_id: 1
bands_string: ''
cm_per_pixel: 5
status: 'complete'

three:
three:
map_id: 3
user_id: 2
bands_string: ''
cm_per_pixel: 3
status: 'failed'
70 changes: 70 additions & 0 deletions test/functional/annotations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'test_helper'

class AnnotationsControllerTest < ActionController::TestCase
# called before every single test
def setup
@map = maps(:saugus)
@annotation = annotations(:one)
end

# called after every single test
def teardown; end

test 'should create annotation if logged in ' do
before_count = @map.annotations.count
session[:user_id] = 1
post :create,
format: :json,
map_id: 1,
annotation: {
properties: {
annotation_type: 'polyline',
textContent: 'Some cool text'
},
geometry: { coordinates: [10, 33] }
}

@map.reload
assert_response 302
assert_not_equal before_count, @map.annotations.count
assert_redirected_to map_annotation_url(@map, @map.annotations.last)
end

test 'should create annotation if not logged in ' do
before_count = @map.annotations.count
post :create,
format: :json,
map_id: 1,
annotation: {
properties: {
annotation_type: 'polyline',
textContent: 'Some cool text'
},
geometry: { coordinates: [10, 33] }
}

@map.reload
assert_response 302
assert_not_equal before_count, @map.annotations.count
assert_redirected_to map_annotation_url(@map, @map.annotations.last)
end

# test 'should show annotations' do
# get :show, map_id: 1, id: @annotation.id

# assert_response 200
# assert_includes @response.body, @annotation.text
# end

test 'should update annotations' do
Copy link
Member

Choose a reason for hiding this comment

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

This should be blank?

Copy link
Member Author

Choose a reason for hiding this comment

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

The update and destroy actions don't work
So i left them blank for now.

end

test 'should destroy annotations' do
Copy link
Member

Choose a reason for hiding this comment

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

This should be blank?

end

# test 'should display index' do
# get :index, map_id: 1
# assert_response :success
# assert_includes @response.body, @annotation.text
# end
end
108 changes: 52 additions & 56 deletions test/functional/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'test_helper'

class CommentsControllerTest < ActionController::TestCase

# called before every single test
def setup
@map = maps(:saugus)
end
end

# called after every single test
def teardown
Expand All @@ -14,11 +13,11 @@ def teardown
test "should not create comment if not logged in" do
before_count = Comment.count

post(:create,
map_id: @map.slug,
comment: {
user_id: 1
})
post(:create,
map_id: @map.slug,
comment: {
user_id: 1
})

assert_response :success
assert_equal before_count, Comment.count
Expand All @@ -28,11 +27,11 @@ def teardown
session[:user_id] = 1
before_count = Comment.count

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

assert_response :success
assert_not_equal before_count, Comment.count
Expand All @@ -44,13 +43,13 @@ def teardown
session[:user_id] = 4

put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

#refresh the object
# refresh the object
@comment.reload

assert_redirected_to "/maps/" + @map.slug
Expand All @@ -62,15 +61,15 @@ def teardown
session[:user_id] = 3

put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

@comment.reload

assert_redirected_to "/login"
assert_redirected_to "/login"
assert_not_equal "I'm gonna troll you!", @comment.body
assert_equal "I'll just leave a comment, why don't I.", @comment.body
assert_equal "You do not have permissions to update that comment.", flash[:error]
Expand All @@ -80,15 +79,15 @@ def teardown
@comment = comments(:one)

put(:update,
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
id: @comment.id,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

@comment.reload

assert_redirected_to "/login"
assert_redirected_to "/login"
assert_not_equal "I'm gonna troll you!", @comment.body
assert_equal "I'll just leave a comment, why don't I.", @comment.body
assert_equal "You do not have permissions to update that comment.", flash[:error]
Expand All @@ -101,8 +100,7 @@ def teardown

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)
map_id: @map.slug)

assert_redirected_to "/maps/" + @map.slug
assert_not_equal before_count, Comment.count
Expand All @@ -116,8 +114,7 @@ def teardown

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)
map_id: @map.slug)

assert_redirected_to "/maps/" + @map.slug
assert_equal before_count, Comment.count
Expand All @@ -130,8 +127,7 @@ def teardown

delete(:destroy,
id: @comment.id,
map_id: @map.slug
)
map_id: @map.slug)

assert_redirected_to "/maps/" + @map.slug
assert_equal before_count, Comment.count
Expand All @@ -142,11 +138,11 @@ def teardown
@user = users(:quentin)
session[:user_id] = @user.id

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

assert_response :success
assert !ActionMailer::Base.deliveries.collect(&:to).include?([@user.email])
Expand All @@ -156,11 +152,11 @@ def teardown
@user = users(:quentin)
session[:user_id] = 3

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

email = ActionMailer::Base.deliveries.last

Expand All @@ -175,19 +171,19 @@ def teardown

session[:user_id] = @chris.id

post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})
post(:create,
map_id: @map.slug,
comment: {
body: "I'm gonna troll you!"
})

session[:user_id] = @joshua.id

post(:create,
map_id: @map.slug,
comment: {
body: "Yeah we'll see!"
})
post(:create,
map_id: @map.slug,
comment: {
body: "Yeah we'll see!"
})

email = ActionMailer::Base.deliveries.last

Expand Down
Loading