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.

UpdateTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 static java.sql.Date.valueOf;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import com.iciql.Db;
  25. import com.iciql.test.models.Customer;
  26. import com.iciql.test.models.Order;
  27. import com.iciql.test.models.Product;
  28. /**
  29. * Tests the Db.update() function.
  30. *
  31. * @author dmoebius at scoop dash gmbh dot de
  32. */
  33. public class UpdateTest {
  34. private Db db;
  35. @Before
  36. public void setUp() throws Exception {
  37. db = Db.open("jdbc:h2:mem:", "sa", "sa");
  38. db.insertAll(Product.getList());
  39. db.insertAll(Customer.getList());
  40. db.insertAll(Order.getList());
  41. }
  42. @After
  43. public void tearDown() {
  44. db.close();
  45. }
  46. @Test
  47. public void testSimpleUpdate() {
  48. Product p = new Product();
  49. Product pChang = db.from(p).where(p.productName).is("Chang").selectFirst();
  50. // update unitPrice from 19.0 to 19.5
  51. pChang.unitPrice = 19.5;
  52. // update unitsInStock from 17 to 16
  53. pChang.unitsInStock = 16;
  54. db.update(pChang);
  55. Product p2 = new Product();
  56. Product pChang2 = db.from(p2).where(p2.productName).is("Chang").selectFirst();
  57. assertEquals(19.5, pChang2.unitPrice.doubleValue(), 0.001);
  58. assertEquals(16, pChang2.unitsInStock.intValue());
  59. // undo update
  60. pChang.unitPrice = 19.0;
  61. pChang.unitsInStock = 17;
  62. db.update(pChang);
  63. }
  64. @Test
  65. public void testSimpleUpdateWithCombinedPrimaryKey() {
  66. Order o = new Order();
  67. Order ourOrder = db.from(o).where(o.orderDate).is(valueOf("2007-01-02")).selectFirst();
  68. ourOrder.orderDate = valueOf("2007-01-03");
  69. db.update(ourOrder);
  70. Order ourUpdatedOrder = db.from(o).where(o.orderDate).is(valueOf("2007-01-03")).selectFirst();
  71. assertTrue("updated order not found", ourUpdatedOrder != null);
  72. // undo update
  73. ourOrder.orderDate = valueOf("2007-01-02");
  74. db.update(ourOrder);
  75. }
  76. @Test
  77. public void testSimpleMerge() {
  78. Product p = new Product();
  79. Product pChang = db.from(p).where(p.productName).is("Chang").selectFirst();
  80. // update unitPrice from 19.0 to 19.5
  81. pChang.unitPrice = 19.5;
  82. // update unitsInStock from 17 to 16
  83. pChang.unitsInStock = 16;
  84. db.merge(pChang);
  85. Product p2 = new Product();
  86. Product pChang2 = db.from(p2).where(p2.productName).is("Chang").selectFirst();
  87. assertEquals(19.5, pChang2.unitPrice, 0.001);
  88. assertEquals(16, pChang2.unitsInStock.intValue());
  89. // undo update
  90. pChang.unitPrice = 19.0;
  91. pChang.unitsInStock = 17;
  92. db.merge(pChang);
  93. }
  94. @Test
  95. public void testSimpleMergeWithCombinedPrimaryKey() {
  96. Order o = new Order();
  97. Order ourOrder = db.from(o).where(o.orderDate).is(valueOf("2007-01-02")).selectFirst();
  98. ourOrder.orderDate = valueOf("2007-01-03");
  99. db.merge(ourOrder);
  100. Order ourUpdatedOrder = db.from(o).where(o.orderDate).is(valueOf("2007-01-03")).selectFirst();
  101. assertTrue("updated order not found", ourUpdatedOrder != null);
  102. // undo update
  103. ourOrder.orderDate = valueOf("2007-01-02");
  104. db.merge(ourOrder);
  105. }
  106. @Test
  107. public void testSetColumns() {
  108. Product p = new Product();
  109. Product original = db.from(p).where(p.productId).is(1).selectFirst();
  110. // update string and double columns
  111. db.from(p).set(p.productName).to("updated").increment(p.unitPrice).by(3.14).increment(p.unitsInStock)
  112. .by(2).where(p.productId).is(1).update();
  113. // confirm the data was properly updated
  114. Product revised = db.from(p).where(p.productId).is(1).selectFirst();
  115. assertEquals("updated", revised.productName);
  116. assertEquals(original.unitPrice + 3.14, revised.unitPrice, 0.001);
  117. assertEquals(original.unitsInStock + 2, revised.unitsInStock.intValue());
  118. // restore the data
  119. db.from(p).set(p.productName).to(original.productName).set(p.unitPrice).to(original.unitPrice)
  120. .increment(p.unitsInStock).by(-2).where(p.productId).is(1).update();
  121. // confirm the data was properly restored
  122. Product restored = db.from(p).where(p.productId).is(1).selectFirst();
  123. assertEquals(original.productName, restored.productName);
  124. assertEquals(original.unitPrice, restored.unitPrice);
  125. assertEquals(original.unitsInStock, restored.unitsInStock);
  126. double unitPriceOld = db.from(p).where(p.productId).is(1).selectFirst().unitPrice;
  127. // double the unit price
  128. db.from(p).increment(p.unitPrice).by(p.unitPrice).where(p.productId).is(1).update();
  129. double unitPriceNew = db.from(p).where(p.productId).is(1).selectFirst().unitPrice;
  130. assertEquals(unitPriceOld * 2, unitPriceNew, 0.001);
  131. }
  132. }