Search in sources :

Example 1 with Filter

use of com.orm.androrm.Filter in project androrm by androrm.

the class FilterTest method testIs.

public void testIs() {
    Filter f = new Filter();
    f.is("product__name", "foo");
    List<Rule> filters = f.getRules();
    Rule filter = filters.get(0);
    Statement s = filter.getStatement();
    Set<String> keys = s.getKeys();
    assertEquals("product__name", filter.getKey());
    assertTrue(keys.contains("name"));
    assertEquals("name = 'foo'", s.toString());
    Product p = new Product();
    p.setName("test product");
    p.save(getContext());
    f = new Filter();
    f.is("supplier__product", p);
    filters = f.getRules();
    filter = filters.get(0);
    s = filter.getStatement();
    keys = s.getKeys();
    assertEquals("supplier__product", filter.getKey());
    assertTrue(keys.contains("product"));
    assertEquals("product = '" + p.getId() + "'", s.toString());
}
Also used : Filter(com.orm.androrm.Filter) InStatement(com.orm.androrm.statement.InStatement) Statement(com.orm.androrm.statement.Statement) LikeStatement(com.orm.androrm.statement.LikeStatement) Product(com.orm.androrm.impl.Product) Rule(com.orm.androrm.Rule)

Example 2 with Filter

use of com.orm.androrm.Filter in project androrm by androrm.

the class FilterTest method testContains.

public void testContains() {
    Filter set = new Filter();
    set.contains("supplier__name", "foo");
    List<Rule> filters = set.getRules();
    Rule filter = filters.get(0);
    Statement s = filter.getStatement();
    Set<String> keys = s.getKeys();
    assertEquals("supplier__name", filter.getKey());
    assertTrue(keys.contains("name"));
    assertTrue(s instanceof LikeStatement);
    assertEquals("name LIKE '%foo%'", s.toString());
}
Also used : LikeStatement(com.orm.androrm.statement.LikeStatement) Filter(com.orm.androrm.Filter) InStatement(com.orm.androrm.statement.InStatement) Statement(com.orm.androrm.statement.Statement) LikeStatement(com.orm.androrm.statement.LikeStatement) Rule(com.orm.androrm.Rule)

Example 3 with Filter

use of com.orm.androrm.Filter in project androrm by androrm.

the class FilterTest method testIn.

public void testIn() {
    Filter set = new Filter();
    List<Integer> values = new ArrayList<Integer>();
    values.add(3);
    values.add(5);
    set.in("product__id", values);
    List<Rule> filters = set.getRules();
    Rule filter = filters.get(0);
    Statement s = filter.getStatement();
    Set<String> keys = s.getKeys();
    assertEquals("product__id", filter.getKey());
    assertTrue(keys.contains("id"));
    assertTrue(s instanceof InStatement);
    assertEquals("id IN ('3','5')", s.toString());
    List<Product> products = new ArrayList<Product>();
    Product p1 = new Product();
    p1.setName("test 1");
    p1.save(getContext());
    Product p2 = new Product();
    p2.setName("test 2");
    p2.save(getContext());
    products.add(p1);
    products.add(p2);
    set = new Filter();
    set.in("supplier__product", products);
    filters = set.getRules();
    filter = filters.get(0);
    s = filter.getStatement();
    keys = s.getKeys();
    assertEquals("supplier__product", filter.getKey());
    assertTrue(keys.contains("product"));
    assertTrue(s instanceof InStatement);
    assertEquals("product IN ('" + p1.getId() + "','" + p2.getId() + "')", s.toString());
}
Also used : InStatement(com.orm.androrm.statement.InStatement) Filter(com.orm.androrm.Filter) InStatement(com.orm.androrm.statement.InStatement) Statement(com.orm.androrm.statement.Statement) LikeStatement(com.orm.androrm.statement.LikeStatement) ArrayList(java.util.ArrayList) Product(com.orm.androrm.impl.Product) Rule(com.orm.androrm.Rule)

Example 4 with Filter

use of com.orm.androrm.Filter in project androrm by androrm.

