Skip to content

Commit

Permalink
Multifile completions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SevInf committed May 10, 2024
1 parent f684c71 commit 2572777
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model Test {
id Int @id
name String @default(<|>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model TestB {
id String @id
name Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"isIncomplete": false,
"items": [
{
"label": "map: ",
"kind": 10
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
model A {
id Int @id
val String
@@index([val], type: <|>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generator js {
provider = "prisma-client-js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"isIncomplete": false,
"items": [
{
"label": "BTree",
"kind": 13,
"detail": "Can handle equality and range queries on data that can be sorted into some ordering (default)."
},
{
"label": "Hash",
"kind": 13,
"detail": "Can handle simple equality queries, but no ordering. Faster than BTree, if ordering is not needed."
},
{
"label": "Gist",
"kind": 13,
"detail": "Generalized Search Tree. A framework for building specialized indices for custom data types."
},
{
"label": "Gin",
"kind": 13,
"detail": "Generalized Inverted Index. Useful for indexing composite items, such as arrays or text."
},
{
"label": "SpGist",
"kind": 13,
"detail": "Space-partitioned Generalized Search Tree. For implenting a wide range of different non-balanced data structures."
},
{
"label": "Brin",
"kind": 13,
"detail": "Block Range Index. If the data has some natural correlation with their physical location within the table, can compress very large amount of data into a small space."
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model Test {
id Int @id
name String @default(<|>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model TestB {
id String @id
name Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"isIncomplete": false,
"items": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id], onDelete: <|>)
authorId Int
}

model User {
id Int @id @default(autoincrement())
posts Post[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"isIncomplete": false,
"items": [
{
"label": "Cascade",
"kind": 13,
"detail": "Delete the child records when the parent record is deleted."
},
{
"label": "Restrict",
"kind": 13,
"detail": "Prevent deleting a parent record as long as it is referenced."
},
{
"label": "NoAction",
"kind": 13,
"detail": "Prevent deleting a parent record as long as it is referenced."
},
{
"label": "SetNull",
"kind": 13,
"detail": "Set the referencing fields to NULL when the referenced record is deleted."
}
]
}
62 changes: 51 additions & 11 deletions prisma-fmt/tests/text_document_completion/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,38 @@ static UPDATE_EXPECT: Lazy<bool> = Lazy::new(|| std::env::var("UPDATE_EXPECT").i
pub(crate) fn test_scenario(scenario_name: &str) {
let mut path = String::with_capacity(SCENARIOS_PATH.len() + 12);

let schema = {
write!(path, "{SCENARIOS_PATH}/{scenario_name}/schema.prisma").unwrap();
std::fs::read_to_string(&path).unwrap()
let schema_files = {
write!(path, "{SCENARIOS_PATH}/{scenario_name}").unwrap();
std::fs::read_dir(&path)
.unwrap()
.into_iter()
.map(Result::unwrap)
.filter_map(|entry| {
let ft = entry.file_type().ok()?;
if ft.is_dir() {
return None;
}
let path = entry.path();
let ext = path.extension()?;
if ext == "prisma" {
Some((
path.to_str().unwrap().to_owned(),
std::fs::read_to_string(&path).unwrap(),
))
} else {
None
}
})
.collect::<Vec<_>>()
};

assert!(schema_files.len() > 0);

path.clear();
write!(path, "{SCENARIOS_PATH}/{scenario_name}/result.json").unwrap();
let expected_result = std::fs::read_to_string(&path).unwrap_or_else(|_| String::new());

let (cursor_position, schema) = take_cursor(&schema);
let (initiating_file, cursor_position, schema_files) = take_cursor(schema_files);
let params = lsp_types::CompletionParams {
text_document_position: lsp_types::TextDocumentPositionParams {
text_document: lsp_types::TextDocumentIdentifier {
Expand All @@ -32,10 +54,9 @@ pub(crate) fn test_scenario(scenario_name: &str) {
context: None,
};

let schema_files = serde_json::to_string_pretty(&[("schema.prisma", schema)]).unwrap();
let result = prisma_fmt::text_document_completion(
schema_files,
"schema.prisma".into(),
serde_json::to_string_pretty(&schema_files).unwrap(),
initiating_file,
&serde_json::to_string_pretty(&params).unwrap(),
);
// Prettify the JSON
Expand Down Expand Up @@ -78,7 +99,24 @@ fn format_chunks(chunks: Vec<dissimilar::Chunk>) -> String {
buf
}

fn take_cursor(schema: &str) -> (lsp_types::Position, String) {
fn take_cursor(schema_files: Vec<(String, String)>) -> (String, lsp_types::Position, Vec<(String, String)>) {
let mut result = Vec::with_capacity(schema_files.len());
let mut file_and_pos = None;
for (file_name, content) in schema_files {
if let Some((pos, without_cursor)) = take_cursor_one(&content) {
file_and_pos = Some((file_name.clone(), pos));
result.push((file_name, without_cursor));
} else {
result.push((file_name, content));
}
}

let (file_name, position) = file_and_pos.expect("Could not find a cursor in any of the schema files");

(file_name, position, result)
}

fn take_cursor_one(schema: &str) -> Option<(lsp_types::Position, String)> {
let mut schema_without_cursor = String::with_capacity(schema.len() - 3);
let mut cursor_position = lsp_types::Position { character: 0, line: 0 };
let mut cursor_found = false;
Expand All @@ -101,11 +139,13 @@ fn take_cursor(schema: &str) -> (lsp_types::Position, String) {
}
}

assert!(cursor_found);
if !cursor_found {
return None;
}
// remove extra newline
schema_without_cursor.truncate(schema_without_cursor.len() - 1);

(cursor_position, schema_without_cursor)
Some((cursor_position, schema_without_cursor))
}

#[test]
Expand All @@ -121,7 +161,7 @@ fn take_cursor_works() {
}
"#;

let (pos, schema) = take_cursor(schema);
let (pos, schema) = take_cursor_one(schema).unwrap();
assert_eq!(pos.line, 2);
assert_eq!(pos.character, 28);
assert_eq!(schema, expected_schema);
Expand Down
4 changes: 4 additions & 0 deletions prisma-fmt/tests/text_document_completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ scenarios! {
argument_after_trailing_comma
default_map_end_of_args_list
default_map_mssql
default_map_mssql_multifile
empty_schema
extended_indexes_basic
extended_indexes_types_postgres
extended_indexes_types_postgres_multifile
extended_indexes_types_mysql
extended_indexes_types_sqlserver
extended_indexes_types_sqlite
Expand All @@ -30,6 +32,8 @@ scenarios! {
extended_indexes_operators_cockroach_gin
language_tools_relation_directive
no_default_map_on_postgres
no_default_map_on_postgres_multifile
referential_actions_multifile
referential_actions_end_of_args_list
referential_actions_in_progress
referential_actions_in_progress_2
Expand Down

0 comments on commit 2572777

Please sign in to comment.