Skip to content

Commit

Permalink
Add tests for controllers (publiclab#615)
Browse files Browse the repository at this point in the history
* add utility test

* Add exports and annotations controller tests'

* Add tests for images

* Fix codeclimate issues

* Single quoted strings, huh :/

* Fix export controller test

* Fix histogram ordering

* Fix codeclimate issues

* Fix image controller tests

* Small fixes

* Add rubocop.yml

* Fix rubocop

* Increase linelength

* Fix rubocop

* Rename tests for better readability

* Rename tests

* Fixing Codeclimate offenses
  • Loading branch information
kaustubh-nair authored and chen-robert committed Dec 5, 2019
1 parent ed40fef commit 28ffb60
Show file tree
Hide file tree
Showing 18 changed files with 458 additions and 218 deletions.
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')
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
end

test 'should destroy annotations' do
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

0 comments on commit 28ffb60

Please sign in to comment.