|
1 | 1 | # encoding: utf-8
|
2 | 2 | from __future__ import unicode_literals
|
3 | 3 |
|
| 4 | +import unittest |
| 5 | + |
4 | 6 | from django.test import TestCase
|
5 | 7 |
|
| 8 | +from rest_framework.compat import coreapi, coreschema |
6 | 9 | from rest_framework.relations import Hyperlink
|
7 | 10 | from rest_framework.templatetags import rest_framework
|
8 | 11 | from rest_framework.templatetags.rest_framework import (
|
9 | 12 | add_nested_class, add_query_param, as_string, break_long_headers,
|
10 |
| - format_value, get_pagination_html, urlize_quoted_links |
| 13 | + format_value, get_pagination_html, schema_links, urlize_quoted_links |
11 | 14 | )
|
12 | 15 | from rest_framework.test import APIRequestFactory
|
13 | 16 |
|
@@ -300,3 +303,313 @@ def test_json_with_url(self):
|
300 | 303 | data['"foo_set": [\n "http://api/foos/1/"\n], '] = \
|
301 | 304 | '"foo_set": [\n "<a href="http://api/foos/1/">http://api/foos/1/</a>"\n], '
|
302 | 305 | self._urlize_dict_check(data)
|
| 306 | + |
| 307 | + |
| 308 | +@unittest.skipUnless(coreapi, 'coreapi is not installed') |
| 309 | +class SchemaLinksTests(TestCase): |
| 310 | + |
| 311 | + def test_schema_with_empty_links(self): |
| 312 | + schema = coreapi.Document( |
| 313 | + url='', |
| 314 | + title='Example API', |
| 315 | + content={ |
| 316 | + 'users': { |
| 317 | + 'list': {} |
| 318 | + } |
| 319 | + } |
| 320 | + ) |
| 321 | + section = schema['users'] |
| 322 | + flat_links = schema_links(section) |
| 323 | + assert len(flat_links) is 0 |
| 324 | + |
| 325 | + def test_single_action(self): |
| 326 | + schema = coreapi.Document( |
| 327 | + url='', |
| 328 | + title='Example API', |
| 329 | + content={ |
| 330 | + 'users': { |
| 331 | + 'list': coreapi.Link( |
| 332 | + url='/users/', |
| 333 | + action='get', |
| 334 | + fields=[] |
| 335 | + ) |
| 336 | + } |
| 337 | + } |
| 338 | + ) |
| 339 | + section = schema['users'] |
| 340 | + flat_links = schema_links(section) |
| 341 | + assert len(flat_links) is 1 |
| 342 | + assert 'list' in flat_links |
| 343 | + |
| 344 | + def test_default_actions(self): |
| 345 | + schema = coreapi.Document( |
| 346 | + url='', |
| 347 | + title='Example API', |
| 348 | + content={ |
| 349 | + 'users': { |
| 350 | + 'create': coreapi.Link( |
| 351 | + url='/users/', |
| 352 | + action='post', |
| 353 | + fields=[] |
| 354 | + ), |
| 355 | + 'list': coreapi.Link( |
| 356 | + url='/users/', |
| 357 | + action='get', |
| 358 | + fields=[] |
| 359 | + ), |
| 360 | + 'read': coreapi.Link( |
| 361 | + url='/users/{id}/', |
| 362 | + action='get', |
| 363 | + fields=[ |
| 364 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 365 | + ] |
| 366 | + ), |
| 367 | + 'update': coreapi.Link( |
| 368 | + url='/users/{id}/', |
| 369 | + action='patch', |
| 370 | + fields=[ |
| 371 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 372 | + ] |
| 373 | + ) |
| 374 | + } |
| 375 | + } |
| 376 | + ) |
| 377 | + section = schema['users'] |
| 378 | + flat_links = schema_links(section) |
| 379 | + assert len(flat_links) is 4 |
| 380 | + assert 'list' in flat_links |
| 381 | + assert 'create' in flat_links |
| 382 | + assert 'read' in flat_links |
| 383 | + assert 'update' in flat_links |
| 384 | + |
| 385 | + def test_default_actions_and_single_custom_action(self): |
| 386 | + schema = coreapi.Document( |
| 387 | + url='', |
| 388 | + title='Example API', |
| 389 | + content={ |
| 390 | + 'users': { |
| 391 | + 'create': coreapi.Link( |
| 392 | + url='/users/', |
| 393 | + action='post', |
| 394 | + fields=[] |
| 395 | + ), |
| 396 | + 'list': coreapi.Link( |
| 397 | + url='/users/', |
| 398 | + action='get', |
| 399 | + fields=[] |
| 400 | + ), |
| 401 | + 'read': coreapi.Link( |
| 402 | + url='/users/{id}/', |
| 403 | + action='get', |
| 404 | + fields=[ |
| 405 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 406 | + ] |
| 407 | + ), |
| 408 | + 'update': coreapi.Link( |
| 409 | + url='/users/{id}/', |
| 410 | + action='patch', |
| 411 | + fields=[ |
| 412 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 413 | + ] |
| 414 | + ), |
| 415 | + 'friends': coreapi.Link( |
| 416 | + url='/users/{id}/friends', |
| 417 | + action='get', |
| 418 | + fields=[ |
| 419 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 420 | + ] |
| 421 | + ) |
| 422 | + } |
| 423 | + } |
| 424 | + ) |
| 425 | + section = schema['users'] |
| 426 | + flat_links = schema_links(section) |
| 427 | + assert len(flat_links) is 5 |
| 428 | + assert 'list' in flat_links |
| 429 | + assert 'create' in flat_links |
| 430 | + assert 'read' in flat_links |
| 431 | + assert 'update' in flat_links |
| 432 | + assert 'friends' in flat_links |
| 433 | + |
| 434 | + def test_default_actions_and_single_custom_action_two_methods(self): |
| 435 | + schema = coreapi.Document( |
| 436 | + url='', |
| 437 | + title='Example API', |
| 438 | + content={ |
| 439 | + 'users': { |
| 440 | + 'create': coreapi.Link( |
| 441 | + url='/users/', |
| 442 | + action='post', |
| 443 | + fields=[] |
| 444 | + ), |
| 445 | + 'list': coreapi.Link( |
| 446 | + url='/users/', |
| 447 | + action='get', |
| 448 | + fields=[] |
| 449 | + ), |
| 450 | + 'read': coreapi.Link( |
| 451 | + url='/users/{id}/', |
| 452 | + action='get', |
| 453 | + fields=[ |
| 454 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 455 | + ] |
| 456 | + ), |
| 457 | + 'update': coreapi.Link( |
| 458 | + url='/users/{id}/', |
| 459 | + action='patch', |
| 460 | + fields=[ |
| 461 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 462 | + ] |
| 463 | + ), |
| 464 | + 'friends': { |
| 465 | + 'list': coreapi.Link( |
| 466 | + url='/users/{id}/friends', |
| 467 | + action='get', |
| 468 | + fields=[ |
| 469 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 470 | + ] |
| 471 | + ), |
| 472 | + 'create': coreapi.Link( |
| 473 | + url='/users/{id}/friends', |
| 474 | + action='post', |
| 475 | + fields=[ |
| 476 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 477 | + ] |
| 478 | + ) |
| 479 | + } |
| 480 | + } |
| 481 | + } |
| 482 | + ) |
| 483 | + section = schema['users'] |
| 484 | + flat_links = schema_links(section) |
| 485 | + assert len(flat_links) is 6 |
| 486 | + assert 'list' in flat_links |
| 487 | + assert 'create' in flat_links |
| 488 | + assert 'read' in flat_links |
| 489 | + assert 'update' in flat_links |
| 490 | + assert 'friends > list' in flat_links |
| 491 | + assert 'friends > create' in flat_links |
| 492 | + |
| 493 | + def test_multiple_nested_routes(self): |
| 494 | + schema = coreapi.Document( |
| 495 | + url='', |
| 496 | + title='Example API', |
| 497 | + content={ |
| 498 | + 'animals': { |
| 499 | + 'dog': { |
| 500 | + 'vet': { |
| 501 | + 'list': coreapi.Link( |
| 502 | + url='/animals/dog/{id}/vet', |
| 503 | + action='get', |
| 504 | + fields=[ |
| 505 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 506 | + ] |
| 507 | + ) |
| 508 | + }, |
| 509 | + 'read': coreapi.Link( |
| 510 | + url='/animals/dog/{id}', |
| 511 | + action='get', |
| 512 | + fields=[ |
| 513 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 514 | + ] |
| 515 | + ) |
| 516 | + }, |
| 517 | + 'cat': { |
| 518 | + 'list': coreapi.Link( |
| 519 | + url='/animals/cat/', |
| 520 | + action='get', |
| 521 | + fields=[ |
| 522 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 523 | + ] |
| 524 | + ), |
| 525 | + 'create': coreapi.Link( |
| 526 | + url='/aniamls/cat', |
| 527 | + action='post', |
| 528 | + fields=[] |
| 529 | + ) |
| 530 | + } |
| 531 | + } |
| 532 | + } |
| 533 | + ) |
| 534 | + section = schema['animals'] |
| 535 | + flat_links = schema_links(section) |
| 536 | + assert len(flat_links) is 4 |
| 537 | + assert 'cat > create' in flat_links |
| 538 | + assert 'cat > list' in flat_links |
| 539 | + assert 'dog > read' in flat_links |
| 540 | + assert 'dog > vet > list' in flat_links |
| 541 | + |
| 542 | + def test_multiple_resources_with_multiple_nested_routes(self): |
| 543 | + schema = coreapi.Document( |
| 544 | + url='', |
| 545 | + title='Example API', |
| 546 | + content={ |
| 547 | + 'animals': { |
| 548 | + 'dog': { |
| 549 | + 'vet': { |
| 550 | + 'list': coreapi.Link( |
| 551 | + url='/animals/dog/{id}/vet', |
| 552 | + action='get', |
| 553 | + fields=[ |
| 554 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 555 | + ] |
| 556 | + ) |
| 557 | + }, |
| 558 | + 'read': coreapi.Link( |
| 559 | + url='/animals/dog/{id}', |
| 560 | + action='get', |
| 561 | + fields=[ |
| 562 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 563 | + ] |
| 564 | + ) |
| 565 | + }, |
| 566 | + 'cat': { |
| 567 | + 'list': coreapi.Link( |
| 568 | + url='/animals/cat/', |
| 569 | + action='get', |
| 570 | + fields=[ |
| 571 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 572 | + ] |
| 573 | + ), |
| 574 | + 'create': coreapi.Link( |
| 575 | + url='/aniamls/cat', |
| 576 | + action='post', |
| 577 | + fields=[] |
| 578 | + ) |
| 579 | + } |
| 580 | + }, |
| 581 | + 'farmers': { |
| 582 | + 'silo': { |
| 583 | + 'soy': { |
| 584 | + 'list': coreapi.Link( |
| 585 | + url='/farmers/silo/{id}/soy', |
| 586 | + action='get', |
| 587 | + fields=[ |
| 588 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 589 | + ] |
| 590 | + ) |
| 591 | + }, |
| 592 | + 'list': coreapi.Link( |
| 593 | + url='/farmers/silo', |
| 594 | + action='get', |
| 595 | + fields=[ |
| 596 | + coreapi.Field('id', required=True, location='path', schema=coreschema.String()) |
| 597 | + ] |
| 598 | + ) |
| 599 | + } |
| 600 | + } |
| 601 | + } |
| 602 | + ) |
| 603 | + section = schema['animals'] |
| 604 | + flat_links = schema_links(section) |
| 605 | + assert len(flat_links) is 4 |
| 606 | + assert 'cat > create' in flat_links |
| 607 | + assert 'cat > list' in flat_links |
| 608 | + assert 'dog > read' in flat_links |
| 609 | + assert 'dog > vet > list' in flat_links |
| 610 | + |
| 611 | + section = schema['farmers'] |
| 612 | + flat_links = schema_links(section) |
| 613 | + assert len(flat_links) is 2 |
| 614 | + assert 'silo > list' in flat_links |
| 615 | + assert 'silo > soy > list' in flat_links |
0 commit comments