Skip to content

Commit

Permalink
Expand unit tests (#507)
Browse files Browse the repository at this point in the history
* warpable model tests

* jpg and geotiff export functional tests

* Add few tests for user, map and export models
  • Loading branch information
cesswairimu authored and jywarren committed Apr 9, 2019
1 parent 05cb559 commit 9b09a9b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 37 deletions.
4 changes: 2 additions & 2 deletions app/models/warpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def fup_json
def fup_error_json
{"name" => read_attribute(:image_filename),
"size" => read_attribute(:image_size),
"error" => self.errors["base"]}
"error" => self.errors["base"]}
end

after_save :save_dimensions

# this runs each time warpable is moved/distorted,
# this runs each time warpable is moved/distorted,
# to calculate resolution
def save_dimensions
if Rails.env.production?
Expand Down
26 changes: 25 additions & 1 deletion test/fixtures/maps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ saugus:
created_at: <%= Time.now %>
license: publicdomain
user_id: 1

cubbon:
id: 2
name: Cubbon Park
Expand All @@ -23,3 +23,27 @@ cubbon:
created_at: <%= Time.now %>
license: publicdomain
user_id: 1

nairobi:
id: 3
name: Nairobi City
slug: nairobi-city
location: Kenya
lat: -1.2920659
lon: 36.8219462
description: Capital of Kenya
created_at: <% Time.now %>
license: publicdomain
user_id: 2

village:
id: 4
name: Village Market
slug: village-market
location: Kenya
lat: -0.023559
lon: 37.90619300000003
description: A mall in Nairobi
created_at: <% Time.now %>
license: publicdomain
user_id: 2
29 changes: 18 additions & 11 deletions test/functional/export_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ def teardown
assert assigns[:week]
end

#stored in public path which is git ignored
# test "jpg" do
# get :jpg, id: @map.slug
# assert_response :success
# assert_includes '"image/jpeg', response.content_type
# end
test "jpg" do
map = maps(:cubbon)
system('mkdir -p public/warps/cubbon-park')
system('cp test/fixtures/demo.png public/warps/cubbon-park/cubbon-park.jpg')

# test "geotiff" do
# get :geotiff, id: @map.slug
# assert_response :success
# assert_includes '"image/tiff', response.content_type
# end
get :jpg, id: map.slug
assert_response :success
assert_includes '"image/jpeg', response.content_type
end

test "geotiff" do
map = maps(:cubbon)
system('mkdir -p public/warps/cubbon-park')
system('cp test/fixtures/demo.png public/warps/cubbon-park/cubbon-park-geo.tif')

get :geotiff, id: map.slug
assert_response :success
assert_includes '"image/tiff', response.content_type
end

test "cancel fails if not logged in" do
get :cancel, id: @map.id
Expand Down
2 changes: 2 additions & 0 deletions test/unit/export_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class ExportTest < ActiveSupport::TestCase
test "count methods" do
average = Export.all.map(&:cm_per_pixel).sum/Export.count
assert_not_nil Export.average_cm_per_pixel
assert_equal average, Export.average_cm_per_pixel
assert_not_nil Export.histogram_cm_per_pixel
assert_not_nil Export.histogram_cm_per_pixel_in_tens
assert_not_nil Export.export_count
Expand Down
25 changes: 23 additions & 2 deletions test/unit/map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MapTest < ActiveSupport::TestCase
assert_not_nil Map.authors
assert_not_nil Map.new_maps

map = Map.first
map = maps(:saugus)
assert_not_nil map.license_link
assert_not_nil map.author
assert_not_nil map.name
Expand All @@ -29,12 +29,17 @@ class MapTest < ActiveSupport::TestCase
assert_not_nil map.images_histogram
assert_not_nil map.grouped_images_histogram(10)
assert_not_nil map.nearby_maps(100) # in degrees lat/lon
assert_equal Map.count, Map.new_maps.size
end

test "export functions" do
map = Map.first
map = maps(:saugus)
assert_not_nil map.average_scale

placed = map.warpables(&:placed?)
assert_not_nil map.placed_warpables
assert_equal placed, map.placed_warpables

