|
| 1 | +package testdb; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.util.Optional; |
| 7 | +import org.junit.Test; |
| 8 | +import testdb.brands.*; |
| 9 | +import testdb.categories.*; |
| 10 | +import testdb.customtypes.Defaulted.Provided; |
| 11 | +import testdb.customtypes.Defaulted.UseDefault; |
| 12 | +import testdb.product_categories.*; |
| 13 | +import testdb.products.*; |
| 14 | + |
| 15 | +/** Tests for composite primary keys using the product_categories table. */ |
| 16 | +public class CompositeIdTest { |
| 17 | + private final ProductCategoriesRepoImpl productCategoriesRepo = new ProductCategoriesRepoImpl(); |
| 18 | + private final ProductsRepoImpl productsRepo = new ProductsRepoImpl(); |
| 19 | + private final CategoriesRepoImpl categoriesRepo = new CategoriesRepoImpl(); |
| 20 | + private final BrandsRepoImpl brandsRepo = new BrandsRepoImpl(); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testInsertWithCompositeId() { |
| 24 | + MariaDbTestHelper.run( |
| 25 | + c -> { |
| 26 | + var brand = brandsRepo.insert(new BrandsRowUnsaved("TestBrand", "test-brand"), c); |
| 27 | + |
| 28 | + var product = |
| 29 | + productsRepo.insert( |
| 30 | + new ProductsRowUnsaved( |
| 31 | + "SKU001", |
| 32 | + "Test Product", |
| 33 | + new BigDecimal("99.99"), |
| 34 | + new Provided<>(Optional.of(brand.brandId())), |
| 35 | + new UseDefault<>(), |
| 36 | + new UseDefault<>(), |
| 37 | + new UseDefault<>(), |
| 38 | + new UseDefault<>(), |
| 39 | + new UseDefault<>(), |
| 40 | + new UseDefault<>(), |
| 41 | + new UseDefault<>(), |
| 42 | + new UseDefault<>(), |
| 43 | + new UseDefault<>(), |
| 44 | + new UseDefault<>(), |
| 45 | + new UseDefault<>(), |
| 46 | + new UseDefault<>(), |
| 47 | + new UseDefault<>()), |
| 48 | + c); |
| 49 | + |
| 50 | + var category = |
| 51 | + categoriesRepo.insert(new CategoriesRowUnsaved("Electronics", "electronics"), c); |
| 52 | + |
| 53 | + var productCategory = |
| 54 | + productCategoriesRepo.insert( |
| 55 | + new ProductCategoriesRowUnsaved(product.productId(), category.categoryId()), c); |
| 56 | + |
| 57 | + assertEquals(product.productId(), productCategory.productId()); |
| 58 | + assertEquals(category.categoryId(), productCategory.categoryId()); |
| 59 | + |
| 60 | + var compositeId = productCategory.compositeId(); |
| 61 | + assertEquals(product.productId(), compositeId.productId()); |
| 62 | + assertEquals(category.categoryId(), compositeId.categoryId()); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testSelectByCompositeId() { |
| 68 | + MariaDbTestHelper.run( |
| 69 | + c -> { |
| 70 | + var product = |
| 71 | + productsRepo.insert( |
| 72 | + new ProductsRowUnsaved("SKU002", "Product", new BigDecimal("50.00")), c); |
| 73 | + var category = categoriesRepo.insert(new CategoriesRowUnsaved("Category", "cat-slug"), c); |
| 74 | + |
| 75 | + productCategoriesRepo.insert( |
| 76 | + new ProductCategoriesRowUnsaved( |
| 77 | + product.productId(), |
| 78 | + category.categoryId(), |
| 79 | + new Provided<>(true), |
| 80 | + new Provided<>((short) 1)), |
| 81 | + c); |
| 82 | + |
| 83 | + var compositeId = new ProductCategoriesId(product.productId(), category.categoryId()); |
| 84 | + var selected = productCategoriesRepo.selectById(compositeId, c); |
| 85 | + |
| 86 | + assertTrue(selected.isPresent()); |
| 87 | + assertEquals(product.productId(), selected.get().productId()); |
| 88 | + assertEquals(category.categoryId(), selected.get().categoryId()); |
| 89 | + assertTrue(selected.get().isPrimary()); |
| 90 | + assertEquals((short) 1, (short) selected.get().sortOrder()); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testSelectByCompositeIds() { |
| 96 | + MariaDbTestHelper.run( |
| 97 | + c -> { |
| 98 | + var product = |
| 99 | + productsRepo.insert( |
| 100 | + new ProductsRowUnsaved("SKU003", "Product", new BigDecimal("75.00")), c); |
| 101 | + var category1 = |
| 102 | + categoriesRepo.insert(new CategoriesRowUnsaved("Category1", "cat1-slug"), c); |
| 103 | + var category2 = |
| 104 | + categoriesRepo.insert(new CategoriesRowUnsaved("Category2", "cat2-slug"), c); |
| 105 | + var category3 = |
| 106 | + categoriesRepo.insert(new CategoriesRowUnsaved("Category3", "cat3-slug"), c); |
| 107 | + |
| 108 | + productCategoriesRepo.insert( |
| 109 | + new ProductCategoriesRowUnsaved(product.productId(), category1.categoryId()), c); |
| 110 | + productCategoriesRepo.insert( |
| 111 | + new ProductCategoriesRowUnsaved(product.productId(), category2.categoryId()), c); |
| 112 | + productCategoriesRepo.insert( |
| 113 | + new ProductCategoriesRowUnsaved(product.productId(), category3.categoryId()), c); |
| 114 | + |
| 115 | + var ids = |
| 116 | + new ProductCategoriesId[] { |
| 117 | + new ProductCategoriesId(product.productId(), category1.categoryId()), |
| 118 | + new ProductCategoriesId(product.productId(), category3.categoryId()) |
| 119 | + }; |
| 120 | + |
| 121 | + var selected = productCategoriesRepo.selectByIds(ids, c); |
| 122 | + assertEquals(2, selected.size()); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testSelectByIdsTracked() { |
| 128 | + MariaDbTestHelper.run( |
| 129 | + c -> { |
| 130 | + var product = |
| 131 | + productsRepo.insert( |
| 132 | + new ProductsRowUnsaved("SKU004", "Product", new BigDecimal("100.00")), c); |
| 133 | + var category1 = categoriesRepo.insert(new CategoriesRowUnsaved("Cat1", "cat1"), c); |
| 134 | + var category2 = categoriesRepo.insert(new CategoriesRowUnsaved("Cat2", "cat2"), c); |
| 135 | + |
| 136 | + productCategoriesRepo.insert( |
| 137 | + new ProductCategoriesRowUnsaved( |
| 138 | + product.productId(), |
| 139 | + category1.categoryId(), |
| 140 | + new Provided<>(true), |
| 141 | + new Provided<>((short) 1)), |
| 142 | + c); |
| 143 | + productCategoriesRepo.insert( |
| 144 | + new ProductCategoriesRowUnsaved( |
| 145 | + product.productId(), |
| 146 | + category2.categoryId(), |
| 147 | + new Provided<>(false), |
| 148 | + new Provided<>((short) 2)), |
| 149 | + c); |
| 150 | + |
| 151 | + var id1 = new ProductCategoriesId(product.productId(), category1.categoryId()); |
| 152 | + var id2 = new ProductCategoriesId(product.productId(), category2.categoryId()); |
| 153 | + var ids = new ProductCategoriesId[] {id1, id2}; |
| 154 | + |
| 155 | + var tracked = productCategoriesRepo.selectByIdsTracked(ids, c); |
| 156 | + |
| 157 | + assertEquals(2, tracked.size()); |
| 158 | + assertTrue(tracked.get(id1).isPrimary()); |
| 159 | + assertFalse(tracked.get(id2).isPrimary()); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testUpdateWithCompositeId() { |
| 165 | + MariaDbTestHelper.run( |
| 166 | + c -> { |
| 167 | + var product = |
| 168 | + productsRepo.insert( |
| 169 | + new ProductsRowUnsaved("SKU005", "Product", new BigDecimal("25.00")), c); |
| 170 | + var category = categoriesRepo.insert(new CategoriesRowUnsaved("Category", "cat-slug"), c); |
| 171 | + |
| 172 | + var inserted = |
| 173 | + productCategoriesRepo.insert( |
| 174 | + new ProductCategoriesRowUnsaved( |
| 175 | + product.productId(), |
| 176 | + category.categoryId(), |
| 177 | + new Provided<>(false), |
| 178 | + new Provided<>((short) 10)), |
| 179 | + c); |
| 180 | + |
| 181 | + var updated = inserted.withIsPrimary(true).withSortOrder((short) 5); |
| 182 | + var success = productCategoriesRepo.update(updated, c); |
| 183 | + assertTrue(success); |
| 184 | + |
| 185 | + var selected = productCategoriesRepo.selectById(inserted.compositeId(), c); |
| 186 | + assertTrue(selected.isPresent()); |
| 187 | + assertTrue(selected.get().isPrimary()); |
| 188 | + assertEquals((short) 5, (short) selected.get().sortOrder()); |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void testDeleteByCompositeId() { |
| 194 | + MariaDbTestHelper.run( |
| 195 | + c -> { |
| 196 | + var product = |
| 197 | + productsRepo.insert( |
| 198 | + new ProductsRowUnsaved("SKU006", "Product", new BigDecimal("15.00")), c); |
| 199 | + var category = |
| 200 | + categoriesRepo.insert(new CategoriesRowUnsaved("ToDelete", "delete-cat"), c); |
| 201 | + |
| 202 | + var inserted = |
| 203 | + productCategoriesRepo.insert( |
| 204 | + new ProductCategoriesRowUnsaved(product.productId(), category.categoryId()), c); |
| 205 | + |
| 206 | + var deleted = productCategoriesRepo.deleteById(inserted.compositeId(), c); |
| 207 | + assertTrue(deleted); |
| 208 | + |
| 209 | + var selected = productCategoriesRepo.selectById(inserted.compositeId(), c); |
| 210 | + assertTrue(selected.isEmpty()); |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void testDeleteByCompositeIds() { |
| 216 | + MariaDbTestHelper.run( |
| 217 | + c -> { |
| 218 | + var product = |
| 219 | + productsRepo.insert( |
| 220 | + new ProductsRowUnsaved("SKU007", "Product", new BigDecimal("200.00")), c); |
| 221 | + var category1 = categoriesRepo.insert(new CategoriesRowUnsaved("Del1", "del1"), c); |
| 222 | + var category2 = categoriesRepo.insert(new CategoriesRowUnsaved("Del2", "del2"), c); |
| 223 | + var category3 = categoriesRepo.insert(new CategoriesRowUnsaved("Keep", "keep"), c); |
| 224 | + |
| 225 | + productCategoriesRepo.insert( |
| 226 | + new ProductCategoriesRowUnsaved(product.productId(), category1.categoryId()), c); |
| 227 | + productCategoriesRepo.insert( |
| 228 | + new ProductCategoriesRowUnsaved(product.productId(), category2.categoryId()), c); |
| 229 | + productCategoriesRepo.insert( |
| 230 | + new ProductCategoriesRowUnsaved(product.productId(), category3.categoryId()), c); |
| 231 | + |
| 232 | + var idsToDelete = |
| 233 | + new ProductCategoriesId[] { |
| 234 | + new ProductCategoriesId(product.productId(), category1.categoryId()), |
| 235 | + new ProductCategoriesId(product.productId(), category2.categoryId()) |
| 236 | + }; |
| 237 | + |
| 238 | + var count = productCategoriesRepo.deleteByIds(idsToDelete, c); |
| 239 | + assertEquals(2, (int) count); |
| 240 | + |
| 241 | + var remaining = productCategoriesRepo.selectAll(c); |
| 242 | + assertEquals(1, remaining.size()); |
| 243 | + assertEquals(category3.categoryId(), remaining.get(0).categoryId()); |
| 244 | + }); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void testUpsertWithCompositeId() { |
| 249 | + MariaDbTestHelper.run( |
| 250 | + c -> { |
| 251 | + var product = |
| 252 | + productsRepo.insert( |
| 253 | + new ProductsRowUnsaved("SKU008", "Product", new BigDecimal("300.00")), c); |
| 254 | + var category = categoriesRepo.insert(new CategoriesRowUnsaved("Upsert", "upsert"), c); |
| 255 | + |
| 256 | + var row = |
| 257 | + new ProductCategoriesRow( |
| 258 | + product.productId(), category.categoryId(), false, (short) 1); |
| 259 | + var inserted = productCategoriesRepo.upsert(row, c); |
| 260 | + assertFalse(inserted.isPrimary()); |
| 261 | + assertEquals((short) 1, (short) inserted.sortOrder()); |
| 262 | + |
| 263 | + var updatedRow = |
| 264 | + new ProductCategoriesRow( |
| 265 | + product.productId(), category.categoryId(), true, (short) 99); |
| 266 | + var updated = productCategoriesRepo.upsert(updatedRow, c); |
| 267 | + assertTrue(updated.isPrimary()); |
| 268 | + assertEquals((short) 99, (short) updated.sortOrder()); |
| 269 | + |
| 270 | + var all = productCategoriesRepo.selectAll(c); |
| 271 | + assertEquals(1, all.size()); |
| 272 | + }); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + public void testDSLWithCompositeId() { |
| 277 | + MariaDbTestHelper.run( |
| 278 | + c -> { |
| 279 | + var product1 = |
| 280 | + productsRepo.insert( |
| 281 | + new ProductsRowUnsaved("SKU-DSL1", "Product1", new BigDecimal("10.00")), c); |
| 282 | + var product2 = |
| 283 | + productsRepo.insert( |
| 284 | + new ProductsRowUnsaved("SKU-DSL2", "Product2", new BigDecimal("20.00")), c); |
| 285 | + var category = categoriesRepo.insert(new CategoriesRowUnsaved("DSL-Cat", "dsl-cat"), c); |
| 286 | + |
| 287 | + productCategoriesRepo.insert( |
| 288 | + new ProductCategoriesRowUnsaved( |
| 289 | + product1.productId(), |
| 290 | + category.categoryId(), |
| 291 | + new Provided<>(true), |
| 292 | + new Provided<>((short) 1)), |
| 293 | + c); |
| 294 | + productCategoriesRepo.insert( |
| 295 | + new ProductCategoriesRowUnsaved( |
| 296 | + product2.productId(), |
| 297 | + category.categoryId(), |
| 298 | + new Provided<>(false), |
| 299 | + new Provided<>((short) 2)), |
| 300 | + c); |
| 301 | + |
| 302 | + var primaries = |
| 303 | + productCategoriesRepo.select().where(f -> f.isPrimary().isEqual(true)).toList(c); |
| 304 | + assertEquals(1, primaries.size()); |
| 305 | + assertEquals(product1.productId(), primaries.get(0).productId()); |
| 306 | + |
| 307 | + productCategoriesRepo |
| 308 | + .update() |
| 309 | + .setValue(f -> f.sortOrder(), (short) 100) |
| 310 | + .where(f -> f.productId().isEqual(product2.productId())) |
| 311 | + .execute(c); |
| 312 | + |
| 313 | + var updated = |
| 314 | + productCategoriesRepo.selectById( |
| 315 | + new ProductCategoriesId(product2.productId(), category.categoryId()), c); |
| 316 | + assertEquals((short) 100, (short) updated.get().sortOrder()); |
| 317 | + |
| 318 | + productCategoriesRepo.delete().where(f -> f.isPrimary().isEqual(false)).execute(c); |
| 319 | + |
| 320 | + var remaining = productCategoriesRepo.selectAll(c); |
| 321 | + assertEquals(1, remaining.size()); |
| 322 | + assertTrue(remaining.get(0).isPrimary()); |
| 323 | + }); |
| 324 | + } |
| 325 | +} |
0 commit comments