blob: b6bb9889767edd789d6f739cfcb0618cf3758a78 [file] [log] [blame]
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -05001# 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
15from mock import Mock
16import random
17
18def mock_enumerator(items):
19 e = lambda:None
20 e.all = lambda:items
21 return e
22
Scott Bakerd1702d72017-11-17 12:12:00 -080023# A list of all mock object stores that have been created
24AllMockObjectStores = []
25
26class 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:
57 raise Exception("No objects matching %s" % str(kwargs))
58 return objs[0]
59
60class 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
77class MockObject:
78 objects = None
79 id = None
80 deleted = False
81
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -050082 def __init__(self, **kwargs):
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -050083 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 Bakerd1702d72017-11-17 12:12:00 -080090 @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 Bakere0499552018-05-18 12:07:53 -070098 def save(self, update_fields=[], always_update_timestamp=False):
Scott Bakerd1702d72017-11-17 12:12:00 -080099 if self.objects:
100 self.objects.save(self)
101
102 def delete(self):
103 pass
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -0500104
105 def tologdict(self):
106 return {}
107
Scott Bakerd1702d72017-11-17 12:12:00 -0800108def get_MockObjectStore(x):
109 store = globals()["Mock%sObjects" % x]()
110 if not store in AllMockObjectStores:
111 AllMockObjectStores.append(store)
112 return store
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -0500113
114class 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 Bakerd1702d72017-11-17 12:12:00 -0800137 def reset_all_object_stores(self):
138 for store in AllMockObjectStores:
139 store.items = []
140
Matteo Scandoloa6a25c42018-03-13 16:04:22 -0700141 def get_model_class(self, classname):
142 return globals()[classname]
143
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -0500144model_accessor = ModelAccessor()
145
146class 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 Bakerd1702d72017-11-17 12:12:00 -0800158class Mock{{ m.name }}Objects(MockObjectStore): pass
159class {{ m.name }}(MockObject):
160 objects = get_MockObjectStore("{{ m.name }}")
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -0500161 {% for f in xproto_base_fields(m, proto.message_table) + m.fields -%}
162 {{ f.name }} = {{ xproto_first_non_empty([f.options.default, "None"]) }}
Scott Bakerd1702d72017-11-17 12:12:00 -0800163 {% if f.link -%}{{ f.name }}_id = None{% endif %}
Sapan Bhatiabcbe8ce2017-11-14 13:02:30 -0500164 {% endfor %}
165 leaf_model_name = "{{ m.name }}"
166{% endfor %}