var seqCount = 0; var usedIds = {}; var makeUnique = { 'index.html': true, 'style.css': true, 'script.js': true, 'unit.js': true, 'spec.js': true, 'scenario.js': true } function ids(list) { return list.map(function(item) { return item.id; }).join(' '); }; exports.Example = function(scenarios) { this.module = ''; this.deps = ['angular.js']; this.html = []; this.css = []; this.js = []; this.unit = []; this.scenario = []; this.scenarios = scenarios; } exports.Example.prototype.setModule = function(module) { if (module) { this.module = module; } }; exports.Example.prototype.addDeps = function(deps) { deps && deps.split(/[\s\,]/).forEach(function(dep) { if (dep) { this.deps.push(dep); } }, this); }; exports.Example.prototype.addSource = function(name, content) { var ext = name == 'scenario.js' ? 'scenario' : name.split('.')[1], id = name; if (makeUnique[name] && usedIds[id]) { id = name + '-' + (seqCount++); } usedIds[id] = true; this[ext].push({name: name, content: content, id: id}); if (name.match(/\.js$/) && name !== 'spec.js' && name !== 'unit.js' && name != 'scenario.js') { this.deps.push(name); } if (ext == 'scenario') { this.scenarios.push(content); } }; exports.Example.prototype.toHtml = function() { return '

Source

\n' + this.toHtmlEdit() + this.toHtmlTabs() + '

Demo

\n' + this.toHtmlEmbed(); }; exports.Example.prototype.toHtmlEdit = function() { var out = []; out.push('
\n'); return out.join(''); }; exports.Example.prototype.toHtmlTabs = function() { var out = [], self = this; out.push('
'); htmlTabs(this.html); htmlTabs(this.css); htmlTabs(this.js); htmlTabs(this.unit); htmlTabs(this.scenario); out.push('
'); return out.join(''); function htmlTabs(sources) { sources.forEach(function(source) { var wrap = '', isCss = source.name.match(/\.css$/), name = source.name; if (name === 'index.html') { wrap = ' ng-html-wrap="' + self.module + ' ' + self.deps.join(' ') + '"'; } if (name == 'scenario.js') name = 'End to end test'; out.push( '
\n' + '
\n' +
          (isCss
             ? ('\n')
             : ('\n') ) +
        '
\n'); }); } }; exports.Example.prototype.toHtmlEmbed = function() { var out = []; out.push('
'); out.push('
'); return out.join(''); }; e6d459e5cfc4eba0ffdf64'>tests/files.py
blob: 487046aca493f87a708d2875edcbbc78aa3bf0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import unicode_literals
from django.test import TestCase
from rest_framework import serializers
from rest_framework.compat import BytesIO
from rest_framework.compat import six
import datetime


class UploadedFile(object):
    def __init__(self, file, created=None):
        self.file = file
        self.created = created or datetime.datetime.now()


class UploadedFileSerializer(serializers.Serializer):
    file = serializers.FileField()
    created = serializers.DateTimeField()

    def restore_object(self, attrs, instance=None):
        if instance:
            instance.file = attrs['file']
            instance.created = attrs['created']
            return instance
        return UploadedFile(**attrs)


class FileSerializerTests(TestCase):
    def test_create(self):
        now = datetime.datetime.now()
        file = BytesIO(six.b('stuff'))
        file.name = 'stuff.txt'
        file.size = len(file.getvalue())
        serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
        uploaded_file = UploadedFile(file=file, created=now)
        self.assertTrue(serializer.is_valid())
        self.assertEqual(serializer.object.created, uploaded_file.created)
        self.assertEqual(serializer.object.file, uploaded_file.file)
        self.assertFalse(serializer.object is uploaded_file)

    def test_creation_failure(self):
        """
        Passing files=None should result in an ValidationError

        Regression test for:
        https://github.com/tomchristie/django-rest-framework/issues/542
        """
        now = datetime.datetime.now()

        serializer = UploadedFileSerializer(data={'created': now})
        self.assertFalse(serializer.is_valid())
        self.assertIn('file', serializer.errors)