@Test public void queryWithRawQuery() { final List<User> users = TestFactory.newUsers(20); int counter = 1; for (int i = 0; i < users.size(); i++) { char[] chars = new char[counter++]; Arrays.fill(chars, '*'); // wtf is going on? users.set(i, User.newInstance(null, new String(chars))); } putUsersBlocking(users); final List<User> usersWithLongName = new ArrayList<User>(users.size()); int lengthSum = 0; for (User user : users) { lengthSum += user.email().length(); } final int avrLength = lengthSum / users.size(); for (User user : users) { if (user.email().length() > avrLength) { usersWithLongName.add(user); } } final String query = "Select * from " + UserTableMeta.TABLE + " where length(" + UserTableMeta.COLUMN_EMAIL + ") > " + "(select avg(length(" + UserTableMeta.COLUMN_EMAIL + ")) from users)"; final List<User> usersFromQuery = storIOSQLite .get() .listOfObjects(User.class) .withQuery(RawQuery.builder().query(query).build()) .prepare() .executeAsBlocking(); assertThat(usersFromQuery).isEqualTo(usersWithLongName); }
@Test public void testIt() { User u = new User(); u.email = "*****@*****.**"; u.ignored = "test"; ds.save(u); User uLoaded = ds.find(User.class).get(); Assert.assertEquals("never, never", uLoaded.ignored); }
global void updateUser(Id userId, Id portalId, Auth.UserData data){ User u = new User(id=userId); u.email = data.email; u.lastName = data.lastName; u.firstName = data.firstName; String contactId = [SELECT contactId FROM user WHERE Id= :userId].contactId; Contact c = new Contact(Id=contactId); c.Facebook_ID__c = data.username; System.debug('Updating user record for '+data.username); System.debug('Updating contact ID is '+contactId); update(u); update(c); }
@Test public void queryWithRawQueryAndArguments() { final User testUser = User.newInstance(null, "testUserName"); final List<User> users = TestFactory.newUsers(10); users.add(testUser); putUsersBlocking(users); final String query = "SELECT * FROM " + UserTableMeta.TABLE + " WHERE " + UserTableMeta.COLUMN_EMAIL + " LIKE ?"; final List<User> usersFromQuery = storIOSQLite .get() .listOfObjects(User.class) .withQuery(RawQuery.builder().query(query).args(testUser.email()).build()) .prepare() .executeAsBlocking(); assertThat(usersFromQuery).isNotNull(); assertThat(usersFromQuery).hasSize(1); assertThat(usersFromQuery.get(0)).isEqualTo(testUser); }
@Test public void queryOneByField() { final List<User> users = putUsersBlocking(3); for (User user : users) { final List<User> usersFromQuery = storIOSQLite .get() .listOfObjects(User.class) .withQuery( Query.builder() .table(UserTableMeta.TABLE) .where(UserTableMeta.COLUMN_EMAIL + "=?") .whereArgs(user.email()) .build()) .prepare() .executeAsBlocking(); assertThat(usersFromQuery).isNotNull(); assertThat(usersFromQuery).hasSize(1); assertThat(usersFromQuery.get(0)).isEqualTo(user); } }
global User createUser(Id portalId, Auth.UserData data){ Account a = [SELECT Id FROM account WHERE name='CloudCable Customer Mobile App']; Contact c = new Contact(); c.accountId = a.Id; c.email = data.email; c.firstName = data.firstName; c.lastName = data.lastName; c.Facebook_ID__c = data.username; insert(c); User u = new User(); Profile p = [SELECT Id FROM profile WHERE name='API Portal User']; // Use incoming email for username, since we're creating a portal user u.username = data.email; u.email = data.email; u.lastName = data.lastName; u.firstName = data.firstName; u.alias = (data.username != null) ? data.username : data.identifier; if (u.alias.length() > 8) { u.alias = u.alias.substring(0, 8); } u.languagelocalekey = UserInfo.getLocale(); u.localesidkey = UserInfo.getLocale(); u.emailEncodingKey = 'UTF-8'; u.timeZoneSidKey = 'America/Los_Angeles'; u.profileId = p.Id; u.contactId = c.Id; System.debug('Returning new user record for '+data.username); return u; }