Repository: realm/realm-swift
File: Realm/Tests/QueryTests.m (Objective-C)

Code before:
    [PersonObject createInRealm:realm withObject:@[@"Tim", @29]];
    [PersonObject createInRealm:realm withObject:@[@"Ari", @33]];
    [realm commitWriteTransaction];

    XCTAssertEqual(2U, [[PersonObject objectsWhere:@"name == 'Ari' or age < 30"] count]);
    XCTAssertEqual(1U, [[PersonObject objectsWhere:@"name == 'Ari' or age > 40"] count]);
}

- (void)testCompoundAndQuery {
    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];
    [PersonObject createInRealm:realm withObject:@[@"Tim", @29]];
    [PersonObject createInRealm:realm withObject:@[@"Ari", @33]];
    [realm commitWriteTransaction];

    XCTAssertEqual(1U, [[PersonObject objectsWhere:@"name == 'Ari' and age > 30"] count]);
    XCTAssertEqual(0U, [[PersonObject objectsWhere:@"name == 'Ari' and age > 40"] count]);
}

- (void)testINPredicate
{
    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];
    StringObject *so = [StringObject createInRealm:realm withObject:(@[@"abc"])];
    [AllTypesObject createInRealm:realm withObject:@[@YES, @1, @1.0f, @1.0, @"a", [@"a" dataUsingEncoding:NSUTF8StringEncoding], [NSDate dateWithTimeIntervalSince1970:1], @YES, @1LL, @1, so]];
    [realm commitWriteTransaction];

    // Tests for each type always follow: none, some, more

    ////////////////////////
    // Literal Predicates
    ////////////////////////

    // BOOL
    XCTAssertEqual(0U, [[AllTypesObject objectsWhere:@"boolCol IN {NO}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"boolCol IN {YES}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"boolCol IN {NO, YES}"] count]);

    // int
    XCTAssertEqual(0U, [[AllTypesObject objectsWhere:@"intCol IN {0, 2, 3}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"intCol IN {1}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"intCol IN {1, 2}"] count]);

    // float
    XCTAssertEqual(0U, [[AllTypesObject objectsWhere:@"floatCol IN {0, 2, 3}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"floatCol IN {1}"] count]);
    XCTAssertEqual(1U, [[AllTypesObject objectsWhere:@"floatCol IN {1, 2}"] count]);

    // double

Review comment (suggestion):
You could just create an NSPredicate here and passing that in rather than passing in the format and args.


Code after:
    [PersonObject createInRealm:realm withObject:@[@"Tim", @29]];
    [PersonObject createInRealm:realm withObject:@[@"Ari", @33]];
    [realm commitWriteTransaction];

    XCTAssertEqual(2U, [[PersonObject objectsWhere:@"name == 'Ari' or age < 30"] count]);
    XCTAssertEqual(1U, [[PersonObject objectsWhere:@"name == 'Ari' or age > 40"] count]);
}

- (void)testCompoundAndQuery {
    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];
    [PersonObject createInRealm:realm withObject:@[@"Tim", @29]];
    [PersonObject createInRealm:realm withObject:@[@"Ari", @33]];
    [realm commitWriteTransaction];

    XCTAssertEqual(1U, [[PersonObject objectsWhere:@"name == 'Ari' and age > 30"] count]);
    XCTAssertEqual(0U, [[PersonObject objectsWhere:@"name == 'Ari' and age > 40"] count]);
}

- (void)testClass:(Class)class
  withNormalCount:(NSUInteger)normalCount
         notCount:(NSUInteger)notCount
            where:(NSString *)predicateFormat, ...
{
    va_list args;
    va_start(args, predicateFormat);
    va_end(args);
    XCTAssertEqual(normalCount, [[class objectsWhere:predicateFormat args:args] count]);
    predicateFormat = [NSString stringWithFormat:@"NOT(%@)", predicateFormat];
    va_start(args, predicateFormat);
    va_end(args);
    XCTAssertEqual(notCount, [[class objectsWhere:predicateFormat args:args] count]);
}

- (void)testINPredicate
{
    RLMRealm *realm = [RLMRealm defaultRealm];

    [realm beginWriteTransaction];
    StringObject *so = [StringObject createInRealm:realm withObject:(@[@"abc"])];
    [AllTypesObject createInRealm:realm withObject:@[@YES, @1, @1.0f, @1.0, @"a", [@"a" dataUsingEncoding:NSUTF8StringEncoding], [NSDate dateWithTimeIntervalSince1970:1], @YES, @1LL, @1, so]];
    [realm commitWriteTransaction];

    // Tests for each type always follow: none, some, more

    ////////////////////////
    // Literal Predicates
    ////////////////////////

    // BOOL