| 1 |  |  | # pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from freezegun import freeze_time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from . import demo, other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | def describe_text(): | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     def it_includes_the_caller_location(expect, caplog): | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         demo.greet("caller") | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         expect(caplog.text) == "ERROR    tests.demo:demo.py:5 Hello, caller!\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     @pytest.mark.last | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     @freeze_time('2019-01-15') | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     def it_can_be_formatted_with_init(expect, caplog): | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         log.init( | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             level=log.WARNING, | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             format='%(asctime)s %(relpath)s:%(lineno)s: %(message)s', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             datefmt='%Y-%m', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         ) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         demo.greet("format") | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         if os.name == 'nt': | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             expect(caplog.text) == "2019-01 tests\\demo.py:5: Hello, format!\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         else: | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             expect(caplog.text) == "2019-01 tests/demo.py:5: Hello, format!\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     def it_can_include_exceptions(expect, caplog): | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         try: | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             print(1 / 0) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         except ZeroDivisionError: | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             log.exception("exception") | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         expect(caplog.text).contains('Traceback ') | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         expect(caplog.text).contains('test_records.py", line 37, ') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         expect(caplog.text).contains('ZeroDivisionError') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | def describe_silence(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def when_off(expect, caplog): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         log.silence('3rd-party') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         other.do_3rd_party_thing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         expect(caplog.records) == [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def with_errors(expect, caplog): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         log.silence('3rd-party', allow_error=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         other.do_3rd_party_thing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         expect(len(caplog.records)) == 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     def with_warnings(expect, caplog): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         log.silence('3rd-party', allow_warning=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         other.do_3rd_party_thing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         expect(len(caplog.records)) == 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def with_infos(expect, caplog): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         log.silence('3rd-party', allow_info=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         other.do_3rd_party_thing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |         expect(len(caplog.records)) == 3 | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |