Skip to content

Commit

Permalink
feat: add support for parsing, inspecting and autocompleting in a add…
Browse files Browse the repository at this point in the history
…Fields stage written using Spring Data MongoDB INTELLIJ-177 (#125)
  • Loading branch information
himanshusinghs authored Jan 22, 2025
1 parent 0c8164e commit 6cfdaa4
Show file tree
Hide file tree
Showing 10 changed files with 1,770 additions and 27 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-177](https://jira.mongodb.org/browse/INTELLIJ-177) Add support for parsing, inspecting and autocompleting in a addFields stage written using `Aggregation.addFields` and chained `AddFieldsOperation`s using `addFieldWithValue`, `addFieldWithValueOf`, `addField().withValue()` and `addField().withValueOf()`. Parsing boxed Java values is not supported yet.
* [INTELLIJ-174](https://jira.mongodb.org/browse/INTELLIJ-174) Add support for parsing, inspecting and autocompleting in a sort stage written using `Aggregation.sort` and chained `SortOperation`s using `and`. All the overloads of creating a `Sort` object are supported.
* [INTELLIJ-188](https://jira.mongodb.org/browse/INTELLIJ-188) Support for generating sort in the query generator.
* [INTELLIJ-186](https://jira.mongodb.org/browse/INTELLIJ-186) Support for parsing Sorts in the Java Driver.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,306 @@ class Repository {
},
)
}

@ParsingTest(
fileName = "Repository.java",
value = """
import org.springframework.data.domain.Sort;
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.addFields().addFieldWithValueOf("addedField", "<caret>")
),
Book.class,
Book.class
).getMappedResults();
}
}
""",
)
fun `should autocomplete fields from the current namespace in Aggregation#addFields#addFieldsWithValueOf 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.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.Fields;
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.addFields().addFieldWithValueOf("addedField", Fields.field("<caret>"))
),
Book.class,
Book.class
).getMappedResults();
}
}
""",
)
fun `should autocomplete fields from the current namespace in Fields#field call passed to Aggregation#addFields#addFieldsWithValueOf 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.domain.Sort;
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.addFields().addField("addedField").withValueOf("<caret>")
),
Book.class,
Book.class
).getMappedResults();
}
}
""",
)
fun `should autocomplete fields from the current namespace in Aggregation#addFields#withValueOf 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.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.Fields;
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.addFields().addField("addedField").withValueOf(Fields.field("<caret>"))
),
Book.class,
Book.class
).getMappedResults();
}
}
""",
)
fun `should autocomplete fields from the current namespace in Fields#field call passed to Aggregation#addFields#withValueOf 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 6cfdaa4

Please sign in to comment.