1
0

location_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. require 'rails_helper'
  2. describe Location do
  3. let(:location) {
  4. Location.new(
  5. lat: BigDecimal('2.0'),
  6. lng: BigDecimal('3.0'),
  7. radius: 300,
  8. speed: 2,
  9. course: 30)
  10. }
  11. it "converts values to Float" do
  12. expect(location.lat).to be_a Float
  13. expect(location.lat).to eq 2.0
  14. expect(location.lng).to be_a Float
  15. expect(location.lng).to eq 3.0
  16. expect(location.radius).to be_a Float
  17. expect(location.radius).to eq 300.0
  18. expect(location.speed).to be_a Float
  19. expect(location.speed).to eq 2.0
  20. expect(location.course).to be_a Float
  21. expect(location.course).to eq 30.0
  22. end
  23. it "provides hash-style access to its properties with both symbol and string keys" do
  24. expect(location[:lat]).to be_a Float
  25. expect(location[:lat]).to eq 2.0
  26. expect(location['lat']).to be_a Float
  27. expect(location['lat']).to eq 2.0
  28. end
  29. it "has a convenience accessor for combined latitude and longitude" do
  30. expect(location.latlng).to eq "2.0,3.0"
  31. end
  32. it "does not allow hash-style assignment" do
  33. expect {
  34. location[:lat] = 2.0
  35. }.to raise_error(NoMethodError)
  36. end
  37. it "ignores invalid values" do
  38. location2 = Location.new(
  39. lat: 2,
  40. lng: 3,
  41. radius: -1,
  42. speed: -1,
  43. course: -1)
  44. expect(location2.radius).to be_nil
  45. expect(location2.speed).to be_nil
  46. expect(location2.course).to be_nil
  47. end
  48. it "considers a location empty if either latitude or longitude is missing" do
  49. expect(Location.new.empty?).to be_truthy
  50. expect(Location.new(lat: 2, radius: 1).present?).to be_falsy
  51. expect(Location.new(lng: 3, radius: 1).present?).to be_falsy
  52. end
  53. it "is droppable" do
  54. {
  55. '{{location.lat}}' => '2.0',
  56. '{{location.latitude}}' => '2.0',
  57. '{{location.lng}}' => '3.0',
  58. '{{location.longitude}}' => '3.0',
  59. '{{location.latlng}}' => '2.0,3.0',
  60. }.each { |template, result|
  61. expect(Liquid::Template.parse(template).render('location' => location.to_liquid)).to eq(result),
  62. "expected #{template.inspect} to expand to #{result.inspect}"
  63. }
  64. end
  65. end