the class RenameModelMigrationTest method testRenameRelation.

public void testRenameRelation() {
    Migrator<NewModelWithRelation> migrator = new Migrator<NewModelWithRelation>(NewModelWithRelation.class);
    assertTrue(mHelper.hasRelationTable(ModelWithRelation.class));
    assertTrue(mHelper.tableExists("emptymodel_modelwithrelation"));
    ModelWithRelation model1 = new ModelWithRelation();
    model1.save(getContext());
    EmptyModel empty = new EmptyModel();
    empty.save(getContext());
    model1.addRelation(empty);
    model1.save(getContext());
    assertEquals(1, model1.getRelations(getContext()).count());
    assertEquals(0, NewModelWithRelation.objects(getContext()).count());
    migrator.renameModel("ModelWithRelation", NewModelWithRelation.class);
    migrator.migrate(getContext());
    NewModelWithRelation one = NewModelWithRelation.objects(getContext()).get(1);
    assertEquals(1, one.getRelations(getContext()).count());
    assertTrue(mHelper.hasRelationTable(NewModelWithRelation.class));
    assertFalse(mHelper.hasRelationTable(ModelWithRelation.class));
    assertFalse(mHelper.tableExists("emptymodel_modelwithrelation"));
    Filter filter = new Filter();
    filter.is("mModel", "newmodelwithrelation").is("mAction", "rename_relation").is("mValue", "emptymodel_modelwithrelation");
    assertEquals(1, Migration.objects(getContext()).filter(filter).count());
}
Also used : NewModelWithRelation(com.orm.androrm.impl.migration.NewModelWithRelation) Filter(com.orm.androrm.Filter) NewModelWithRelation(com.orm.androrm.impl.migration.NewModelWithRelation) ModelWithRelation(com.orm.androrm.impl.migration.ModelWithRelation) EmptyModel(com.orm.androrm.impl.migration.EmptyModel) NewEmptyModel(com.orm.androrm.impl.migration.NewEmptyModel) Migrator(com.orm.androrm.migration.Migrator)

Example 5 with Filter

use of com.orm.androrm.Filter in project androrm by androrm.

the class FilterTest method testInFilterString.

public void testInFilterString() {
    Person tom = new Person();
    tom.setName("tom");
    tom.save(getContext());
    Person peter = new Person();
    peter.setName("peter");
    peter.save(getContext());
    Person susan = new Person();
    susan.setName("susan");
    susan.save(getContext());
    List<String> names = new ArrayList<String>();
    names.add("tom");
    names.add("peter");
    Filter filter = new Filter();
    filter.in("mName", names);
    QuerySet<Person> people = Person.objects(getContext()).filter(filter);
    assertEquals(2, people.count());
}
Also used : Filter(com.orm.androrm.Filter) ArrayList(java.util.ArrayList) Person(com.orm.androrm.impl.Person)

Aggregations

Filter (com.orm.androrm.Filter)21 ArrayList (java.util.ArrayList)7 Supplier (com.orm.androrm.impl.Supplier)6 Branch (com.orm.androrm.impl.Branch)5 Product (com.orm.androrm.impl.Product)5 Rule (com.orm.androrm.Rule)3 InStatement (com.orm.androrm.statement.InStatement)3 LikeStatement (com.orm.androrm.statement.LikeStatement)3 Statement (com.orm.androrm.statement.Statement)3 Person (com.orm.androrm.impl.Person)2 EmptyModel (com.orm.androrm.impl.migration.EmptyModel)2 NewEmptyModel (com.orm.androrm.impl.migration.NewEmptyModel)2 Migrator (com.orm.androrm.migration.Migrator)2 QuerySet (com.orm.androrm.QuerySet)1 NoSuchFieldException (com.orm.androrm.field.NoSuchFieldException)1 Car (com.orm.androrm.impl.Car)1 ModelWithRelation (com.orm.androrm.impl.migration.ModelWithRelation)1 NewModelWithRelation (com.orm.androrm.impl.migration.NewModelWithRelation)1