# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import fixtures
import textwrap

import mock

from reno import cache
from reno import config
from reno.tests import base


class TestCache(base.TestCase):

    scanner_output = {
        '0.0.0': [('note1', 'shaA')],
        '1.0.0': [('note2', 'shaB'), ('note3', 'shaC')],
    }

    note_bodies = {
        'note1': textwrap.dedent("""
        prelude: >
          This is the prelude.
        """),
        'note2': textwrap.dedent("""
        issues:
          - This is the first issue.
          - This is the second issue.
        """),
        'note3': textwrap.dedent("""
        features:
          - We added a feature!
        """)
    }

    def _get_note_body(self, filename, sha):
        return self.note_bodies.get(filename, '')

    def setUp(self):
        super(TestCache, self).setUp()
        self.useFixture(
            fixtures.MockPatch('reno.scanner.Scanner.get_file_at_commit',
                               new=self._get_note_body)
        )
        self.c = config.Config('.')

    def test_build_cache_db(self):
        with mock.patch('reno.scanner.Scanner.get_notes_by_version') as gnbv:
            gnbv.return_value = self.scanner_output
            db = cache.build_cache_db(
                self.c,
                versions_to_include=[],
            )
            expected = {
                'notes': [
                    {'version': k, 'files': v}
                    for k, v in self.scanner_output.items()
                ],
                'file-contents': {
                    'note1': {
                        'prelude': 'This is the prelude.\n',
                    },
                    'note2': {
                        'issues': [
                            'This is the first issue.',
                            'This is the second issue.',
                        ],
                    },
                    'note3': {
                        'features': ['We added a feature!'],
                    },
                },
            }
            self.assertEqual(expected, db)

@@ -1581,52 +1581,9 @@
     
-def test_build_cache_db(self):%0A        with 
+@
 mock
@@ -1637,22 +1637,50 @@
 on')
- as
+%0A    def test_build_cache_db(self,
  gnbv
+)
 :%0A
-    
     
@@ -1727,129 +1727,8 @@
 put%0A
-            db = cache.build_cache_db(%0A                self.c,%0A                versions_to_include=%5B%5D,%0A            )%0A    
     
@@ -1752,28 +1752,24 @@
             
-    
 'notes': %5B%0A 
@@ -1763,28 +1763,24 @@
  'notes': %5B%0A
-    
             
@@ -1826,20 +1826,16 @@
         
-   
- 
 for k, v
@@ -1874,35 +1874,27 @@
             
-    %5D,%0A    
+%5D,%0A
             
@@ -1924,28 +1924,24 @@
             
-  
-  
 'note1': %7B%0A 
@@ -1935,28 +1935,24 @@
  'note1': %7B%0A
-    
             
@@ -2004,36 +2004,32 @@
                 
-    
 %7D,%0A             
@@ -2027,28 +2027,24 @@
             
- 
-   
 'note2': %7B%0A 
@@ -2038,28 +2038,24 @@
  'note2': %7B%0A
-    
             
@@ -2070,28 +2070,24 @@
 'issues': %5B%0A
-    
             
@@ -2142,36 +2142,32 @@
                 
-    
 'This is the sec
@@ -2195,34 +2195,27 @@
             
-    %5D,%0A   
+%5D,%0A
             
@@ -2214,32 +2214,27 @@
             
- 
 %7D,%0A
-    
             
@@ -2244,28 +2244,24 @@
  'note3': %7B%0A
-    
             
@@ -2305,32 +2305,43 @@
 ure!'%5D,%0A        
+        %7D,%0A
             %7D,%0A 
@@ -2343,32 +2343,35 @@
  %7D,%0A        
+%7D%0A%0A
         
 %7D,%0A         
@@ -2362,29 +2362,102 @@
     
-%7D,%0A            %7D%0A    
+db = cache.build_cache_db(%0A            self.c,%0A            versions_to_include=%5B%5D,%0A        )%0A%0A
     

Use mock decorator instead of context manager