You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelsTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.test;
  18. import com.iciql.Db;
  19. import com.iciql.DbInspector;
  20. import com.iciql.ValueCount;
  21. import com.iciql.ValidationRemark;
  22. import com.iciql.test.models.Product;
  23. import com.iciql.test.models.ProductAnnotationOnly;
  24. import com.iciql.test.models.ProductMixedAnnotation;
  25. import com.iciql.test.models.SupportedTypes;
  26. import com.iciql.util.StringUtils;
  27. import org.junit.After;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.ErrorCollector;
  32. import java.sql.SQLException;
  33. import java.text.MessageFormat;
  34. import java.util.List;
  35. import static com.iciql.test.IciqlSuite.assertEqualsIgnoreCase;
  36. import static org.junit.Assert.assertEquals;
  37. import static org.junit.Assert.assertTrue;
  38. /**
  39. * Test that the mapping between classes and tables is done correctly.
  40. */
  41. public class ModelsTest {
  42. /*
  43. * The ErrorCollector Rule allows execution of a test to continue after the
  44. * first problem is found and report them all at once
  45. */
  46. @Rule
  47. public ErrorCollector errorCollector = new ErrorCollector();
  48. private Db db;
  49. @Before
  50. public void setUp() {
  51. db = IciqlSuite.openNewDb();
  52. db.insertAll(Product.getList());
  53. db.insertAll(ProductAnnotationOnly.getList());
  54. db.insertAll(ProductMixedAnnotation.getList());
  55. }
  56. @After
  57. public void tearDown() {
  58. db.close();
  59. }
  60. @Test
  61. public void testValidateModels() {
  62. // SQLite metadata mapping in the JDBC driver needs improvement
  63. String schemaName = IciqlSuite.getDefaultSchema(db);
  64. DbInspector inspector = new DbInspector(db);
  65. validateModel(inspector, schemaName, new ProductAnnotationOnly(), IciqlSuite.isSQLite(db) ? 5 : 2);
  66. validateModel(inspector, schemaName, new ProductMixedAnnotation(), IciqlSuite.isSQLite(db) ? 6 : 4);
  67. }
  68. private void validateModel(DbInspector inspector, String schemaName, Object o, int expected) {
  69. List<ValidationRemark> remarks = inspector.validateModel(o, false);
  70. assertTrue("validation remarks are null for " + o.getClass().getName(), remarks != null);
  71. StringBuilder sb = new StringBuilder();
  72. sb.append("validation remarks for " + o.getClass().getName());
  73. sb.append('\n');
  74. for (ValidationRemark remark : remarks) {
  75. sb.append(remark.toString());
  76. sb.append('\n');
  77. if (remark.isError()) {
  78. errorCollector.addError(new SQLException(remark.toString()));
  79. }
  80. }
  81. if (IciqlSuite.isSQLite(db)) {
  82. assertEquals(sb.toString(), expected, remarks.size());
  83. } else if (StringUtils.isNullOrEmpty(schemaName)) {
  84. // no schema expected
  85. assertEquals(sb.toString(), expected - 1, remarks.size());
  86. } else {
  87. assertEquals(sb.toString(), expected, remarks.size());
  88. assertEqualsIgnoreCase(MessageFormat.format("@IQSchema(\"{0}\")", schemaName),
  89. remarks.get(0).message);
  90. }
  91. }
  92. @Test
  93. public void testSupportedTypes() {
  94. List<SupportedTypes> original = SupportedTypes.createList();
  95. db.insertAll(original);
  96. List<SupportedTypes> retrieved = db.from(SupportedTypes.SAMPLE).select();
  97. assertEquals(original.size(), retrieved.size());
  98. for (int i = 0; i < original.size(); i++) {
  99. SupportedTypes o = original.get(i);
  100. SupportedTypes r = retrieved.get(i);
  101. assertTrue(o.equivalentTo(r));
  102. }
  103. }
  104. @Test
  105. public void testModelGeneration() {
  106. List<SupportedTypes> original = SupportedTypes.createList();
  107. db.insertAll(original);
  108. DbInspector inspector = new DbInspector(db);
  109. List<String> models = inspector.generateModel(null, "SupportedTypes", "com.iciql.test.models", true,
  110. true);
  111. assertEquals(1, models.size());
  112. // a poor test, but a start
  113. String dbName = IciqlSuite.getDatabaseEngineName(db);
  114. if (dbName.equals("H2")) {
  115. assertEquals(1587, models.get(0).length());
  116. } else if (dbName.startsWith("HSQL")) {
  117. // HSQL uses Double instead of Float
  118. assertEquals(1591, models.get(0).length());
  119. } else if (dbName.equals("Apache Derby")) {
  120. // Derby uses java.sql.Timestamp not java.util.Date
  121. // Derby uses username as schema name
  122. assertEquals(1601, models.get(0).length());
  123. } else if (dbName.equals("PostgreSQL")) {
  124. assertEquals(1643, models.get(0).length());
  125. } else if (dbName.equals("MySQL")) {
  126. // MySQL uses timestamp default values like
  127. // 0000-00-00 00:00:00 and CURRENT_TIMESTAMP
  128. assertEquals(1571, models.get(0).length());
  129. } else if (dbName.equals("SQLite")) {
  130. assertEquals(1566, models.get(0).length());
  131. } else {
  132. // unknown database
  133. assertEquals(0, models.get(0).length());
  134. }
  135. }
  136. @Test
  137. public void testDiscreteUpdateStringTrimming() {
  138. List<SupportedTypes> original = SupportedTypes.createList();
  139. db.insertAll(original);
  140. SupportedTypes s1 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
  141. db.from(SupportedTypes.SAMPLE)
  142. .set(SupportedTypes.SAMPLE.myString)
  143. .to(s1.myString + s1.myString + s1.myString + s1.myString)
  144. .update();
  145. SupportedTypes s2 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
  146. assertEquals(40, s2.myString.length());
  147. }
  148. @Test
  149. public void testColumnSelection() {
  150. List<SupportedTypes> original = SupportedTypes.createList();
  151. db.insertAll(original);
  152. List<String> myStrings = db.from(SupportedTypes.SAMPLE)
  153. .select(SupportedTypes.SAMPLE.myString);
  154. assertEquals(10, myStrings.size());
  155. List<Integer> ids = db.from(SupportedTypes.SAMPLE)
  156. .orderByDesc(SupportedTypes.SAMPLE.myInteger)
  157. .selectDistinct(SupportedTypes.SAMPLE.myInteger);
  158. assertEquals(10, ids.size());
  159. assertEquals("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]", ids.toString());
  160. }
  161. @Test
  162. public void testGroupByCount() {
  163. Product products = new Product();
  164. List<ValueCount<String>> categories = db.from(products)
  165. .selectCount(products.category);
  166. assertEquals(5, categories.size());
  167. assertEquals("[Meat/Poultry=1, Produce=1, Seafood=1, Beverages=2, Condiments=5]", categories.toString());
  168. }
  169. @Test
  170. public void testGroupByCountDesc() {
  171. Product products = new Product();
  172. List<ValueCount<String>> categories = db.from(products)
  173. .selectCountDesc(products.category);
  174. assertEquals(5, categories.size());
  175. assertEquals("[Condiments=5, Beverages=2, Seafood=1, Produce=1, Meat/Poultry=1]", categories.toString());
  176. }
  177. @Test
  178. public void testFilteredGroupByCount() {
  179. Product products = new Product();
  180. List<ValueCount<String>> categories = db.from(products)
  181. .where(products.category).isNot("Seafood")
  182. .selectCount(products.category);
  183. assertEquals(4, categories.size());
  184. assertEquals("[Meat/Poultry=1, Produce=1, Beverages=2, Condiments=5]", categories.toString());
  185. }
  186. @Test
  187. public void testFilteredGroupByCountDesc() {
  188. Product products = new Product();
  189. List<ValueCount<String>> categories = db.from(products)
  190. .where(products.category).isNot("Seafood")
  191. .selectCountDesc(products.category);
  192. assertEquals(4, categories.size());
  193. assertEquals("[Condiments=5, Beverages=2, Produce=1, Meat/Poultry=1]", categories.toString());
  194. }
  195. @Test
  196. public void testWhereByString() {
  197. Product products = new Product();
  198. List<Product> seafoodProducts = db.from(products).where(products.category).is("Seafood").select();
  199. assertEquals(1, seafoodProducts.size());
  200. }
  201. }