Skip to content

Commit

Permalink
chore: add support for parsing aggregateStream
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshusinghs committed Jan 14, 2025
1 parent a76c4c8 commit 12e1672
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object SpringCriteriaDialectParser : DialectParser<PsiElement> {
)
)
)
"aggregate" -> {
"aggregate", "aggregateStream" -> {
val expressions = mongoOpCall.argumentList.expressions
val newAggregationCall = expressions.getOrNull(0)?.resolveToMethodCallExpression {
_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Repository {
}
"""
)
fun `should be able to parse an empty aggregation with a class type provided for target collection`(
fun `should be able to parse an empty aggregation using aggregate call with a class type provided for target collection`(
psiFile: PsiFile
) {
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks")
Expand Down Expand Up @@ -94,7 +94,103 @@ class Repository {
}
"""
)
fun `should be able to parse an empty aggregation with a string type provided for target collection`(
fun `should be able to parse an empty aggregation using aggregate call with a string type provided for target collection`(
psiFile: PsiFile
) {
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks")
SpringCriteriaDialectParser.parse(query).assert(IsCommand.CommandType.AGGREGATE) {
component<HasSourceDialect> {
assertEquals(HasSourceDialect.DialectName.SPRING_CRITERIA, name)
}

collection<HasCollectionReference.OnlyCollection<PsiElement>> {
assertEquals("booksAsString", collection)
}

component<HasAggregation<PsiElement>> {
assertEquals(0, children.size)
}
}
}

@ParsingTest(
fileName = "Book.java",
"""
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public AggregationResults<Book> allReleasedBooks() {
return template.aggregateStream(
Aggregation.newAggregation(),
Book.class,
Book.class
);
}
}
"""
)
fun `should be able to parse an empty aggregation using aggregateStream call with a class type provided for target collection`(
psiFile: PsiFile
) {
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks")
SpringCriteriaDialectParser.parse(query).assert(IsCommand.CommandType.AGGREGATE) {
component<HasSourceDialect> {
assertEquals(HasSourceDialect.DialectName.SPRING_CRITERIA, name)
}

collection<HasCollectionReference.OnlyCollection<PsiElement>> {
assertEquals("book", collection)
}

component<HasAggregation<PsiElement>> {
assertEquals(0, children.size)
}
}
}

@ParsingTest(
fileName = "Book.java",
"""
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public AggregationResults<Book> allReleasedBooks() {
return template.aggregateStream(
Aggregation.newAggregation(),
"booksAsString",
Book.class
);
}
}
"""
)
fun `should be able to parse an empty aggregation using aggregateStream call with a string type provided for target collection`(
psiFile: PsiFile
) {
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks")
Expand Down

0 comments on commit 12e1672

Please sign in to comment.