|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2020 The Matrix.org Foundation C.I.C. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from synapse.storage.database import make_tuple_comparison_clause |
| 17 | +from synapse.storage.engines import BaseDatabaseEngine |
| 18 | + |
| 19 | +from tests import unittest |
| 20 | + |
| 21 | + |
| 22 | +def _stub_db_engine(**kwargs) -> BaseDatabaseEngine: |
| 23 | + # returns a DatabaseEngine, circumventing the abc mechanism |
| 24 | + # any kwargs are set as attributes on the class before instantiating it |
| 25 | + t = type( |
| 26 | + "TestBaseDatabaseEngine", (BaseDatabaseEngine,), BaseDatabaseEngine.__dict__, |
| 27 | + ) |
| 28 | + # defeat the abc mechanism |
| 29 | + t.__abstractmethods__ = set() |
| 30 | + for k, v in kwargs.items(): |
| 31 | + setattr(t, k, v) |
| 32 | + return t(None, None) |
| 33 | + |
| 34 | + |
| 35 | +class TupleComparisonClauseTestCase(unittest.TestCase): |
| 36 | + def test_native_tuple_comparison(self): |
| 37 | + db_engine = _stub_db_engine(supports_tuple_comparison=True) |
| 38 | + clause, args = make_tuple_comparison_clause(db_engine, [("a", 1), ("b", 2)]) |
| 39 | + self.assertEqual(clause, "(a,b) > (?,?)") |
| 40 | + self.assertEqual(args, [1, 2]) |
| 41 | + |
| 42 | + def test_emulated_tuple_comparison(self): |
| 43 | + db_engine = _stub_db_engine(supports_tuple_comparison=False) |
| 44 | + clause, args = make_tuple_comparison_clause( |
| 45 | + db_engine, [("a", 1), ("b", 2), ("c", 3)] |
| 46 | + ) |
| 47 | + self.assertEqual( |
| 48 | + clause, "(a >= ? AND (a > ? OR (b >= ? AND (b > ? OR c > ?))))" |
| 49 | + ) |
| 50 | + self.assertEqual(args, [1, 1, 2, 2, 3]) |
0 commit comments