|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Comprehensive tests for nested field support in PyMongoSQL |
| 4 | +""" |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pymongosql.error import SqlSyntaxError |
| 8 | +from pymongosql.sql.parser import SQLParser |
| 9 | + |
| 10 | + |
| 11 | +class TestSQLParserNestedFields: |
| 12 | + """Test suite for nested field querying functionality""" |
| 13 | + |
| 14 | + def test_basic_single_level_nesting_select(self): |
| 15 | + """Test basic single-level nested fields in SELECT""" |
| 16 | + sql = "SELECT c.a, c.b FROM collection" |
| 17 | + parser = SQLParser(sql) |
| 18 | + |
| 19 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 20 | + |
| 21 | + execution_plan = parser.get_execution_plan() |
| 22 | + assert execution_plan.collection == "collection" |
| 23 | + assert execution_plan.projection_stage == {"c.a": 1, "c.b": 1} |
| 24 | + assert execution_plan.filter_stage == {} |
| 25 | + |
| 26 | + def test_basic_single_level_nesting_where(self): |
| 27 | + """Test basic single-level nested fields in WHERE clause""" |
| 28 | + sql = "SELECT * FROM users WHERE profile.status = 'active'" |
| 29 | + parser = SQLParser(sql) |
| 30 | + |
| 31 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 32 | + |
| 33 | + execution_plan = parser.get_execution_plan() |
| 34 | + assert execution_plan.collection == "users" |
| 35 | + assert execution_plan.filter_stage == {"profile.status": "active"} |
| 36 | + |
| 37 | + def test_multi_level_nesting_non_reserved_words(self): |
| 38 | + """Test multi-level nested fields with non-reserved words""" |
| 39 | + sql = "SELECT account.profile.name FROM users WHERE account.settings.theme = 'dark'" |
| 40 | + parser = SQLParser(sql) |
| 41 | + |
| 42 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 43 | + |
| 44 | + execution_plan = parser.get_execution_plan() |
| 45 | + assert execution_plan.collection == "users" |
| 46 | + assert execution_plan.projection_stage == {"account.profile.name": 1} |
| 47 | + assert execution_plan.filter_stage == {"account.settings.theme": "dark"} |
| 48 | + |
| 49 | + def test_array_bracket_notation_select(self): |
| 50 | + """Test array access using bracket notation in SELECT""" |
| 51 | + sql = "SELECT items[0], items[1].name FROM orders" |
| 52 | + parser = SQLParser(sql) |
| 53 | + |
| 54 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 55 | + |
| 56 | + execution_plan = parser.get_execution_plan() |
| 57 | + assert execution_plan.collection == "orders" |
| 58 | + assert execution_plan.projection_stage == {"items[0]": 1, "items[1].name": 1} |
| 59 | + |
| 60 | + def test_array_bracket_notation_where(self): |
| 61 | + """Test array access using bracket notation in WHERE""" |
| 62 | + sql = "SELECT * FROM orders WHERE items[0].price > 100" |
| 63 | + parser = SQLParser(sql) |
| 64 | + |
| 65 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 66 | + |
| 67 | + execution_plan = parser.get_execution_plan() |
| 68 | + assert execution_plan.collection == "orders" |
| 69 | + assert execution_plan.filter_stage == {"items[0].price": {"$gt": 100}} |
| 70 | + |
| 71 | + def test_quoted_reserved_words(self): |
| 72 | + """Test using quoted reserved words as field names - currently limited support""" |
| 73 | + # Note: This test documents current limitations with quoted identifiers in complex paths |
| 74 | + sql = 'SELECT "user" FROM collection' # Simplified test that works |
| 75 | + parser = SQLParser(sql) |
| 76 | + |
| 77 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 78 | + |
| 79 | + execution_plan = parser.get_execution_plan() |
| 80 | + assert execution_plan.collection == "collection" |
| 81 | + assert execution_plan.projection_stage == {'"user"': 1} |
| 82 | + |
| 83 | + def test_complex_nested_query(self): |
| 84 | + """Test complex query with multiple nested field types""" |
| 85 | + sql = """ |
| 86 | + SELECT |
| 87 | + customer.profile.name, |
| 88 | + orders[0].total, |
| 89 | + settings.preferences.theme |
| 90 | + FROM transactions |
| 91 | + WHERE customer.profile.age > 18 |
| 92 | + AND orders[0].status = 'completed' |
| 93 | + AND settings.notifications = true |
| 94 | + """ |
| 95 | + parser = SQLParser(sql) |
| 96 | + |
| 97 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 98 | + |
| 99 | + execution_plan = parser.get_execution_plan() |
| 100 | + assert execution_plan.collection == "transactions" |
| 101 | + |
| 102 | + expected_projection = {"customer.profile.name": 1, "orders[0].total": 1, "settings.preferences.theme": 1} |
| 103 | + assert execution_plan.projection_stage == expected_projection |
| 104 | + |
| 105 | + # The filter should be a combination of conditions |
| 106 | + expected_filter = { |
| 107 | + "$and": [ |
| 108 | + {"customer.profile.age": {"$gt": 18}}, |
| 109 | + {"orders[0].status": "completed"}, |
| 110 | + {"settings.notifications": True}, |
| 111 | + ] |
| 112 | + } |
| 113 | + assert execution_plan.filter_stage == expected_filter |
| 114 | + |
| 115 | + def test_reserved_word_user_fails(self): |
| 116 | + """Test that unquoted 'user' keyword fails""" |
| 117 | + sql = "SELECT user.profile.name FROM users" |
| 118 | + |
| 119 | + with pytest.raises(SqlSyntaxError) as exc_info: |
| 120 | + parser = SQLParser(sql) |
| 121 | + parser.get_execution_plan() |
| 122 | + |
| 123 | + assert "no viable alternative" in str(exc_info.value) |
| 124 | + |
| 125 | + def test_reserved_word_value_fails(self): |
| 126 | + """Test that unquoted 'value' keyword fails""" |
| 127 | + sql = "SELECT data.value FROM items" |
| 128 | + |
| 129 | + with pytest.raises(SqlSyntaxError) as exc_info: |
| 130 | + parser = SQLParser(sql) |
| 131 | + parser.get_execution_plan() |
| 132 | + |
| 133 | + assert "no viable alternative" in str(exc_info.value) |
| 134 | + |
| 135 | + def test_numeric_dot_notation_fails(self): |
| 136 | + """Test that numeric dot notation fails""" |
| 137 | + sql = "SELECT c.0.name FROM collection" |
| 138 | + |
| 139 | + with pytest.raises(SqlSyntaxError) as exc_info: |
| 140 | + parser = SQLParser(sql) |
| 141 | + parser.get_execution_plan() |
| 142 | + |
| 143 | + assert "mismatched input" in str(exc_info.value) |
| 144 | + |
| 145 | + def test_nested_with_comparison_operators(self): |
| 146 | + """Test nested fields with various comparison operators""" |
| 147 | + # Test supported comparison operators with non-reserved field names |
| 148 | + test_cases = [ |
| 149 | + ("profile.age > 18", {"profile.age": {"$gt": 18}}), |
| 150 | + ("settings.total < 100", {"settings.total": {"$lt": 100}}), # Changed from 'count' (reserved) |
| 151 | + ("status.active = true", {"status.active": True}), |
| 152 | + ("config.name != 'default'", {"config.name": {"$ne": "default"}}), |
| 153 | + ] |
| 154 | + |
| 155 | + for where_clause, expected_filter in test_cases: |
| 156 | + sql = f"SELECT * FROM collection WHERE {where_clause}" |
| 157 | + parser = SQLParser(sql) |
| 158 | + |
| 159 | + assert not parser.has_errors, f"Parser errors for '{where_clause}': {parser.errors}" |
| 160 | + |
| 161 | + execution_plan = parser.get_execution_plan() |
| 162 | + assert execution_plan.filter_stage == expected_filter |
| 163 | + |
| 164 | + def test_nested_with_logical_operators(self): |
| 165 | + """Test nested fields with logical operators""" |
| 166 | + sql = """ |
| 167 | + SELECT * FROM users |
| 168 | + WHERE profile.age > 18 |
| 169 | + AND settings.active = true |
| 170 | + OR profile.vip = true |
| 171 | + """ |
| 172 | + parser = SQLParser(sql) |
| 173 | + |
| 174 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 175 | + |
| 176 | + execution_plan = parser.get_execution_plan() |
| 177 | + # The exact structure depends on operator precedence handling |
| 178 | + assert "profile.age" in str(execution_plan.filter_stage) |
| 179 | + assert "settings.active" in str(execution_plan.filter_stage) |
| 180 | + assert "profile.vip" in str(execution_plan.filter_stage) |
| 181 | + |
| 182 | + def test_nested_with_aliases(self): |
| 183 | + """Test nested fields with column aliases""" |
| 184 | + sql = "SELECT profile.name AS fullname, settings.theme AS ui_theme FROM users" |
| 185 | + parser = SQLParser(sql) |
| 186 | + |
| 187 | + assert not parser.has_errors, f"Parser errors: {parser.errors}" |
| 188 | + |
| 189 | + execution_plan = parser.get_execution_plan() |
| 190 | + assert execution_plan.collection == "users" |
| 191 | + # Note: Current implementation uses original field names in projection |
| 192 | + # Aliases are handled at the result processing level |
| 193 | + assert execution_plan.projection_stage == {"profile.name": 1, "settings.theme": 1} |
0 commit comments