|
| 1 | +//! Minimal Schema Integration Tests |
| 2 | +//! |
| 3 | +//! Tests the complete scanner-parser pipeline with minimal but valid |
| 4 | +//! Prisma schemas. Focuses on basic functionality and successful parsing. |
| 5 | +
|
| 6 | +use crate::scanner_parser::{assert_ast_structure, parse_schema_end_to_end}; |
| 7 | + |
| 8 | +use prisma_rs::core::parser::ast::{Declaration, EnumMember, ModelMember}; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn empty_schema() { |
| 12 | + let input = ""; |
| 13 | + let result = parse_schema_end_to_end(input); |
| 14 | + |
| 15 | + assert!( |
| 16 | + result.schema.is_some(), |
| 17 | + "Empty schema should parse successfully" |
| 18 | + ); |
| 19 | + let schema = result.schema.unwrap(); |
| 20 | + assert_eq!( |
| 21 | + schema.declarations.len(), |
| 22 | + 0, |
| 23 | + "Empty schema should have no declarations" |
| 24 | + ); |
| 25 | + assert!( |
| 26 | + result.diagnostics.is_empty(), |
| 27 | + "Empty schema should produce no diagnostics" |
| 28 | + ); |
| 29 | + assert!(result.token_count >= 1, "Should contain at least EOF token"); |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn whitespace_only_schema() { |
| 34 | + let input = " \n\t \n "; |
| 35 | + let result = parse_schema_end_to_end(input); |
| 36 | + |
| 37 | + assert!( |
| 38 | + result.schema.is_some(), |
| 39 | + "Whitespace-only schema should parse successfully" |
| 40 | + ); |
| 41 | + let schema = result.schema.unwrap(); |
| 42 | + assert_eq!( |
| 43 | + schema.declarations.len(), |
| 44 | + 0, |
| 45 | + "Whitespace schema should have no declarations" |
| 46 | + ); |
| 47 | + assert!( |
| 48 | + result.diagnostics.is_empty(), |
| 49 | + "Whitespace schema should produce no diagnostics" |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn single_empty_model() { |
| 55 | + let input = r" |
| 56 | +model User { |
| 57 | +} |
| 58 | +"; |
| 59 | + let result = parse_schema_end_to_end(input); |
| 60 | + |
| 61 | + assert!( |
| 62 | + result.schema.is_some(), |
| 63 | + "Single empty model should parse successfully" |
| 64 | + ); |
| 65 | + let schema = result.schema.unwrap(); |
| 66 | + assert_ast_structure(&schema, 1, 0, 0); |
| 67 | + assert!( |
| 68 | + result.diagnostics.is_empty(), |
| 69 | + "Empty model should produce no diagnostics" |
| 70 | + ); |
| 71 | + |
| 72 | + // Verify model structure |
| 73 | + if let Some(Declaration::Model(model)) = schema.declarations.first() { |
| 74 | + assert_eq!(model.name.text, "User"); |
| 75 | + assert!( |
| 76 | + model.members.is_empty(), |
| 77 | + "Empty model should have no members" |
| 78 | + ); |
| 79 | + } else { |
| 80 | + panic!("Expected first declaration to be a model"); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn single_model_with_field() { |
| 86 | + let input = r" |
| 87 | +model User { |
| 88 | + id String |
| 89 | +} |
| 90 | +"; |
| 91 | + let result = parse_schema_end_to_end(input); |
| 92 | + |
| 93 | + assert!( |
| 94 | + result.schema.is_some(), |
| 95 | + "Model with single field should parse successfully" |
| 96 | + ); |
| 97 | + let schema = result.schema.unwrap(); |
| 98 | + assert_ast_structure(&schema, 1, 0, 0); |
| 99 | + assert!( |
| 100 | + result.diagnostics.is_empty(), |
| 101 | + "Simple model should produce no diagnostics" |
| 102 | + ); |
| 103 | + |
| 104 | + // Verify model and field structure |
| 105 | + if let Some(Declaration::Model(model)) = schema.declarations.first() { |
| 106 | + assert_eq!(model.name.text, "User"); |
| 107 | + assert_eq!(model.members.len(), 1, "Model should have one field"); |
| 108 | + |
| 109 | + if let Some(ModelMember::Field(field)) = model.members.first() { |
| 110 | + assert_eq!(field.name.text, "id"); |
| 111 | + // Additional field type validation could be added here |
| 112 | + } else { |
| 113 | + panic!("Expected first member to be a field"); |
| 114 | + } |
| 115 | + } else { |
| 116 | + panic!("Expected first declaration to be a model"); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[test] |
| 121 | +fn single_empty_enum() { |
| 122 | + let input = r" |
| 123 | +enum Status { |
| 124 | +} |
| 125 | +"; |
| 126 | + let result = parse_schema_end_to_end(input); |
| 127 | + |
| 128 | + assert!( |
| 129 | + result.schema.is_some(), |
| 130 | + "Single empty enum should parse successfully" |
| 131 | + ); |
| 132 | + let schema = result.schema.unwrap(); |
| 133 | + assert_ast_structure(&schema, 0, 1, 0); |
| 134 | + assert!( |
| 135 | + result.diagnostics.is_empty(), |
| 136 | + "Empty enum should produce no diagnostics" |
| 137 | + ); |
| 138 | + |
| 139 | + // Verify enum structure |
| 140 | + if let Some(Declaration::Enum(enum_decl)) = schema.declarations.first() { |
| 141 | + assert_eq!(enum_decl.name.text, "Status"); |
| 142 | + assert!( |
| 143 | + enum_decl.members.is_empty(), |
| 144 | + "Empty enum should have no values" |
| 145 | + ); |
| 146 | + } else { |
| 147 | + panic!("Expected first declaration to be an enum"); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[test] |
| 152 | +fn single_enum_with_value() { |
| 153 | + let input = r" |
| 154 | +enum Status { |
| 155 | + ACTIVE |
| 156 | +} |
| 157 | +"; |
| 158 | + let result = parse_schema_end_to_end(input); |
| 159 | + |
| 160 | + assert!( |
| 161 | + result.schema.is_some(), |
| 162 | + "Enum with single value should parse successfully" |
| 163 | + ); |
| 164 | + let schema = result.schema.unwrap(); |
| 165 | + assert_ast_structure(&schema, 0, 1, 0); |
| 166 | + assert!( |
| 167 | + result.diagnostics.is_empty(), |
| 168 | + "Simple enum should produce no diagnostics" |
| 169 | + ); |
| 170 | + |
| 171 | + // Verify enum and value structure |
| 172 | + if let Some(Declaration::Enum(enum_decl)) = schema.declarations.first() { |
| 173 | + assert_eq!(enum_decl.name.text, "Status"); |
| 174 | + assert_eq!(enum_decl.members.len(), 1, "Enum should have one value"); |
| 175 | + |
| 176 | + if let Some(EnumMember::Value(value)) = enum_decl.members.first() { |
| 177 | + assert_eq!(value.name.text, "ACTIVE"); |
| 178 | + } else { |
| 179 | + panic!("Expected first member to be a value"); |
| 180 | + } |
| 181 | + } else { |
| 182 | + panic!("Expected first declaration to be an enum"); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +#[test] |
| 187 | +fn minimal_datasource() { |
| 188 | + let input = r#" |
| 189 | +datasource db { |
| 190 | + provider = "postgresql" |
| 191 | + url = "postgresql://localhost/test" |
| 192 | +} |
| 193 | +"#; |
| 194 | + let result = parse_schema_end_to_end(input); |
| 195 | + |
| 196 | + assert!( |
| 197 | + result.schema.is_some(), |
| 198 | + "Minimal datasource should parse successfully" |
| 199 | + ); |
| 200 | + let schema = result.schema.unwrap(); |
| 201 | + assert_ast_structure(&schema, 0, 0, 1); |
| 202 | + // Note: May produce diagnostics for unsupported constructs - this is expected |
| 203 | + println!("Datasource diagnostics: {:?}", result.diagnostics); |
| 204 | + |
| 205 | + // Verify datasource structure |
| 206 | + if let Some(Declaration::Datasource(ds)) = schema.declarations.first() { |
| 207 | + assert_eq!(ds.name.text, "db"); |
| 208 | + // Note: assignments may be empty if parsing failed, but the declaration should exist |
| 209 | + println!("Datasource assignments: {:?}", ds.assignments.len()); |
| 210 | + } else { |
| 211 | + panic!("Expected first declaration to be a datasource"); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +#[test] |
| 216 | +fn minimal_generator() { |
| 217 | + let input = r#" |
| 218 | +generator client { |
| 219 | + provider = "prisma-client-js" |
| 220 | +} |
| 221 | +"#; |
| 222 | + let result = parse_schema_end_to_end(input); |
| 223 | + |
| 224 | + assert!( |
| 225 | + result.schema.is_some(), |
| 226 | + "Minimal generator should parse successfully" |
| 227 | + ); |
| 228 | + let schema = result.schema.unwrap(); |
| 229 | + |
| 230 | + // Verify generator structure |
| 231 | + let mut generator_count = 0; |
| 232 | + for declaration in &schema.declarations { |
| 233 | + if matches!(declaration, Declaration::Generator(_)) { |
| 234 | + generator_count += 1; |
| 235 | + } |
| 236 | + } |
| 237 | + assert_eq!(generator_count, 1, "Should have one generator"); |
| 238 | + // Note: May produce diagnostics for unsupported constructs - this is expected |
| 239 | + println!("Generator diagnostics: {:?}", result.diagnostics); |
| 240 | +} |
| 241 | + |
| 242 | +#[test] |
| 243 | +fn comment_only_schema() { |
| 244 | + let input = r" |
| 245 | +// This is a comment |
| 246 | +/* This is a block comment */ |
| 247 | +/// This is a doc comment |
| 248 | +"; |
| 249 | + let result = parse_schema_end_to_end(input); |
| 250 | + |
| 251 | + assert!( |
| 252 | + result.schema.is_some(), |
| 253 | + "Comment-only schema should parse successfully" |
| 254 | + ); |
| 255 | + let schema = result.schema.unwrap(); |
| 256 | + assert_eq!( |
| 257 | + schema.declarations.len(), |
| 258 | + 0, |
| 259 | + "Comment-only schema should have no declarations" |
| 260 | + ); |
| 261 | + assert!( |
| 262 | + result.diagnostics.is_empty(), |
| 263 | + "Comments should produce no diagnostics" |
| 264 | + ); |
| 265 | +} |
| 266 | + |
| 267 | +#[test] |
| 268 | +fn mixed_minimal_declarations() { |
| 269 | + let input = r#" |
| 270 | +model User { |
| 271 | + id String |
| 272 | +} |
| 273 | +
|
| 274 | +enum Status { |
| 275 | + ACTIVE |
| 276 | +} |
| 277 | +
|
| 278 | +datasource db { |
| 279 | + provider = "postgresql" |
| 280 | + url = "postgresql://localhost/test" |
| 281 | +} |
| 282 | +
|
| 283 | +generator client { |
| 284 | + provider = "prisma-client-js" |
| 285 | +} |
| 286 | +"#; |
| 287 | + let result = parse_schema_end_to_end(input); |
| 288 | + |
| 289 | + assert!( |
| 290 | + result.schema.is_some(), |
| 291 | + "Mixed declarations should parse successfully" |
| 292 | + ); |
| 293 | + let schema = result.schema.unwrap(); |
| 294 | + assert!( |
| 295 | + schema.declarations.len() >= 4, |
| 296 | + "Should have at least 4 declarations" |
| 297 | + ); |
| 298 | + // Note: May produce diagnostics for unsupported constructs - this is expected |
| 299 | + println!("Mixed schema diagnostics: {:?}", result.diagnostics); |
| 300 | + |
| 301 | + // Verify we have the expected types |
| 302 | + let mut models = 0; |
| 303 | + let mut enums = 0; |
| 304 | + let mut datasources = 0; |
| 305 | + let mut generators = 0; |
| 306 | + |
| 307 | + for declaration in &schema.declarations { |
| 308 | + match declaration { |
| 309 | + Declaration::Model(_) => models += 1, |
| 310 | + Declaration::Enum(_) => enums += 1, |
| 311 | + Declaration::Datasource(_) => datasources += 1, |
| 312 | + Declaration::Generator(_) => generators += 1, |
| 313 | + Declaration::Type(_) => {} |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + assert_eq!(models, 1, "Should have one model"); |
| 318 | + assert_eq!(enums, 1, "Should have one enum"); |
| 319 | + assert_eq!(datasources, 1, "Should have one datasource"); |
| 320 | + assert_eq!(generators, 1, "Should have one generator"); |
| 321 | +} |
| 322 | + |
| 323 | +#[test] |
| 324 | +fn parse_time_reasonable() { |
| 325 | + let input = r" |
| 326 | +model User { |
| 327 | + id String @id |
| 328 | + name String |
| 329 | + email String @unique |
| 330 | +} |
| 331 | +"; |
| 332 | + let result = parse_schema_end_to_end(input); |
| 333 | + |
| 334 | + assert!(result.schema.is_some(), "Schema should parse successfully"); |
| 335 | + assert!( |
| 336 | + result.parse_time.as_millis() < 100, |
| 337 | + "Parse time should be under 100ms for small schema" |
| 338 | + ); |
| 339 | + assert!( |
| 340 | + result.token_count > 10, |
| 341 | + "Should produce reasonable number of tokens" |
| 342 | + ); |
| 343 | +} |
| 344 | + |
| 345 | +#[test] |
| 346 | +fn token_count_accuracy() { |
| 347 | + let input = "model User { id String }"; |
| 348 | + let result = parse_schema_end_to_end(input); |
| 349 | + |
| 350 | + assert!(result.schema.is_some(), "Schema should parse successfully"); |
| 351 | + // Expected tokens: model, User, {, id, String, }, EOF = 7 minimum |
| 352 | + assert!(result.token_count >= 7, "Should have at least 7 tokens"); |
| 353 | + assert!( |
| 354 | + result.token_count < 20, |
| 355 | + "Should not have excessive tokens for simple input" |
| 356 | + ); |
| 357 | +} |
0 commit comments