PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vows/url_vows.py

https://github.com/mrcrabby/thumbor
Python | 180 lines | 112 code | 61 blank | 7 comment | 0 complexity | 102b3b9a390e8ff255746d874408ddb5 MD5 | raw file
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # thumbor imaging service
  4. # https://github.com/globocom/thumbor/wiki
  5. # Licensed under the MIT license:
  6. # http://www.opensource.org/licenses/mit-license
  7. # Copyright (c) 2011 globo.com timehome@corp.globo.com
  8. from pyvows import Vows, expect
  9. from thumbor.url import Url
  10. def ctx(**kw):
  11. class Context(Vows.Context):
  12. def topic(self):
  13. return Url.generate_options(**kw)
  14. return Context
  15. @Vows.batch
  16. class UrlVows(Vows.Context):
  17. class UrlGeneration(Vows.Context):
  18. class Default(ctx()):
  19. def should_return_empty_url(self, topic):
  20. expect(topic).to_be_empty()
  21. class Minimum(ctx(width=300, height=200)):
  22. def should_return_proper_url(self, topic):
  23. expect(topic).to_equal('300x200')
  24. class Smart(ctx(width=300, height=200, smart=True)):
  25. def should_return_proper_url(self, topic):
  26. expect(topic).to_equal('300x200/smart')
  27. class Alignments(ctx(halign='left', valign='top')):
  28. def should_return_proper_url(self, topic):
  29. expect(topic).to_equal('left/top')
  30. class Flipping(ctx(width=300, height=200, smart=True, horizontal_flip=True, vertical_flip=True)):
  31. def should_return_proper_url(self, topic):
  32. expect(topic).to_equal('-300x-200/smart')
  33. class Crop(ctx(width=300, height=200, crop_left=10, crop_top=11, crop_right=12, crop_bottom=13)):
  34. def should_return_proper_url(self, topic):
  35. expect(topic).to_equal('10x11:12x13/300x200')
  36. class Meta(ctx(meta=True)):
  37. def should_return_proper_url(self, topic):
  38. expect(topic).to_equal('meta')
  39. class FitIn(ctx(fit_in=True)):
  40. def should_return_proper_url(self, topic):
  41. expect(topic).to_equal('fit-in')
  42. class Complete(ctx(width=300, height=200, smart=True, fit_in=True, meta=True, horizontal_flip=True, vertical_flip=True, crop_left=10, crop_top=11, crop_right=12, crop_bottom=13)):
  43. def should_return_proper_url(self, topic):
  44. expect(topic).to_equal('meta/10x11:12x13/fit-in/-300x-200/smart')
  45. class Regex(Vows.Context):
  46. def topic(self):
  47. return Url.regex()
  48. def should_contain_unsafe(self, topic):
  49. expect(topic).to_include('/?unsafe/')
  50. def should_contain_meta(self, topic):
  51. expect(topic).to_include('(?:(?P<meta>meta)/)?')
  52. def should_contain_crop(self, topic):
  53. expect(topic).to_include('(?:(?P<crop_left>\d+)x(?P<crop_top>\d+):(?P<crop_right>\d+)x(?P<crop_bottom>\d+)/)?')
  54. def should_contain_fit_in(self, topic):
  55. expect(topic).to_include('(?:(?P<fit_in>fit-in)/)?')
  56. def should_contain_dimensions(self, topic):
  57. expect(topic).to_include('(?:(?P<horizontal_flip>-)?(?P<width>\d+)?x(?P<vertical_flip>-)?(?P<height>\d+)?/)?')
  58. def should_contain_halign(self, topic):
  59. expect(topic).to_include('(?:(?P<halign>left|right|center)/)?')
  60. def should_contain_valign(self, topic):
  61. expect(topic).to_include('(?:(?P<valign>top|bottom|middle)/)?')
  62. def should_contain_smart(self, topic):
  63. expect(topic).to_include('(?:(?P<smart>smart)/)?')
  64. def should_contain_image(self, topic):
  65. expect(topic).to_include('(?P<image>.+)')
  66. class WithoutImage(Vows.Context):
  67. def topic(self):
  68. return Url.regex(include_image=False)
  69. def should_not_include_image(self, topic):
  70. expect(topic).not_to_include('(?P<image>.+)')
  71. class Parse(Vows.Context):
  72. class WithoutInitialSlash(Vows.Context):
  73. def topic(self):
  74. return Url.parse_options('unsafe/meta/10x11:12x13/-300x-200/left/top/smart/')
  75. def should_not_be_null(self, topic):
  76. expect(topic).not_to_be_null()
  77. class WithoutResult(Vows.Context):
  78. def topic(self):
  79. return Url.parse_options("some fake url")
  80. def should_be_null(self, topic):
  81. expect(topic).to_be_null()
  82. class WithoutImage(Vows.Context):
  83. def topic(self):
  84. return Url.parse_options('/unsafe/meta/10x11:12x13/fit-in/-300x-200/left/top/smart/')
  85. def should_have_meta(self, topic):
  86. expect(topic['meta']).to_be_true()
  87. def should_have_crop_left_of_10(self, topic):
  88. expect(topic['crop']['left']).to_equal(10)
  89. def should_have_crop_top_of_11(self, topic):
  90. expect(topic['crop']['top']).to_equal(11)
  91. def should_have_crop_right_of_12(self, topic):
  92. expect(topic['crop']['right']).to_equal(12)
  93. def should_have_crop_bottom_of_13(self, topic):
  94. expect(topic['crop']['bottom']).to_equal(13)
  95. def should_have_width_of_300(self, topic):
  96. expect(topic['width']).to_equal(300)
  97. def should_have_height_of_200(self, topic):
  98. expect(topic['height']).to_equal(200)
  99. def should_have_horizontal_flip(self, topic):
  100. expect(topic['horizontal_flip']).to_be_true()
  101. def should_have_vertical_flip(self, topic):
  102. expect(topic['vertical_flip']).to_be_true()
  103. def should_have_halign_of_left(self, topic):
  104. expect(topic['halign']).to_equal('left')
  105. def should_have_valign_of_top(self, topic):
  106. expect(topic['valign']).to_equal('top')
  107. def should_have_smart(self, topic):
  108. expect(topic['smart']).to_be_true()
  109. def should_have_fit_in(self, topic):
  110. expect(topic['fit_in']).to_be_true()
  111. class WithImage(Vows.Context):
  112. def topic(self):
  113. image_url = 's.glbimg.com/es/ge/f/original/2011/03/29/orlandosilva_60.jpg'
  114. return Url.parse('/unsafe/meta/10x11:12x13/-300x-200/left/top/smart/%s' % image_url)
  115. def should_have_image(self, topic):
  116. expect(topic).to_include('image')
  117. def should_have_image_like_url(self, topic):
  118. image_url = 's.glbimg.com/es/ge/f/original/2011/03/29/orlandosilva_60.jpg'
  119. expect(topic['image']).to_equal(image_url)