Skip to content

Commit

Permalink
Add test only for orphan removal to TestOrphanRemovalOverwrite
Browse files Browse the repository at this point in the history
Using clear() and add() when the same id value is used
  • Loading branch information
rbygrave committed Feb 7, 2024
1 parent 66940c0 commit f63e7ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public OmBeanListChild(String name) {
public Long getId() {
return id;
}

public OmBeanListChild setId(Long id) {
this.id = id;
return this;
}
}


Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.tests.model.orphanremoval;

import io.ebean.DB;
import io.ebean.test.LoggedSql;
import org.junit.jupiter.api.Test;

import java.util.List;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class TestOrphanRemovalOverwrite {
class TestOrphanRemovalOverwrite {

@Test
public void testOverwritingMapping() {
void testOverwritingMapping() {
OmBeanListParent parent = new OmBeanListParent();
parent.save();

Expand All @@ -37,4 +40,37 @@ public void testOverwritingMapping() {
assertEquals(child, refreshedChild);
assertEquals(childList, parent.getChildren());
}

@Test
void clearAddAll() {
OmBeanListChild c1 = new OmBeanListChild("c1");
OmBeanListChild c2 = new OmBeanListChild("c2");

OmBeanListParent parent = new OmBeanListParent();
parent.getChildren().add(c1);
parent.getChildren().add(c2);
parent.save();


OmBeanListChild c3 = new OmBeanListChild("c3");
c3.setId(c1.getId());

OmBeanListParent p1 = DB.find(OmBeanListParent.class, parent.getId());
List<OmBeanListChild> children = p1.getChildren();
children.clear();
children.add(c3);

LoggedSql.start();
DB.save(p1);

List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(7);
assertThat(sql.get(0)).contains("delete from om_bean_list_child where id=?");
assertThat(sql.get(1)).contains(" -- bind");
assertThat(sql.get(2)).contains(" -- bind");
assertThat(sql.get(3)).contains(" -- executeBatch()");
assertThat(sql.get(4)).contains(" insert into om_bean_list_child");
assertThat(sql.get(5)).contains(" -- bind");
assertThat(sql.get(6)).contains(" -- executeBatch()");
}
}

0 comments on commit f63e7ed

Please sign in to comment.