Search in sources :

Example 1 with ArraySlice

use of dev.morphia.query.ArraySlice in project morphia by mongodb.

the class TestQuery method testProjectArrayField.

@Test
public void testProjectArrayField() {
    int[] ints = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
    IntVector vector = new IntVector(ints);
    getDs().save(vector);
    assertEquals(copy(ints, 0, 4), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(4)).limit(1)).next().scalars);
    assertEquals(copy(ints, 5, 4), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(5, 4)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 10, 6), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(-10, 6)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 12, 12), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(-12)).limit(1)).next().scalars);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Example 2 with ArraySlice

use of dev.morphia.query.ArraySlice in project morphia by mongodb.

the class TestLegacyQuery method testMixedProjection.

@Test
public void testMixedProjection() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").projection().exclude("last_name"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name", "last_name").projection().exclude("_id"));
    } catch (ValidationException e) {
        fail("An exception should not have been thrown indication a mixed projection because _id suppression is a special case");
    }
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().exclude("first_name", "last_name").projection().include("_id"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(IntVector.class).execute(new FindOptions().projection().exclude("name").projection().project("scalars", new ArraySlice(5)));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Example 3 with ArraySlice

use of dev.morphia.query.ArraySlice in project morphia by mongodb.

the class TestLegacyQuery method testProjectArrayField.

@Test
public void testProjectArrayField() {
    int[] ints = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
    IntVector vector = new IntVector(ints);
    getDs().save(vector);
    assertEquals(copy(ints, 0, 4), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(4)).limit(1)).next().scalars);
    assertEquals(copy(ints, 5, 4), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(5, 4)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 10, 6), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(-10, 6)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 12, 12), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(-12)).limit(1)).next().scalars);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Example 4 with ArraySlice

use of dev.morphia.query.ArraySlice in project morphia by mongodb.

the class TestQuery method testMixedProjection.

@Test
public void testMixedProjection() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    try {
        getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name").projection().exclude("last_name"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name", "last_name").projection().exclude("_id"));
    } catch (ValidationException e) {
        fail("An exception should not have been thrown indication a mixed projection because _id suppression is a special case");
    }
    try {
        getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().exclude("first_name", "last_name").projection().include("_id"));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
    try {
        getDs().find(IntVector.class).iterator(new FindOptions().projection().exclude("name").projection().project("scalars", new ArraySlice(5)));
        fail("An exception should have been thrown indication a mixed projection");
    } catch (ValidationException e) {
    // all good
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Aggregations

ArraySlice (dev.morphia.query.ArraySlice)4 FindOptions (dev.morphia.query.FindOptions)4 Test (org.testng.annotations.Test)4 ValidationException (dev.morphia.query.ValidationException)2