Search in sources :

Example 16 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class QueryImplTest method nestOr.

@Test
public void nestOr() {
    Query<Entity> q = new QueryImpl<>().nest().eq("field", "value1").or().eq("field", "value2").unnest();
    QueryRule expectedRule = new QueryRule(Arrays.asList(new QueryRule("field", Operator.EQUALS, "value1"), new QueryRule(Operator.OR), new QueryRule("field", Operator.EQUALS, "value2")));
    assertEquals(q.getRules(), Arrays.asList(expectedRule));
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 17 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class QueryImplTest method nestDeep.

@Test
public void nestDeep() {
    // A OR (B AND (C OR D))
    Query<Entity> q = new QueryImpl<>().eq("field1", "value1").or().nest().eq("field2", "value2").and().nest().eq("field3", "value3").or().eq("field4", "value4").unnest().unnest();
    QueryRule expectedRule1 = new QueryRule("field1", Operator.EQUALS, "value1");
    QueryRule expectedRule1a = new QueryRule("field2", Operator.EQUALS, "value2");
    QueryRule expectedRule1b1 = new QueryRule("field3", Operator.EQUALS, "value3");
    QueryRule expectedRule1b2 = new QueryRule("field4", Operator.EQUALS, "value4");
    QueryRule expectedRule1b = new QueryRule(Arrays.asList(expectedRule1b1, new QueryRule(Operator.OR), expectedRule1b2));
    QueryRule expectedRule2 = new QueryRule(Arrays.asList(expectedRule1a, new QueryRule(Operator.AND), expectedRule1b));
    assertEquals(q.getRules(), Arrays.asList(expectedRule1, new QueryRule(Operator.OR), expectedRule2));
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 18 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class QueryImplTest method nestAnd.

@Test
public void nestAnd() {
    Query<Entity> q = new QueryImpl<>().nest().eq("field", "value1").and().eq("field", "value2").unnest();
    QueryRule expectedRule = new QueryRule(Arrays.asList(new QueryRule("field", Operator.EQUALS, "value1"), new QueryRule(Operator.AND), new QueryRule("field", Operator.EQUALS, "value2")));
    assertEquals(q.getRules(), Arrays.asList(expectedRule));
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 19 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class QueryImplTest method equals.

@Test
public void equals() {
    QueryImpl<Entity> q1 = new QueryImpl<>();
    {
        QueryRule geRule = new QueryRule("jaar", Operator.GREATER_EQUAL, "1995");
        QueryRule andRule = new QueryRule(Operator.AND);
        QueryRule leRule = new QueryRule("jaar", Operator.LESS_EQUAL, "1995");
        List<QueryRule> subSubNestedRules = Arrays.asList(geRule, andRule, leRule);
        List<QueryRule> subNestedRules = Arrays.asList(new QueryRule(subSubNestedRules));
        List<QueryRule> nestedRules = Arrays.asList(new QueryRule(subNestedRules));
        QueryRule rule = new QueryRule(nestedRules);
        q1.addRule(rule);
    }
    QueryImpl<Entity> q2 = new QueryImpl<>();
    {
        QueryRule geRule = new QueryRule("jaar", Operator.GREATER_EQUAL, "1996");
        QueryRule andRule = new QueryRule(Operator.AND);
        QueryRule leRule = new QueryRule("jaar", Operator.LESS_EQUAL, "1996");
        List<QueryRule> subSubNestedRules = Arrays.asList(geRule, andRule, leRule);
        List<QueryRule> subNestedRules = Arrays.asList(new QueryRule(subSubNestedRules));
        List<QueryRule> nestedRules = Arrays.asList(new QueryRule(subNestedRules));
        QueryRule rule = new QueryRule(nestedRules);
        q2.addRule(rule);
    }
    assertNotEquals(q1, q2);
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) List(java.util.List) Test(org.testng.annotations.Test)

Example 20 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class PostgreSqlRepositoryTest method findAllQueryOneToManyEquals.

@Test
public void findAllQueryOneToManyEquals() throws Exception {
    String oneToManyAttrName = "oneToManyAttr";
    String refIdAttrName = "refEntityId";
    Attribute refIdAttr = mock(Attribute.class);
    when(refIdAttr.getName()).thenReturn(refIdAttrName);
    when(refIdAttr.getDataType()).thenReturn(INT);
    when(refIdAttr.isUnique()).thenReturn(true);
    String xrefAttrName = "xrefAttr";
    Attribute xrefAttr = mock(Attribute.class);
    when(xrefAttr.getName()).thenReturn(xrefAttrName);
    EntityType refEntityMeta = mock(EntityType.class);
    when(refEntityMeta.getId()).thenReturn("refEntityId");
    when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr);
    doReturn(refIdAttr).when(refEntityMeta).getAttribute(refIdAttrName);
    String idAttrName = "entityId";
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.getName()).thenReturn(idAttrName);
    when(idAttr.getDataType()).thenReturn(STRING);
    Attribute oneToManyAttr = mock(Attribute.class);
    when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName);
    when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY);
    when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta);
    when(oneToManyAttr.isMappedBy()).thenReturn(true);
    when(oneToManyAttr.getMappedBy()).thenReturn(xrefAttr);
    when(entityType.getId()).thenReturn("entityId");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getAttribute("entityId")).thenReturn(idAttr);
    doReturn(oneToManyAttr).when(entityType).getAttribute(oneToManyAttrName);
    when(entityType.getAtomicAttributes()).thenReturn(newArrayList(idAttr, oneToManyAttr));
    EntityType entityType = this.entityType;
    postgreSqlRepo = new PostgreSqlRepository(postgreSqlEntityFactory, jdbcTemplate, dataSource, entityType);
    int queryValue = 2;
    QueryRule queryRule = new QueryRule(oneToManyAttrName, EQUALS, queryValue);
    when(query.getRules()).thenReturn(singletonList(queryRule));
    String sql = "SELECT DISTINCT this.\"entityId\", (SELECT array_agg(\"refEntityId\" ORDER BY \"refEntityId\" ASC) FROM \"refEntityId#07f902bf\" WHERE this.\"entityId\" = \"refEntityId#07f902bf\".\"xrefAttr\") AS \"oneToManyAttr\" FROM \"entityId#fc2928f6\" AS this LEFT JOIN \"refEntityId#07f902bf\" AS \"oneToManyAttr_filter1\" ON (this.\"entityId\" = \"oneToManyAttr_filter1\".\"xrefAttr\") WHERE \"oneToManyAttr_filter1\".\"refEntityId\" = ? ORDER BY \"entityId\" ASC LIMIT 1000";
    when(postgreSqlEntityFactory.createRowMapper(entityType, null)).thenReturn(rowMapper);
    Entity entity0 = mock(Entity.class);
    when(jdbcTemplate.query(sql, new Object[] { queryValue }, rowMapper)).thenReturn(singletonList(entity0));
    assertEquals(postgreSqlRepo.findAll(query).collect(toList()), singletonList(entity0));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Aggregations

QueryRule (org.molgenis.data.QueryRule)24 Entity (org.molgenis.data.Entity)16 Test (org.testng.annotations.Test)15 QueryImpl (org.molgenis.data.support.QueryImpl)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)4 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)4 StringUtils (org.apache.commons.lang3.StringUtils)3 Operator (org.molgenis.data.QueryRule.Operator)3 OntologyTermHitEntity (org.molgenis.ontology.sorta.bean.OntologyTermHitEntity)3 Sets (com.google.common.collect.Sets)2 java.util (java.util)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors (java.util.stream.Collectors)2 DataService (org.molgenis.data.DataService)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 FluentIterable (com.google.common.collect.FluentIterable)1 Iterables (com.google.common.collect.Iterables)1