Skip to content

Commit

Permalink
Add new tests for models (#610)
Browse files Browse the repository at this point in the history
* Add annotation tests and remove redundant code

* Add export tests

* Remove some code

* Fix codeclimate issues

* Fix codeclimate issues

* Fix rubocop block length

* Removing rubocop cfg since #547 and renaming tests
  • Loading branch information
kaustubh-nair authored and jywarren committed Jun 3, 2019
1 parent 62eec1e commit 03deef1
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 46 deletions.
18 changes: 2 additions & 16 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,15 @@ class Map < ActiveRecord::Base
has_many :annotations, dependent: :destroy
belongs_to :user

has_many :warpables
scope :active, -> { where(archived: false) }
scope :has_user, -> { where('user_id != ?', 0) }

has_many :warpables do
def public_filenames
filenames = {}
each do |warpable|
filenames[warpable.id] = {}
sizes = Array.new(Warpable::THUMBNAILS.keys).push(nil)
sizes.each do |size|
key = !size.nil? ? size : "original"
filenames[warpable.id][key] = warpable.public_filename(size)
end
end
filenames
end
end

def validate
lat >= -90 && lat <= 90 && lon >= -180 && lat <= 180 if name != 'untitled'
end

# Hash the password before saving the record
# Hash the password before saving the record.
def before_create
self.password = Password.update(password) if password != ""
end
Expand Down
6 changes: 0 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class User < ActiveRecord::Base
# We really need a Dispatch Chain here or something.
# This will also let us return a human error message.
#
def self.authenticate(login, password)
return nil if login.blank? || password.blank?

u = find_by_login(login.downcase) # need to get the salt
u&.authenticated?(password) ? u : nil
end

def login=(value)
write_attribute :login, (value ? value.downcase : nil)
Expand Down
17 changes: 14 additions & 3 deletions test/unit/annotation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ def setup
@annotation = annotations(:lorem)
end

test 'should save annotation' do
test 'should save an annotation' do
map = maps(:saugus)
ann = map.annotations.new
ann.save!

assert_equal Annotation.count, @count + 1
end

test 'should return author' do
test 'should return an author' do
assert_equal 'quentin', @annotation.author
end

test 'geometry type' do
test 'should retrieve a geometry type' do
@annotation.annotation_type = 'Some random Geometry'
assert_equal @annotation.geometry_type, 'Point'

@annotation.annotation_type = 'polyline'
assert_equal @annotation.geometry_type, 'LineString'

@annotation.annotation_type = 'polygon'
assert_equal @annotation.geometry_type, 'Polygon'

@annotation.annotation_type = 'rectangle'
assert_equal @annotation.geometry_type, 'Polygon'
end
end
8 changes: 6 additions & 2 deletions test/unit/export_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
require 'test_helper'

class ExportTest < ActiveSupport::TestCase
test "count methods" do
test 'should 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
assert_not_nil Export.exporting

Export.delete_all
assert_empty Export.histogram_cm_per_pixel
assert_equal 0, Export.average_cm_per_pixel
end

test "export running" do
test 'should export running' do
assert !Export.last.running?
end
end
2 changes: 1 addition & 1 deletion test/unit/exporter_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'

class ExporterTest < ActiveSupport::TestCase
test "isolated exporter lib" do
test 'should export warpable using isolated exporter lib' do

# make a sample image
system('mkdir -p public/system/images/1/original')
Expand Down
30 changes: 22 additions & 8 deletions test/unit/map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

class MapTest < ActiveSupport::TestCase

test "basics" do
test 'should not have empty default attributes' do
assert_not_nil Map.bbox(0,0,90,180)
assert_not_nil Map.authors
assert_not_nil Map.new_maps
end

test 'should create map' do
map = maps(:saugus)

assert_not_nil map.license_link
assert_not_nil map.author
assert_not_nil map.name
Expand All @@ -25,28 +28,32 @@ class MapTest < ActiveSupport::TestCase
assert_not_nil map.user
assert_not_nil map.private
assert_not_nil map.anonymous?

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_true Map.first.validate
assert_equal Map.count, Map.new_maps.size
end

test "export functions" do
test 'should export map related functions' do
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
assert_not_nil map.latest_export
assert_not_nil map.nodes
assert_not_nil map.average_cm_per_pixel

# use a map fixture with no warpables
village = maps(:village)
assert_equal 0, village.average_cm_per_pixel

resolution = 20
assert_not_nil map.run_export(users(:quentin), resolution) #map.average_cm_per_pixel)

Expand Down Expand Up @@ -80,27 +87,34 @@ class MapTest < ActiveSupport::TestCase

end

test 'histograms' do
test 'should have 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
test 'should have nearby maps' do
map = maps(:nairobi)
near_maps = map.nearby_maps(5)

assert_not_nil near_maps
assert_includes near_maps, maps(:village)

saugus = maps(:saugus)
saugus.lat = 0

assert_empty saugus.nearby_maps(100)
end

test "tag basics" do
test 'should create tag basics in map' do
map = Map.first

assert !map.has_tag('test')
assert map.add_tag('test', User.first)
assert map.has_tag('test')
end

end
4 changes: 0 additions & 4 deletions test/unit/node_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
require 'test_helper'

class NodeTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 3 additions & 1 deletion test/unit/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

class TagTest < ActiveSupport::TestCase

test "basics" do
test 'should create sample tag' do
tag = Tag.first

assert_not_nil tag.name
assert_not_nil tag.maps

tag = Tag.find_by_name 'nice'

assert_not_nil tag.name
assert_not_nil tag.maps
assert_not_nil tag.maps.last.has_tag('nice')
Expand Down
2 changes: 1 addition & 1 deletion test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UserTest < ActiveSupport::TestCase
)
end

test 'user simple query methods' do
test 'should edit and delete map' do
user = users(:quentin)
map = maps(:saugus)
assert user.owns?(map)
Expand Down
11 changes: 7 additions & 4 deletions test/unit/warpable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@map = maps(:saugus)
end

test 'basic warpable attributes' do
test 'should have 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
Expand All @@ -19,7 +19,7 @@ def setup
assert_equal "1,2,3,4", @warp.nodes
end

test "json format methods" do
test 'should output 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
Expand All @@ -33,7 +33,7 @@ def setup
assert_equal 3, @warp.fup_error_json.size
end

test 'warpable small methods' do
test 'should execute warpable small methods' do
assert_not_nil @warp.placed?
assert @warp.placed?
assert_equal false, Warpable.new.placed?
Expand All @@ -49,9 +49,12 @@ def setup

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

Warpable.delete_all
assert_empty Warpable.histogram_cm_per_pixel
end

test "try export" do
test 'should try export warpables' do
# make a sample image
system('mkdir -p public/warps/saugus-landfill-incinerator-working')
system('mkdir -p public/system/images/1/original')
Expand Down

0 comments on commit 03deef1

Please sign in to comment.