|
| 1 | +//! Shared WASM test suite for both Node.js and browser environments. |
| 2 | +//! |
| 3 | +//! This module contains tests that run in both environments. The test runner |
| 4 | +//! is configured based on which test file includes this module. |
| 5 | +
|
| 6 | +#![cfg(target_arch = "wasm32")] |
| 7 | + |
| 8 | +use cql2_wasm::{parse_json, parse_text, CQL2Expression}; |
| 9 | +use wasm_bindgen_test::*; |
| 10 | + |
| 11 | +#[wasm_bindgen_test] |
| 12 | +fn is_valid() { |
| 13 | + let expr = |
| 14 | + CQL2Expression::new("landsat:scene_id = 'LC82030282019133LGN00'".to_string()).unwrap(); |
| 15 | + assert!(expr.is_valid()); |
| 16 | +} |
| 17 | + |
| 18 | +#[wasm_bindgen_test] |
| 19 | +fn test_parse_text() { |
| 20 | + let expr = parse_text("landsat:scene_id = 'LC82030282019133LGN00'").unwrap(); |
| 21 | + assert!(expr.is_valid()); |
| 22 | +} |
| 23 | + |
| 24 | +#[wasm_bindgen_test] |
| 25 | +fn test_parse_json() { |
| 26 | + let json = r#"{"op":"=","args":[{"property":"landsat:scene_id"},"LC82030282019133LGN00"]}"#; |
| 27 | + let expr = parse_json(json).unwrap(); |
| 28 | + assert!(expr.is_valid()); |
| 29 | +} |
| 30 | + |
| 31 | +#[wasm_bindgen_test] |
| 32 | +fn test_validate_valid_expression() { |
| 33 | + let expr = |
| 34 | + CQL2Expression::new("landsat:scene_id = 'LC82030282019133LGN00'".to_string()).unwrap(); |
| 35 | + assert!(expr.validate().is_ok()); |
| 36 | +} |
| 37 | + |
| 38 | +#[wasm_bindgen_test] |
| 39 | +fn test_to_json() { |
| 40 | + let expr = |
| 41 | + CQL2Expression::new("landsat:scene_id = 'LC82030282019133LGN00'".to_string()).unwrap(); |
| 42 | + let json = expr.to_json().unwrap(); |
| 43 | + assert!(json.contains("landsat:scene_id")); |
| 44 | + assert!(json.contains("LC82030282019133LGN00")); |
| 45 | +} |
| 46 | + |
| 47 | +#[wasm_bindgen_test] |
| 48 | +fn test_to_json_pretty() { |
| 49 | + let expr = |
| 50 | + CQL2Expression::new("landsat:scene_id = 'LC82030282019133LGN00'".to_string()).unwrap(); |
| 51 | + let json = expr.to_json_pretty().unwrap(); |
| 52 | + assert!(json.contains("landsat:scene_id")); |
| 53 | + assert!(json.contains("\n")); // Should have newlines for pretty printing |
| 54 | +} |
| 55 | + |
| 56 | +#[wasm_bindgen_test] |
| 57 | +fn test_to_text() { |
| 58 | + let json = r#"{"op":"=","args":[{"property":"landsat:scene_id"},"LC82030282019133LGN00"]}"#; |
| 59 | + let expr = parse_json(json).unwrap(); |
| 60 | + let text = expr.to_text().unwrap(); |
| 61 | + assert!(text.contains("landsat:scene_id")); |
| 62 | + assert!(text.contains("LC82030282019133LGN00")); |
| 63 | +} |
| 64 | + |
| 65 | +#[wasm_bindgen_test] |
| 66 | +fn test_to_sql() { |
| 67 | + let expr = |
| 68 | + CQL2Expression::new("landsat:scene_id = 'LC82030282019133LGN00'".to_string()).unwrap(); |
| 69 | + let sql = expr.to_sql().unwrap(); |
| 70 | + assert!(sql.contains("landsat:scene_id")); |
| 71 | + assert!(sql.contains("LC82030282019133LGN00")); |
| 72 | +} |
| 73 | + |
| 74 | +#[wasm_bindgen_test] |
| 75 | +fn test_matches_with_matching_item() { |
| 76 | + let expr = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 77 | + let item = r#"{"id": 1, "name": "test"}"#; |
| 78 | + let result = expr.matches(Some(item.to_string())).unwrap(); |
| 79 | + assert!(result); |
| 80 | +} |
| 81 | + |
| 82 | +#[wasm_bindgen_test] |
| 83 | +fn test_matches_with_non_matching_item() { |
| 84 | + let expr = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 85 | + let item = r#"{"id": 2, "name": "test"}"#; |
| 86 | + let result = expr.matches(Some(item.to_string())).unwrap(); |
| 87 | + assert!(!result); |
| 88 | +} |
| 89 | + |
| 90 | +#[wasm_bindgen_test] |
| 91 | +fn test_matches_without_item() { |
| 92 | + let expr = CQL2Expression::new("true".to_string()).unwrap(); |
| 93 | + let result = expr.matches(None).unwrap(); |
| 94 | + assert!(result); |
| 95 | +} |
| 96 | + |
| 97 | +#[wasm_bindgen_test] |
| 98 | +fn test_reduce_without_item() { |
| 99 | + let expr = CQL2Expression::new("1 + 2".to_string()).unwrap(); |
| 100 | + let reduced = expr.reduce(None).unwrap(); |
| 101 | + let text = reduced.to_text().unwrap(); |
| 102 | + assert_eq!(text, "3"); |
| 103 | +} |
| 104 | + |
| 105 | +#[wasm_bindgen_test] |
| 106 | +fn test_reduce_with_item() { |
| 107 | + let expr = CQL2Expression::new("id + 10".to_string()).unwrap(); |
| 108 | + let item = r#"{"id": 5}"#; |
| 109 | + let reduced = expr.reduce(Some(item.to_string())).unwrap(); |
| 110 | + let text = reduced.to_text().unwrap(); |
| 111 | + assert_eq!(text, "15"); |
| 112 | +} |
| 113 | + |
| 114 | +#[wasm_bindgen_test] |
| 115 | +fn test_add_expressions() { |
| 116 | + let expr1 = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 117 | + let expr2 = CQL2Expression::new("name = 'test'".to_string()).unwrap(); |
| 118 | + let combined = expr1.add(&expr2); |
| 119 | + let text = combined.to_text().unwrap(); |
| 120 | + assert!(text.contains("id")); |
| 121 | + assert!(text.contains("name")); |
| 122 | + assert!(text.contains("AND") || text.contains("and")); |
| 123 | +} |
| 124 | + |
| 125 | +#[wasm_bindgen_test] |
| 126 | +fn test_equals_same_expressions() { |
| 127 | + let expr1 = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 128 | + let expr2 = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 129 | + assert!(expr1.equals(&expr2)); |
| 130 | +} |
| 131 | + |
| 132 | +#[wasm_bindgen_test] |
| 133 | +fn test_equals_different_expressions() { |
| 134 | + let expr1 = CQL2Expression::new("id = 1".to_string()).unwrap(); |
| 135 | + let expr2 = CQL2Expression::new("id = 2".to_string()).unwrap(); |
| 136 | + assert!(!expr1.equals(&expr2)); |
| 137 | +} |
0 commit comments