Back to Repositories

Testing Geographic Shape Search Operations in Searchkick

This test suite validates geographic shape searching functionality in Searchkick, focusing on polygon-based territory searches and spatial relationships. It tests various geometric operations including envelopes, polygons, and multipolygons with coordinate-based searching.

Test Coverage Overview

The test suite provides comprehensive coverage of geo-shape searching capabilities:

  • Envelope-based geographic searches
  • Polygon and multipolygon territory matching
  • Spatial relationship testing (disjoint, within)
  • Combined text and geo-shape searching
  • Coordinate format handling (lat/lon)

Implementation Analysis

The testing approach uses a structured setup with predefined regions containing polygon territories. Each test case validates specific geometric operations and spatial relationships using coordinate-based search criteria. The implementation leverages Minitest’s assertion framework with custom search assertions.

Technical Details

Testing components include:

  • Minitest framework for test organization
  • Custom setup_region helper method
  • Coordinate-based territory definitions
  • Geographic shape query parameters
  • Search assertion utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for each geometric operation
  • Clear test data setup and organization
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Well-structured test scenarios

ankane/searchkick

test/geo_shape_test.rb

            
require_relative "test_helper"

class GeoShapeTest < Minitest::Test
  def setup
    setup_region
    store [
      {
        name: "Region A",
        text: "The witch had a cat",
        territory: {
          type: "polygon",
          coordinates: [[[30, 40], [35, 45], [40, 40], [40, 30], [30, 30], [30, 40]]]
        }
      },
      {
        name: "Region B",
        text: "and a very tall hat",
        territory: {
          type: "polygon",
          coordinates: [[[50, 60], [55, 65], [60, 60], [60, 50], [50, 50], [50, 60]]]
        }
      },
      {
        name: "Region C",
        text: "and long ginger hair which she wore in a plait",
        territory: {
          type: "polygon",
          coordinates: [[[10, 20], [15, 25], [20, 20], [20, 10], [10, 10], [10, 20]]]
        }
      }
    ]
  end

  def test_envelope
    assert_search "*", ["Region A"], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            coordinates: [[28, 42], [32, 38]]
          }
        }
      }
    }
  end

  def test_polygon
    assert_search "*", ["Region A"], {
      where: {
        territory: {
          geo_shape: {
            type: "polygon",
            coordinates: [[[38, 42], [42, 42], [42, 38], [38, 38], [38, 42]]]
          }
        }
      }
    }
  end

  def test_multipolygon
    assert_search "*", ["Region A", "Region B"], {
      where: {
        territory: {
          geo_shape: {
            type: "multipolygon",
            coordinates: [
              [[[38, 42], [42, 42], [42, 38], [38, 38], [38, 42]]],
              [[[58, 62], [62, 62], [62, 58], [58, 58], [58, 62]]]
            ]
          }
        }
      }
    }
  end

  def test_disjoint
    assert_search "*", ["Region B", "Region C"], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            relation: "disjoint",
            coordinates: [[28, 42], [32, 38]]
          }
        }
      }
    }
  end

  def test_within
    assert_search "*", ["Region A"], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            relation: "within",
            coordinates: [[20, 50], [50, 20]]
          }
        }
      }
    }
  end

  def test_search_match
    assert_search "witch", ["Region A"], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            coordinates: [[28, 42], [32, 38]]
          }
        }
      }
    }
  end

  def test_search_no_match
    assert_search "ginger hair", [], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            coordinates: [[28, 42], [32, 38]]
          }
        }
      }
    }
  end

  def test_latlon
    assert_search "*", ["Region A"], {
      where: {
        territory: {
          geo_shape: {
            type: "envelope",
            coordinates: [{lat: 42, lon: 28}, {lat: 38, lon: 32}]
          }
        }
      }
    }
  end

  def default_model
    Region
  end
end