| 
									
										
										
										
											2021-10-23 17:10:41 +05:30
										 |  |  | from collections.abc import Iterable, Mapping | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:22 -05:00
										 |  |  | from uuid import UUID | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 14:47:31 +05:30
										 |  |  | from sure import assertion | 
					
						
							| 
									
										
										
										
											2013-07-27 16:24:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 14:47:31 +05:30
										 |  |  | @assertion | 
					
						
							|  |  |  | def containing_item_with_attributes(context, **kwargs): | 
					
						
							|  |  |  |     contains = False | 
					
						
							|  |  |  |     if kwargs and isinstance(context.obj, Iterable): | 
					
						
							|  |  |  |         for item in context.obj: | 
					
						
							|  |  |  |             if not isinstance(item, dict): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             for k, v in kwargs.items(): | 
					
						
							|  |  |  |                 if k not in item or item[k] != v: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 contains = True | 
					
						
							|  |  |  |     if context.negative: | 
					
						
							|  |  |  |         assert not contains, f"{context.obj} contains matching item {kwargs}" | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         assert contains, f"{context.obj} does not contain matching item {kwargs}" | 
					
						
							|  |  |  |     return True | 
					
						
							| 
									
										
										
										
											2021-10-23 17:10:41 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @assertion | 
					
						
							|  |  |  | def match_dict(context, dict_value): | 
					
						
							|  |  |  |     assert isinstance(dict_value, Mapping), f"Invalid match target value: {dict_value}" | 
					
						
							|  |  |  |     assert isinstance( | 
					
						
							|  |  |  |         context.obj, Mapping | 
					
						
							|  |  |  |     ), f"Expected dict like object, but got: {context.obj}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for k, v in dict_value.items(): | 
					
						
							|  |  |  |         assert k in context.obj, f"No such key '{k}' in {context.obj}" | 
					
						
							|  |  |  |         context.obj[k].should.equal(v) | 
					
						
							|  |  |  |     return True | 
					
						
							| 
									
										
										
										
											2022-02-11 15:11:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @assertion | 
					
						
							|  |  |  | def match_uuid4(context): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         uuid_obj = UUID(context.obj, version=4) | 
					
						
							|  |  |  |     except ValueError: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     return str(uuid_obj) == context.obj |