Commits

Ronny Pfannschmidt  committed 83346dd

add support for downloaded externals

  • Participants
  • Parent commits b822b12

Comments (0)

Files changed (3)

File couchdb_compose/composer.py

 
     def add_attachment(self, name, content):
         attachments = self.doc.setdefault('_attachments', {})
-        attachments[name] = {
+        info = attachments[name] = {
             'data': content,
             'content_type': mimetypes.guess_type(name)[0],
         }
+        return info
 
 
 

File couchdb_compose/externals.py

 
 
 def add_external(composer, external, path):
-    pass
+    filename = external['url'].split('/')[-1]
+    attachment = external.get('attachment')
+    content = path.join(filename).read()
+    if attachment:
+        info = composer.add_attachment(attachment, content)
+        info.update(
+            url = external.get('url'),
+            name = external.get('name'),
+        )
+    else:
+        composer.push(
+            external.get('path', [external['name']]),
+            content)
 
 
 def include_externals(composer):

File testing/test_externals.py

 import mock
 import restkit
 from couchdb_compose import externals
+from couchdb_compose import composer
+
+UNDERSCORE = {
+    'name': 'underscore',
+    'url': 'http://underscorejs.org/underscore.js',
+}
+
 
 def test_make_cache(tmpdir):
     cache = externals.make_cache(tmpdir)
 
 
 def test_download_external(tmpdir, monkeypatch):
-    underscore = {
-        'name': 'underscore',
-        'url': 'http://underscorejs.org/underscore.js',
-    }
 
-    externals.maybe_download_external(underscore, tmpdir)
+    externals.maybe_download_external(UNDERSCORE, tmpdir)
     assert tmpdir.join('underscore.js').check()
     assert tmpdir.join('.completed').check()
 
                         'request',
                         mock.Mock(side_effect=RuntimeError()))
 
-    externals.maybe_download_external(underscore, tmpdir)
-    
+    externals.maybe_download_external(UNDERSCORE, tmpdir)
+
+
+def test_add_external_commonjs(tmpdir):
+    tmpdir.join('underscore.js').write('test')
+
+    comp = composer.Composer(tmpdir, {})
+    assert 'underscore' not in comp.doc
+
+    externals.add_external(comp, UNDERSCORE, tmpdir)
+    assert 'underscore' in comp.doc
+    assert '_attachments' not in comp.doc
+    underscore_attachment = dict(UNDERSCORE, attachment='js/underscore.js')
+
+    del comp.doc['underscore']
+    externals.add_external(comp, underscore_attachment, tmpdir)
+    assert 'underscore' not in comp.doc
+    assert 'js/underscore.js' in comp.doc['_attachments']
+ 
+    underscore_withpath = dict(UNDERSCORE, path= ['foo', 'bar'])
+   
+    del comp.doc['_attachments']
+
+    externals.add_external(comp, underscore_withpath, tmpdir)
+    assert 'underscore' not in comp.doc
+    assert comp.doc['foo']['bar'] == 'test'