aboutsummaryrefslogtreecommitdiffstats
path: root/test/AngularSpec.js
blob: 1cb6af5e8796d01ae4133c5bb9b6c59c50055f90 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Useful tool to run the test suite for djangorestframework and generate a coverage report.
"""

# http://ericholscher.com/blog/2009/jun/29/enable-setuppy-test-your-django-apps/
# http://www.travisswicegood.com/2010/01/17/django-virtualenv-pip-and-fabric/
# http://code.djangoproject.com/svn/django/trunk/tests/runtests.py
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangorestframework.runtests.settings'

from django.conf import settings
from django.test.utils import get_runner
from coverage import coverage
from itertools import chain
import djangorestframework

def main():
    """Run the tests for djangorestframework and generate a coverage report."""
    
    # Discover the list of all modules that we should test coverage for
    project_dir = os.path.dirname(djangorestframework.__file__)
    cov_files = []
    for (path, dirs, files) in os.walk(project_dir):
        # Drop tests and runtests directories from the test coverage report
        if os.path.basename(path) == 'tests' or os.path.basename(path) == 'runtests':
            continue
        cov_files.extend([os.path.join(path, file) for file in files if file.endswith('.py')])

    cov = coverage()
    cov.erase()
    cov.start()
    TestRunner = get_runner(settings)

    if hasattr(TestRunner, 'func_name'):
        # Pre 1.2 test runners were just functions,
        # and did not support the 'failfast' option.
        import warnings
        warnings.warn(
            'Function-based test runners are deprecated. Test runners should be classes with a run_tests() method.',
            DeprecationWarning
        )
        failures = TestRunner(['djangorestframework'])
    else:
        test_runner = TestRunner()
        failures = test_runner.run_tests(['djangorestframework'])

    cov.stop()
    cov.report(cov_files)
    cov.xml_report(cov_files)
    sys.exit(failures)

if __name__ == '__main__':
    main()
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
'use strict';

describe('angular', function() {
  describe('case', function() {
    it('should change case', function() {
      expect(lowercase('ABC90')).toEqual('abc90');
      expect(manualLowercase('ABC90')).toEqual('abc90');
      expect(uppercase('abc90')).toEqual('ABC90');
      expect(manualUppercase('abc90')).toEqual('ABC90');
    });
  });

  describe("copy", function() {
    it("should return same object", function () {
      var obj = {};
      var arr = [];
      expect(copy({}, obj)).toBe(obj);
      expect(copy([], arr)).toBe(arr);
    });

    it("should copy Date", function() {
      var date = new Date(123);
      expect(copy(date) instanceof Date).toBeTruthy();
      expect(copy(date).getTime()).toEqual(123);
      expect(copy(date) === date).toBeFalsy();
    });

    it("should copy array", function() {
      var src = [1, {name:"value"}];
      var dst = [{key:"v"}];
      expect(copy(src, dst)).toBe(dst);
      expect(dst).toEqual([1, {name:"value"}]);
      expect(dst[1]).toEqual({name:"value"});
      expect(dst[1]).not.toBe(src[1]);
    });

    it('should copy empty array', function() {
      var src = [];
      var dst = [{key: "v"}];
      expect(copy(src, dst)).toEqual([]);
      expect(dst).toEqual([]);
    });

    it("should copy object", function() {
      var src = {a:{name:"value"}};
      var dst = {b:{key:"v"}};
      expect(copy(src, dst)).toBe(dst);
      expect(dst).toEqual({a:{name:"value"}});
      expect(dst.a).toEqual(src.a);
      expect(dst.a).not.toBe(src.a);
    });

    it("should copy primitives", function() {
      expect(copy(null)).toEqual(null);
      expect(copy('')).toBe('');
      expect(copy('lala')).toBe('lala');
      expect(copy(123)).toEqual(123);
      expect(copy([{key:null}])).toEqual([{key:null}]);
    });

    it('should throw an exception if a Scope is being copied', inject(function($rootScope) {
      expect(function() { copy($rootScope.$new()); }).toThrow("Can't copy Window or Scope");
    }));

    it('should throw an exception if a Window is being copied', function() {
      expect(function() { copy(window); }).toThrow("Can't copy Window or Scope");
    });
  });

  describe('equals', function() {
    it('should return true if same object', function() {
      var o = {};
      expect(equals(o, o)).toEqual(true);
      expect(equals(o, {})).toEqual(true);
      expect(equals(1, '1')).toEqual(false);
      expect(equals(1, '2')).toEqual(false);
    });

    it('should recurse into object', function() {
      expect(equals({}, {})).toEqual(true);
      expect(equals({name:'misko'}, {name:'misko'})).toEqual(true);
      expect(equals({name:'misko', age:1}, {name:'misko'})).toEqual(false);
      expect(equals({name:'misko'}, {name:'misko', age:1})).toEqual(false);
      expect(equals({name:'misko'}, {name:'adam'})).toEqual(false);
      expect(equals(['misko'], ['misko'])).toEqual(true);
      expect(equals(['misko'], ['adam'])).toEqual(false);
      expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
    });

    it('should ignore $ member variables', function() {
      expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
      expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
      expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
    });

    it('should ignore functions', function() {
      expect(equals({func: function() {}}, {bar: function() {}})).toEqual(true);
    });

    it('should work well with nulls', function() {
      expect(equals(null, '123')).toBe(false);
      expect(equals('123', null)).toBe(false);

      var obj = {foo:'bar'};
      expect(equals(null, obj)).toBe(false);
      expect(equals(obj, null)).toBe(false);

      expect(equals(null, null)).toBe(true);
    });

    it('should work well with undefined', function() {
      expect(equals(undefined, '123')).toBe(false);
      expect(equals('123', undefined)).toBe(false);

      var obj = {foo:'bar'};
      expect(equals(undefined, obj)).toBe(false);
      expect(equals(obj, undefined)).toBe(false);

      expect(equals(undefined, undefined)).toBe(true);
    });

    it('should treat two NaNs as equal', function() {
      expect(equals(NaN, NaN)).toBe(true);
    });

    it('should compare Scope instances only by identity', inject(function($rootScope) {
      var scope1 = $rootScope.$new(),
          scope2 = $rootScope.$new();

      expect(equals(scope1, scope1)).toBe(true);
      expect(equals(scope1, scope2)).toBe(false);
      expect(equals($rootScope, scope1)).toBe(false);
      expect(equals(undefined, scope1)).toBe(false);
    }));

    it('should compare Window instances only by identity', function() {
      expect(equals(window, window)).toBe(true);
      expect(equals(window, window.parent)).toBe(false);
      expect(equals(window, undefined)).toBe(false);
    });
  });

  describe('size', function() {
    it('should return the number of items in an array', function() {
      expect(size([])).toBe(0);
      expect(size(['a', 'b', 'c'])).toBe(3);
    });

    it('should return the number of properties of an object', function() {
      expect(size({})).toBe(0);
      expect(size({a:1, b:'a', c:noop})).toBe(3);
    });

    it('should return the number of own properties of an object', function() {
      var obj = inherit({protoProp: 'c', protoFn: noop}, {a:1, b:'a', c:noop});

      expect(size(obj)).toBe(5);
      expect(size(obj, true)).toBe(3);
    });

    it('should return the string length', function() {
      expect(size('')).toBe(0);
      expect(size('abc')).toBe(3);
    });

    it('should not rely on length property of an object to determine its size', function() {
      expect(size({length:99})).toBe(1);
    });
  });


  describe('parseKeyValue', function() {
    it('should parse a string into key-value pairs', function() {
      expect(parseKeyValue('')).toEqual({});
      expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
      expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
      expect(parseKeyValue('escaped%20key=escaped%20value')).
      toEqual({'escaped key': 'escaped value'});
      expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
      expect(parseKeyValue('flag1&key=value&flag2')).
      toEqual({flag1: true, key: 'value', flag2: true});
    });
  });

  describe('toKeyValue', function() {
    it('should parse key-value pairs into string', function() {
      expect(toKeyValue({})).toEqual('');
      expect(toKeyValue({simple: 'pair'})).toEqual('simple=pair');
      expect(toKeyValue({first: '1', second: '2'})).toEqual('first=1&second=2');
      expect(toKeyValue({'escaped key': 'escaped value'})).
      toEqual('escaped%20key=escaped%20value');
      expect(toKeyValue({emptyKey: ''})).toEqual('emptyKey=');
    });

    it('should parse true values into flags', function() {
      expect(toKeyValue({flag1: true, key: 'value', flag2: true})).toEqual('flag1&key=value&flag2');
    });
  });

  describe('sortedKeys', function() {
    it('should collect keys from object', function() {
      expect(sortedKeys({c:0, b:0, a:0})).toEqual(['a', 'b', 'c']);
    });
  });


  describe('encodeUriSegment', function() {
    it('should correctly encode uri segment and not encode chars defined as pchar set in rfc3986',
        function() {
      //don't encode alphanum
      expect(encodeUriSegment('asdf1234asdf')).
        toEqual('asdf1234asdf');

      //don't encode unreserved'
      expect(encodeUriSegment("-_.!~*'() -_.!~*'()")).
        toEqual("-_.!~*'()%20-_.!~*'()");

      //don't encode the rest of pchar'
      expect(encodeUriSegment(':@&=+$, :@&=+$,')).
        toEqual(':@&=+$,%20:@&=+$,');

      //encode '/', ';' and ' ''
      expect(encodeUriSegment('/; /;')).
        toEqual('%2F%3B%20%2F%3B');
    });
  });


  describe('encodeUriQuery', function() {
    it('should correctly encode uri query and not encode chars defined as pchar set in rfc3986',
        function() {
      //don't encode alphanum
      expect(encodeUriQuery('asdf1234asdf')).
        toEqual('asdf1234asdf');

      //don't encode unreserved
      expect(encodeUriQuery("-_.!~*'() -_.!~*'()")).
        toEqual("-_.!~*'()+-_.!~*'()");

      //don't encode the rest of pchar
      expect(encodeUriQuery(':@$, :@$,')).
        toEqual(':@$,+:@$,');

      //encode '&', ';', '=', '+', and '#'
      expect(encodeUriQuery('&;=+# &;=+#')).
        toEqual('%26%3B%3D%2B%23+%26%3B%3D%2B%23');

      //encode ' ' as '+'
      expect(encodeUriQuery('  ')).
        toEqual('++');

      //encode ' ' as '%20' when a flag is used
      expect(encodeUriQuery('  ', true)).
        toEqual('%20%20');
    });
  });


  describe('angularJsConfig', function() {
    it('should always consider angular.js script tag to be the last script tag', function() {
      var doc = {
        getElementsByTagName: function(tagName) {
          expect(tagName).toEqual('script');
          return [{nodeName: 'SCRIPT', src: 'random.js',
                    attributes: [{name: 'ng:autobind', value: 'wrong'}]},
                  {nodeName: 'SCRIPT', src: 'angular.js',
                    attributes: [{name: 'ng:autobind', value: 'correct'}]}];
        }
      };

      expect(angularJsConfig(doc)).toEqual({autobind: 'correct'});

      doc = {
        getElementsByTagName: function(tagName) {
          expect(tagName).toEqual('script');
          return [{nodeName: 'SCRIPT', src: 'angular.js',
                    attributes: [{name: 'ng:autobind', value: 'wrong'}]},
                  {nodeName: 'SCRIPT', src: 'concatinatedAndObfuscadedScriptWithOurScript.js',
                    attributes: [{name: 'ng:autobind', value: 'correct'}]}];
        }
      };

      expect(angularJsConfig(doc)).toEqual({autobind: 'correct'});
    });


    it('should extract angular config from the ng: attributes', function() {
      var doc = { getElementsByTagName: function(tagName) {
        expect(lowercase(tagName)).toEqual('script');
        return [{
          nodeName: 'SCRIPT',
          src: 'angularjs/angular.js',
          attributes: [{name: 'ng:autobind', value:'elementIdToCompile'},
                       {name: 'ng:css', value: 'css/my_custom_angular.css'}] }];
      }};

      expect(angularJsConfig(doc)).toEqual({
        autobind: 'elementIdToCompile',
        css: 'css/my_custom_angular.css'
      });
    });


    it('should extract angular config and default autobind value to true if present', function() {
      var doc = { getElementsByTagName: function(tagName) {
        expect(lowercase(tagName)).toEqual('script');
        return [{
          nodeName: 'SCRIPT',
          src: 'angularjs/angular.js',
          attributes: [{name: 'ng:autobind', value:undefined}]}];
      }};

      expect(angularJsConfig(doc)).toEqual({autobind: true});
    });


    it('should extract angular autobind config from the script hashpath attributes', function() {
      var doc = { getElementsByTagName: function(tagName) {
        expect(lowercase(tagName)).toEqual('script');
        return [{
          nodeName: 'SCRIPT',
          src: 'angularjs/angular.js#autobind'}];
      }};

      expect(angularJsConfig(doc)).toEqual({autobind: true});
    });


    it('should extract autobind config with element id from the script hashpath', function() {
      var doc = { getElementsByTagName: function(tagName) {
        expect(lowercase(tagName)).toEqual('script');
        return [{
          nodeName: 'SCRIPT',
          src: 'angularjs/angular.js#autobind=foo'}];
      }};

      expect(angularJsConfig(doc)).toEqual({autobind: 'foo'});
    });


    it('should default to versioned ie-compat file if angular file is versioned', function() {
      var doc = { getElementsByTagName: function(tagName) {
        expect(lowercase(tagName)).toEqual('script');
        return [{
          nodeName: 'SCRIPT',
          src: 'js/angular-0.9.0.js'}];
      }};

      expect(angularJsConfig(doc)).toEqual({});
    });
  });


  describe('angularInit', function() {
    var dom;

    beforeEach(function() {
      dom = jqLite('<div foo="{{1+2}}">{{2+3}}' +
                     '<div id="child" bar="{{3+4}}">{{4+5}}</div>' +
                   '</div>')[0];
    });


    afterEach(function() {
      dealoc(dom);
    });


    it('should not compile anything if autobind is missing or false', function() {
      angularInit({}, dom);
      expect(sortedHtml(dom)).toEqual('<div foo="{{1+2}}">{{2+3}}' +
                                        '<div bar="{{3+4}}" id="child">{{4+5}}</div>' +
                                      '</div>');
    });


    it('should compile the document if autobind is true', function() {
      angularInit({autobind: true}, dom);
      expect(sortedHtml(dom)).toEqual('<div foo="3" ng:bind-attr="{"foo":"{{1+2}}"}">' +
                                        '<span ng:bind="2+3">5</span>' +
                                        '<div bar="7" id="child" ng:bind-attr="{"bar":"{{3+4}}"}">'+
                                          '<span ng:bind="4+5">9</span>' +
                                        '</div>' +
                                      '</div>');
    });


    it('should compile only the element specified via autobind', function() {
      dom.getElementById = function() {
        return this.childNodes[1];
      };


      angularInit({autobind: 'child'}, dom);

      expect(sortedHtml(dom)).toEqual('<div foo="{{1+2}}">{{2+3}}' +
                                        '<div bar="7" id="child" ng:bind-attr="{"bar":"{{3+4}}"}">'+
                                          '<span ng:bind="4+5">9</span>' +
                                        '</div>' +
                                      '</div>');
    });
  });


  describe('angular service', function() {
    it('should override services', inject(function($provide){
      $provide.value('fake', 'old');
      $provide.value('fake', 'new');
    }, function(fake) {
      expect(fake).toEqual('new');
    }));

    it('should inject dependencies specified by $inject and ignore function argument name', function() {
      expect(angular.injector(function($provide){
        $provide.factory('svc1', function() { return 'svc1'; });
        $provide.factory('svc2', ['svc1', function(s) { return 'svc2-' + s; }]);
      }).get('svc2')).toEqual('svc2-svc1');
    });

  });


  describe('directive', function() {
    it('should register directives with case-insensitive id', inject(function($compile) {
      angularDirective('ALLCAPS', function(val, el) {el.text('+' + val + '+')});
      angularDirective('lowercase', function(val, el) {el.text('-' + val + '-')});

      var el = jqLite('<div>' +
                        '<span allcaps="xx1"></span>' +
                        '<span ALLcaps="xx2"></span>' +
                        '<span ALLCAPS="xx3"></span>' +
                        '<span lowerCASE="XX4">xx4</span>' +
                      '</div>');
      $compile(el);
      expect(lowercase(sortedHtml(el))).toBe('<div>' +
                                                '<span allcaps="xx1">+xx1+</span>' +
                                                '<span allcaps="xx2">+xx2+</span>' +
                                                '<span allcaps="xx3">+xx3+</span>' +
                                                '<span lowercase="xx4">-xx4-</span>' +
                                              '</div>');
    }));
  });


  describe('isDate', function() {
    it('should return true for Date object', function() {
      expect(isDate(new Date())).toBe(true);
    });

    it('should return false for non Date objects', function() {
      expect(isDate([])).toBe(false);
      expect(isDate('')).toBe(false);
      expect(isDate(23)).toBe(false);
      expect(isDate({})).toBe(false);
    });
  });

  describe('compile', function() {
    it('should link to existing node and create scope', inject(function($rootScope, $compile) {
      var template = angular.element('<div>{{greeting = "hello world"}}</div>');
      $compile(template)($rootScope);
      $rootScope.$digest();
      expect(template.text()).toEqual('hello world');
      expect($rootScope.greeting).toEqual('hello world');
    }));

    it('should link to existing node and given scope', inject(function($rootScope, $compile) {
      var template = angular.element('<div>{{greeting = "hello world"}}</div>');
      $compile(template)($rootScope);
      $rootScope.$digest();
      expect(template.text()).toEqual('hello world');
    }));

    it('should link to new node and given scope', inject(function($rootScope, $compile) {
      var template = jqLite('<div>{{greeting = "hello world"}}</div>');

      var templateFn = $compile(template);
      var templateClone = template.clone();

      var element = templateFn($rootScope, function(clone){
        templateClone = clone;
      });
      $rootScope.$digest();

      expect(template.text()).toEqual('');
      expect(element.text()).toEqual('hello world');
      expect(element).toEqual(templateClone);
      expect($rootScope.greeting).toEqual('hello world');
    }));

    it('should link to cloned node and create scope', inject(function($rootScope, $compile) {
      var template = jqLite('<div>{{greeting = "hello world"}}</div>');
      var element = $compile(template)($rootScope, noop);
      $rootScope.$digest();
      expect(template.text()).toEqual('');
      expect(element.text()).toEqual('hello world');
      expect($rootScope.greeting).toEqual('hello world');
    }));
  });


  describe('nodeName_', function() {
    it('should correctly detect node name with "namespace" when xmlns is defined', function() {
      var div = jqLite('<div xmlns:ngtest="http://angularjs.org/">' +
                         '<ngtest:foo ngtest:attr="bar"></ngtest:foo>' +
                       '</div>')[0];
      expect(nodeName_(div.childNodes[0])).toBe('NGTEST:FOO');
      expect(div.childNodes[0].getAttribute('ngtest:attr')).toBe('bar');
    });

    if (!msie || msie >= 9) {
      it('should correctly detect node name with "namespace" when xmlns is NOT defined', function() {
        var div = jqLite('<div xmlns:ngtest="http://angularjs.org/">' +
                           '<ngtest:foo ngtest:attr="bar"></ng:test>' +
                         '</div>')[0];
        expect(nodeName_(div.childNodes[0])).toBe('NGTEST:FOO');
        expect(div.childNodes[0].getAttribute('ngtest:attr')).toBe('bar');
      });
    }
  });


  describe('nextUid()', function() {
    it('should return new id per call', function() {
      var seen = {};
      var count = 100;

      while(count--) {
        var current = nextUid();
        expect(current.match(/[\d\w]+/)).toBeTruthy();
        expect(seen[current]).toBeFalsy();
        seen[current] = true;
      }
    });
  });


  describe('version', function() {
    it('version should have full/major/minor/dot/codeName properties', function() {
      expect(version).toBeDefined();
      expect(version.full).toBe('"NG_VERSION_FULL"');
      expect(version.major).toBe("NG_VERSION_MAJOR");
      expect(version.minor).toBe("NG_VERSION_MINOR");
      expect(version.dot).toBe("NG_VERSION_DOT");
      expect(version.codeName).toBe('"NG_VERSION_CODENAME"');
    });
  });

  describe('bootstrap', function() {
    it('should bootstrap app', function(){
      var element = jqLite('<div>{{1+2}}</div>');
      var injector;
      angular.bootstrap(element, [function($injector){ injector = $injector; }]);
      expect(injector).toBeDefined();
      expect(element.data('$injector')).toBe(injector);
      dealoc(element);
    });
  });
});