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.

472 lines
14 KiB

  1. package test;
  2. import Common;
  3. import service.OrderService;
  4. /**
  5. * Test order making, updating and deleting
  6. *
  7. * @author fbarbut
  8. */
  9. class TestOrders extends haxe.unit.TestCase
  10. {
  11. public function new(){
  12. super();
  13. }
  14. var c : db.Contract;
  15. var p : db.Product;
  16. var bob : db.User;
  17. /**
  18. * get a contract + a user + a product + empty orders
  19. */
  20. override function setup(){
  21. TestSuite.initDB();
  22. TestSuite.initDatas();
  23. db.Basket.emptyCache();
  24. c = TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE.contract;
  25. p = TestSuite.STRAWBERRIES;
  26. p.lock();
  27. p.stock = 8;
  28. p.update();
  29. bob = db.User.manager.get(1);
  30. }
  31. /**
  32. Test Basket creation and numbering
  33. **/
  34. public function testBasket(){
  35. var d = TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE;
  36. var o = OrderService.make(TestSuite.FRANCOIS, 3, TestSuite.STRAWBERRIES, d.id);
  37. assertEquals(1, o.basket.num);
  38. var o = OrderService.make(TestSuite.SEB, 1, TestSuite.STRAWBERRIES, d.id);
  39. assertEquals(2, o.basket.num);
  40. //order again, should keep existing basket number
  41. var o = OrderService.make(TestSuite.FRANCOIS, 1, TestSuite.APPLES, d.id);
  42. assertEquals(1, o.basket.num);
  43. //check bug of 2018-07 : changing the date and place of the distribution leads to lost basket (because basket were indexed on user-date-place)
  44. d.lock();
  45. d.date = new Date(2028,1,1,0,0,0);
  46. var place = new db.Place();
  47. place.name = "Chez Momo";
  48. place.zipCode = "54";
  49. place.amap = d.contract.amap;
  50. place.insert();
  51. d.place = place;
  52. d.update();
  53. //Seb's basket is still 2
  54. var basket = db.Basket.get(TestSuite.SEB,place,d.date);
  55. assertEquals(2, basket.num);
  56. var o = OrderService.make(TestSuite.SEB, 1, TestSuite.APPLES, d.id);
  57. assertEquals(2, o.basket.num);
  58. var o2 = OrderService.edit(o,5,true,null,false);
  59. assertEquals(2, o2.basket.num);
  60. //order to a different distrib in same contract should start a new numbering
  61. var d2 = service.DistributionService.create(d.contract,new Date(2026,6,6,0,0,0),new Date(2026,6,6,1,0,0),place.id,null,null,null,null,new Date(2026,6,4,0,0,0),new Date(2026,6,5,0,0,0));
  62. var o = OrderService.make(TestSuite.SEB, 12, TestSuite.APPLES, d2.id);
  63. assertEquals(1, o.basket.num);
  64. }
  65. /**
  66. * make orders & stock management
  67. */
  68. public function testStocks(){
  69. var stock = p.stock;
  70. assertTrue(c.type == db.Contract.TYPE_VARORDER);
  71. assertTrue(c.flags.has(db.Contract.ContractFlags.StockManagement));
  72. assertTrue(stock == 8);
  73. //bob orders 3 strawberries, stock fall to 2
  74. //order is update to 6 berries
  75. App.current.eventDispatcher.addOnce(function(e:Event){
  76. switch(e){
  77. case StockMove(e):
  78. assertTrue(e.move==-3);
  79. assertTrue(e.product==p);
  80. default:
  81. }
  82. });
  83. var o = OrderService.make(bob, 3, p, TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE.id);
  84. assertTrue(p.stock == 5);
  85. assertTrue(o.quantity == 3);
  86. //bob orders 6 more. stock fall to 0, order is reduced to 5
  87. //quantity is not 9 but 8
  88. App.current.eventDispatcher.addOnce(function(e:Event){
  89. switch(e){
  90. case StockMove(e):
  91. assertTrue(e.move==-5);
  92. assertTrue(e.product==p);
  93. default:
  94. }
  95. });
  96. var o = OrderService.make(bob, 6, p, TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE.id);
  97. assertTrue(p.stock == 0);
  98. assertTrue(o.quantity == 8);
  99. //bob orders again but cant order anything
  100. var o = OrderService.make(bob, 3, p, TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE.id);
  101. assertTrue(p.stock == 0);
  102. assertTrue(o.quantity == 8);
  103. }
  104. /**
  105. * test edit orders and stock management
  106. */
  107. function testOrderEdit(){
  108. var o = db.UserContract.manager.select( $user == bob && $product == p, true);
  109. //no order, stock at 8
  110. assertEquals(p.stock , 8);
  111. assertEquals(o , null);
  112. //bob orders 3 strawberries
  113. var o = OrderService.make(bob, 3, p , TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE.id);
  114. assertEquals(o.product.name, p.name);
  115. assertEquals(o.quantity, 3);
  116. assertEquals(p.stock , 5);
  117. //order edit, order 6 berries
  118. App.current.eventDispatcher.addOnce(function(e:Event){
  119. switch(e){
  120. case StockMove(e):
  121. assertTrue(e.move==-3);
  122. assertTrue(e.product==p);
  123. default:
  124. }
  125. });
  126. var o = OrderService.edit(o, 6);
  127. assertTrue(p.stock == 2);
  128. assertTrue(o.quantity == 6);
  129. //order edit, order 9 berries. ( 3 more, but stock fall to 0, reduced to 2 )
  130. App.current.eventDispatcher.addOnce(function(e:Event){
  131. switch(e){
  132. case StockMove(e):
  133. assertEquals( -2.0 , e.move );
  134. assertEquals( p , e.product);
  135. default:
  136. }
  137. });
  138. var o = OrderService.edit(o, 9);
  139. assertEquals(0.0 , p.stock);
  140. assertEquals(8.0 , o.quantity);
  141. //order more, but stock at 0
  142. var o = OrderService.edit(o, 12);
  143. assertEquals(0.0 , p.stock);
  144. assertEquals(8.0 , o.quantity);
  145. //order less
  146. var o = OrderService.edit(o, 6);
  147. assertEquals(2.0 , p.stock);
  148. assertEquals(6.0 , o.quantity);
  149. //floatQt : ordering float quantities should throw an exception
  150. var err = null;
  151. try{
  152. var o = OrderService.edit(o, 6.4);
  153. }catch(e:tink.core.Error){
  154. err = e.message;
  155. }
  156. assertTrue( err!=null );
  157. }
  158. /**
  159. * test orders with multiweight product
  160. */
  161. function testOrderWithMultiWeightProduct(){
  162. var chicken = TestSuite.CHICKEN;
  163. var distrib = db.Distribution.manager.select($contract == chicken.contract, false);
  164. var order = OrderService.make(bob, 1, chicken, distrib.id);
  165. assertEquals(1.0, order.quantity);
  166. assertEquals(chicken.id, order.product.id);
  167. assertEquals(chicken.price, order.productPrice);
  168. //order 2 more, should not aggregate because multiWeight is true
  169. var order2 = OrderService.make(bob, 2, chicken, distrib.id);
  170. assertTrue(order2.id != order.id);
  171. //we should get 3 different orders
  172. var orders = distrib.getOrders();
  173. //trace(OrderService.prepare(orders));
  174. assertEquals(3, orders.length);
  175. for ( o in orders){
  176. assertEquals(o.user.id, bob.id);
  177. assertEquals(o.product.id, chicken.id);
  178. assertEquals(1.0, o.quantity);
  179. }
  180. }
  181. /**
  182. * @author fbarbut
  183. * @date 2018-01-26
  184. * order a product, edit and set qt to zero, order again.
  185. * the same record should be re-used ( if not multiweight )
  186. */
  187. function testMakeOrderAndZeroQuantity(){
  188. var fraises = TestSuite.STRAWBERRIES;
  189. var distrib = db.Distribution.manager.select($contract == fraises.contract, false);
  190. var order = OrderService.make(bob, 1, fraises, distrib.id);
  191. order = OrderService.edit(order, 0);
  192. assertEquals(0.0, order.quantity);
  193. var order2 = OrderService.make(bob, 1, fraises, distrib.id);
  194. var bobOrders = [];
  195. for ( o in distrib.getOrders()) if (o.user.id == bob.id) bobOrders.push(o);
  196. assertFalse(fraises.multiWeight);
  197. assertEquals(1, bobOrders.length);
  198. assertEquals(1.0, bobOrders[0].quantity);
  199. }
  200. /**
  201. * Test order deletion and operation deletion in various contexts
  202. @author jbarbic
  203. */
  204. function testDelete(){
  205. var t = sugoi.i18n.Locale.texts;
  206. //[Test case] Should throw an error when trying to delete order and that the quantity is not zero
  207. var amapDistrib = TestSuite.DISTRIB_CONTRAT_AMAP;
  208. var amapContract = amapDistrib.contract;
  209. var order = OrderService.make(TestSuite.FRANCOIS, 1, TestSuite.PANIER_AMAP_LEGUMES, amapDistrib.id);
  210. var orderId = order.id;
  211. db.Operation.onOrderConfirm([order]);
  212. var e1 = null;
  213. try {
  214. service.OrderService.delete(order);
  215. }
  216. catch(x:tink.core.Error){
  217. e1 = x;
  218. }
  219. assertEquals(e1.message, "Deletion not possible: quantity is not zero.");
  220. assertTrue(db.UserContract.manager.get(orderId) != null);
  221. //[Test case] Amap contract and quantity zero with payments disabled
  222. //Check that order is deleted
  223. order = OrderService.edit(order, 0);
  224. var e2 = null;
  225. try {
  226. OrderService.delete(order);
  227. }
  228. catch(x:tink.core.Error){
  229. e2 = x;
  230. }
  231. assertEquals(e2, null);
  232. assertEquals(db.UserContract.manager.get(orderId), null);
  233. //[Test case] Amap contract and quantity zero with payments enabled and 2 orders
  234. //Check that first order is deleted but operation amount is at 0
  235. //Check that operation is deleted only at the second order deletion
  236. var order1 = OrderService.make(TestSuite.FRANCOIS, 1, TestSuite.PANIER_AMAP_LEGUMES, amapDistrib.id);
  237. db.Operation.onOrderConfirm([order1]);
  238. var order1Id = order1.id;
  239. order1 = OrderService.edit(order1, 0);
  240. db.Operation.onOrderConfirm([order1]);
  241. var order2 = OrderService.make(TestSuite.FRANCOIS, 1, TestSuite.PANIER_AMAP_LEGUMES, amapDistrib.id);
  242. db.Operation.onOrderConfirm([order2]);
  243. var order2Id = order2.id;
  244. order2 = OrderService.edit(order2, 0);
  245. db.Operation.onOrderConfirm([order2]);
  246. var operation = db.Operation.findCOrderTransactionFor(amapContract, TestSuite.FRANCOIS);
  247. var operationId = operation.id;
  248. var e3 = null;
  249. try {
  250. service.OrderService.delete(order1);
  251. }
  252. catch(x:tink.core.Error){
  253. e3 = x;
  254. }
  255. assertEquals(e3, null);
  256. assertEquals(db.UserContract.manager.get(order1Id), null);
  257. assertTrue(db.Operation.manager.get(operationId) != null);
  258. assertEquals(operation.name, "Contrat AMAP Légumes (La ferme de la Galinette) 1 deliveries");
  259. assertEquals(operation.amount, 0);
  260. var e4 = null;
  261. try {
  262. service.OrderService.delete(order2);
  263. }
  264. catch(x:tink.core.Error){
  265. e4 = x;
  266. }
  267. assertEquals(null, e4);
  268. assertEquals(null, db.UserContract.manager.get(order2Id));
  269. assertEquals(null, db.Operation.manager.get(operationId));
  270. //[Test case] Var Order contract and quantity zero with payments disabled
  271. //Check that order is deleted
  272. var variableDistrib = TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE;
  273. var g = variableDistrib.contract.amap;
  274. g.lock();
  275. g.flags.unset(HasPayments);
  276. g.update();
  277. var order = OrderService.make(TestSuite.FRANCOIS, 2, TestSuite.STRAWBERRIES, variableDistrib.id);
  278. var orderId = order.id;
  279. db.Operation.onOrderConfirm([order]);
  280. order = OrderService.edit(order, 0);
  281. db.Operation.onOrderConfirm([order]);
  282. var e1 = null;
  283. try {
  284. service.OrderService.delete(order);
  285. }
  286. catch(x:tink.core.Error){
  287. e1 = x;
  288. }
  289. assertEquals(false,variableDistrib.contract.amap.hasPayments() );
  290. assertEquals(null, e1);
  291. assertEquals(null, db.UserContract.manager.get(orderId));
  292. //[Test case] Var Order contract and quantity zero with payments enabled and 2 orders
  293. //Check that first order is deleted
  294. //Check that operation is deleted only at the second order deletion
  295. variableDistrib = TestSuite.DISTRIB_FRUITS_PLACE_DU_VILLAGE;
  296. var variableContract = variableDistrib.contract;
  297. var g = variableDistrib.contract.amap;
  298. g.lock();
  299. g.flags.set(HasPayments);
  300. g.update();
  301. assertTrue(variableContract.amap.hasPayments());
  302. var order1 = OrderService.make(TestSuite.FRANCOIS, 2, TestSuite.STRAWBERRIES, variableDistrib.id);
  303. db.Operation.onOrderConfirm([order1]);
  304. var order1Id = order1.id;
  305. var order2 = OrderService.make(TestSuite.FRANCOIS, 3, TestSuite.APPLES, variableDistrib.id);
  306. db.Operation.onOrderConfirm([order2]);
  307. var order2Id = order2.id;
  308. order1 = OrderService.edit(order1, 0);
  309. db.Operation.onOrderConfirm([order1]);
  310. order2 = OrderService.edit(order2, 0);
  311. db.Operation.onOrderConfirm([order2]);
  312. assertEquals(2, variableContract.getUserOrders(TestSuite.FRANCOIS,variableDistrib).length); //François has 2 orders
  313. var basket = db.Basket.get(TestSuite.FRANCOIS,variableDistrib.place,variableDistrib.date);
  314. assertEquals(2, basket.getOrders().length);
  315. var operation1 = db.Operation.findVOrderTransactionFor(order1.distribution.getKey(), TestSuite.FRANCOIS, variableContract.amap);
  316. var operation1Id = operation1.id;
  317. var operation2 = db.Operation.findVOrderTransactionFor(order2.distribution.getKey(), TestSuite.FRANCOIS, variableContract.amap);
  318. var operation2Id = operation2.id;
  319. assertEquals(operation1Id,operation2Id);
  320. var e2 = null;
  321. try {
  322. //delete strawberries order
  323. service.OrderService.delete(order1);
  324. }
  325. catch(x:tink.core.Error){
  326. e2 = x;
  327. }
  328. assertEquals(null, e2);
  329. assertEquals(null, db.UserContract.manager.get(order1Id) ); //order 1 is deleted
  330. assertTrue( db.Operation.manager.get(operation1Id) != null); //operation should be here
  331. assertEquals(0.0,operation1.amount);
  332. assertEquals(1 , basket.getOrders().length);
  333. var e3 = null;
  334. try {
  335. //delete apple order
  336. service.OrderService.delete(order2);
  337. }
  338. catch(x:tink.core.Error){
  339. e3 = x;
  340. }
  341. assertEquals(null, e3);
  342. assertEquals(null, db.UserContract.manager.get(order2Id)); //order 2 is deleted
  343. assertEquals(null, db.Operation.manager.get(operation1Id)); //operation should be deleted
  344. assertEquals(0 , basket.getOrders().length);
  345. //[Test case] 2 VarOrderContracts and quantity zero with payments enabled and 1 order each
  346. //Check that first order is deleted but operation amount is at 0
  347. //Check that operation is deleted only at the second order deletion
  348. var variableDistrib1 = TestSuite.DISTRIB_LEGUMES_RUE_SAUCISSE;
  349. var order1 = OrderService.make(TestSuite.FRANCOIS, 2, TestSuite.COURGETTES, variableDistrib1.id);
  350. assertTrue(order1.basket!=null);
  351. // trace("WE GOT A BASKET "+order1.basket.id);
  352. // trace("... THEN RE-GET BASKET user "+TestSuite.FRANCOIS.id+" place "+variableDistrib1.place.id+" date "+variableDistrib1.date);
  353. var basket = db.Basket.get(TestSuite.FRANCOIS,variableDistrib1.place,variableDistrib1.date);
  354. assertTrue(basket!=null);
  355. assertEquals(1, basket.getOrders().length);
  356. db.Operation.onOrderConfirm([order1]);
  357. var order1Id = order1.id;
  358. var variableDistrib2 = TestSuite.DISTRIB_PATISSERIES;
  359. var order2 = OrderService.make(TestSuite.FRANCOIS, 3, TestSuite.FLAN, variableDistrib2.id);
  360. db.Operation.onOrderConfirm([order2]);
  361. var order2Id = order2.id;
  362. order1 = OrderService.edit(order1, 0);
  363. db.Operation.onOrderConfirm([order1]);
  364. order2 = OrderService.edit(order2, 0);
  365. db.Operation.onOrderConfirm([order2]);
  366. //check basket
  367. var basket = db.Basket.get(TestSuite.FRANCOIS,variableDistrib1.place,variableDistrib1.date);
  368. assertEquals(2, basket.getOrders().length);
  369. var operation = db.Operation.findVOrderTransactionFor(order1.distribution.getKey(), TestSuite.FRANCOIS, variableDistrib1.contract.amap);
  370. var operationId = operation.id;
  371. var e4 = null;
  372. try {
  373. service.OrderService.delete(order1);
  374. }
  375. catch(x:tink.core.Error){
  376. e4 = x;
  377. }
  378. assertEquals(e4, null);
  379. assertEquals(db.UserContract.manager.get(order1Id), null);
  380. assertTrue(db.Operation.manager.get(operationId) != null); //op should still be here
  381. assertEquals(0.0,operation.amount);//...with amount 0
  382. var e5 = null;
  383. try {
  384. service.OrderService.delete(order2);
  385. }
  386. catch(x:tink.core.Error){
  387. e5 = x;
  388. }
  389. assertEquals(e5, null);
  390. assertEquals(db.UserContract.manager.get(order2Id), null);
  391. assertEquals(null, db.Operation.manager.get(operationId), null); //op should have been deleted
  392. }
  393. }