Skip to content

Commit

Permalink
feat: add support for parsing, inspecting and autocompleting in an ag…
Browse files Browse the repository at this point in the history
…gregation and a match stage written using Spring Data MongoDB INTELLIJ-172 (#116)
  • Loading branch information
himanshusinghs authored Jan 14, 2025
1 parent 09a1ec1 commit 5afd7c9
Show file tree
Hide file tree
Showing 13 changed files with 2,107 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ MongoDB plugin for IntelliJ IDEA.
## [Unreleased]

### Added
* [INTELLIJ-172](https://jira.mongodb.org/browse/INTELLIJ-172) Add support for parsing, inspecting and autocompleting in an aggregation written using Spring Data MongoDB (`MongoTemplate.aggregate`, `MongoTemplate.aggregateStream`) and a match stage written using `Aggregation.match`.
* [INTELLIJ-179](https://jira.mongodb.org/browse/INTELLIJ-179) Telemetry when Create Index intention is clicked.
It can be disabled in the Plugin settings.
* [INTELLIJ-178](https://jira.mongodb.org/browse/INTELLIJ-178) Telemetry when the Run Query button is clicked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ record Book() {}
)
}

@Suppress("TOO_LONG_FUNCTION")
@ParsingTest(
fileName = "Repository.java",
value = """
Expand Down Expand Up @@ -124,4 +123,273 @@ class Repository {
},
)
}

@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 org.springframework.data.mongodb.core.query.Criteria;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public List<Book> allReleasedBooks() {
return template.aggregate(Aggregation.newAggregation(), "<caret>"
}
}
""",
)
fun `should autocomplete collections from the current connection in an aggregate call`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDatabase("myDatabase")
fixture.specifyDialect(SpringCriteriaDialect)

`when`(
readModelProvider.slice(eq(dataSource), eq(ListCollections.Slice("myDatabase")))
).thenReturn(
ListCollections(
listOf(
ListCollections.Collection("myCollection", "collection"),
ListCollections.Collection("anotherCollection", "collection"),
),
),
)

val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myCollection"
},
)

assertTrue(
elements.containsElements {
it.lookupString == "anotherCollection"
},
)
}

@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 org.springframework.data.mongodb.core.query.Criteria;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public List<Book> allReleasedBooks() {
return template.aggregateStream(Aggregation.newAggregation(), "<caret>"
}
}
""",
)
fun `should autocomplete collections from the current connection in an aggregateStream call`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDatabase("myDatabase")
fixture.specifyDialect(SpringCriteriaDialect)

`when`(
readModelProvider.slice(eq(dataSource), eq(ListCollections.Slice("myDatabase")))
).thenReturn(
ListCollections(
listOf(
ListCollections.Collection("myCollection", "collection"),
ListCollections.Collection("anotherCollection", "collection"),
),
),
)

val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myCollection"
},
)

assertTrue(
elements.containsElements {
it.lookupString == "anotherCollection"
},
)
}

@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 org.springframework.data.mongodb.core.query.Criteria;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public List<Book> allReleasedBooks() {
return template.aggregate(
Aggregation.newAggregation(
Aggregation.match(where("<caret>"))
),
Book.class,
Book.class
).getMappedResults();
}
}
""",
)
fun `should autocomplete fields from the current namespace in a Criteria inside an aggregate call`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDatabase("myDatabase")
fixture.specifyDialect(SpringCriteriaDialect)

val namespace = Namespace("myDatabase", "book")

`when`(
readModelProvider.slice(eq(dataSource), eq(GetCollectionSchema.Slice(namespace)))
).thenReturn(
GetCollectionSchema(
CollectionSchema(
namespace,
BsonObject(
mapOf(
"myField" to BsonString,
"myField2" to BsonString,
),
),
),
),
)

val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myField"
},
)

assertTrue(
elements.containsElements {
it.lookupString == "myField2"
},
)
}

@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 org.springframework.data.mongodb.core.query.Criteria;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@Document
record Book() {}
class Repository {
private final MongoTemplate template;
public Repository(MongoTemplate template) {
this.template = template;
}
public List<Book> allReleasedBooks() {
return template.aggregateStream(
Aggregation.newAggregation(
Aggregation.match(where("<caret>"))
),
Book.class,
Book.class
).toList();
}
}
""",
)
fun `should autocomplete fields from the current namespace in a Criteria inside an aggregateStream call`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDatabase("myDatabase")
fixture.specifyDialect(SpringCriteriaDialect)

val namespace = Namespace("myDatabase", "book")

`when`(
readModelProvider.slice(eq(dataSource), eq(GetCollectionSchema.Slice(namespace)))
).thenReturn(
GetCollectionSchema(
CollectionSchema(
namespace,
BsonObject(
mapOf(
"myField" to BsonString,
"myField2" to BsonString,
),
),
),
),
)

val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myField"
},
)

assertTrue(
elements.containsElements {
it.lookupString == "myField2"
},
)
}
}
Loading

0 comments on commit 5afd7c9

Please sign in to comment.