assert_not_nil map.best_cm_per_pixel
assert_not_nil map.exporting?
assert_not_nil map.export
Expand Down Expand Up @@ -75,6 +80,22 @@ class MapTest < ActiveSupport::TestCase

end

test 'histograms' do
map = maps(:saugus)
hist = map.images_histogram
assert_not_nil hist
assert_not_nil map.grouped_images_histogram(3)
assert_equal hist.count/3, map.grouped_images_histogram(3).count

end

test 'nearby maps' do
map = maps(:nairobi)
near_maps = map.nearby_maps(5)
assert_not_nil near_maps
assert_includes near_maps, maps(:village)
end

test "tag basics" do
map = Map.first
assert !map.has_tag('test')
Expand Down
8 changes: 8 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class UserTest < ActiveSupport::TestCase
# assert_not_nil users(:quentin).remember_token_expires_at
# assert users(:quentin).remember_token_expires_at.between?(before, after)
# end
test 'user simple query methods' do
user = users(:quentin)
map = maps(:saugus)
assert user.owns?(map)
assert user.can_delete?(map)
assert user.can_edit?(map)
assert_equal map.updated_at, user.last_action
end

protected
def create_user(options = {})
Expand Down
70 changes: 49 additions & 21 deletions test/unit/warpable_test.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
require 'test_helper'

class WarpableTest < ActiveSupport::TestCase
test "basics" do
w = warpables(:one)
assert_not_nil w.as_json
assert_not_nil w.fup_json
assert_not_nil w.fup_error_json
assert_not_nil w.placed?
assert w.placed?
assert !Warpable.new.placed?
assert_not_nil w.poly_area
assert_not_nil w.get_cm_per_pixel

assert_not_nil Warpable.histogram_cm_per_pixel
assert_not_nil w.nodes
assert_not_nil w.nodes_array
assert_equal 4, w.nodes_array.length

# TODO: test with local image?
#assert_not_nil w.url()

def setup
@warp = warpables(:one)
end

test 'basic warpable attributes' do
assert_equal "demo.png", @warp.image_file_name
assert_equal "image/png", @warp.image_content_type
assert_equal 392, @warp.height
assert_equal 756, @warp.width
assert_equal '', @warp.history
assert_equal false, @warp.locked
assert_equal false, @warp.deleted
assert_equal 500, @warp.cm_per_pixel
assert_equal "1,2,3,4", @warp.nodes
end

test "json format methods" do
assert_not_nil @warp.as_json
assert_equal Hash, @warp.as_json.class
assert_equal @warp.attributes.size + 2, @warp.as_json.size

assert_not_nil @warp.fup_json
assert_equal Hash, @warp.fup_json.class
assert_equal 8, @warp.fup_json.size

assert_not_nil @warp.fup_error_json
assert_equal Hash, @warp.fup_error_json.class
assert_equal 3, @warp.fup_error_json.size
end

test 'warpable small methods' do
assert_not_nil @warp.placed?
assert @warp.placed?
assert_equal false, Warpable.new.placed?

assert_not_nil @warp.poly_area
assert_not_nil @warp.get_cm_per_pixel
assert_equal Float, @warp.get_cm_per_pixel.class
assert_equal 100, Warpable.histogram_cm_per_pixel.length

node_count = @warp.nodes.split(',').length
assert_not_nil @warp.nodes_array
assert_equal node_count, @warp.nodes_array.length

assert_not_nil @warp.user_id
assert_equal @warp.map.user_id, @warp.user_id
end

test "try export" do
Expand All @@ -28,9 +57,8 @@ class WarpableTest < ActiveSupport::TestCase
system('mkdir -p public/warps/saugus-landfill-incinerator')
system('touch public/warps/saugus-landfill-incinerator/folder')
assert File.exist?('public/warps/saugus-landfill-incinerator/folder')
w = warpables(:one)
assert_not_nil w.save_dimensions
assert_not_nil w.user_id
assert_not_nil @warp.save_dimensions
assert_not_nil @warp.user_id
assert File.exist?('public/warps/saugus-landfill-incinerator/1-geo.tif')
end
end

0 comments on commit 9b09a9b

Please sign in to comment.