Package org.yaac.server.egql.evaluator

Source Code of org.yaac.server.egql.evaluator.FunctionEvaluatorTest

package org.yaac.server.egql.evaluator;

import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.yaac.server.egql.TestUtil.parser;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import org.yaac.shared.ErrorCode;
import org.yaac.shared.SharedConstants;
import org.yaac.test.GAETestBase;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.IMHandle;
import com.google.appengine.api.datastore.IMHandle.Scheme;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.datastore.PostalAddress;
import com.google.appengine.api.datastore.ShortBlob;
import com.google.appengine.api.users.User;


/**
* @author Max Zhu (thebbsky@gmail.com)
*
*/
public class FunctionEvaluatorTest extends GAETestBase {

  @Test
  public void testCASE() throws RecognitionException {
    assertEquals(new EvaluationResult("AB124DF"),
        parser("ucase('ab124Df')").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(new BigDecimal("12356")).withWarning(ErrorCode.W103),
        parser("ucase(12356)").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(new BigDecimal("12356")).withWarning(ErrorCode.W101),
        parser("ucase(12356, now())").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(null).withWarning(ErrorCode.W101),
        parser("ucase()").bool_exp().e.evaluate(null));
   
    assertEquals("ab124df", parser("lcase('ab124Df')").bool_exp().e.evaluate(null).getPayload());
    assertEquals(new EvaluationResult(new BigDecimal("12356")).withWarning(ErrorCode.W104),
        parser("lcase(12356)").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(new BigDecimal("12356")).withWarning(ErrorCode.W102),
        parser("lcase(12356, now())").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(null).withWarning(ErrorCode.W102),
        parser("lcase()").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testFORMAT() throws RecognitionException {
    assertEquals("1234.560",
        parser("format(1234.56, '####.000')").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testLEN() throws RecognitionException {
    assertEquals(new EvaluationResult(new BigDecimal("1234.56")).withWarning(ErrorCode.W108),
        parser("len(1234.56)").bool_exp().e.evaluate(null));
    assertEquals(new EvaluationResult(new Long("7")),
        parser("len('1234.56')").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testMID() throws RecognitionException {
    assertEquals("cdefg",
        parser("mid('abcdefg', 2)").bool_exp().e.evaluate(null).getPayload());
    assertEquals(new EvaluationResult("cde"),
        parser("mid('abcdefg', 2, 3)").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testROUND() throws RecognitionException {
    assertEquals(new BigDecimal("10.24"),
        parser("round(10.2356, 2)").bool_exp().e.evaluate(null).getPayload());
   
    assertEquals("ab124Df",
        parser("round('ab124Df', 3)").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testTYPE() throws RecognitionException {
    assertEquals("decimal", parser("type(10.2356)").bool_exp().e.evaluate(null).getPayload());
    assertEquals("bool", parser("type(n)").bool_exp().e.evaluate(null).getPayload());
    assertEquals("string", parser("type('10.2356')").bool_exp().e.evaluate(null).getPayload());
    assertEquals("datetime", parser("type(now())").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testDATETIME() throws RecognitionException {   
    Calendar expected = Calendar.getInstance();
    expected.clear();
    expected.set(Calendar.YEAR, 2011);
    expected.set(Calendar.MONTH, Calendar.AUGUST);
    expected.set(Calendar.DAY_OF_MONTH, 8);
   
    assertEquals(expected.getTime(),
        parser("datetime('20110808')").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testUser() throws RecognitionException {
    assertEquals(new User("a@b.c", SharedConstants.User.DEFAULT_AUTH_DOMAIN),
        parser("user('a@b.c')").bool_exp().e.evaluate(null).getPayload());
   
    assertEquals(new User("a@b.c", "cust.com"),
        parser("user('a@b.c', 'cust.com')").bool_exp().e.evaluate(null).getPayload());
   
    assertEquals(new EvaluationResult(new BigDecimal("123456")).withWarning(ErrorCode.W121),
        parser("user(123456)").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testKey1() throws Exception {
    assertEquals(new EvaluationResult("abcde").withWarning(ErrorCode.W129),
        parser("key('abcde')").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testKey2() throws Exception {   
    // a valid key string
    String keyStr = "ahZ5ZXRhbm90aGVyYWRtaW5jb25zb2xlci0LEgNqb2IiJDU5YzNhZDFkLTI0ZTItNDQ3Zi05ZmM3LThkMGJkNDM2ODczMww";
    Key expected = KeyFactory.stringToKey(keyStr);
   
    assertEquals(expected,
        parser("key('" + keyStr + "')").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testKey3() throws Exception {
    Key key1 = KeyFactory.createKey("kind1", 10L);
   
    assertEquals(key1,
        parser("key('kind1', 10)").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testKey4() throws Exception {
    Key key1 = KeyFactory.createKey("kind1", 10L);
    Key key2 = KeyFactory.createKey(key1, "kind2", "name2");
    Key key3 = KeyFactory.createKey(key2, "kind3", 30L);
    Key key4 = KeyFactory.createKey(key3, "kind4", "name4");
    Key key5 = KeyFactory.createKey(key4, "kind5", 50L);
   
    assertEquals(key5,
        parser("key('kind1',10,'kind2','name2','kind3',30,'kind4','name4','kind5',50)").bool_exp().e.evaluate(
            null).getPayload());
  }
 
  /**
   * normal case
   *
   * @throws Exception
   */
  @Test
  public void testIMHandle() throws Exception {
    IMHandle expected = new IMHandle(Scheme.sip, "address1");
    assertEquals(expected, parser("imhandle('sip', 'address1')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * invalid protocol
   *
   * @throws Exception
   */
  @Test
  public void testIMHandle2() throws Exception {
    assertEquals(new EvaluationResult("sip1").withWarning(ErrorCode.W142),
        parser("imhandle('sip1', 'address1')").bool_exp().e.evaluate(null));
  }
 
  @Test
  public void testList() throws Exception {
    @SuppressWarnings("unchecked")
    List<?> expected = newArrayList("str1", new BigDecimal("10"), 100D, 101L);
    assertEquals(expected,
        parser("list('str1', 10, double(100), long(101))").bool_exp().e.evaluate(null).getPayload());
  }
 
  @Test
  public void testNull() throws Exception {
    assertNull(parser("null()").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * shortblob('true')
   *
   * @throws Exception
   */
  @Test
  public void testShortBlob() throws Exception {
    assertEquals(new ShortBlob("abcde".getBytes()),
        parser("shortblob('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * postaladdress('true')
   *
   * @throws Exception
   */
  @Test
  public void testPostalAddress() throws Exception {
    assertEquals(new PostalAddress("abcde"),
        parser("postaladdress('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * phonenumber('true')
   *
   * @throws Exception
   */
  @Test
  public void testPhoneNumber() throws Exception {
    assertEquals(new PhoneNumber("abcde"),
        parser("phonenumber('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * link('true')
   *
   * @throws Exception
   */
  @Test
  public void testLink() throws Exception {
    assertEquals(new Link("abcde"),
        parser("link('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * geopt(1.0,2.0)
   *
   * @throws Exception
   */
  @Test
  public void testGeoPt() throws Exception {
    assertEquals(new GeoPt(1f, 2f),
        parser("geopt(1, 2)").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * email('true')
   *
   * @throws Exception
   */
  @Test
  public void testEmail() throws Exception {
    assertEquals(new Email("abcde"),
        parser("email('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   * blobkey('abc')
   *
   * @throws Exception
   */
  @Test
  public void testBlobKey() throws Exception {
    assertEquals(new BlobKey("abcde"),
        parser("blobkey('abcde')").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   *
   * @throws Exception
   */
  @Test
  public void testLong() throws Exception {
    assertEquals(5l,
        parser("long(5.5)").bool_exp().e.evaluate(null).getPayload());
  }
 
  /**
   *
   * @throws Exception
   */
  @Test
  public void testDouble() throws Exception {
    assertEquals(5d,
        parser("double(5)").bool_exp().e.evaluate(null).getPayload());
  }
}
TOP

Related Classes of org.yaac.server.egql.evaluator.FunctionEvaluatorTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.