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.

ForeignKeyTest.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2012 Frédéric Gaillard.
  3. * Copyright 2012 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.IciqlException;
  20. import com.iciql.test.models.CategoryAnnotationOnly;
  21. import com.iciql.test.models.ProductAnnotationOnlyWithForeignKey;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import org.junit.Ignore;
  25. import org.junit.Test;
  26. import static org.junit.Assert.assertEquals;
  27. import static org.junit.Assert.assertTrue;
  28. /**
  29. * Tests of Foreign Keys.
  30. */
  31. public class ForeignKeyTest {
  32. /**
  33. * This object represents a database (actually a connection to the
  34. * database).
  35. */
  36. private Db db;
  37. @Before
  38. public void setUp() {
  39. db = IciqlSuite.openNewDb();
  40. db.insertAll(CategoryAnnotationOnly.getList());
  41. db.insertAll(ProductAnnotationOnlyWithForeignKey.getList());
  42. }
  43. @After
  44. public void tearDown() {
  45. db.dropTable(ProductAnnotationOnlyWithForeignKey.class);
  46. db.dropTable(CategoryAnnotationOnly.class);
  47. db.close();
  48. }
  49. @Test
  50. public void testForeignKeyWithOnDeleteCascade() {
  51. ProductAnnotationOnlyWithForeignKey p = new ProductAnnotationOnlyWithForeignKey();
  52. long count1 = db.from(p).selectCount();
  53. // should remove 2 associated products
  54. CategoryAnnotationOnly c = new CategoryAnnotationOnly();
  55. db.from(c).where(c.categoryId).is(1L).delete();
  56. long count2 = db.from(p).selectCount();
  57. assertEquals(count1, count2 + 2L);
  58. }
  59. @Test
  60. @Ignore
  61. public void testForeignKeyDropReferenceTable() {
  62. try {
  63. db.dropTable(CategoryAnnotationOnly.class);
  64. assertTrue("Should not be able to drop reference table!", false);
  65. } catch (IciqlException e) {
  66. assertEquals(e.getMessage(), IciqlException.CODE_CONSTRAINT_VIOLATION, e.getIciqlCode());
  67. }
  68. }
  69. }