|
1 | 1 | package previewcode.backend.database; |
2 | 2 |
|
| 3 | +import io.vavr.Tuple2; |
| 4 | +import org.jooq.DSLContext; |
| 5 | +import org.jooq.exception.DataAccessException; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
3 | 7 | import org.junit.jupiter.api.Test; |
4 | 8 |
|
5 | | -import static org.junit.Assert.fail; |
| 9 | +import static org.assertj.core.api.Assertions.*; |
| 10 | +import static previewcode.backend.database.model.Tables.GROUPS; |
| 11 | +import static previewcode.backend.database.model.Tables.HUNK; |
| 12 | +import static previewcode.backend.database.model.Tables.PULL_REQUEST; |
| 13 | +import static previewcode.backend.services.actions.DatabaseActions.*; |
6 | 14 |
|
7 | 15 | public class DatabaseInterpreter_HunksTest extends DatabaseInterpreterTest { |
8 | 16 |
|
| 17 | + private static final String hunkID = "ABCDEF"; |
| 18 | + |
| 19 | + private static final GroupID group_A_id = new GroupID(0L); |
| 20 | + private static final GroupID group_B_id = new GroupID(1L); |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + @Override |
| 24 | + public void setup(DSLContext db) { |
| 25 | + super.setup(db); |
| 26 | + db.insertInto(PULL_REQUEST, PULL_REQUEST.ID, PULL_REQUEST.OWNER, PULL_REQUEST.NAME, PULL_REQUEST.NUMBER) |
| 27 | + .values(dbPullId.id, owner, name, number) |
| 28 | + .execute(); |
| 29 | + |
| 30 | + db.insertInto(GROUPS) |
| 31 | + .columns(GROUPS.ID, GROUPS.PULL_REQUEST_ID, GROUPS.TITLE, GROUPS.DESCRIPTION) |
| 32 | + .values(group_A_id.id, dbPullId.id, "A", "B") |
| 33 | + .values(group_B_id.id, dbPullId.id, "C", "D") |
| 34 | + .execute(); |
| 35 | + } |
| 36 | + |
9 | 37 | @Test |
10 | | - public void assignHunk_groupMustExist() { |
11 | | - fail(); |
| 38 | + public void assignHunk_groupMustExist() throws Exception { |
| 39 | + GroupID invalidID = new GroupID(-1L); |
| 40 | + assertThatExceptionOfType(DataAccessException.class) |
| 41 | + .isThrownBy(() -> eval(assignToGroup(invalidID, hunkID))); |
12 | 42 | } |
13 | 43 |
|
14 | 44 | @Test |
15 | 45 | public void assignHunk_cannotAssignTwice_toSameGroup() { |
16 | | - fail(); |
| 46 | + AssignHunkToGroup assign = assignToGroup(group_A_id, hunkID); |
| 47 | + |
| 48 | + assertThatExceptionOfType(DataAccessException.class) |
| 49 | + .isThrownBy(() -> eval(assign.then(assign))); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void assignHunk_insertsIntoHunkTable(DSLContext db) throws Exception { |
| 54 | + eval(assignToGroup(group_A_id, hunkID)); |
| 55 | + |
| 56 | + assertThat( |
| 57 | + db.selectCount().from(HUNK).fetchOneInto(Integer.class) |
| 58 | + ).isOne(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void assignHunk_canInsertDuplicates(DSLContext db) throws Exception { |
| 63 | + eval(assignToGroup(group_A_id, hunkID).then(assignToGroup(group_B_id, hunkID))); |
| 64 | + |
| 65 | + assertThat( |
| 66 | + db.selectCount().from(HUNK).fetchOneInto(Integer.class) |
| 67 | + ).isEqualTo(2); |
17 | 68 | } |
18 | 69 |
|
19 | 70 | @Test |
20 | | - public void assignHunk_cannotAssignTwice_toDifferentGroups() { |
21 | | - fail(); |
| 71 | + public void assignHunk_insertsCorrectData(DSLContext db) throws Exception { |
| 72 | + eval(assignToGroup(group_A_id, hunkID)); |
| 73 | + |
| 74 | + Tuple2 record = db.select(HUNK.GROUP_ID, HUNK.ID).from(HUNK).fetchOneInto(Tuple2.class); |
| 75 | + assertThat(record).isEqualTo(new Tuple2(group_A_id.id, hunkID)); |
22 | 76 | } |
| 77 | + |
23 | 78 | } |
0 commit comments