Skip to content

Commit

Permalink
chore: added inspection tests for Spring aggregate
Browse files Browse the repository at this point in the history
- covers field check inspections and namespace check inspection
  • Loading branch information
himanshusinghs committed Jan 14, 2025
1 parent 3c61499 commit 04efff6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SpringCriteriaFieldCheckLinterInspectionTest {
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
Expand All @@ -50,6 +51,18 @@ class BookRepository {
Book.class
)</warning>;
}
public void allReleasedBooksAggregate() {
<warning descr="No connection available to run this check.">template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is(true)
)
),
Book.class,
Book.class
)</warning>;
}
}
""",
)
Expand All @@ -65,6 +78,7 @@ class BookRepository {
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
Expand All @@ -86,6 +100,18 @@ class BookRepository {
Book.class
)</warning>;
}
public void allReleasedBooksAggregate() {
<warning descr="No database selected to run this check.">template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is(true)
)
),
Book.class,
Book.class
)</warning>;
}
}
""",
)
Expand Down Expand Up @@ -141,6 +167,7 @@ class BookRepository {
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
Expand All @@ -162,6 +189,18 @@ class BookRepository {
Book.class
);
}
public void allReleasedBooksAggregate() {
template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where(<warning descr="Field \"released\" does not exist in collection \"bad_db.book\"">"released"</warning>).is(true)
)
),
Book.class,
Book.class
);
}
}
""",
)
Expand Down Expand Up @@ -222,6 +261,7 @@ class BookRepository {
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
Expand All @@ -243,6 +283,18 @@ class BookRepository {
Book.class
);
}
public void allReleasedBooksAggregate() {
template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is(<warning descr="A \"String\"(type of provided value) cannot be assigned to \"boolean\"(type of \"released\")">"true"</warning>)
)
),
Book.class,
Book.class
);
}
}
""",
)
Expand Down Expand Up @@ -306,6 +358,7 @@ class BookRepository {
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
Expand All @@ -327,6 +380,18 @@ class BookRepository {
Book.class
);
}
public void allReleasedBooksAggregate() {
template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is("true")
)
),
Book.class,
Book.class
);
}
}
""",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import org.mockito.Mockito.`when`
import org.mockito.kotlin.any
import org.mockito.kotlin.eq

// Suppressing
// - LONG_LINE because the complaint is about the templated error description which needs to be in the same line for the
// match to happen correctly
// - TOO_LONG_FUNCTION because it is better to keep test logic within the tests and not make them "too smart" otherwise
// reading through them becomes a task in its own
@Suppress("LONG_LINE", "TOO_LONG_FUNCTION")
@CodeInsightTest
class SpringCriteriaNamespaceCheckLinterInspectionTest {
@ParsingTest(
Expand Down Expand Up @@ -65,4 +59,69 @@ class BookRepository {
fixture.enableInspections(NamespaceCheckInspectionBridge::class.java)
fixture.testHighlighting()
}

@ParsingTest(
fileName = "Repository.java",
value = """
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@Document
record Book() {}
class BookRepository {
private final MongoTemplate template;
public BookRepository(MongoTemplate template) {
this.template = template;
}
public void allReleasedBooks() {
template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is(true)
)
),
<warning descr="Cannot resolve \"book\" collection in \"myDb\" database in the connected data source.">Book.class</warning>,
Book.class
);
}
public void allReleasedBooksVariant2() {
template.aggregate(
Aggregation.newAggregation(
Aggregation.match(
where("released").is(true)
)
),
<warning descr="Cannot resolve \"book\" collection in \"myDb\" database in the connected data source.">"book"</warning>,
Book.class
);
}
}
""",
)
fun `shows an inspection when the collection does not exist in the current data source for an aggregation`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDatabase("myDb")
fixture.specifyDialect(SpringCriteriaDialect)

`when`(readModelProvider.slice(eq(dataSource), eq(ListDatabases.Slice))).thenReturn(
ListDatabases(listOf(ListDatabases.Database("myDb")))
)

`when`(readModelProvider.slice(eq(dataSource), any<ListCollections.Slice>())).thenReturn(
ListCollections(emptyList())
)

fixture.enableInspections(NamespaceCheckInspectionBridge::class.java)
fixture.testHighlighting()
}
}

0 comments on commit 04efff6

Please sign in to comment.