<commit_before>package org.mongodb.morphia.query;


import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import org.mongodb.morphia.utils.IndexDirection;
import com.mongodb.MongoException;
import com.mongodb.MongoInternalException;

import static org.mongodb.morphia.query.Shape.Point;


public class TestGeoQueries extends TestBase {
  public static final String GEO_QUERY_2_2_ERROR_STRING = "geo field only has 1 element";

  @Entity
  private static class Place {
    @Id
    protected ObjectId id;
    protected String name = "";
    @Indexed(IndexDirection.GEO2D)
    protected double[] loc;

    public Place(final String name, final double[] loc) {
      this.name = name;
      this.loc = loc;
    }

    private Place() {
    }
  }


  @Override
  public void setUp() {
    super.setUp();
    morphia.map(Place.class);
  }

  @Test
  public void testNear() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .near(0, 0)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testNearMaxDistance() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .near(0, 0, 1.5)
      .get();
    Assert.assertNotNull(found);
    final Place notFound = ds.find(Place.class)
      .field("loc")
      .near(0, 0, 1)
      .get();
    Assert.assertNull(notFound);
  }

  @Test
  public void testNearNoIndex() throws Exception {
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    Place found = null;
    try {
      found = ds.find(Place.class)
        .field("loc")
        .near(0, 0)
        .get();
      Assert.assertFalse(true);
    } catch (MongoInternalException e) {
      Assert.assertNull(found);
    } catch (MongoException e) {
      Assert.assertNull(found);
    }
  }

  @Test
  public void testWithinRadius() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0, 1, 1.1)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testGeoWithinRadius2() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(Shape.centerSphere(new Point(0.5, 0.5), 0.77))
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testGeoWithinRadius() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(Shape.center(new Point(0, 1), 1.1))
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testWithinRadius2() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0.5, 0.5, 0.77)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testWithinRadiusSphere() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0, 1, 1, true)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testGeoWithinRadiusSphere() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(Shape.centerSphere(new Point(0, 1), 1))
      .get();
    Assert.assertNotNull(found);
  }

  @Test<commit_msg>check against failures on 2.2 which don't support this form of geo queries<commit_after>package org.mongodb.morphia.query;


import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import org.mongodb.morphia.utils.IndexDirection;
import com.mongodb.MongoException;
import com.mongodb.MongoInternalException;

import static org.mongodb.morphia.query.Shape.Point;


public class TestGeoQueries extends TestBase {
  public static final String GEO_QUERY_2_2_ERROR_STRING = "geo field only has 1 element";

  @Entity
  private static class Place {
    @Id
    protected ObjectId id;
    protected String name = "";
    @Indexed(IndexDirection.GEO2D)
    protected double[] loc;

    public Place(final String name, final double[] loc) {
      this.name = name;
      this.loc = loc;
    }

    private Place() {
    }
  }


  @Override
  public void setUp() {
    super.setUp();
    morphia.map(Place.class);
  }

  @Test
  public void testNear() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .near(0, 0)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testNearMaxDistance() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .near(0, 0, 1.5)
      .get();
    Assert.assertNotNull(found);
    final Place notFound = ds.find(Place.class)
      .field("loc")
      .near(0, 0, 1)
      .get();
    Assert.assertNull(notFound);
  }

  @Test
  public void testNearNoIndex() throws Exception {
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    Place found = null;
    try {
      found = ds.find(Place.class)
        .field("loc")
        .near(0, 0)
        .get();
      Assert.assertFalse(true);
    } catch (MongoInternalException e) {
      Assert.assertNull(found);
    } catch (MongoException e) {
      Assert.assertNull(found);
    }
  }

  @Test
  public void testWithinRadius() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0, 1, 1.1)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testGeoWithinRadius2() throws Exception {
    try {
      ds.ensureIndexes();
      final Place place1 = new Place("place1", new double[] {1, 1});
      ds.save(place1);
      final Place found = ds.find(Place.class)
        .field("loc")
        .within(Shape.centerSphere(new Point(0.5, 0.5), 0.77))
        .get();
      Assert.assertNotNull(found);
    } catch (MongoException e) {
      if (!GEO_QUERY_2_2_ERROR_STRING.equals(e.getMessage())) {
        throw e;
      }
    }
  }

  @Test
  public void testGeoWithinRadius() throws Exception {
    try {
      ds.ensureIndexes();
      final Place place1 = new Place("place1", new double[] {1, 1});
      ds.save(place1);
      final Place found = ds.find(Place.class)
        .field("loc")
        .within(Shape.center(new Point(0, 1), 1.1))
        .get();
      Assert.assertNotNull(found);
    } catch (MongoException e) {
      if (!GEO_QUERY_2_2_ERROR_STRING.equals(e.getMessage())) {
        throw e;
      }
    }
  }

  @Test
  public void testWithinRadius2() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0.5, 0.5, 0.77)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testWithinRadiusSphere() throws Exception {
    ds.ensureIndexes();
    final Place place1 = new Place("place1", new double[] {1, 1});
    ds.save(place1);
    final Place found = ds.find(Place.class)
      .field("loc")
      .within(0, 1, 1, true)
      .get();
    Assert.assertNotNull(found);
  }

  @Test
  public void testGeoWithinRadiusSphere() throws Exception {
    try {
      ds.ensureIndexes();
      final Place place1 = new Place("place1", new double[] {1, 1});
      ds.save(place1);
      final Place found = ds.find(Place.class)
        .field("loc")
        .within(Shape.centerSphere(new Point(0, 1), 1))
        .get();
      Assert.assertNotNull(found);
    } catch (MongoException e) {
      if (!GEO_QUERY_2_2_ERROR_STRING.equals(e.getMessage())) {
        throw e;
      }
    }
  }

  @Test
{'fraction_non_alphanumeric': 0.13873853971059316, 'fraction_numerical': 0.0207665967082735, 'mean_word_length': 2.8106060606060606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6816657', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2985, 'n_words': 717}