Skip to content

Commit cd9d002

Browse files
Merge pull request #11 from JavaWebStack/TimothyGillespie/testFromClauses
From Query Clauses Test
2 parents c4ab711 + c84f6ac commit cd9d002

File tree

12 files changed

+159
-4
lines changed

12 files changed

+159
-4
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
<version>8.0.22</version>
5454
<scope>test</scope>
5555
</dependency>
56+
<dependency>
57+
<groupId>org.atteo</groupId>
58+
<artifactId>evo-inflector</artifactId>
59+
<version>1.2.2</version>
60+
</dependency>
5661
<dependency>
5762
<groupId>org.reflections</groupId>
5863
<artifactId>reflections</artifactId>

src/main/java/org/javawebstack/orm/TableInfo.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.javawebstack.orm;
22

3+
import org.atteo.evo.inflector.English;
34
import org.javawebstack.orm.annotation.*;
45
import org.javawebstack.orm.exception.ORMConfigurationException;
56
import org.javawebstack.orm.util.Helper;
@@ -45,10 +46,7 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
4546
Table table = model.getDeclaredAnnotationsByType(Table.class)[0];
4647
tableName = table.value();
4748
} else {
48-
tableName = Helper.toSnakeCase(model.getSimpleName());
49-
tableName += tableName.endsWith("ss") ? "es" : "s";
50-
if (tableName.endsWith("ys"))
51-
tableName = tableName.substring(0, tableName.length() - 2) + "ies";
49+
tableName = Helper.toSnakeCase(English.plural(model.getSimpleName()));
5250
}
5351
if (model.isAnnotationPresent(MorphType.class)) {
5452
morphType = model.getDeclaredAnnotationsByType(MorphType.class)[0].value();
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.javawebstack.orm.test.querybuilding;
2+
3+
import org.atteo.evo.inflector.English;
4+
import org.javawebstack.orm.Model;
5+
import org.javawebstack.orm.ORM;
6+
import org.javawebstack.orm.Repo;
7+
import org.javawebstack.orm.exception.ORMConfigurationException;
8+
import org.javawebstack.orm.test.ORMTestCase;
9+
import org.javawebstack.orm.test.shared.models.tablenames.*;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
/**
16+
* The focus does not lie on a correct plural with this test, therefore we are only testing it sample-wise.
17+
* For tests see the library that is used in TableInfo: https://github.com/atteo/evo-inflector
18+
*/
19+
class FromClauseTest extends ORMTestCase {
20+
21+
@Test
22+
void testOneWordSAppendixPlural() throws ORMConfigurationException {
23+
String query = getBaseQuery(Word.class);
24+
assertTrue(query.contains("FROM `words`"));
25+
}
26+
27+
@Test
28+
void testTwoWordsBecomeSnakeCases() throws ORMConfigurationException {
29+
String query = getBaseQuery(TwoWord.class);
30+
assertTrue(query.contains("FROM `two_words`"));
31+
}
32+
33+
@Test
34+
void testThreeWordBecomeSnakeCases() throws ORMConfigurationException {
35+
String query = getBaseQuery(ThreeWordClass.class);
36+
assertTrue(query.contains("FROM `three_word_classes`"));
37+
}
38+
39+
@Test
40+
void testIrregularWordPlural() throws ORMConfigurationException {
41+
String query = getBaseQuery(Mouse.class);
42+
assertTrue(query.contains("FROM `mice`"));
43+
}
44+
45+
/*
46+
* Error / Not Closer Specified Cases
47+
*/
48+
@Test
49+
void testOneWordAlreadyInPluralDoesntWork() throws ORMConfigurationException {
50+
String query = getBaseQuery(Words.class);
51+
// Should try to find a non-sense plural and not map to itself as plural; for the purpose of
52+
// not breaking existing code
53+
assertFalse(query.contains("FROM `words`"));
54+
}
55+
56+
@Test
57+
void testOverwrittenTableName() throws ORMConfigurationException {
58+
String query = getBaseQuery(OverwritteTableName.class);
59+
assertTrue(query.contains("FROM `oVer_writtenValue`"));
60+
}
61+
62+
/*
63+
* Boiler Code Reduction Funtions
64+
*/
65+
66+
/**
67+
* Gets the generated base query for a model when only calling .query() on it.
68+
* @return
69+
*/
70+
private String getBaseQuery(Class<? extends Model> clazz) throws ORMConfigurationException {
71+
ORM.register(clazz, sql());
72+
return Repo.get(clazz).query().getQueryString().getQuery();
73+
}
74+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# General Remark About The Methodology used
2+
In the encapsulation via ´´ will be assumed. If this quotation is removed the SQL query can behave differently, which is why it must be a stable aspect.
3+
Also they are required to use reserved keyword.

src/test/java/org/javawebstack/orm/test/shared/models/OnlyIdModel.java

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javawebstack.orm.test.shared.models.tablenames;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.annotation.Column;
5+
6+
/**
7+
* This class' name has an irregular plural (mice) and is here for this purpose.
8+
*/
9+
public class Mouse extends Model {
10+
@Column
11+
int id;
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javawebstack.orm.test.shared.models.tablenames;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.annotation.Column;
5+
import org.javawebstack.orm.annotation.Table;
6+
7+
/**
8+
* This class overwrites the model name to a seemingly random word to test multiple cases at once.
9+
*/
10+
@Table("oVer_writtenValue")
11+
public class OverwritteTableName extends Model {
12+
@Column
13+
int id;
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Purposes of classes in this package
2+
3+
These classes are designed to cover different that can occur with the table names. For example - but not limited to:
4+
5+
- class with a single word name
6+
- class with irregular plural
7+
- class with an already plural name
8+
9+
10+
Since the focus is on their name they will usually only contain the - to date - required id field.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.javawebstack.orm.test.shared.models.tablenames;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.annotation.Column;
5+
6+
public class ThreeWordClass extends Model {
7+
@Column
8+
int id;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.javawebstack.orm.test.shared.models.tablenames;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.annotation.Column;
5+
6+
public class TwoWord extends Model {
7+
@Column
8+
int id;
9+
}

0 commit comments

Comments
 (0)