@Test
  public void settingMetricRecalculatesMaxDistance() {

    NearQuery query = NearQuery.near(2.5, 2.5, Metrics.KILOMETERS).maxDistance(150);

    query.inMiles();
    assertThat(query.getMetric(), is((Metric) Metrics.MILES));
  }
  @Test
  public void settingUpNearWithMetricRecalculatesDistance() {

    NearQuery query = NearQuery.near(2.5, 2.5, Metrics.KILOMETERS).maxDistance(150);

    assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
    assertThat(query.getMetric(), is((Metric) Metrics.KILOMETERS));
    assertThat(query.isSpherical(), is(true));
  }
  /** @see DATAMONGO-445 */
  @Test
  public void shouldTakeSkipAndLimitSettingsFromGivenPageable() {

    Pageable pageable = new PageRequest(3, 5);
    NearQuery query = NearQuery.near(new Point(1, 1)).with(pageable);

    assertThat(query.getSkip(), is(pageable.getPageNumber() * pageable.getPageSize()));
    assertThat(
        (Integer) query.toDBObject().get("num"),
        is((pageable.getPageNumber() + 1) * pageable.getPageSize()));
  }
  /** @see DATAMONGO-445 */
  @Test
  public void shouldTakeSkipAndLimitSettingsFromGivenQuery() {

    int limit = 10;
    int skip = 5;
    NearQuery query =
        NearQuery.near(new Point(1, 1))
            .query(Query.query(Criteria.where("foo").is("bar")).limit(limit).skip(skip));

    assertThat(query.getSkip(), is(skip));
    assertThat((Integer) query.toDBObject().get("num"), is(limit));
  }
  /** @see DATAMONGO-445 */
  @Test
  public void shouldTakeSkipAndLimitSettingsFromPageableEvenIfItWasSpecifiedOnQuery() {

    int limit = 10;
    int skip = 5;
    Pageable pageable = new PageRequest(3, 5);
    NearQuery query =
        NearQuery.near(new Point(1, 1))
            .query(Query.query(Criteria.where("foo").is("bar")).limit(limit).skip(skip))
            .with(pageable);

    assertThat(query.getSkip(), is(pageable.getPageNumber() * pageable.getPageSize()));
    assertThat(
        (Integer) query.toDBObject().get("num"),
        is((pageable.getPageNumber() + 1) * pageable.getPageSize()));
  }
  @Test
  public void configuresResultMetricCorrectly() {

    NearQuery query = NearQuery.near(2.5, 2.1);
    assertThat(query.getMetric(), is((Metric) Metrics.NEUTRAL));

    query = query.maxDistance(ONE_FIFTY_KILOMETERS);
    assertThat(query.getMetric(), is((Metric) Metrics.KILOMETERS));
    assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
    assertThat(query.isSpherical(), is(true));

    query = query.in(Metrics.MILES);
    assertThat(query.getMetric(), is((Metric) Metrics.MILES));
    assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
    assertThat(query.isSpherical(), is(true));

    query = query.maxDistance(new Distance(200, Metrics.KILOMETERS));
    assertThat(query.getMetric(), is((Metric) Metrics.MILES));
  }
 @Test(expected = IllegalArgumentException.class)
 public void rejectsNullPoint() {
   NearQuery.near(null);
 }