parseModelTests parses the model tests from the YAML file
(paths []string, name string, data *DataYAML, connector, modelName, assert string)
| 284 | |
| 285 | // parseModelTests parses the model tests from the YAML file |
| 286 | func (p *Parser) parseModelTest(paths []string, name string, data *DataYAML, connector, modelName, assert string) (*runtimev1.ModelTest, []ResourceName, error) { |
| 287 | // Validate required name field |
| 288 | if name == "" { |
| 289 | return nil, nil, fmt.Errorf(`test must have a "name" defined`) |
| 290 | } |
| 291 | |
| 292 | hasSQL := data.SQL != "" |
| 293 | hasAssertion := assert != "" |
| 294 | |
| 295 | // Validate that exactly one of "sql" or "assert" is provided |
| 296 | switch { |
| 297 | case hasSQL && hasAssertion: |
| 298 | return nil, nil, fmt.Errorf(`test %q must not have both "sql" and "assert" defined`, name) |
| 299 | case !hasSQL && !hasAssertion: |
| 300 | return nil, nil, fmt.Errorf(`test %q must have either "sql" or "assert" defined`, name) |
| 301 | case hasAssertion: |
| 302 | // Wrap assertion condition in a SQL query following SQLMesh audit pattern |
| 303 | // Query for rows that violate the assertion (bad data) |
| 304 | data.SQL = fmt.Sprintf("SELECT * FROM %s WHERE NOT (%s)", modelName, assert) |
| 305 | } |
| 306 | |
| 307 | resolver, props, refs, err := p.parseDataYAML(paths, data, connector) |
| 308 | if err != nil { |
| 309 | return nil, nil, err |
| 310 | } |
| 311 | return &runtimev1.ModelTest{ |
| 312 | Name: name, |
| 313 | Resolver: resolver, |
| 314 | ResolverProperties: props, |
| 315 | }, refs, nil |
| 316 | } |
| 317 | |
| 318 | // inferSQLRefs attempts to infer table references from the node's SQL. |
| 319 | // The provided node must have a non-empty SQL field. |
no test coverage detected