|
| 1 | +package org.javawebstack.orm.test.queryexecution; |
| 2 | + |
| 3 | +import org.javawebstack.orm.ORM; |
| 4 | +import org.javawebstack.orm.Repo; |
| 5 | +import org.javawebstack.orm.test.ORMTestCase; |
| 6 | +import org.javawebstack.orm.test.shared.models.OnlyIdModel; |
| 7 | +import org.javawebstack.orm.test.shared.setup.ModelSetup; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +class OrderByTest extends ORMTestCase { |
| 15 | + |
| 16 | + @Test |
| 17 | + void testOrderByCanPullResults() { |
| 18 | + ModelSetup.setUpModel(OnlyIdModel.class); |
| 19 | + ORM.autoMigrate(true); |
| 20 | + |
| 21 | + new OnlyIdModel().save(); |
| 22 | + new OnlyIdModel().save(); |
| 23 | + new OnlyIdModel().save(); |
| 24 | + |
| 25 | + assertDoesNotThrow(() -> Repo.get(OnlyIdModel.class) |
| 26 | + .query() |
| 27 | + .order("id") |
| 28 | + .get() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testOrderByWorksWithAsc() { |
| 34 | + ModelSetup.setUpModel(OnlyIdModel.class); |
| 35 | + ORM.autoMigrate(true); |
| 36 | + |
| 37 | + new OnlyIdModel().save(); |
| 38 | + new OnlyIdModel().save(); |
| 39 | + new OnlyIdModel().save(); |
| 40 | + |
| 41 | + List<OnlyIdModel> orderedList = Repo.get(OnlyIdModel.class) |
| 42 | + .query() |
| 43 | + .order("id", false) |
| 44 | + .get(); |
| 45 | + |
| 46 | + for(int i = 1; i <= 3; i++) { |
| 47 | + assertEquals(i, orderedList.get(i - 1).getId()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testOrderByWorksWithDesc() { |
| 53 | + ModelSetup.setUpModel(OnlyIdModel.class); |
| 54 | + ORM.autoMigrate(true); |
| 55 | + |
| 56 | + new OnlyIdModel().save(); |
| 57 | + new OnlyIdModel().save(); |
| 58 | + new OnlyIdModel().save(); |
| 59 | + |
| 60 | + List<OnlyIdModel> orderedList = Repo.get(OnlyIdModel.class) |
| 61 | + .query() |
| 62 | + .order("id", true) |
| 63 | + .get(); |
| 64 | + |
| 65 | + for(int i = 3; i >= 1; i--) { |
| 66 | + assertEquals(i, orderedList.get(3 - i).getId()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments