Best Python code snippet using gherkin-python
nodes.py
Source:nodes.py
...35 """36 def __init__(self, lineno=None, filename=None):37 self.lineno = lineno38 self.filename = filename39 def get_items(self):40 return []41 def get_child_nodes(self):42 return [x for x in self.get_items() if isinstance(x, Node)]43 def allows_assignments(self):44 return False45 def __repr__(self):46 return 'Node()'47class Text(Node):48 """49 Node that represents normal text.50 """51 def __init__(self, text, variables, lineno=None, filename=None):52 Node.__init__(self, lineno, filename)53 self.text = text54 self.variables = variables55 def get_items(self):56 return [self.text] + list(self.variables)57 def __repr__(self):58 return 'Text(%r, %r)' % (59 self.text,60 self.variables61 )62class NodeList(list, Node):63 """64 A node that stores multiple childnodes.65 """66 def __init__(self, data, lineno=None, filename=None):67 Node.__init__(self, lineno, filename)68 list.__init__(self, data)69 def get_items(self):70 return list(self)71 def __repr__(self):72 return 'NodeList(%s)' % list.__repr__(self)73class Template(Node):74 """75 Node that represents a template.76 """77 def __init__(self, extends, body, lineno=None, filename=None):78 Node.__init__(self, lineno, filename)79 self.extends = extends80 self.body = body81 def get_items(self):82 return [self.extends, self.body]83 def __repr__(self):84 return 'Template(%r, %r)' % (85 self.extends,86 self.body87 )88class ForLoop(Node):89 """90 A node that represents a for loop91 """92 def __init__(self, item, seq, body, else_, recursive, lineno=None,93 filename=None):94 Node.__init__(self, lineno, filename)95 self.item = item96 self.seq = seq97 self.body = body98 self.else_ = else_99 self.recursive = recursive100 def get_items(self):101 return [self.item, self.seq, self.body, self.else_, self.recursive]102 def __repr__(self):103 return 'ForLoop(%r, %r, %r, %r, %r)' % (104 self.item,105 self.seq,106 self.body,107 self.else_,108 self.recursive109 )110class IfCondition(Node):111 """112 A node that represents an if condition.113 """114 def __init__(self, tests, else_, lineno=None, filename=None):115 Node.__init__(self, lineno, filename)116 self.tests = tests117 self.else_ = else_118 def get_items(self):119 result = []120 for test in self.tests:121 result.extend(test)122 result.append(self.else_)123 return result124 def __repr__(self):125 return 'IfCondition(%r, %r)' % (126 self.tests,127 self.else_128 )129class Cycle(Node):130 """131 A node that represents the cycle statement.132 """133 def __init__(self, seq, lineno=None, filename=None):134 Node.__init__(self, lineno, filename)135 self.seq = seq136 def get_items(self):137 return [self.seq]138 def __repr__(self):139 return 'Cycle(%r)' % (self.seq,)140class Print(Node):141 """142 A node that represents variable tags and print calls.143 """144 def __init__(self, expr, lineno=None, filename=None):145 Node.__init__(self, lineno, filename)146 self.expr = expr147 def get_items(self):148 return [self.expr]149 def __repr__(self):150 return 'Print(%r)' % (self.expr,)151class Macro(Node):152 """153 A node that represents a macro.154 """155 def __init__(self, name, arguments, body, lineno=None, filename=None):156 Node.__init__(self, lineno, filename)157 self.name = name158 self.arguments = arguments159 self.body = body160 def get_items(self):161 return [self.name] + list(chain(*self.arguments)) + [self.body]162 def __repr__(self):163 return 'Macro(%r, %r, %r)' % (164 self.name,165 self.arguments,166 self.body167 )168class Call(Node):169 """170 A node that represents am extended macro call.171 """172 def __init__(self, expr, body, lineno=None, filename=None):173 Node.__init__(self, lineno, filename)174 self.expr = expr175 self.body = body176 def get_items(self):177 return [self.expr, self.body]178 def __repr__(self):179 return 'Call(%r, %r)' % (180 self.expr,181 self.body182 )183class Set(Node):184 """185 Allows defining own variables.186 """187 def __init__(self, name, expr, scope_local, lineno=None, filename=None):188 Node.__init__(self, lineno, filename)189 self.name = name190 self.expr = expr191 self.scope_local = scope_local192 def get_items(self):193 return [self.name, self.expr, self.scope_local]194 def __repr__(self):195 return 'Set(%r, %r, %r)' % (196 self.name,197 self.expr,198 self.scope_local199 )200class Filter(Node):201 """202 Node for filter sections.203 """204 def __init__(self, body, filters, lineno=None, filename=None):205 Node.__init__(self, lineno, filename)206 self.body = body207 self.filters = filters208 def get_items(self):209 return [self.body] + list(self.filters)210 def __repr__(self):211 return 'Filter(%r, %r)' % (212 self.body,213 self.filters214 )215class Block(Node):216 """217 A node that represents a block.218 """219 def __init__(self, name, body, lineno=None, filename=None):220 Node.__init__(self, lineno, filename)221 self.name = name222 self.body = body223 def replace(self, node):224 """225 Replace the current data with the copied data of another block226 node.227 """228 assert node.__class__ is Block229 self.lineno = node.lineno230 self.filename = node.filename231 self.name = node.name232 self.body = copy(node.body)233 def clone(self):234 """235 Create an independent clone of this node.236 """237 return copy(self)238 def get_items(self):239 return [self.name, self.body]240 def __repr__(self):241 return 'Block(%r, %r)' % (242 self.name,243 self.body244 )245class Include(Node):246 """247 A node that represents the include tag.248 """249 def __init__(self, template, lineno=None, filename=None):250 Node.__init__(self, lineno, filename)251 self.template = template252 def get_items(self):253 return [self.template]254 def __repr__(self):255 return 'Include(%r)' % (256 self.template257 )258class Trans(Node):259 """260 A node for translatable sections.261 """262 def __init__(self, singular, plural, indicator, replacements,263 lineno=None, filename=None):264 Node.__init__(self, lineno, filename)265 self.singular = singular266 self.plural = plural267 self.indicator = indicator268 self.replacements = replacements269 def get_items(self):270 rv = [self.singular, self.plural, self.indicator]271 if self.replacements:272 rv.extend(self.replacements.values())273 rv.extend(self.replacements.keys())274 return rv275 def __repr__(self):276 return 'Trans(%r, %r, %r, %r)' % (277 self.singular,278 self.plural,279 self.indicator,280 self.replacements281 )282class Expression(Node):283 """284 Baseclass for all expressions.285 """286class BinaryExpression(Expression):287 """288 Baseclass for all binary expressions.289 """290 def __init__(self, left, right, lineno=None, filename=None):291 Expression.__init__(self, lineno, filename)292 self.left = left293 self.right = right294 def get_items(self):295 return [self.left, self.right]296 def __repr__(self):297 return '%s(%r, %r)' % (298 self.__class__.__name__,299 self.left,300 self.right301 )302class UnaryExpression(Expression):303 """304 Baseclass for all unary expressions.305 """306 def __init__(self, node, lineno=None, filename=None):307 Expression.__init__(self, lineno, filename)308 self.node = node309 def get_items(self):310 return [self.node]311 def __repr__(self):312 return '%s(%r)' % (313 self.__class__.__name__,314 self.node315 )316class ConstantExpression(Expression):317 """318 any constat such as {{ "foo" }}319 """320 def __init__(self, value, lineno=None, filename=None):321 Expression.__init__(self, lineno, filename)322 self.value = value323 def get_items(self):324 return [self.value]325 def __repr__(self):326 return 'ConstantExpression(%r)' % (self.value,)327class UndefinedExpression(Expression):328 """329 represents the special 'undefined' value.330 """331 def __repr__(self):332 return 'UndefinedExpression()'333class RegexExpression(Expression):334 """335 represents the regular expression literal.336 """337 def __init__(self, value, lineno=None, filename=None):338 Expression.__init__(self, lineno, filename)339 self.value = value340 def get_items(self):341 return [self.value]342 def __repr__(self):343 return 'RegexExpression(%r)' % (self.value,)344class NameExpression(Expression):345 """346 any name such as {{ foo }}347 """348 def __init__(self, name, lineno=None, filename=None):349 Expression.__init__(self, lineno, filename)350 self.name = name351 def get_items(self):352 return [self.name]353 def allows_assignments(self):354 return self.name != '_'355 def __repr__(self):356 return 'NameExpression(%r)' % self.name357class ListExpression(Expression):358 """359 any list literal such as {{ [1, 2, 3] }}360 """361 def __init__(self, items, lineno=None, filename=None):362 Expression.__init__(self, lineno, filename)363 self.items = items364 def get_items(self):365 return list(self.items)366 def __repr__(self):367 return 'ListExpression(%r)' % (self.items,)368class DictExpression(Expression):369 """370 any dict literal such as {{ {1: 2, 3: 4} }}371 """372 def __init__(self, items, lineno=None, filename=None):373 Expression.__init__(self, lineno, filename)374 self.items = items375 def get_items(self):376 return list(chain(*self.items))377 def __repr__(self):378 return 'DictExpression(%r)' % (self.items,)379class SetExpression(Expression):380 """381 any set literal such as {{ @(1, 2, 3) }}382 """383 def __init__(self, items, lineno=None, filename=None):384 Expression.__init__(self, lineno, filename)385 self.items = items386 def get_items(self):387 return self.items[:]388 def __repr__(self):389 return 'SetExpression(%r)' % (self.items,)390class ConditionalExpression(Expression):391 """392 {{ foo if bar else baz }}393 """394 def __init__(self, test, expr1, expr2, lineno=None, filename=None):395 Expression.__init__(self, lineno, filename)396 self.test = test397 self.expr1 = expr1398 self.expr2 = expr2399 def get_items(self):400 return [self.test, self.expr1, self.expr2]401 def __repr__(self):402 return 'ConstantExpression(%r, %r, %r)' % (403 self.test,404 self.expr1,405 self.expr2406 )407class FilterExpression(Expression):408 """409 {{ foo|bar|baz }}410 """411 def __init__(self, node, filters, lineno=None, filename=None):412 Expression.__init__(self, lineno, filename)413 self.node = node414 self.filters = filters415 def get_items(self):416 result = [self.node]417 for filter, args in self.filters:418 result.append(filter)419 result.extend(args)420 return result421 def __repr__(self):422 return 'FilterExpression(%r, %r)' % (423 self.node,424 self.filters425 )426class TestExpression(Expression):427 """428 {{ foo is lower }}429 """430 def __init__(self, node, name, args, lineno=None, filename=None):431 Expression.__init__(self, lineno, filename)432 self.node = node433 self.name = name434 self.args = args435 def get_items(self):436 return [self.node, self.name] + list(self.args)437 def __repr__(self):438 return 'TestExpression(%r, %r, %r)' % (439 self.node,440 self.name,441 self.args442 )443class CallExpression(Expression):444 """445 {{ foo(bar) }}446 """447 def __init__(self, node, args, kwargs, dyn_args, dyn_kwargs,448 lineno=None, filename=None):449 Expression.__init__(self, lineno, filename)450 self.node = node451 self.args = args452 self.kwargs = kwargs453 self.dyn_args = dyn_args454 self.dyn_kwargs = dyn_kwargs455 def get_items(self):456 return [self.node, self.args, self.kwargs, self.dyn_args,457 self.dyn_kwargs]458 def __repr__(self):459 return 'CallExpression(%r, %r, %r, %r, %r)' % (460 self.node,461 self.args,462 self.kwargs,463 self.dyn_args,464 self.dyn_kwargs465 )466class SubscriptExpression(Expression):467 """468 {{ foo.bar }} and {{ foo['bar'] }} etc.469 """470 def __init__(self, node, arg, lineno=None, filename=None):471 Expression.__init__(self, lineno, filename)472 self.node = node473 self.arg = arg474 def get_items(self):475 return [self.node, self.arg]476 def __repr__(self):477 return 'SubscriptExpression(%r, %r)' % (478 self.node,479 self.arg480 )481class SliceExpression(Expression):482 """483 1:2:3 etc.484 """485 def __init__(self, start, stop, step, lineno=None, filename=None):486 Expression.__init__(self, lineno, filename)487 self.start = start488 self.stop = stop489 self.step = step490 def get_items(self):491 return [self.start, self.stop, self.step]492 def __repr__(self):493 return 'SliceExpression(%r, %r, %r)' % (494 self.start,495 self.stop,496 self.step497 )498class TupleExpression(Expression):499 """500 For loop unpacking and some other things like multiple arguments501 for subscripts.502 """503 def __init__(self, items, lineno=None, filename=None):504 Expression.__init__(self, lineno, filename)505 self.items = items506 def get_items(self):507 return list(self.items)508 def allows_assignments(self):509 for item in self.items:510 if not item.allows_assignments():511 return False512 return True513 def __repr__(self):514 return 'TupleExpression(%r)' % (self.items,)515class ConcatExpression(Expression):516 """517 For {{ foo ~ bar }}. Because of various reasons (especially because518 unicode conversion takes place for the left and right expression and519 is better optimized that way)520 """521 def __init__(self, args, lineno=None, filename=None):522 Expression.__init__(self, lineno, filename)523 self.args = args524 def get_items(self):525 return list(self.args)526 def __repr__(self):527 return 'ConcatExpression(%r)' % (self.items,)528class CompareExpression(Expression):529 """530 {{ foo == bar }}, {{ foo >= bar }} etc.531 """532 def __init__(self, expr, ops, lineno=None, filename=None):533 Expression.__init__(self, lineno, filename)534 self.expr = expr535 self.ops = ops536 def get_items(self):537 return [self.expr] + list(chain(*self.ops))538 def __repr__(self):539 return 'CompareExpression(%r, %r)' % (540 self.expr,541 self.ops542 )543class MulExpression(BinaryExpression):544 """545 {{ foo * bar }}546 """547class DivExpression(BinaryExpression):548 """549 {{ foo / bar }}550 """...
property.py
Source:property.py
...92 x: [] for x in access.matches93 }94 for prop in properties:95 for a in access.matches:96 new_props[a].append(prop.get_items(a))97 for a, props in new_props.items():98 if props:99 result._items[a] = action(*props)100 return result101 @staticmethod102 def union(properties: Iterable[Property], access: Access) -> Property:103 return Property._set_action(properties, access, set.union)104 def union_update(self, property: Property, access: Access):105 for access in access.matches:106 self._items[access].update(property._items[access])107 @staticmethod108 def intersection(properties: Iterable[Property], access: Access) -> Property:109 return Property._set_action(properties, access, set.intersection)110 def intersection_update(self, property: Property, access: Access):111 for access in access.matches:112 self._items[access].intersection_update(property._items[access])113 @staticmethod114 def difference(properties: Iterable[Property], access: Access) -> Property:115 return Property._set_action(properties, access, set.difference)116 def difference_update(self, property: Property, access: Access):117 for access in access.matches:118 self._items[access].difference_update(property._items[access])119 def get_all_items(self) -> Set[str]:120 return set.union(*(self._items[x] for x in Access.PUBLIC.matches))121 def get_items(self, access: Access) -> Set[str]:122 return set.intersection(*(self._items[x] for x in access.matches))123 def set_items(self, items: Set[str], access: Access):124 for access in access.matches:125 self._items[access] = items126 def add_item(self, item: str, access: Access):127 for access in access.matches:128 self._items[access].add(item)129 def add_items(self, items: Set[str], access: Access):130 for access in access.matches:131 self._items[access].update(items)132 def remove_item(self, item: str, access: Access):133 for access in access.matches:134 if item in self._items[access]:135 self._items[access].remove(item)136 def remove_items(self, items: Set[str], access: Access):137 for access in access.matches:138 self._items[access].difference_update(items)139class PropertyTestCase(TestCase):140 def setUp(self):141 self.example = Property.from_json({142 "public": ["A", "B"],143 "private": ["C", "D", "E"],144 "interface": ["F", "E"]145 })146 self.assertSetEqual(147 self.example.get_items(Access.PUBLIC),148 {"A", "B", "E"}149 )150 self.assertSetEqual(151 self.example.get_items(Access.PRIVATE),152 {"A", "B", "C", "D", "E"}153 )154 self.assertSetEqual(155 self.example.get_items(Access.INTERFACE),156 {"A", "B", "E", "F"}157 )158 self.assertSetEqual(159 self.example.get_all_items(),160 {"A", "B", "C", "D", "E", "F"}161 )162 def test_json_invalid_property(self):163 self.assertRaises(164 ValidationError,165 lambda: Property.from_json({"publi": []})166 )167 def test_example_json(self):168 expected_json = {169 "public": ["A", "B", "E"],170 "private": ["C", "D"],171 "interface": ["F"]172 }173 self.assertEqual(174 self.example.to_json(),175 expected_json176 )177 def test_private_json_only(self):178 example_private = Property.from_json({179 "private": ["A", "B"]180 })181 self.assertSetEqual(182 example_private.get_items(Access.PUBLIC),183 set()184 )185 self.assertSetEqual(186 example_private.get_items(Access.PRIVATE),187 {"A", "B"}188 )189 self.assertSetEqual(190 example_private.get_items(Access.INTERFACE),191 set()192 )193 def test_inline_items(self):194 props = Property(public={"A", "B"},195 private={"C", "D", "E"},196 interface={"F", "E"})197 for access in Access:198 self.assertSetEqual(199 props.get_items(access),200 self.example.get_items(access)201 )202 def test_set_items(self):203 self.example.set_items({"P1", "P2", "P3"}, Access.PRIVATE)204 self.example.set_items({"P1", "P2", "I1", "I2"}, Access.INTERFACE)205 self.assertSetEqual(206 self.example.get_items(Access.PUBLIC),207 {"P1", "P2"}208 )209 self.assertSetEqual(210 self.example.get_items(Access.PRIVATE),211 {"P1", "P2", "P3"}212 )213 self.assertSetEqual(214 self.example.get_items(Access.INTERFACE),215 {"P1", "P2", "I1", "I2"}216 )217 def test_add_item(self):218 self.example.add_item("P3", Access.PUBLIC)219 self.example.add_item("P4", Access.PRIVATE)220 self.example.add_item("I3", Access.INTERFACE)221 self.assertSetEqual(222 self.example.get_items(Access.PUBLIC),223 {"A", "B", "E", "P3"}224 )225 self.assertSetEqual(226 self.example.get_items(Access.PRIVATE),227 {"A", "B", "C", "D", "E", "P3", "P4"}228 )229 self.assertSetEqual(230 self.example.get_items(Access.INTERFACE),231 {"A", "B", "F", "E", "I3", "P3"}232 )233 def test_add_items(self):234 self.example.add_items({"B", "P4"}, Access.PUBLIC)235 self.example.add_items(set(), Access.PRIVATE)236 self.example.add_items({"I1", "I2"}, Access.INTERFACE)237 self.assertSetEqual(238 self.example.get_items(Access.PUBLIC),239 {"A", "B", "E", "P4"}240 )241 self.assertSetEqual(242 self.example.get_items(Access.PRIVATE),243 {"A", "B", "E", "P4", "C", "D"}244 )245 self.assertSetEqual(246 self.example.get_items(Access.INTERFACE),247 {"A", "B", "E", "P4", "F", "I1", "I2"}248 )249 def test_remove_item(self):250 self.example.remove_item("F", Access.PUBLIC)251 self.example.remove_item("C", Access.PRIVATE)252 self.example.remove_item("E", Access.INTERFACE)253 self.assertSetEqual(254 self.example.get_items(Access.PUBLIC),255 {"A", "B"}256 )257 self.assertSetEqual(258 self.example.get_items(Access.PRIVATE),259 {"A", "B", "D", "E"}260 )261 self.assertSetEqual(262 self.example.get_items(Access.INTERFACE),263 {"A", "B"}264 )265 def test_remove_items(self):266 self.example.remove_items(set(["A", "B"]), Access.PUBLIC)267 self.example.remove_items(set(), Access.PRIVATE)268 self.example.remove_items(set(["Z", "Y"]), Access.INTERFACE)269 self.assertSetEqual(270 self.example.get_items(Access.PUBLIC),271 {"E"}272 )273 self.assertSetEqual(274 self.example.get_items(Access.PRIVATE),275 {"C", "D", "E"}276 )277 self.assertSetEqual(278 self.example.get_items(Access.INTERFACE),279 {"E", "F"}280 )281 def test_union(self):282 prop1 = Property.from_json({283 "public": ["A", "B", "C"],284 "private": ["D", "E"],285 "interface": ["E", "F"]286 })287 prop2 = Property.from_json({288 "public": ["D"],289 "private": ["G", "H"],290 "interface": ["E", "F"]291 })292 union = Property.union([prop1, prop2], Access.PUBLIC)293 self.assertSetEqual(294 union.get_items(Access.PUBLIC),295 {"A", "B", "C", "D", "E"}296 )297 self.assertSetEqual(298 union.get_items(Access.PRIVATE),299 {"A", "B", "C", "D", "E", "G", "H"}300 )301 self.assertSetEqual(302 union.get_items(Access.INTERFACE),303 {"A", "B", "C", "D", "E", "F"}304 )305 def test_intersection(self):306 prop1 = Property.from_json({307 "public": ["A", "B", "C"],308 "private": ["PA", "PB"],309 "interface": ["I1", "I2"]310 })311 prop2 = Property.from_json({312 "public": ["D"],313 "private": ["PA", "PC"],314 "interface": ["I1", "I2"]315 })316 intersection = Property.intersection([prop1, prop2], Access.PUBLIC)317 self.assertSetEqual(318 intersection.get_items(Access.PUBLIC),319 set()320 )321 self.assertSetEqual(322 intersection.get_items(Access.PRIVATE),323 {"PA"}324 )325 self.assertSetEqual(326 intersection.get_items(Access.INTERFACE),327 {"I1", "I2"}328 )329 def test_union_update(self):330 prop = Property.from_json({331 "public": ["C"],332 "private": ["B", "C", "D"],333 "interface": ["Z"]334 })335 self.example.union_update(prop, Access.PUBLIC)336 self.assertSetEqual(337 self.example.get_items(Access.PUBLIC),338 {"A", "B", "C", "E"}339 )340 self.assertSetEqual(341 self.example.get_items(Access.PRIVATE),342 {"A", "B", "C", "D", "E"}343 )344 self.assertSetEqual(345 self.example.get_items(Access.INTERFACE),346 {"A", "B", "C", "F", "E", "Z"}347 )348 def test_intersection_update(self):349 prop = Property.from_json({350 "public": ["C"],351 "private": ["B", "C", "D"],352 "interface": ["Z"]353 })354 self.example.intersection_update(prop, Access.PUBLIC)355 self.assertSetEqual(356 self.example.get_items(Access.PUBLIC),357 set()358 )359 self.assertSetEqual(360 self.example.get_items(Access.PRIVATE),361 {"C", "B", "D"}362 )363 self.assertSetEqual(364 self.example.get_items(Access.INTERFACE),365 set()366 )367 def test_difference_update(self):368 prop = Property.from_json({369 "public": ["C"],370 "private": ["B", "C", "D"],371 "interface": ["Z"]372 })373 self.example.difference_update(prop, Access.PUBLIC)374 self.assertSetEqual(375 self.example.get_items(Access.PUBLIC),376 {"A", "E"}377 )378 self.assertSetEqual(379 self.example.get_items(Access.PRIVATE),380 {"A", "E"}381 )382 self.assertSetEqual(383 self.example.get_items(Access.INTERFACE),384 {"A", "B", "F", "E"}...
test_libraries_index.py
Source:test_libraries_index.py
...28 result1 = self._create_library(slug="test-lib-index-1", title="Title 1", description="Description")29 result2 = self._create_library(slug="test-lib-index-2", title="Title 2", description="Description")30 for result in [result1, result2]:31 library_key = LibraryLocatorV2.from_string(result['id'])32 response = ContentLibraryIndexer.get_items([library_key])[0]33 self.assertEqual(response['id'], result['id'])34 self.assertEqual(response['title'], result['title'])35 self.assertEqual(response['description'], result['description'])36 self.assertEqual(response['uuid'], result['bundle_uuid'])37 self.assertEqual(response['num_blocks'], 0)38 self.assertEqual(response['version'], result['version'])39 self.assertEqual(response['last_published'], None)40 self.assertEqual(response['has_unpublished_changes'], False)41 self.assertEqual(response['has_unpublished_deletes'], False)42 def test_schema_updates(self):43 """44 Test that outdated indexes aren't retrieved45 """46 with patch("openedx.core.djangoapps.content_libraries.libraries_index.ContentLibraryIndexer.SCHEMA_VERSION",47 new=0):48 result = self._create_library(slug="test-lib-schemaupdates-1", title="Title 1", description="Description")49 library_key = LibraryLocatorV2.from_string(result['id'])50 self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 1)51 with patch("openedx.core.djangoapps.content_libraries.libraries_index.ContentLibraryIndexer.SCHEMA_VERSION",52 new=1):53 self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 0)54 call_command("reindex_content_library", all=True, force=True)55 self.assertEqual(len(ContentLibraryIndexer.get_items([library_key])), 1)56 def test_remove_all_libraries(self):57 """58 Test if remove_all_items() deletes all libraries59 """60 lib1 = self._create_library(slug="test-lib-rm-all-1", title="Title 1", description="Description")61 lib2 = self._create_library(slug="test-lib-rm-all-2", title="Title 2", description="Description")62 library_key1 = LibraryLocatorV2.from_string(lib1['id'])63 library_key2 = LibraryLocatorV2.from_string(lib2['id'])64 self.assertEqual(len(ContentLibraryIndexer.get_items([library_key1, library_key2])), 2)65 ContentLibraryIndexer.remove_all_items()66 self.assertEqual(len(ContentLibraryIndexer.get_items()), 0)67 def test_update_libraries(self):68 """69 Test if indexes are updated when libraries are updated70 """71 lib = self._create_library(slug="test-lib-update", title="Title", description="Description")72 library_key = LibraryLocatorV2.from_string(lib['id'])73 self._update_library(lib['id'], title="New Title", description="New Title")74 response = ContentLibraryIndexer.get_items([library_key])[0]75 self.assertEqual(response['id'], lib['id'])76 self.assertEqual(response['title'], "New Title")77 self.assertEqual(response['description'], "New Title")78 self.assertEqual(response['uuid'], lib['bundle_uuid'])79 self.assertEqual(response['num_blocks'], 0)80 self.assertEqual(response['version'], lib['version'])81 self.assertEqual(response['last_published'], None)82 self.assertEqual(response['has_unpublished_changes'], False)83 self.assertEqual(response['has_unpublished_deletes'], False)84 self._delete_library(lib['id'])85 self.assertEqual(ContentLibraryIndexer.get_items([library_key]), [])86 ContentLibraryIndexer.get_items([library_key])87 def test_update_library_blocks(self):88 """89 Test if indexes are updated when blocks in libraries are updated90 """91 def commit_library_and_verify(library_key):92 """93 Commit library changes, and verify that there are no uncommited changes anymore94 """95 last_published = ContentLibraryIndexer.get_items([library_key])[0]['last_published']96 self._commit_library_changes(str(library_key))97 response = ContentLibraryIndexer.get_items([library_key])[0]98 self.assertEqual(response['has_unpublished_changes'], False)99 self.assertEqual(response['has_unpublished_deletes'], False)100 self.assertGreaterEqual(response['last_published'], last_published)101 return response102 def verify_uncommitted_libraries(library_key, has_unpublished_changes, has_unpublished_deletes):103 """104 Verify uncommitted changes and deletes in the index105 """106 response = ContentLibraryIndexer.get_items([library_key])[0]107 self.assertEqual(response['has_unpublished_changes'], has_unpublished_changes)108 self.assertEqual(response['has_unpublished_deletes'], has_unpublished_deletes)109 return response110 lib = self._create_library(slug="test-lib-update-block", title="Title", description="Description")111 library_key = LibraryLocatorV2.from_string(lib['id'])112 # Verify uncommitted new blocks113 block = self._add_block_to_library(lib['id'], "problem", "problem1")114 response = verify_uncommitted_libraries(library_key, True, False)115 self.assertEqual(response['last_published'], None)116 self.assertEqual(response['num_blocks'], 1)117 # Verify committed new blocks118 self._commit_library_changes(lib['id'])119 response = verify_uncommitted_libraries(library_key, False, False)120 self.assertEqual(response['num_blocks'], 1)121 # Verify uncommitted deleted blocks122 self._delete_library_block(block['id'])123 response = verify_uncommitted_libraries(library_key, True, True)124 self.assertEqual(response['num_blocks'], 0)125 # Verify committed deleted blocks126 self._commit_library_changes(lib['id'])127 response = verify_uncommitted_libraries(library_key, False, False)128 self.assertEqual(response['num_blocks'], 0)129 block = self._add_block_to_library(lib['id'], "problem", "problem1")130 self._commit_library_changes(lib['id'])131 # Verify changes to blocks132 # Verify OLX updates on blocks133 self._set_library_block_olx(block["id"], "<problem/>")134 verify_uncommitted_libraries(library_key, True, False)135 commit_library_and_verify(library_key)136 # Verify asset updates on blocks137 self._set_library_block_asset(block["id"], "whatever.png", b"data")138 verify_uncommitted_libraries(library_key, True, False)139 commit_library_and_verify(library_key)140 self._delete_library_block_asset(block["id"], "whatever.png", expect_response=204)141 verify_uncommitted_libraries(library_key, True, False)142 commit_library_and_verify(library_key)143 lib2 = self._create_library(slug="test-lib-update-block-2", title="Title 2", description="Description")144 self._add_block_to_library(lib2["id"], "problem", "problem1")145 self._commit_library_changes(lib2["id"])146 #Verify new links on libraries147 self._link_to_library(lib["id"], "library_2", lib2["id"])148 verify_uncommitted_libraries(library_key, True, False)149 #Verify reverting uncommitted changes150 self._revert_library_changes(lib["id"])151 verify_uncommitted_libraries(library_key, False, False)152@override_settings(FEATURES={**settings.FEATURES, 'ENABLE_CONTENT_LIBRARY_INDEX': True})153@elasticsearch_test154class LibraryBlockIndexerTest(ContentLibrariesRestApiTest):155 """156 Tests the operation of LibraryBlockIndexer157 """158 @elasticsearch_test159 def setUp(self):160 super().setUp()161 ContentLibraryIndexer.remove_all_items()162 LibraryBlockIndexer.remove_all_items()163 self.searcher = SearchEngine.get_search_engine(LibraryBlockIndexer.INDEX_NAME)164 def test_index_block(self):165 """166 Test if libraries are being indexed correctly167 """168 lib = self._create_library(slug="test-lib-index-1", title="Title 1", description="Description")169 block1 = self._add_block_to_library(lib['id'], "problem", "problem1")170 block2 = self._add_block_to_library(lib['id'], "problem", "problem2")171 self.assertEqual(len(LibraryBlockIndexer.get_items()), 2)172 for block in [block1, block2]:173 usage_key = LibraryUsageLocatorV2.from_string(block['id'])174 response = LibraryBlockIndexer.get_items([usage_key])[0]175 self.assertEqual(response['id'], block['id'])176 self.assertEqual(response['def_key'], block['def_key'])177 self.assertEqual(response['block_type'], block['block_type'])178 self.assertEqual(response['display_name'], block['display_name'])179 self.assertEqual(response['has_unpublished_changes'], block['has_unpublished_changes'])180 def test_schema_updates(self):181 """182 Test that outdated indexes aren't retrieved183 """184 lib = self._create_library(slug="test-lib--block-schemaupdates-1", title="Title 1", description="Description")185 with patch("openedx.core.djangoapps.content_libraries.libraries_index.LibraryBlockIndexer.SCHEMA_VERSION",186 new=0):187 block = self._add_block_to_library(lib['id'], "problem", "problem1")188 self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 1)189 with patch("openedx.core.djangoapps.content_libraries.libraries_index.LibraryBlockIndexer.SCHEMA_VERSION",190 new=1):191 self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 0)192 call_command("reindex_content_library", all=True, force=True)193 self.assertEqual(len(LibraryBlockIndexer.get_items([block['id']])), 1)194 def test_remove_all_items(self):195 """196 Test if remove_all_items() deletes all libraries197 """198 lib1 = self._create_library(slug="test-lib-rm-all", title="Title 1", description="Description")199 self._add_block_to_library(lib1['id'], "problem", "problem1")200 self._add_block_to_library(lib1['id'], "problem", "problem2")201 self.assertEqual(len(LibraryBlockIndexer.get_items()), 2)202 LibraryBlockIndexer.remove_all_items()203 self.assertEqual(len(LibraryBlockIndexer.get_items()), 0)204 def test_crud_block(self):205 """206 Test that CRUD operations on blocks are reflected in the index207 """208 lib = self._create_library(slug="test-lib-crud-block", title="Title", description="Description")209 block = self._add_block_to_library(lib['id'], "problem", "problem1")210 # Update OLX, verify updates in index211 self._set_library_block_olx(block["id"], '<problem display_name="new_name"/>')212 response = LibraryBlockIndexer.get_items([block['id']])[0]213 self.assertEqual(response['display_name'], "new_name")214 self.assertEqual(response['has_unpublished_changes'], True)215 # Verify has_unpublished_changes after committing library216 self._commit_library_changes(lib['id'])217 response = LibraryBlockIndexer.get_items([block['id']])[0]218 self.assertEqual(response['has_unpublished_changes'], False)219 # Verify has_unpublished_changes after reverting library220 self._set_library_block_asset(block["id"], "whatever.png", b"data")221 response = LibraryBlockIndexer.get_items([block['id']])[0]222 self.assertEqual(response['has_unpublished_changes'], True)223 self._revert_library_changes(lib['id'])224 response = LibraryBlockIndexer.get_items([block['id']])[0]225 self.assertEqual(response['has_unpublished_changes'], False)226 # Verify that deleting block removes it from index227 self._delete_library_block(block['id'])228 self.assertEqual(LibraryBlockIndexer.get_items([block['id']]), [])229 # Verify that deleting a library removes its blocks from index too230 self._add_block_to_library(lib['id'], "problem", "problem1")231 LibraryBlockIndexer.get_items([block['id']])232 self._delete_library(lib['id'])...
pizza.py
Source:pizza.py
...38 self.__list_ing['name'] = self.title39 self.__list_ing['weight'] = self.weight40 self.__list_ing['calor'] = self.weight / 100 * self.calorific # ÐалоÑийноÑÑÑ Ð¸Ð½Ð³ÑедиенÑа: веÑ_ингÑедиенÑа / 100 * калоÑийноÑÑÑ_пÑодÑкÑа 41 self.__list_ing['price'] = self.weight / 100 * self.cost # СебеÑÑоимоÑÑÑ: веÑ_ингÑедиенÑа / 100 * ÑебеÑÑоимоÑÑÑ_пÑодÑкÑа42 def get_items(self):43 return self.__list_ing 44 def __str__(self): 45 return f'Ðазвание: {self.name}\t веÑ: {self.weight}\t калоÑий: {toFixed(self.calor, 0)}\t ÑÑоимоÑÑÑ: {toFixed(self.price, 2)}'46 47class Pizza(): # пиÑÑа...48 49 def __init__(self, title, ingredients=[]): # ingredients - ингÑедиенÑÑ. СпиÑок знаÑений клаÑÑа Ingredient.50 if Product.value_empty(title):51 self.title = title # название пиÑÑ. ÐбÑзаÑелÑнÑй аÑÑибÑÑ. Ðе Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð¿ÑÑÑÑм52 self.__ingred = ingredients53 else:54 raise ValueError55 self.__title = title56 self.__calor = 0.057 self.__price = 0.058 self.__ingred = ingredients59 self.calc()60 @staticmethod61 def value_empty(zstr):62 return zstr != ''63 def calc(self):64 for i in self.__ingred:65 self.__calor = self.__calor + i['calor']66 self.__price = self.__price + i['price']67 68 def get_sostav(self):69 self.__sostav = ''70 self.__sostav = 'СоÑÑав: '71 for i in self.__ingred:72 self.__sostav = self.__sostav + i['name'] + '; '73 return self.__sostav74 def __str__(self):75 return f'{self.__title} ({toFixed(self.__calor, 1)} kkal) - {toFixed(self.__price, 2)} ÑÑб.'76os.system('cls')77list_pizza_1 = []78list_pizza_2 = []79list_pizza_3 = []80# калоÑии - ккал, Ñена - ÑÑблÑÑ
, Ð²ÐµÑ - гÑаммÑ81list_pizza_1.append(Ingredient('Ð¡Ð¾Ð»Ñ Ð¿Ð¾Ð²Ð°ÑÐµÐ½Ð½Ð°Ñ Ð¿Ð¸ÑеваÑ', 0, 2, 6).get_items())82list_pizza_1.append(Ingredient('ÐÑка пÑениÑнаÑ', 342, 5, 200).get_items())83list_pizza_1.append(Ingredient('Ðода', 0, 0, 70).get_items())84list_pizza_1.append(Ingredient('ÐÑожжи СаÑ-ÐоменÑ', 370, 12, 20).get_items())85list_pizza_1.append(Ingredient('ÐаÑло подÑолнеÑное Ñ Ð¾Ð»Ð¸Ð²ÐºÐ¾Ð²Ñм', 899, 11, 50).get_items())86list_pizza_1.append(Ingredient('СÑÑ ÐаÑда', 356, 48, 100).get_items())87list_pizza_1.append(Ingredient('ТомаÑÐ½Ð°Ñ Ð¿Ð°ÑÑа', 54, 52, 80).get_items())88list_pizza_1.append(Ingredient('ÐеÑÐµÑ Ð¼Ð¾Ð»Ð¾ÑÑй', 263, 40, 1).get_items())89list_pizza_1.append(Ingredient('ÐолбаÑа ÑалÑми', 345, 115, 150).get_items())90list_pizza_2.append(Ingredient('Ð¡Ð¾Ð»Ñ Ð¿Ð¾Ð²Ð°ÑÐµÐ½Ð½Ð°Ñ Ð¿Ð¸ÑеваÑ', 0, 2, 6).get_items())91list_pizza_2.append(Ingredient('ÐÑка пÑениÑнаÑ', 342, 5, 200).get_items())92list_pizza_2.append(Ingredient('Ðода', 0, 0, 70).get_items())93list_pizza_2.append(Ingredient('ÐÑожжи СаÑ-ÐоменÑ', 370, 12, 20).get_items())94list_pizza_2.append(Ingredient('ÐаÑло подÑолнеÑное Ñ Ð¾Ð»Ð¸Ð²ÐºÐ¾Ð²Ñм', 899, 11, 50).get_items())95list_pizza_2.append(Ingredient('СÑÑ ÐаÑда', 356, 48, 100).get_items())96list_pizza_2.append(Ingredient('ТомаÑÐ½Ð°Ñ Ð¿Ð°ÑÑа', 54, 52, 80).get_items())97list_pizza_2.append(Ingredient('ÐеÑÐµÑ Ð¼Ð¾Ð»Ð¾ÑÑй', 263, 40, 1).get_items())98list_pizza_2.append(Ingredient('ÐеÑÑина', 279, 130, 170).get_items())99list_pizza_3.append(Ingredient('Ð¡Ð¾Ð»Ñ Ð¿Ð¾Ð²Ð°ÑÐµÐ½Ð½Ð°Ñ Ð¿Ð¸ÑеваÑ', 0, 2, 6).get_items())100list_pizza_3.append(Ingredient('ÐÑка пÑениÑнаÑ', 342, 5, 200).get_items())101list_pizza_3.append(Ingredient('Ðода', 0, 0, 70).get_items())102list_pizza_3.append(Ingredient('ÐÑожжи СаÑ-ÐоменÑ', 370, 12, 20).get_items())103list_pizza_3.append(Ingredient('ÐаÑло подÑолнеÑное Ñ Ð¾Ð»Ð¸Ð²ÐºÐ¾Ð²Ñм', 899, 11, 50).get_items())104list_pizza_3.append(Ingredient('СÑÑ ÐаÑда', 356, 48, 100).get_items())105list_pizza_3.append(Ingredient('ТомаÑÐ½Ð°Ñ Ð¿Ð°ÑÑа', 54, 52, 80).get_items())106list_pizza_3.append(Ingredient('ÐеÑÐµÑ Ð¼Ð¾Ð»Ð¾ÑÑй', 263, 40, 1).get_items())107list_pizza_3.append(Ingredient('Ðливки', 296, 72, 100).get_items())108print(Pizza('ÐиÑÑа Ñ ÐºÐ¾Ð»Ð±Ð°Ñой', list_pizza_1))109print(Pizza('ÐиÑÑа Ñ ÐºÐ¾Ð»Ð±Ð°Ñой', list_pizza_1).get_sostav())110print()111print(Pizza('ÐиÑÑа Ñ Ð²ÐµÑÑиной', list_pizza_2))112print(Pizza('ÐиÑÑа Ñ ÐºÐ¾Ð»Ð±Ð°Ñой', list_pizza_2).get_sostav())113print()114print(Pizza('ÐиÑÑа оливками', list_pizza_3))115print(Pizza('ÐиÑÑа Ñ ÐºÐ¾Ð»Ð±Ð°Ñой', list_pizza_3).get_sostav())...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!