|
| 1 | +# |
| 2 | +# This source file is part of the EdgeDB open source project. |
| 3 | +# |
| 4 | +# Copyright 2016-present MagicStack Inc. and the EdgeDB authors. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | + |
| 20 | +# ███ ██ ██████ ████████ ███████ |
| 21 | +# ████ ██ ██ ██ ██ ██ |
| 22 | +# ██ ██ ██ ██ ██ ██ █████ |
| 23 | +# ██ ██ ██ ██ ██ ██ ██ |
| 24 | +# ██ ████ ██████ ██ ███████ |
| 25 | +# |
| 26 | +# Run `python tools/gen_models.py` to generate the models |
| 27 | +# module from the `orm.gel` schema and get your IDE to |
| 28 | +# recognize `from models.orm import default`. |
| 29 | +# |
| 30 | +# Don't forget to re-run if you are messing with codegen |
| 31 | +# implementation or testing different versions of Gel. |
| 32 | + |
| 33 | +import os |
| 34 | + |
| 35 | +from gel import _testbase as tb |
| 36 | + |
| 37 | + |
| 38 | +class TestSyncModel(tb.ModelTestCase): |
| 39 | + SCHEMA = os.path.join( |
| 40 | + os.path.dirname(__file__), "dbsetup", "chemistry.gel" |
| 41 | + ) |
| 42 | + |
| 43 | + SETUP = os.path.join( |
| 44 | + os.path.dirname(__file__), "dbsetup", "chemistry.esdl" |
| 45 | + ) |
| 46 | + |
| 47 | + def test_sync_model_new_obj_computed_01(self): |
| 48 | + # Computeds from backlinks but no links set |
| 49 | + |
| 50 | + from models.chemistry import default |
| 51 | + |
| 52 | + # Create new objects |
| 53 | + reactor = default.Reactor() |
| 54 | + |
| 55 | + # Sync |
| 56 | + self.client.sync(reactor) |
| 57 | + |
| 58 | + # Check that value is initialized |
| 59 | + self.assertEqual(reactor.total_weight, 0) |
| 60 | + self.assertEqual(reactor.atom_weights, ()) |
| 61 | + |
| 62 | + def test_sync_model_new_obj_computed_02(self): |
| 63 | + # Computeds from links to existing object |
| 64 | + |
| 65 | + from models.chemistry import default |
| 66 | + |
| 67 | + # Create new objects |
| 68 | + hydrogen = self.client.query_required_single( |
| 69 | + default.Element.filter(symbol="H").limit(1) |
| 70 | + ) |
| 71 | + ref_atom = default.RefAtom(element=hydrogen) |
| 72 | + |
| 73 | + # Sync |
| 74 | + self.client.sync(ref_atom) |
| 75 | + |
| 76 | + # Check that computed values are fetched |
| 77 | + self.assertEqual(ref_atom.weight, 1.008) |
| 78 | + |
| 79 | + @tb.xfail |
| 80 | + def test_sync_model_new_obj_computed_03(self): |
| 81 | + # Computed from links to existing and new items |
| 82 | + |
| 83 | + from models.chemistry import default |
| 84 | + |
| 85 | + # Existing objects |
| 86 | + helium = self.client.query_required_single( |
| 87 | + default.Element.filter(symbol="He").limit(1) |
| 88 | + ) |
| 89 | + |
| 90 | + # Create new objects |
| 91 | + reactor = default.Reactor() |
| 92 | + new_atom = default.Atom(reactor=reactor, element=helium) |
| 93 | + |
| 94 | + # Sync |
| 95 | + self.client.sync(reactor, new_atom) |
| 96 | + |
| 97 | + # Check that values are fetched |
| 98 | + self.assertEqual(new_atom.weight, 4.0026) |
| 99 | + self.assertEqual(new_atom.total_bond_count, 0) |
| 100 | + self.assertEqual(new_atom.total_bond_weight, 0) |
| 101 | + self.assertEqual(reactor.total_weight, 4.0026) # Failing |
| 102 | + self.assertEqual(reactor.atom_weights, [4.0026]) # Failing |
| 103 | + |
| 104 | + @tb.xfail |
| 105 | + def test_sync_model_new_obj_computed_04(self): |
| 106 | + # Computed from links to existing and new items |
| 107 | + |
| 108 | + from models.chemistry import default |
| 109 | + |
| 110 | + # Existing objects |
| 111 | + hydrogen = self.client.query_required_single( |
| 112 | + default.Element.select(weight=True).filter(symbol="H").limit(1) |
| 113 | + ) |
| 114 | + oxygen = self.client.query_required_single( |
| 115 | + default.Element.select(weight=True).filter(symbol="O").limit(1) |
| 116 | + ) |
| 117 | + |
| 118 | + # Create new objects |
| 119 | + reactor = default.Reactor() |
| 120 | + |
| 121 | + o_1 = default.Atom(reactor=reactor, element=oxygen) |
| 122 | + h_1 = default.Atom(reactor=reactor, element=hydrogen) |
| 123 | + h_2 = default.Atom(reactor=reactor, element=hydrogen) |
| 124 | + o_1.bonds = [ |
| 125 | + default.Atom.bonds.link(h_1, count=1), |
| 126 | + default.Atom.bonds.link(h_2, count=1), |
| 127 | + ] |
| 128 | + h_1.bonds = [default.Atom.bonds.link(o_1, count=1)] |
| 129 | + h_2.bonds = [default.Atom.bonds.link(o_1, count=1)] |
| 130 | + |
| 131 | + hydrogen_atoms = [h_1, h_2] |
| 132 | + oxygen_atoms = [h_1, h_2] |
| 133 | + |
| 134 | + # Sync |
| 135 | + self.client.sync(*hydrogen_atoms, *oxygen_atoms) |
| 136 | + |
| 137 | + # Check that values are fetched |
| 138 | + self.assertEqual( |
| 139 | + [atom.weight for atom in hydrogen_atoms], |
| 140 | + [1.008, 1.008], |
| 141 | + ) |
| 142 | + self.assertEqual( # Failing |
| 143 | + [atom.total_bond_count for atom in hydrogen_atoms], |
| 144 | + [1, 1], |
| 145 | + ) |
| 146 | + self.assertEqual( # Failing |
| 147 | + [atom.total_bond_weight for atom in hydrogen_atoms], |
| 148 | + [15.999, 15.999], |
| 149 | + ) |
| 150 | + |
| 151 | + self.assertEqual( |
| 152 | + [atom.weight for atom in oxygen_atoms], |
| 153 | + [15.999], |
| 154 | + ) |
| 155 | + self.assertEqual( # Failing |
| 156 | + [atom.total_bond_count for atom in oxygen_atoms], |
| 157 | + [2], |
| 158 | + ) |
| 159 | + self.assertEqual( # Failing |
| 160 | + [atom.total_bond_weight for atom in oxygen_atoms], |
| 161 | + [1.008 * 2], |
| 162 | + ) |
| 163 | + |
| 164 | + self.assertEqual( # Failing |
| 165 | + reactor.total_weight, |
| 166 | + 1.008 * 2 + 15.999, |
| 167 | + ) |
| 168 | + self.assertEqual( # Failing |
| 169 | + list(sorted(reactor.atom_weights)), |
| 170 | + (1.008, 1.008, 15.999), |
| 171 | + ) |
0 commit comments