/datastore/src/test/java/org/jboss/test/capedwarf/datastore/test/QueryBasicsTestCase.java

https://github.com/luksa/capedwarf-blue · Java · 104 lines · 57 code · 19 blank · 28 comment · 0 complexity · 56db99d60d02095fb17055979b37b173 MD5 · raw file

  1. /*
  2. *
  3. * * JBoss, Home of Professional Open Source.
  4. * * Copyright 2011, Red Hat, Inc., and individual contributors
  5. * * as indicated by the @author tags. See the copyright.txt file in the
  6. * * distribution for a full listing of individual contributors.
  7. * *
  8. * * This is free software; you can redistribute it and/or modify it
  9. * * under the terms of the GNU Lesser General Public License as
  10. * * published by the Free Software Foundation; either version 2.1 of
  11. * * the License, or (at your option) any later version.
  12. * *
  13. * * This software is distributed in the hope that it will be useful,
  14. * * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * * Lesser General Public License for more details.
  17. * *
  18. * * You should have received a copy of the GNU Lesser General Public
  19. * * License along with this software; if not, write to the Free
  20. * * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  22. *
  23. */
  24. package org.jboss.test.capedwarf.datastore.test;
  25. import com.google.appengine.api.datastore.Entity;
  26. import com.google.appengine.api.datastore.KeyFactory;
  27. import com.google.appengine.api.datastore.PreparedQuery;
  28. import com.google.appengine.api.datastore.Query;
  29. import org.jboss.arquillian.junit.Arquillian;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import static com.google.appengine.api.datastore.Query.FilterOperator.EQUAL;
  33. import static org.junit.Assert.assertEquals;
  34. /**
  35. * Datastore querying basic tests.
  36. *
  37. * @author <a href="mailto:marko.luksa@gmail.com">Marko Luksa</a>
  38. */
  39. @RunWith(Arquillian.class)
  40. public class QueryBasicsTestCase extends QueryTestCase {
  41. @Test
  42. public void queryingByKindOnlyReturnsEntitiesOfRequestedKind() throws Exception {
  43. Entity person = new Entity(KeyFactory.createKey("Person", 1));
  44. service.put(person);
  45. Entity address = new Entity(KeyFactory.createKey("Address", 1));
  46. service.put(address);
  47. assertSingleResult(person, new Query("Person"));
  48. }
  49. @Test(expected = PreparedQuery.TooManyResultsException.class)
  50. public void singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult() throws Exception {
  51. createEntity("Person", 1).store();
  52. createEntity("Person", 2).store();
  53. PreparedQuery preparedQuery = service.prepare(new Query("Person"));
  54. preparedQuery.asSingleEntity();
  55. }
  56. @Test
  57. public void testMultipleFilters() throws Exception {
  58. Entity johnDoe = createEntity("Person", 1)
  59. .withProperty("name", "John")
  60. .withProperty("lastName", "Doe")
  61. .store();
  62. Entity johnBooks = createEntity("Person", 2)
  63. .withProperty("name", "John")
  64. .withProperty("lastName", "Books")
  65. .store();
  66. Entity janeDoe = createEntity("Person", 3)
  67. .withProperty("name", "Jane")
  68. .withProperty("lastName", "Doe")
  69. .store();
  70. Query query = new Query("Person")
  71. .addFilter("name", EQUAL, "John")
  72. .addFilter("lastName", EQUAL, "Doe");
  73. assertSingleResult(johnDoe, query);
  74. }
  75. @Test
  76. public void testKeysOnly() throws Exception {
  77. Entity john = createEntity("Person", 1)
  78. .withProperty("name", "John")
  79. .store();
  80. Query query = new Query("Person").setKeysOnly();
  81. PreparedQuery preparedQuery = service.prepare(query);
  82. Entity entity = preparedQuery.asSingleEntity();
  83. assertEquals(john.getKey(), entity.getKey());
  84. }
  85. }