Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from mock import Mock |
| 16 | import random |
| 17 | |
| 18 | def mock_enumerator(items): |
| 19 | e = lambda:None |
| 20 | e.all = lambda:items |
| 21 | return e |
| 22 | |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 23 | # A list of all mock object stores that have been created |
| 24 | AllMockObjectStores = [] |
| 25 | |
| 26 | class MockObjectList: |
| 27 | item_list = None |
| 28 | |
| 29 | def __init__(self, initial=None): |
| 30 | self.id_counter = 0 |
| 31 | if initial: |
| 32 | self.item_list=initial |
| 33 | elif self.item_list is None: |
| 34 | self.item_list=[] |
| 35 | |
| 36 | def get_items(self): |
| 37 | return self.item_list |
| 38 | |
| 39 | def count(self): |
| 40 | return len(self.get_items()) |
| 41 | |
| 42 | def first(self): |
| 43 | return self.get_items()[0] |
| 44 | |
| 45 | def all(self): |
| 46 | return self.get_items() |
| 47 | |
| 48 | def filter(self, **kwargs): |
| 49 | items = self.get_items() |
| 50 | for (k,v) in kwargs.items(): |
| 51 | items = [x for x in items if getattr(x,k) == v] |
| 52 | return items |
| 53 | |
| 54 | def get(self, **kwargs): |
| 55 | objs = self.filter(**kwargs) |
| 56 | if not objs: |
Matteo Scandolo | aee7e45 | 2018-05-21 16:45:35 -0700 | [diff] [blame] | 57 | raise IndexError("No objects matching %s" % str(kwargs)) |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 58 | return objs[0] |
| 59 | |
| 60 | class MockObjectStore(MockObjectList): |
| 61 | def save(self, o): |
| 62 | if (not hasattr(o,"id")) or (not o.id) or (o.id==98052): |
| 63 | for item in self.get_items(): |
| 64 | if item.id >= self.id_counter: |
| 65 | self.id_counter = item.id + 1 |
| 66 | |
| 67 | o.id = self.id_counter |
| 68 | self.id_counter = self.id_counter + 1 |
| 69 | |
| 70 | for item in self.get_items(): |
| 71 | if item.id == o.id: |
| 72 | item = o |
| 73 | break |
| 74 | else: |
| 75 | self.get_items().append(o) |
| 76 | |
| 77 | class MockObject: |
| 78 | objects = None |
| 79 | id = None |
| 80 | deleted = False |
| 81 | |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 82 | def __init__(self, **kwargs): |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 83 | setattr(self, 'backend_code', 0) |
| 84 | setattr(self, 'id', 98052) |
| 85 | setattr(self, 'pk', random.randint(0, 1<<30)) |
| 86 | |
| 87 | for (k,v) in kwargs.items(): |
| 88 | setattr(self,k,v) |
| 89 | |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 90 | @property |
| 91 | def self_content_type_id(self): |
| 92 | return self.__class__.__name__ |
| 93 | |
| 94 | @property |
| 95 | def leaf_model(self): |
| 96 | return self |
| 97 | |
Scott Baker | e049955 | 2018-05-18 12:07:53 -0700 | [diff] [blame] | 98 | def save(self, update_fields=[], always_update_timestamp=False): |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 99 | if self.objects: |
| 100 | self.objects.save(self) |
| 101 | |
| 102 | def delete(self): |
| 103 | pass |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 104 | |
| 105 | def tologdict(self): |
| 106 | return {} |
| 107 | |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 108 | def get_MockObjectStore(x): |
| 109 | store = globals()["Mock%sObjects" % x]() |
| 110 | if not store in AllMockObjectStores: |
| 111 | AllMockObjectStores.append(store) |
| 112 | return store |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 113 | |
| 114 | class ModelAccessor: |
| 115 | def check_db_connection_ok(self): |
| 116 | return True |
| 117 | |
| 118 | def fetch_pending(self, model, deleted = False): |
| 119 | num = random.randint(1, 5) |
| 120 | object_list = [] |
| 121 | |
| 122 | for i in range(num): |
| 123 | if isinstance(model, list): |
| 124 | model = model[0] |
| 125 | |
| 126 | try: |
| 127 | obj = model() |
| 128 | except: |
| 129 | import pdb |
| 130 | pdb.set_trace() |
| 131 | |
| 132 | obj.name = "Opinionated Berry %d"%i |
| 133 | object_list.append(obj) |
| 134 | |
| 135 | return object_list |
| 136 | |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 137 | def reset_all_object_stores(self): |
| 138 | for store in AllMockObjectStores: |
| 139 | store.items = [] |
| 140 | |
Matteo Scandolo | a6a25c4 | 2018-03-13 16:04:22 -0700 | [diff] [blame] | 141 | def get_model_class(self, classname): |
| 142 | return globals()[classname] |
| 143 | |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 144 | model_accessor = ModelAccessor() |
| 145 | |
| 146 | class ObjectSet(object): |
| 147 | def __init__(self, objects): |
| 148 | self.objects = objects |
| 149 | |
| 150 | def all(self): |
| 151 | return self.objects |
| 152 | |
| 153 | ##### |
| 154 | # DO NOT MODIFY THE CLASSES BELOW. THEY ARE AUTOGENERATED. |
| 155 | # |
| 156 | |
| 157 | {% for m in proto.messages -%} |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 158 | class Mock{{ m.name }}Objects(MockObjectStore): pass |
| 159 | class {{ m.name }}(MockObject): |
| 160 | objects = get_MockObjectStore("{{ m.name }}") |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 161 | {% for f in xproto_base_fields(m, proto.message_table) + m.fields -%} |
| 162 | {{ f.name }} = {{ xproto_first_non_empty([f.options.default, "None"]) }} |
Scott Baker | d1702d7 | 2017-11-17 12:12:00 -0800 | [diff] [blame] | 163 | {% if f.link -%}{{ f.name }}_id = None{% endif %} |
Sapan Bhatia | bcbe8ce | 2017-11-14 13:02:30 -0500 | [diff] [blame] | 164 | {% endfor %} |
| 165 | leaf_model_name = "{{ m.name }}" |
| 166 | {% endfor %} |