|
| 1 | +import pytest |
| 2 | +from bionetgen.modelapi.xmlparsers import RuleBlockXML |
| 3 | + |
| 4 | + |
| 5 | +def test_get_rule_mod(): |
| 6 | + parser = RuleBlockXML([]) |
| 7 | + |
| 8 | + xml_totalrate_int = { |
| 9 | + "@name": "test_rule", |
| 10 | + "ListOfOperations": {}, |
| 11 | + "RateLaw": { |
| 12 | + "@type": "Function", |
| 13 | + "@totalrate": 1, |
| 14 | + "@id": "rule1", |
| 15 | + "@name": "rate1", |
| 16 | + }, |
| 17 | + } |
| 18 | + mod1 = parser.get_rule_mod(xml_totalrate_int) |
| 19 | + assert mod1.type == "TotalRate" |
| 20 | + assert str(mod1.call) == "1" |
| 21 | + |
| 22 | + xml_totalrate_str = { |
| 23 | + "@name": "test_rule", |
| 24 | + "ListOfOperations": {}, |
| 25 | + "RateLaw": { |
| 26 | + "@type": "Function", |
| 27 | + "@totalrate": "1", |
| 28 | + "@id": "rule1", |
| 29 | + "@name": "rate1", |
| 30 | + }, |
| 31 | + } |
| 32 | + mod1_str = parser.get_rule_mod(xml_totalrate_str) |
| 33 | + assert mod1_str.type == "TotalRate" |
| 34 | + assert mod1_str.call == "1" |
| 35 | + |
| 36 | + xml_totalrate_missing = { |
| 37 | + "@name": "test_rule", |
| 38 | + "ListOfOperations": {}, |
| 39 | + "RateLaw": {"@type": "Function", "@id": "rule1", "@name": "rate1"}, |
| 40 | + } |
| 41 | + mod1_miss = parser.get_rule_mod(xml_totalrate_missing) |
| 42 | + assert mod1_miss.type is None |
| 43 | + |
| 44 | + xml_totalrate_zero = { |
| 45 | + "@name": "test_rule", |
| 46 | + "ListOfOperations": {}, |
| 47 | + "RateLaw": { |
| 48 | + "@type": "Function", |
| 49 | + "@totalrate": "0", |
| 50 | + "@id": "rule1", |
| 51 | + "@name": "rate1", |
| 52 | + }, |
| 53 | + } |
| 54 | + mod1_zero = parser.get_rule_mod(xml_totalrate_zero) |
| 55 | + assert mod1_zero.type is None |
| 56 | + |
| 57 | + xml_delete = { |
| 58 | + "@name": "test_rule", |
| 59 | + "ListOfOperations": { |
| 60 | + "Delete": [{"@DeleteMolecules": "1"}, {"@DeleteMolecules": "1"}] |
| 61 | + }, |
| 62 | + } |
| 63 | + mod2 = parser.get_rule_mod(xml_delete) |
| 64 | + assert mod2.type == "DeleteMolecules" |
| 65 | + |
| 66 | + xml_delete_missing = {"@name": "test_rule", "ListOfOperations": {"Delete": [{}]}} |
| 67 | + mod3 = parser.get_rule_mod(xml_delete_missing) |
| 68 | + assert mod3.type is None |
| 69 | + |
| 70 | + xml_move = { |
| 71 | + "@name": "test_rule", |
| 72 | + "ListOfOperations": { |
| 73 | + "ChangeCompartment": [ |
| 74 | + { |
| 75 | + "@moveConnected": "1", |
| 76 | + "@id": "m1", |
| 77 | + "@source": "s1", |
| 78 | + "@destination": "d1", |
| 79 | + "@flipOrientation": "0", |
| 80 | + }, |
| 81 | + { |
| 82 | + "@moveConnected": "1", |
| 83 | + "@id": "m2", |
| 84 | + "@source": "s2", |
| 85 | + "@destination": "d2", |
| 86 | + "@flipOrientation": "1", |
| 87 | + }, |
| 88 | + ] |
| 89 | + }, |
| 90 | + } |
| 91 | + mod4 = parser.get_rule_mod(xml_move) |
| 92 | + assert mod4.type == "MoveConnected" |
| 93 | + assert mod4.id == ["m1", "m2"] |
| 94 | + assert mod4.source == ["s1", "s2"] |
| 95 | + |
| 96 | + xml_move_single = { |
| 97 | + "@name": "test_rule", |
| 98 | + "ListOfOperations": { |
| 99 | + "ChangeCompartment": { |
| 100 | + "@moveConnected": "1", |
| 101 | + "@id": "m1", |
| 102 | + "@source": "s1", |
| 103 | + "@destination": "d1", |
| 104 | + "@flipOrientation": "0", |
| 105 | + } |
| 106 | + }, |
| 107 | + } |
| 108 | + mod5 = parser.get_rule_mod(xml_move_single) |
| 109 | + assert mod5.type == "MoveConnected" |
| 110 | + assert mod5.id == "m1" |
| 111 | + assert mod5.source == "s1" |
| 112 | + |
| 113 | + # Precedence: Delete + RateLaw |
| 114 | + xml_both = { |
| 115 | + "@name": "test_rule", |
| 116 | + "ListOfOperations": {"Delete": [{"@DeleteMolecules": "1"}]}, |
| 117 | + "RateLaw": { |
| 118 | + "@type": "Function", |
| 119 | + "@totalrate": "1", |
| 120 | + "@id": "rule1", |
| 121 | + "@name": "rate1", |
| 122 | + }, |
| 123 | + } |
| 124 | + mod6 = parser.get_rule_mod(xml_both) |
| 125 | + assert mod6.type == "DeleteMolecules" |
0 commit comments