| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import unicode_literals |
| 19 |
|
| 20 |
from six import text_type, PY2 |
| 21 |
from six.moves import xrange |
| 22 |
|
| 23 |
import sure |
| 24 |
from sure.deprecated import that |
| 25 |
from sure.magic import is_cpython |
| 26 |
from sure import VariablesBag, expect |
| •27 |
import unittest |
| 28 |
from sure.compat import compat_repr, safe_repr, text_type_name |
| 29 |
|
| •30 |
class testOldApi(unittest.TestCase): |
| 31 |
def test_setup_with_context(self): |
| 32 |
"sure.with_context() runs setup before the function itself" |
| 33 |
|
| 34 |
def setup(context): |
| 35 |
context.name = "John Resig" |
| 36 |
|
| 37 |
@sure.that_with_context(setup) |
| 38 |
def john_is_within_context(context): |
| 39 |
assert isinstance(context, VariablesBag) |
| 40 |
assert hasattr(context, "name") |
| 41 |
|
| 42 |
john_is_within_context() |
| 43 |
self.assertEqual( |
| 44 |
john_is_within_context.__name__, |
| 45 |
'john_is_within_context', |
| 46 |
) |
| 47 |
|
| 48 |
|
| •49 |
def test_context_is_not_optional(self): |
| 50 |
"sure.that_with_context() when no context is given it fails" |
| 51 |
|
| •52 |
def setup(context): |
| 53 |
context.name = "John Resig" |
| 54 |
|
| •55 |
@sure.that_with_context(setup) |
| 56 |
def it_crashes(): |
| 57 |
assert True |
| 58 |
|
| 59 |
assert that(it_crashes).raises( |
| 60 |
TypeError, ( |
| 61 |
"the function it_crashes defined at test_old_api.py line 56, is being " |
| 62 |
"decorated by either @that_with_context or @scenario, so it should " |
| 63 |
"take at least 1 parameter, which is the test context"), |
| 64 |
) |
| 65 |
|
| 66 |
|
| •67 |
def test_setup_with_context_context_failing(self): |
| 68 |
"sure.that_with_context() in a failing test" |
| 69 |
|
| •70 |
def setup(context): |
| 71 |
context.name = "John Resig" |
| 72 |
|
| •73 |
@sure.that_with_context(setup) |
| 74 |
def it_fails(context): |
| 75 |
assert False, 'should fail with this exception' |
| 76 |
|
| •77 |
assert that(it_fails).raises('should fail with this exception') |
| 78 |
|
| 79 |
|
| •80 |
def test_teardown_with_context(self): |
| 81 |
"sure.with_context() runs teardown before the function itself" |
| 82 |
|
| •83 |
class something: |
| 84 |
pass |
| 85 |
|
| •86 |
def setup(context): |
| 87 |
something.modified = True |
| 88 |
|
| •89 |
def teardown(context): |
| 90 |
del something.modified |
| 91 |
|
| •92 |
@sure.that_with_context(setup, teardown) |
| 93 |
def something_was_modified(context): |
| 94 |
assert hasattr(something, "modified") |
| 95 |
assert something.modified |
| 96 |
|
| •97 |
something_was_modified() |
| 98 |
assert not hasattr(something, "modified") |
| 99 |
|
| 100 |
|
| •101 |
def test_that_is_a(self): |
| 102 |
"that() is_a(object)" |
| 103 |
|
| •104 |
something = "something" |
| 105 |
|
| •106 |
assert that(something).is_a(text_type) |
| 107 |
assert isinstance(something, text_type) |
| 108 |
|
| 109 |
|
| •110 |
def test_that_equals(self): |
| 111 |
"that() equals(string)" |
| 112 |
|
| •113 |
something = "something" |
| 114 |
|
| •115 |
assert that('something').equals(something) |
| 116 |
assert something == 'something' |
| 117 |
|
| 118 |
|
| •119 |
def test_that_differs(self): |
| 120 |
"that() differs(object)" |
| 121 |
|
| •122 |
something = "something" |
| 123 |
|
| •124 |
assert that(something).differs("23123%FYTUGIHOfdf") |
| 125 |
assert something != "23123%FYTUGIHOfdf" |
| 126 |
|
| 127 |
|
| •128 |
def test_that_has(self): |
| 129 |
"that() has(object)" |
| 130 |
class Class: |
| 131 |
name = "some class" |
| 132 |
Object = Class() |
| 133 |
dictionary = { |
| 134 |
'name': 'John', |
| 135 |
} |
| 136 |
name = "john" |
| 137 |
|
| •138 |
assert hasattr(Class, 'name') |
| 139 |
assert that(Class).has("name") |
| 140 |
assert that(Class).like("name") |
| 141 |
assert "name" in that(Class) |
| 142 |
|
| •143 |
assert hasattr(Object, 'name') |
| 144 |
assert that(Object).has("name") |
| 145 |
assert that(Object).like("name") |
| 146 |
assert "name" in that(Object) |
| 147 |
|
| •148 |
assert 'name' in dictionary |
| 149 |
assert that(dictionary).has("name") |
| 150 |
assert that(dictionary).like("name") |
| 151 |
assert "name" in that(dictionary) |
| 152 |
|
| •153 |
assert that(name).has("john") |
| 154 |
assert that(name).like("john") |
| 155 |
assert "john" in that(name) |
| 156 |
assert that(name).has("hn") |
| 157 |
assert that(name).like("hn") |
| 158 |
assert "hn" in that(name) |
| 159 |
assert that(name).has("jo") |
| 160 |
assert that(name).like("jo") |
| 161 |
assert "jo" in that(name) |
| 162 |
|
| 163 |
|
| •164 |
def test_that_at_key_equals(self): |
| 165 |
"that().at(object).equals(object)" |
| 166 |
|
| •167 |
class Class: |
| 168 |
name = "some class" |
| 169 |
Object = Class() |
| 170 |
dictionary = { |
| 171 |
'name': 'John', |
| 172 |
} |
| 173 |
|
| •174 |
assert that(Class).at("name").equals('some class') |
| 175 |
assert that(Object).at("name").equals('some class') |
| 176 |
assert that(dictionary).at("name").equals('John') |
| 177 |
|
| 178 |
|
| •179 |
def test_that_len_is(self): |
| 180 |
"that() len_is(number)" |
| 181 |
|
| •182 |
lst = range(1000) |
| 183 |
|
| •184 |
assert that(lst).len_is(1000) |
| 185 |
assert len(lst) == 1000 |
| 186 |
assert that(lst).len_is(lst) |
| 187 |
|
| 188 |
|
| •189 |
def test_that_len_greater_than(self): |
| 190 |
"that() len_greater_than(number)" |
| 191 |
|
| •192 |
lst = range(1000) |
| 193 |
lst2 = range(100) |
| 194 |
|
| •195 |
assert that(lst).len_greater_than(100) |
| 196 |
assert len(lst) == 1000 |
| 197 |
assert that(lst).len_greater_than(lst2) |
| 198 |
|
| 199 |
|
| •200 |
def test_that_len_greater_than_should_raise_assertion_error(self): |
| 201 |
"that() len_greater_than(number) raise AssertionError" |
| 202 |
|
| •203 |
lst = list(range(1000)) |
| 204 |
try: |
| 205 |
that(lst).len_greater_than(1000) |
| 206 |
except AssertionError as e: |
| 207 |
self.assertEqual( |
| 208 |
str(e), |
| 209 |
'the length of the list should be greater then %d, but is %d' \ |
| 210 |
% (1000, 1000)) |
| 211 |
|
| 212 |
|
| •213 |
def test_that_len_greater_than_or_equals(self): |
| 214 |
"that() len_greater_than_or_equals(number)" |
| 215 |
|
| •216 |
lst = list(range(1000)) |
| 217 |
lst2 = list(range(100)) |
| 218 |
|
| •219 |
assert that(lst).len_greater_than_or_equals(100) |
| 220 |
assert that(lst).len_greater_than_or_equals(1000) |
| 221 |
assert len(lst) == 1000 |
| 222 |
assert that(lst).len_greater_than_or_equals(lst2) |
| 223 |
assert that(lst).len_greater_than_or_equals(lst) |
| 224 |
|
| 225 |
|
| •226 |
def test_that_len_greater_than_or_equals_should_raise_assertion_error(self): |
| 227 |
"that() len_greater_than_or_equals(number) raise AssertionError" |
| 228 |
|
| •229 |
lst = list(range(1000)) |
| 230 |
try: |
| 231 |
that(lst).len_greater_than_or_equals(1001) |
| 232 |
except AssertionError as e: |
| 233 |
self.assertEqual( |
| 234 |
str(e), |
| 235 |
'the length of %r should be greater then or equals %d, but is %d' \ |
| 236 |
% (lst, 1001, 1000)) |
| 237 |
|
| 238 |
|
| •239 |
def test_that_len_lower_than(self): |
| 240 |
"that() len_lower_than(number)" |
| 241 |
|
| •242 |
lst = list(range(100)) |
| 243 |
lst2 = list(range(1000)) |
| 244 |
|
| •245 |
assert that(lst).len_lower_than(101) |
| 246 |
assert len(lst) == 100 |
| 247 |
assert that(lst).len_lower_than(lst2) |
| 248 |
|
| 249 |
|
| •250 |
def test_that_len_lower_than_should_raise_assertion_error(self): |
| 251 |
"that() len_lower_than(number) raise AssertionError" |
| 252 |
|
| •253 |
lst = list(range(1000)) |
| 254 |
try: |
| 255 |
that(lst).len_lower_than(1000) |
| 256 |
except AssertionError as e: |
| 257 |
self.assertEqual( |
| 258 |
str(e), |
| 259 |
'the length of %r should be lower then %d, but is %d' % \ |
| 260 |
(lst, 1000, 1000)) |
| 261 |
|
| 262 |
|
| •263 |
def test_that_len_lower_than_or_equals(self): |
| 264 |
"that() len_lower_than_or_equals(number)" |
| 265 |
|
| •266 |
lst = list(range(1000)) |
| 267 |
lst2 = list(range(1001)) |
| 268 |
|
| •269 |
assert that(lst).len_lower_than_or_equals(1001) |
| 270 |
assert that(lst).len_lower_than_or_equals(1000) |
| 271 |
assert len(lst) == 1000 |
| 272 |
assert that(lst).len_lower_than_or_equals(lst2) |
| 273 |
assert that(lst).len_lower_than_or_equals(lst) |
| 274 |
|
| 275 |
|
| •276 |
def test_that_len_lower_than_or_equals_should_raise_assertion_error(self): |
| 277 |
"that() len_lower_than_or_equals(number) raise AssertionError" |
| 278 |
|
| •279 |
lst = list(range(1000)) |
| 280 |
try: |
| 281 |
that(lst).len_lower_than_or_equals(100) |
| 282 |
except AssertionError as e: |
| 283 |
self.assertEqual( |
| 284 |
str(e), |
| 285 |
'the length of %r should be lower then or equals %d, but is %d' % \ |
| 286 |
(lst, 100, 1000)) |
| 287 |
|
| 288 |
|
| •289 |
def test_that_checking_all_atributes(self): |
| 290 |
"that(iterable).the_attribute('name').equals('value')" |
| 291 |
class shape(object): |
| 292 |
def __init__(self, name): |
| 293 |
self.kind = 'geometrical form' |
| 294 |
self.name = name |
| 295 |
|
| •296 |
shapes = [ |
| 297 |
shape('circle'), |
| 298 |
shape('square'), |
| 299 |
shape('rectangle'), |
| 300 |
shape('triangle'), |
| 301 |
] |
| 302 |
|
| •303 |
assert that(shapes).the_attribute("kind").equals('geometrical form') |
| 304 |
|
| 305 |
|
| •306 |
def test_that_checking_all_atributes_of_range(self): |
| 307 |
"that(iterable, within_range=(1, 2)).the_attribute('name').equals('value')" |
| 308 |
class shape(object): |
| 309 |
def __init__(self, name): |
| 310 |
self.kind = 'geometrical form' |
| 311 |
self.name = name |
| 312 |
|
| •313 |
def __repr__(self): |
| 314 |
return '<%s:%s>' % (self.kind, self.name) |
| 315 |
|
| •316 |
shapes = [ |
| 317 |
shape('circle'), |
| 318 |
shape('square'), |
| 319 |
shape('square'), |
| 320 |
shape('triangle'), |
| 321 |
] |
| 322 |
|
| •323 |
assert shapes[0].name != 'square' |
| 324 |
assert shapes[3].name != 'square' |
| 325 |
|
| •326 |
assert shapes[1].name == 'square' |
| 327 |
assert shapes[2].name == 'square' |
| 328 |
|
| •329 |
assert that(shapes, within_range=(1, 2)). \ |
| 330 |
the_attribute("name"). \ |
| 331 |
equals('square') |
| 332 |
|
| 333 |
|
| •334 |
def test_that_checking_all_elements(self): |
| 335 |
"that(iterable).every_one_is('value')" |
| 336 |
shapes = [ |
| 337 |
'cube', |
| 338 |
'ball', |
| 339 |
'ball', |
| 340 |
'piramid', |
| 341 |
] |
| 342 |
|
| •343 |
assert shapes[0] != 'ball' |
| 344 |
assert shapes[3] != 'ball' |
| 345 |
|
| •346 |
assert shapes[1] == 'ball' |
| 347 |
assert shapes[2] == 'ball' |
| 348 |
|
| •349 |
assert that(shapes, within_range=(1, 2)).every_one_is('ball') |
| 350 |
|
| 351 |
|
| •352 |
def test_that_checking_each_matches(self): |
| 353 |
"that(iterable).in_each('').equals('value')" |
| 354 |
class animal(object): |
| 355 |
def __init__(self, kind): |
| 356 |
self.attributes = { |
| 357 |
'class': 'mammal', |
| 358 |
'kind': kind, |
| 359 |
} |
| 360 |
|
| •361 |
animals = [ |
| 362 |
animal('dog'), |
| 363 |
animal('cat'), |
| 364 |
animal('cow'), |
| 365 |
animal('cow'), |
| 366 |
animal('cow'), |
| 367 |
] |
| 368 |
|
| •369 |
assert animals[0].attributes['kind'] != 'cow' |
| 370 |
assert animals[1].attributes['kind'] != 'cow' |
| 371 |
|
| •372 |
assert animals[2].attributes['kind'] == 'cow' |
| 373 |
assert animals[3].attributes['kind'] == 'cow' |
| 374 |
assert animals[4].attributes['kind'] == 'cow' |
| 375 |
|
| •376 |
assert animals[0].attributes['class'] == 'mammal' |
| 377 |
assert animals[1].attributes['class'] == 'mammal' |
| 378 |
assert animals[2].attributes['class'] == 'mammal' |
| 379 |
assert animals[3].attributes['class'] == 'mammal' |
| 380 |
assert animals[4].attributes['class'] == 'mammal' |
| 381 |
|
| •382 |
assert that(animals).in_each("attributes['class']").matches('mammal') |
| 383 |
assert that(animals).in_each("attributes['class']"). \ |
| 384 |
matches(['mammal','mammal','mammal','mammal','mammal']) |
| 385 |
|
| •386 |
assert that(animals).in_each("attributes['kind']"). \ |
| 387 |
matches(['dog','cat','cow','cow','cow']) |
| 388 |
|
| •389 |
try: |
| 390 |
assert that(animals).in_each("attributes['kind']").matches(['dog']) |
| 391 |
assert False, 'should not reach here' |
| 392 |
except AssertionError as e: |
| 393 |
assert that(text_type(e)).equals( |
| 394 |
'%r has 5 items, but the matching list has 1: %r' % ( |
| 395 |
['dog','cat','cow','cow','cow'], ['dog'], |
| 396 |
) |
| 397 |
) |
| 398 |
|
| 399 |
|
| •400 |
def test_that_raises(self): |
| 401 |
"that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)" |
| 402 |
global called |
| 403 |
|
| •404 |
called = False |
| 405 |
|
| •406 |
def function(arg1=None, arg2=None): |
| 407 |
global called |
| 408 |
called = True |
| 409 |
if arg1 == 1 and arg2 == 2: |
| 410 |
raise RuntimeError('yeah, it failed') |
| 411 |
|
| •412 |
return "OK" |
| 413 |
|
| •414 |
try: |
| 415 |
function(1, 2) |
| 416 |
assert False, 'should not reach here' |
| 417 |
|
| •418 |
except RuntimeError as e: |
| 419 |
assert text_type(e) == 'yeah, it failed' |
| 420 |
|
| •421 |
except Exception: |
| 422 |
assert False, 'should not reach here' |
| 423 |
|
| •424 |
finally: |
| 425 |
assert called |
| 426 |
called = False |
| 427 |
|
| •428 |
self.assertRaises(RuntimeError, function, 1, 2) |
| 429 |
|
| •430 |
called = False |
| 431 |
self.assertEqual(function(3, 5), 'OK') |
| 432 |
assert called |
| 433 |
|
| •434 |
called = False |
| 435 |
assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ |
| 436 |
raises(RuntimeError) |
| 437 |
assert called |
| 438 |
|
| •439 |
called = False |
| 440 |
assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ |
| 441 |
raises(RuntimeError, 'yeah, it failed') |
| 442 |
assert called |
| •443 |
|
| 444 |
called = False |
| •445 |
assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ |
| 446 |
raises('yeah, it failed') |
| 447 |
assert called |
| 448 |
|
| •449 |
called = False |
| 450 |
assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ |
| 451 |
raises(RuntimeError) |
| 452 |
assert called |
| 453 |
|
| •454 |
called = False |
| 455 |
assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ |
| 456 |
raises(RuntimeError, 'yeah, it failed') |
| 457 |
assert called |
| 458 |
|
| •459 |
called = False |
| 460 |
assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ |
| 461 |
raises('yeah, it failed') |
| 462 |
assert called |
| 463 |
|
| •464 |
called = False |
| 465 |
assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ |
| 466 |
raises(r'it fail') |
| 467 |
assert called |
| 468 |
|
| •469 |
called = False |
| 470 |
assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ |
| 471 |
raises(RuntimeError, r'it fail') |
| 472 |
assert called |
| 473 |
|
| 474 |
|
| •475 |
def test_that_looks_like(self): |
| 476 |
"that('String\\n with BREAKLINE').looks_like('string with breakline')" |
| 477 |
assert that('String\n with BREAKLINE').looks_like('string with breakline') |
| 478 |
|
| 479 |
|
| •480 |
def test_that_raises_does_raise_for_exception_type_mismatch(self): |
| 481 |
"that(callable(RuntimeError)).raises(TypeError)" |
| 482 |
|
| •483 |
error = r'.*should raise <class .*FooError.*, but raised <class .*BarError.*' |
| 484 |
|
| •485 |
class FooError(Exception): |
| 486 |
pass |
| 487 |
|
| •488 |
class BarError(Exception): |
| 489 |
pass |
| 490 |
|
| •491 |
def my_function(): |
| 492 |
raise BarError('OOps') |
| 493 |
|
| •494 |
try: |
| 495 |
assert that(my_function).raises(FooError, 'OOps') |
| 496 |
assert False, 'should never reach here' |
| 497 |
except AssertionError as e: |
| 498 |
import re |
| 499 |
assert re.match(error, text_type(e)) |
| 500 |
|
| 501 |
|
| •502 |
def test_that_raises_with_args(self): |
| 503 |
"that(callable, with_args=['foo']).raises(FooError)" |
| 504 |
|
| •505 |
class FooError(Exception): |
| 506 |
pass |
| 507 |
|
| •508 |
def my_function(string): |
| 509 |
if string == 'foo': |
| 510 |
raise FooError('OOps') |
| 511 |
|
| •512 |
assert that(my_function, with_args=['foo']).raises(FooError, 'OOps') |
| 513 |
|
| 514 |
|
| •515 |
def test_that_does_not_raise_with_args(self): |
| 516 |
"that(callable).doesnt_raise(FooError) and does_not_raise" |
| 517 |
|
| •518 |
class FooError(Exception): |
| 519 |
pass |
| 520 |
|
| •521 |
def my_function(string): |
| 522 |
if string == 'foo': |
| 523 |
raise FooError('OOps') |
| 524 |
|
| •525 |
assert that(my_function, with_args=['foo']).raises(FooError, 'OOps') |
| 526 |
|
| 527 |
|
| •528 |
def test_that_contains_string(self): |
| 529 |
"that('foobar').contains('foo')" |
| 530 |
|
| •531 |
assert 'foo' in 'foobar' |
| 532 |
assert that('foobar').contains('foo') |
| 533 |
|
| 534 |
|
| •535 |
def test_that_doesnt_contain_string(self): |
| 536 |
"that('foobar').does_not_contain('123'), .doesnt_contain" |
| 537 |
|
| •538 |
assert '123' not in 'foobar' |
| 539 |
assert that('foobar').doesnt_contain('123') |
| 540 |
assert that('foobar').does_not_contain('123') |
| 541 |
|
| 542 |
|
| •543 |
def test_that_contains_none(self): |
| 544 |
"that('foobar').contains(None)" |
| 545 |
|
| •546 |
def assertions(): |
| 547 |
|
| 548 |
assert that(b'foobar' if PY2 else 'foobar').contains(None) |
| 549 |
|
| •550 |
error_msg = "'in <string>' requires string as left operand, not NoneType" if is_cpython else "'NoneType' does not have the buffer interface" |
| 551 |
|
| •552 |
assert that(assertions).raises( |
| 553 |
TypeError, |
| 554 |
error_msg |
| 555 |
) |
| 556 |
|
| 557 |
|
| •558 |
def test_that_none_contains_string(self): |
| 559 |
"that(None).contains('bungalow')" |
| 560 |
|
| •561 |
try: |
| 562 |
assert that(None).contains('bungalow') |
| 563 |
assert False, 'should not reach here' |
| 564 |
except Exception as e: |
| 565 |
error_msg = "argument of type 'NoneType' is not iterable" if is_cpython else "'NoneType' object is not iterable" |
| 566 |
self.assertEqual( |
| 567 |
text_type(e), |
| 568 |
error_msg |
| 569 |
) |
| 570 |
|
| 571 |
|
| •572 |
def test_that_some_iterable_is_empty(self): |
| 573 |
"that(some_iterable).is_empty and that(something).are_empty" |
| 574 |
|
| •575 |
assert that([]).is_empty |
| 576 |
assert that([]).are_empty |
| 577 |
|
| •578 |
assert that(tuple()).is_empty |
| 579 |
assert that({}).are_empty |
| 580 |
|
| •581 |
def fail_single(): |
| 582 |
assert that((1,)).is_empty |
| 583 |
|
| •584 |
assert that(fail_single).raises('(1,) is not empty, it has 1 item') |
| 585 |
|
| •586 |
def fail_plural(): |
| 587 |
assert that((1, 2)).is_empty |
| 588 |
|
| •589 |
assert that(fail_plural).raises('(1, 2) is not empty, it has 2 items') |
| 590 |
|
| 591 |
|
| •592 |
def test_that_something_is_empty_raises(self): |
| 593 |
"that(something_not_iterable).is_empty and that(something_not_iterable).are_empty raises" |
| 594 |
|
| •595 |
obj = object() |
| 596 |
|
| •597 |
def fail(): |
| 598 |
assert that(obj).is_empty |
| 599 |
assert False, 'should not reach here' |
| 600 |
|
| •601 |
assert that(fail).raises('%r is not iterable' % obj) |
| 602 |
|
| 603 |
|
| •604 |
def test_that_something_iterable_matches_another(self): |
| 605 |
"that(something_iterable).matches(another_iterable)" |
| 606 |
|
| •607 |
|
| 608 |
KlassOne = type(b'KlassOne' if PY2 else 'KlassOne', (object,), {}) |
| 609 |
KlassTwo = type(b'KlassTwo' if PY2 else 'KlassTwo', (object,), {}) |
| 610 |
one = [ |
| 611 |
("/1", KlassOne), |
| 612 |
("/2", KlassTwo), |
| 613 |
] |
| 614 |
|
| •615 |
two = [ |
| 616 |
("/1", KlassOne), |
| 617 |
("/2", KlassTwo), |
| 618 |
] |
| 619 |
|
| •620 |
assert that(one).matches(two) |
| 621 |
assert that(one).equals(two) |
| 622 |
|
| •623 |
def fail_1(): |
| 624 |
assert that([1]).matches(xrange(2)) |
| 625 |
|
| •626 |
class Fail2(object): |
| 627 |
def __init__(self): |
| 628 |
assert that(xrange(1)).matches([2]) |
| 629 |
|
| •630 |
class Fail3(object): |
| 631 |
def __call__(self): |
| 632 |
assert that(xrange(1)).matches([2]) |
| 633 |
|
| •634 |
xrange_name = xrange.__name__ |
| 635 |
assert that(fail_1).raises('X is a list and Y is a {0} instead'.format(xrange_name)) |
| 636 |
assert that(Fail2).raises('X is a {0} and Y is a list instead'.format(xrange_name)) |
| 637 |
assert that(Fail3()).raises('X is a {0} and Y is a list instead'.format(xrange_name)) |
| 638 |
|
| 639 |
|
| •640 |
def test_within_pass(self): |
| 641 |
"within(five=miliseconds) will pass" |
| 642 |
from sure import within, miliseconds |
| 643 |
|
| •644 |
within(five=miliseconds)(lambda *a: None)() |
| 645 |
|
| 646 |
|
| •647 |
def test_within_fail(self): |
| 648 |
"within(five=miliseconds) will fail" |
| 649 |
import time |
| 650 |
from sure import within, miliseconds |
| 651 |
|
| •652 |
def sleepy(*a): |
| 653 |
time.sleep(0.7) |
| 654 |
|
| •655 |
failed = False |
| 656 |
try: |
| 657 |
within(five=miliseconds)(sleepy)() |
| 658 |
except AssertionError as e: |
| 659 |
failed = True |
| 660 |
self.assertEqual('sleepy did not run within five miliseconds', str(e)) |
| 661 |
|
| •662 |
assert failed, 'within(five=miliseconds)(sleepy) did not fail' |
| 663 |
|
| 664 |
|
| •665 |
def test_word_to_number(self): |
| 666 |
self.assertEqual(sure.word_to_number('one'), 1) |
| 667 |
self.assertEqual(sure.word_to_number('two'), 2) |
| 668 |
self.assertEqual(sure.word_to_number('three'), 3) |
| 669 |
self.assertEqual(sure.word_to_number('four'), 4) |
| 670 |
self.assertEqual(sure.word_to_number('five'), 5) |
| 671 |
self.assertEqual(sure.word_to_number('six'), 6) |
| 672 |
self.assertEqual(sure.word_to_number('seven'), 7) |
| 673 |
self.assertEqual(sure.word_to_number('eight'), 8) |
| 674 |
self.assertEqual(sure.word_to_number('nine'), 9) |
| 675 |
self.assertEqual(sure.word_to_number('ten'), 10) |
| 676 |
self.assertEqual(sure.word_to_number('eleven'), 11) |
| 677 |
self.assertEqual(sure.word_to_number('twelve'), 12) |
| 678 |
|
| 679 |
|
| •680 |
def test_word_to_number_fail(self): |
| 681 |
failed = False |
| 682 |
try: |
| 683 |
sure.word_to_number('twenty') |
| 684 |
except AssertionError as e: |
| 685 |
failed = True |
| 686 |
self.assertEqual( |
| 687 |
text_type(e), |
| 688 |
'sure supports only literal numbers from one ' \ |
| 689 |
'to twelve, you tried the word "twenty"') |
| 690 |
|
| •691 |
assert failed, 'should raise assertion error' |
| 692 |
|
| 693 |
|
| •694 |
def test_microsecond_unit(self): |
| 695 |
"testing microseconds convertion" |
| 696 |
cfrom, cto = sure.UNITS[sure.microsecond] |
| 697 |
|
| •698 |
self.assertEqual(cfrom(1), 100000) |
| 699 |
self.assertEqual(cto(1), 1) |
| 700 |
|
| •701 |
cfrom, cto = sure.UNITS[sure.microseconds] |
| 702 |
|
| •703 |
self.assertEqual(cfrom(1), 100000) |
| 704 |
self.assertEqual(cto(1), 1) |
| 705 |
|
| 706 |
|
| •707 |
def test_milisecond_unit(self): |
| 708 |
"testing miliseconds convertion" |
| 709 |
cfrom, cto = sure.UNITS[sure.milisecond] |
| 710 |
|
| •711 |
self.assertEqual(cfrom(1), 1000) |
| 712 |
self.assertEqual(cto(100), 1) |
| 713 |
|
| •714 |
cfrom, cto = sure.UNITS[sure.miliseconds] |
| 715 |
|
| •716 |
self.assertEqual(cfrom(1), 1000) |
| 717 |
self.assertEqual(cto(100), 1) |
| 718 |
|
| 719 |
|
| •720 |
def test_second_unit(self): |
| 721 |
"testing seconds convertion" |
| 722 |
cfrom, cto = sure.UNITS[sure.second] |
| 723 |
|
| •724 |
self.assertEqual(cfrom(1), 1) |
| 725 |
self.assertEqual(cto(100000), 1) |
| 726 |
|
| •727 |
cfrom, cto = sure.UNITS[sure.seconds] |
| 728 |
|
| •729 |
self.assertEqual(cfrom(1), 1) |
| 730 |
self.assertEqual(cto(100000), 1) |
| 731 |
|
| 732 |
|
| •733 |
def test_minute_unit(self): |
| 734 |
"testing minutes convertion" |
| 735 |
cfrom, cto = sure.UNITS[sure.minute] |
| 736 |
|
| •737 |
self.assertEqual(cfrom(60), 1) |
| 738 |
self.assertEqual(cto(1), 6000000) |
| 739 |
|
| •740 |
cfrom, cto = sure.UNITS[sure.minutes] |
| 741 |
|
| •742 |
self.assertEqual(cfrom(60), 1) |
| 743 |
self.assertEqual(cto(1), 6000000) |
| 744 |
|
| 745 |
|
| •746 |
def test_within_pass_utc(self): |
| 747 |
"within(five=miliseconds) gives utc parameter" |
| 748 |
from sure import within, miliseconds |
| 749 |
from datetime import datetime |
| 750 |
|
| •751 |
def assert_utc(utc): |
| 752 |
assert isinstance(utc, datetime) |
| 753 |
|
| •754 |
within(five=miliseconds)(assert_utc)() |
| 755 |
|
| 756 |
|
| •757 |
def test_that_is_a_matcher_should_absorb_callables_to_be_used_as_matcher(self): |
| 758 |
"that.is_a_matcher should absorb callables to be used as matcher" |
| 759 |
@that.is_a_matcher |
| 760 |
def is_truthful(what): |
| 761 |
assert bool(what), '%s is so untrue' % (what) |
| 762 |
return 'foobar' |
| 763 |
|
| •764 |
assert that('friend').is_truthful() |
| 765 |
self.assertEqual(that('friend').is_truthful(), 'foobar') |
| 766 |
|
| 767 |
|
| •768 |
def test_accepts_setup_list(self): |
| 769 |
"sure.with_context() accepts a list of callbacks for setup" |
| 770 |
|
| •771 |
def setup1(context): |
| 772 |
context.first_name = "John" |
| 773 |
|
| •774 |
def setup2(context): |
| 775 |
context.last_name = "Resig" |
| 776 |
|
| •777 |
@sure.that_with_context([setup1, setup2]) |
| 778 |
def john_is_within_context(context): |
| 779 |
assert context.first_name == 'John' |
| 780 |
assert context.last_name == 'Resig' |
| 781 |
|
| •782 |
john_is_within_context() |
| 783 |
self.assertEqual( |
| 784 |
john_is_within_context.__name__, |
| 785 |
'john_is_within_context', |
| 786 |
) |
| 787 |
|
| 788 |
|
| •789 |
def test_accepts_teardown_list(self): |
| 790 |
"sure.with_context() runs teardown before the function itself" |
| 791 |
|
| •792 |
class something: |
| 793 |
modified = True |
| 794 |
finished = 'nope' |
| 795 |
|
| •796 |
def setup(context): |
| 797 |
something.modified = False |
| 798 |
|
| •799 |
def teardown1(context): |
| 800 |
something.modified = True |
| 801 |
|
| •802 |
def teardown2(context): |
| 803 |
something.finished = 'yep' |
| 804 |
|
| •805 |
@sure.that_with_context(setup, [teardown1, teardown2]) |
| 806 |
def something_was_modified(context): |
| 807 |
assert not something.modified |
| 808 |
assert something.finished == 'nope' |
| 809 |
|
| •810 |
something_was_modified() |
| 811 |
assert something.modified |
| 812 |
assert something.finished == 'yep' |
| 813 |
|
| 814 |
|
| •815 |
def test_scenario_is_alias_for_context_on_setup_and_teardown(self): |
| 816 |
"@scenario aliases @that_with_context for setup and teardown" |
| 817 |
from sure import scenario |
| 818 |
|
| 819 |
def setup(context): |
| 820 |
context.name = "Robert C Martin" |
| 821 |
|
| 822 |
def teardown(context): |
| 823 |
self.assertEqual(context.name, "Robert C Martin") |
| 824 |
|
| 825 |
@scenario([setup], [teardown]) |
| 826 |
def robert_is_within_context(context): |
| 827 |
"Robert is within context" |
| 828 |
assert isinstance(context, VariablesBag) |
| 829 |
assert hasattr(context, "name") |
| 830 |
self.assertEqual(context.name, "Robert C Martin") |
| 831 |
|
| 832 |
robert_is_within_context() |
| 833 |
self.assertEqual( |
| 834 |
robert_is_within_context.__name__, |
| 835 |
'robert_is_within_context', |
| 836 |
) |
| 837 |
|
| 838 |
|
| •839 |
def test_actions_returns_context(self): |
| 840 |
"the actions always returns the context" |
| 841 |
from sure import action_for, scenario |
| 842 |
|
| •843 |
def with_setup(context): |
| 844 |
@action_for(context) |
| 845 |
def action1(): |
| 846 |
pass |
| 847 |
|
| •848 |
@action_for(context) |
| 849 |
def action2(): |
| 850 |
pass |
| 851 |
|
| •852 |
@scenario(with_setup) |
| 853 |
def i_can_use_actions(context): |
| 854 |
assert that(context.action1()).equals(context) |
| 855 |
assert that(context.action2()).equals(context) |
| 856 |
return True |
| 857 |
|
| •858 |
assert i_can_use_actions() |
| 859 |
|
| 860 |
|
| •861 |
def test_actions_providing_variables_in_the_context(self): |
| 862 |
"the actions should be able to declare the variables they provide" |
| 863 |
from sure import action_for, scenario |
| 864 |
|
| •865 |
def with_setup(context): |
| 866 |
@action_for(context, provides=['var1', 'foobar']) |
| 867 |
def the_context_has_variables(): |
| 868 |
context.var1 = 123 |
| 869 |
context.foobar = "qwerty" |
| 870 |
|
| •871 |
@scenario(with_setup) |
| 872 |
def the_providers_are_working(Then): |
| 873 |
Then.the_context_has_variables() |
| 874 |
assert hasattr(Then, 'var1') |
| 875 |
assert hasattr(Then, 'foobar') |
| 876 |
assert hasattr(Then, '__sure_providers_of__') |
| 877 |
|
| •878 |
providers = Then.__sure_providers_of__ |
| 879 |
action = Then.the_context_has_variables.__name__ |
| 880 |
|
| •881 |
providers_of_var1 = [p.__name__ for p in providers['var1']] |
| 882 |
assert that(providers_of_var1).contains(action) |
| 883 |
|
| •884 |
providers_of_foobar = [p.__name__ for p in providers['foobar']] |
| 885 |
assert that(providers_of_foobar).contains(action) |
| 886 |
|
| •887 |
return True |
| 888 |
|
| •889 |
assert the_providers_are_working() |
| 890 |
|
| 891 |
|
| •892 |
def test_fails_when_action_doesnt_fulfill_the_agreement_of_provides(self): |
| 893 |
"it fails when an action doesn't fulfill its agreements" |
| 894 |
from sure import action_for, scenario |
| 895 |
|
| •896 |
error = 'the action "bad_action" was supposed to provide the ' \ |
| 897 |
'attribute "two" into the context, but it did not. Please ' \ |
| 898 |
'double check its implementation' |
| 899 |
|
| •900 |
def with_setup(context): |
| 901 |
@action_for(context, provides=['one', 'two']) |
| 902 |
def bad_action(): |
| 903 |
context.one = 123 |
| 904 |
|
| •905 |
@scenario(with_setup) |
| 906 |
def the_providers_are_working(the): |
| 907 |
assert that(the.bad_action).raises(AssertionError, error) |
| 908 |
return True |
| 909 |
|
| •910 |
assert the_providers_are_working() |
| 911 |
|
| 912 |
|
| •913 |
def test_depends_on_failing_due_nothing_found(self): |
| 914 |
"it fails when an action depends on some attribute that is not " \ |
| 915 |
"provided by any other previous action" |
| 916 |
import os |
| 917 |
from sure import action_for, scenario |
| 918 |
|
| •919 |
fullpath = os.path.abspath(__file__).replace('.pyc', '.py') |
| 920 |
error = 'the action "lonely_action" defined at %s:926 ' \ |
| 921 |
'depends on the attribute "something" to be available in the' \ |
| 922 |
' context. It turns out that there are no actions providing ' \ |
| 923 |
'that. Please double-check the implementation' % fullpath |
| 924 |
|
| •925 |
def with_setup(context): |
| 926 |
@action_for(context, depends_on=['something']) |
| 927 |
def lonely_action(): |
| 928 |
pass |
| 929 |
|
| •930 |
@scenario(with_setup) |
| 931 |
def depends_on_fails(the): |
| 932 |
assert that(the.lonely_action).raises(AssertionError, error) |
| 933 |
return True |
| 934 |
|
| •935 |
assert depends_on_fails() |
| 936 |
|
| 937 |
|
| •938 |
def test_depends_on_failing_due_not_calling_a_previous_action(self): |
| 939 |
"it fails when an action depends on some attribute that is being " \ |
| 940 |
"provided by other actions" |
| 941 |
|
| •942 |
import os |
| 943 |
from sure import action_for, scenario |
| 944 |
|
| •945 |
fullpath = os.path.abspath(__file__).replace('.pyc', '.py') |
| 946 |
error = 'the action "my_action" defined at {0}:956 ' \ |
| 947 |
'depends on the attribute "some_attr" to be available in the context.'\ |
| 948 |
' You need to call one of the following actions beforehand:\n' \ |
| 949 |
' -> dependency_action at {0}:952'.replace('{0}', fullpath) |
| 950 |
|
| •951 |
def with_setup(context): |
| 952 |
@action_for(context, provides=['some_attr']) |
| 953 |
def dependency_action(): |
| 954 |
context.some_attr = True |
| 955 |
|
| •956 |
@action_for(context, depends_on=['some_attr']) |
| 957 |
def my_action(): |
| 958 |
pass |
| 959 |
|
| •960 |
@scenario(with_setup) |
| 961 |
def depends_on_fails(the): |
| 962 |
assert that(the.my_action).raises(AssertionError, error) |
| 963 |
return True |
| 964 |
|
| •965 |
assert depends_on_fails() |
| 966 |
|
| 967 |
|
| •968 |
def test_that_contains_dictionary_keys(self): |
| 969 |
"that(dict(name='foobar')).contains('name')" |
| 970 |
|
| •971 |
data = dict(name='foobar') |
| 972 |
assert 'name' in data |
| 973 |
assert 'name' in data.keys() |
| 974 |
assert that(data).contains('name') |
| 975 |
|
| 976 |
|
| •977 |
def test_that_contains_list(self): |
| 978 |
"that(['foobar', '123']).contains('foobar')" |
| 979 |
|
| •980 |
data = ['foobar', '123'] |
| 981 |
assert 'foobar' in data |
| 982 |
assert that(data).contains('foobar') |
| 983 |
|
| 984 |
|
| •985 |
def test_that_contains_set(self): |
| 986 |
"that(set(['foobar', '123']).contains('foobar')" |
| 987 |
|
| •988 |
data = set(['foobar', '123']) |
| 989 |
assert 'foobar' in data |
| 990 |
assert that(data).contains('foobar') |
| 991 |
|
| 992 |
|
| •993 |
def test_that_contains_tuple(self): |
| 994 |
"that(('foobar', '123')).contains('foobar')" |
| 995 |
|
| •996 |
data = ('foobar', '123') |
| 997 |
assert 'foobar' in data |
| 998 |
assert that(data).contains('foobar') |
| 999 |
|
| 1000 |
|
| •1001 |
def test_variables_bag_provides_meaningful_error_on_nonexisting_attribute(self): |
| 1002 |
"VariablesBag() provides a meaningful error when attr does not exist" |
| 1003 |
|
| •1004 |
context = VariablesBag() |
| 1005 |
|
| •1006 |
context.name = "John" |
| 1007 |
context.foo = "bar" |
| 1008 |
|
| •1009 |
assert that(context.name).equals("John") |
| 1010 |
assert that(context.foo).equals("bar") |
| 1011 |
|
| •1012 |
def access_nonexisting_attr(): |
| 1013 |
assert context.bleh == 'crash :(' |
| 1014 |
|
| •1015 |
assert that(access_nonexisting_attr).raises( |
| 1016 |
AssertionError, |
| 1017 |
'you have tried to access the attribute \'bleh\' from the context ' \ |
| 1018 |
'(aka VariablesBag), but there is no such attribute assigned to it. ' \ |
| 1019 |
'Maybe you misspelled it ? Well, here are the options: ' \ |
| 1020 |
'[\'name\', \'foo\']', |
| 1021 |
) |
| 1022 |
|
| 1023 |
|
| •1024 |
def test_actions_providing_dinamically_named_variables(self): |
| 1025 |
"the actions should be able to declare the variables they provide" |
| 1026 |
from sure import action_for, scenario |
| 1027 |
|
| •1028 |
def with_setup(context): |
| 1029 |
@action_for(context, provides=['var1', '{0}']) |
| 1030 |
def the_context_has_variables(first_arg): |
| 1031 |
context.var1 = 123 |
| 1032 |
context[first_arg] = "qwerty" |
| 1033 |
|
| •1034 |
@scenario(with_setup) |
| 1035 |
def the_providers_are_working(Then): |
| 1036 |
Then.the_context_has_variables('JohnDoe') |
| 1037 |
assert hasattr(Then, 'var1') |
| 1038 |
assert 'JohnDoe' in Then |
| 1039 |
assert hasattr(Then, '__sure_providers_of__') |
| 1040 |
|
| •1041 |
providers = Then.__sure_providers_of__ |
| 1042 |
action = Then.the_context_has_variables.__name__ |
| 1043 |
|
| •1044 |
providers_of_var1 = [p.__name__ for p in providers['var1']] |
| 1045 |
assert that(providers_of_var1).contains(action) |
| 1046 |
|
| •1047 |
providers_of_JohnDoe = [p.__name__ for p in providers['JohnDoe']] |
| 1048 |
assert that(providers_of_JohnDoe).contains(action) |
| 1049 |
|
| •1050 |
return True |
| 1051 |
|
| •1052 |
assert the_providers_are_working() |
| 1053 |
|
| 1054 |
|
| •1055 |
def test_deep_equals_dict_level1_success(self): |
| 1056 |
"that() deep_equals(dict) succeeding on level 1" |
| 1057 |
|
| •1058 |
something = { |
| 1059 |
'one': 'yeah', |
| 1060 |
} |
| 1061 |
|
| •1062 |
assert that(something).deep_equals({ |
| 1063 |
'one': 'yeah', |
| 1064 |
}) |
| 1065 |
|
| 1066 |
|
| •1067 |
def test_deep_equals_dict_level1_fail(self): |
| 1068 |
"that() deep_equals(dict) failing on level 1" |
| 1069 |
|
| •1070 |
something = { |
| 1071 |
'one': 'yeah', |
| 1072 |
} |
| 1073 |
|
| •1074 |
def assertions(): |
| 1075 |
assert that(something).deep_equals({ |
| 1076 |
'one': 'oops', |
| 1077 |
}) |
| 1078 |
|
| 1079 |
assert that(assertions).raises( |
| 1080 |
AssertionError, compat_repr( |
| 1081 |
"given\n" \ |
| 1082 |
"X = {'one': 'yeah'}\n" \ |
| 1083 |
" and\n" \ |
| 1084 |
"Y = {'one': 'oops'}\n" \ |
| 1085 |
"X['one'] is 'yeah' whereas Y['one'] is 'oops'", |
| 1086 |
)) |
| 1087 |
|
| 1088 |
|
| •1089 |
def test_deep_equals_list_level1_success(self): |
| 1090 |
"that(list) deep_equals(list) succeeding on level 1" |
| 1091 |
|
| •1092 |
something = ['one', 'yeah'] |
| 1093 |
assert that(something).deep_equals(['one', 'yeah']) |
| 1094 |
|
| 1095 |
|
| •1096 |
def test_deep_equals_list_level1_fail_by_value(self): |
| 1097 |
"that(list) deep_equals(list) failing on level 1" |
| 1098 |
|
| •1099 |
something = ['one', 'yeahs'] |
| 1100 |
|
| •1101 |
def assertions(): |
| 1102 |
assert that(something).deep_equals(['one', 'yeah']) |
| 1103 |
|
| •1104 |
assert that(assertions).raises( |
| 1105 |
AssertionError, compat_repr( |
| 1106 |
"given\n" \ |
| 1107 |
"X = ['one', 'yeahs']\n" \ |
| 1108 |
" and\n" \ |
| 1109 |
"Y = ['one', 'yeah']\n" \ |
| 1110 |
"X[1] is 'yeahs' whereas Y[1] is 'yeah'", |
| 1111 |
)) |
| 1112 |
|
| 1113 |
|
| •1114 |
def test_deep_equals_list_level1_fail_by_length_x_gt_y(self): |
| 1115 |
"that(list) deep_equals(list) failing by length (len(X) > len(Y))" |
| 1116 |
|
| •1117 |
something = ['one', 'yeah', 'awesome!'] |
| 1118 |
|
| •1119 |
def assertions(): |
| 1120 |
assert that(something).deep_equals(['one', 'yeah']) |
| 1121 |
|
| •1122 |
assert that(assertions).raises( |
| 1123 |
AssertionError, compat_repr( |
| 1124 |
"given\n" \ |
| 1125 |
"X = ['one', 'yeah', 'awesome!']\n" \ |
| 1126 |
" and\n" \ |
| 1127 |
"Y = ['one', 'yeah']\n" \ |
| 1128 |
"X has 3 items whereas Y has only 2", |
| 1129 |
)) |
| 1130 |
|
| 1131 |
|
| •1132 |
def test_deep_equals_list_level1_fail_by_length_y_gt_x(self): |
| 1133 |
"that(list) deep_equals(list) failing by length (len(Y) > len(X))" |
| 1134 |
|
| •1135 |
something = ['one', 'yeah'] |
| 1136 |
|
| •1137 |
def assertions(): |
| 1138 |
assert that(something).deep_equals(['one', 'yeah', 'damn']) |
| 1139 |
|
| •1140 |
assert that(assertions).raises( |
| 1141 |
AssertionError, compat_repr( |
| 1142 |
"given\n" |
| 1143 |
"X = ['one', 'yeah']\n" |
| 1144 |
" and\n" |
| 1145 |
"Y = ['one', 'yeah', 'damn']\n" |
| 1146 |
"Y has 3 items whereas X has only 2" |
| 1147 |
) |
| 1148 |
) |
| 1149 |
|
| 1150 |
|
| •1151 |
def test_deep_equals_dict_level1_fails_missing_key_on_y(self): |
| 1152 |
"that(X) deep_equals(Y) fails when Y is missing a key that X has" |
| 1153 |
|
| •1154 |
something = { |
| 1155 |
'one': 'yeah', |
| 1156 |
} |
| 1157 |
|
| •1158 |
def assertions(): |
| 1159 |
assert that(something).deep_equals({ |
| 1160 |
'two': 'yeah', |
| 1161 |
}) |
| 1162 |
|
| 1163 |
assert that(assertions).raises( |
| 1164 |
AssertionError, compat_repr( |
| 1165 |
"given\n" |
| 1166 |
"X = {{'one': 'yeah'}}\n" |
| 1167 |
" and\n" |
| 1168 |
"Y = {{'two': 'yeah'}}\n" |
| 1169 |
"X has the key \"{0}\" whereas Y does not" |
| 1170 |
).format(safe_repr('one')) |
| 1171 |
) |
| 1172 |
|
| 1173 |
|
| •1174 |
def test_deep_equals_failing_basic_vs_complex(self): |
| 1175 |
"that(X) deep_equals(Y) fails with basic vc complex type" |
| 1176 |
|
| •1177 |
def assertions(): |
| 1178 |
assert that('two yeah').deep_equals({ |
| 1179 |
'two': 'yeah', |
| 1180 |
}) |
| 1181 |
|
| 1182 |
assert that(assertions).raises( |
| 1183 |
AssertionError, compat_repr( |
| 1184 |
"given\n" \ |
| 1185 |
"X = 'two yeah'\n" |
| 1186 |
" and\n" \ |
| 1187 |
"Y = {'two': 'yeah'}\n" \ |
| 1188 |
"X is a %s and Y is a dict instead" % text_type_name, |
| 1189 |
)) |
| 1190 |
|
| 1191 |
|
| 1192 |
def test_deep_equals_failing_complex_vs_basic(self): |
| 1193 |
"that(X) deep_equals(Y) fails with complex vc basic type" |
| 1194 |
|
| 1195 |
def assertions(): |
| 1196 |
assert that({'two': 'yeah'}).deep_equals('two yeah') |
| 1197 |
|
| 1198 |
assert that(assertions).raises( |
| 1199 |
AssertionError, compat_repr( |
| 1200 |
"given\n" \ |
| 1201 |
"X = {'two': 'yeah'}\n" \ |
| 1202 |
" and\n" \ |
| 1203 |
"Y = 'two yeah'\n" |
| 1204 |
"X is a dict and Y is a %s instead" % text_type_name, |
| 1205 |
)) |
| 1206 |
|
| 1207 |
|
| •1208 |
def test_deep_equals_tuple_level1_success(self): |
| 1209 |
"that(tuple) deep_equals(tuple) succeeding on level 1" |
| 1210 |
|
| •1211 |
something = ('one', 'yeah') |
| 1212 |
assert that(something).deep_equals(('one', 'yeah')) |
| 1213 |
|
| 1214 |
|
| •1215 |
def test_deep_equals_tuple_level1_fail_by_value(self): |
| 1216 |
"that(tuple) deep_equals(tuple) failing on level 1" |
| 1217 |
|
| •1218 |
something = ('one', 'yeahs') |
| 1219 |
|
| •1220 |
def assertions(): |
| 1221 |
assert that(something).deep_equals(('one', 'yeah')) |
| 1222 |
|
| •1223 |
assert that(assertions).raises( |
| 1224 |
AssertionError, compat_repr( |
| 1225 |
"given\n" \ |
| 1226 |
"X = ('one', 'yeahs')\n" \ |
| 1227 |
" and\n" \ |
| 1228 |
"Y = ('one', 'yeah')\n" \ |
| 1229 |
"X[1] is 'yeahs' whereas Y[1] is 'yeah'", |
| 1230 |
)) |
| 1231 |
|
| 1232 |
|
| •1233 |
def test_deep_equals_tuple_level1_fail_by_length_x_gt_y(self): |
| 1234 |
"that(tuple) deep_equals(tuple) failing by length (len(X) > len(Y))" |
| 1235 |
|
| •1236 |
something = ('one', 'yeah', 'awesome!') |
| 1237 |
|
| •1238 |
def assertions(): |
| 1239 |
assert that(something).deep_equals(('one', 'yeah')) |
| 1240 |
|
| •1241 |
assert that(assertions).raises( |
| 1242 |
AssertionError, compat_repr( |
| 1243 |
"given\n" \ |
| 1244 |
"X = ('one', 'yeah', 'awesome!')\n" \ |
| 1245 |
" and\n" \ |
| 1246 |
"Y = ('one', 'yeah')\n" \ |
| 1247 |
"X has 3 items whereas Y has only 2", |
| 1248 |
)) |
| 1249 |
|
| 1250 |
|
| •1251 |
def test_deep_equals_tuple_level1_fail_by_length_y_gt_x(self): |
| 1252 |
"that(tuple) deep_equals(tuple) failing by length (len(Y) > len(X))" |
| 1253 |
|
| •1254 |
something = ('one', 'yeah') |
| 1255 |
|
| •1256 |
def assertions(): |
| 1257 |
assert that(something).deep_equals(('one', 'yeah', 'damn')) |
| 1258 |
|
| •1259 |
assert that(assertions).raises( |
| 1260 |
AssertionError, compat_repr( |
| 1261 |
"given\n" \ |
| 1262 |
"X = ('one', 'yeah')\n" \ |
| 1263 |
" and\n" \ |
| 1264 |
"Y = ('one', 'yeah', 'damn')\n" \ |
| 1265 |
"Y has 3 items whereas X has only 2", |
| 1266 |
)) |
| 1267 |
|
| 1268 |
|
| •1269 |
def test_deep_equals_fallsback_to_generic_comparator(self): |
| 1270 |
"that() deep_equals(dict) falling back to generic comparator" |
| 1271 |
from datetime import datetime |
| 1272 |
now = datetime.now() |
| 1273 |
something = { |
| 1274 |
'one': 'yeah', |
| 1275 |
'date': now, |
| 1276 |
} |
| 1277 |
|
| 1278 |
assert that(something).deep_equals({ |
| •1279 |
'one': 'yeah', |
| 1280 |
'date': now, |
| 1281 |
}) |
| 1282 |
|
| 1283 |
|
| •1284 |
def test_deep_equals_fallsback_to_generic_comparator_failing(self): |
| 1285 |
"that() deep_equals(dict) with generic comparator failing" |
| 1286 |
from datetime import datetime |
| 1287 |
now = datetime(2012, 3, 5) |
| 1288 |
tomorrow = datetime(2012, 3, 6) |
| 1289 |
something = { |
| 1290 |
'date': now, |
| 1291 |
} |
| 1292 |
|
| 1293 |
def assertions(): |
| 1294 |
assert that(something).deep_equals({ |
| 1295 |
'date': tomorrow, |
| 1296 |
}) |
| 1297 |
|
| 1298 |
assert that(assertions).raises( |
| 1299 |
AssertionError, compat_repr( |
| 1300 |
"given\n" \ |
| 1301 |
"X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ |
| 1302 |
" and\n" \ |
| 1303 |
"Y = {'date': datetime.datetime(2012, 3, 6, 0, 0)}\n" \ |
| 1304 |
"X['date'] != Y['date']", |
| 1305 |
)) |
| 1306 |
|
| 1307 |
|
| 1308 |
def test_deep_equals_fallsback_to_generic_comparator_failing_type(self): |
| 1309 |
"that() deep_equals(dict) with generic comparator failing" |
| 1310 |
from datetime import datetime |
| 1311 |
now = datetime(2012, 3, 5) |
| 1312 |
something = { |
| 1313 |
'date': now, |
| 1314 |
} |
| 1315 |
|
| 1316 |
def assertions(): |
| 1317 |
assert that(something).deep_equals({ |
| 1318 |
'date': None, |
| 1319 |
}) |
| 1320 |
|
| 1321 |
assert that(assertions).raises( |
| 1322 |
AssertionError, compat_repr( |
| 1323 |
"given\n" \ |
| 1324 |
"X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ |
| 1325 |
" and\n" \ |
| 1326 |
"Y = {'date': None}\n" \ |
| 1327 |
"X['date'] is a datetime and Y['date'] is a NoneType instead", |
| 1328 |
)) |
| 1329 |
|
| 1330 |
|
| •1331 |
def test_deep_equals_dict_level2_success(self): |
| 1332 |
"that() deep_equals(dict) succeeding on level 2" |
| 1333 |
|
| 1334 |
something = { |
| 1335 |
'one': 'yeah', |
| 1336 |
'another': { |
| •1337 |
'two': 'cool', |
| 1338 |
}, |
| •1339 |
} |
| 1340 |
|
| 1341 |
assert that(something).deep_equals({ |
| •1342 |
'one': 'yeah', |
| 1343 |
'another': { |
| 1344 |
'two': 'cool', |
| 1345 |
}, |
| 1346 |
}) |
| 1347 |
|
| 1348 |
|
| •1349 |
def test_deep_equals_dict_level2_list_success(self): |
| 1350 |
"that() deep_equals(dict) succeeding on level 2" |
| 1351 |
|
| •1352 |
something = { |
| 1353 |
'one': 'yeah', |
| 1354 |
'another': ['one', 'two', 3], |
| 1355 |
} |
| 1356 |
|
| 1357 |
assert that(something).deep_equals({ |
| •1358 |
'one': 'yeah', |
| 1359 |
'another': ['one', 'two', 3], |
| 1360 |
}) |
| 1361 |
|
| 1362 |
|
| •1363 |
def test_deep_equals_dict_level2_fail(self): |
| 1364 |
"that() deep_equals(dict) failing on level 2" |
| 1365 |
|
| •1366 |
something = { |
| 1367 |
'one': 'yeah', |
| 1368 |
'another': { |
| 1369 |
'two': '##', |
| 1370 |
}, |
| 1371 |
} |
| 1372 |
|
| •1373 |
def assertions(): |
| 1374 |
assert that(something).deep_equals({ |
| 1375 |
'one': 'yeah', |
| 1376 |
'another': { |
| 1377 |
'two': '$$', |
| 1378 |
}, |
| 1379 |
}) |
| 1380 |
assert that(assertions).raises( |
| 1381 |
AssertionError, compat_repr( |
| 1382 |
"given\n" \ |
| 1383 |
"X = {'another': {'two': '##'}, 'one': 'yeah'}\n" \ |
| 1384 |
" and\n" \ |
| 1385 |
"Y = {'another': {'two': '$$'}, 'one': 'yeah'}\n" \ |
| 1386 |
"X['another']['two'] is '##' whereas Y['another']['two'] is '$$'", |
| 1387 |
)) |
| 1388 |
|
| 1389 |
|
| •1390 |
def test_deep_equals_dict_level3_fail_values(self): |
| 1391 |
"that() deep_equals(dict) failing on level 3" |
| 1392 |
|
| •1393 |
something = { |
| 1394 |
'my::all_users': [ |
| 1395 |
{'name': 'John', 'age': 33}, |
| 1396 |
], |
| 1397 |
} |
| 1398 |
|
| •1399 |
def assertions(): |
| 1400 |
assert that(something).deep_equals({ |
| 1401 |
'my::all_users': [ |
| 1402 |
{'name': 'John', 'age': 30}, |
| 1403 |
], |
| 1404 |
}) |
| 1405 |
|
| 1406 |
assert that(assertions).raises( |
| 1407 |
AssertionError, compat_repr( |
| 1408 |
"given\n" \ |
| 1409 |
"X = {'my::all_users': [{'age': 33, 'name': 'John'}]}\n" \ |
| 1410 |
" and\n" \ |
| 1411 |
"Y = {'my::all_users': [{'age': 30, 'name': 'John'}]}\n" \ |
| 1412 |
"X['my::all_users'][0]['age'] is 33 whereas Y['my::all_users'][0]['age'] is 30", |
| 1413 |
)) |
| 1414 |
|
| 1415 |
|
| •1416 |
def test_deep_equals_dict_level3_fails_missing_key(self): |
| 1417 |
"that() deep_equals(dict) failing on level 3 when missing a key" |
| 1418 |
|
| •1419 |
something = { |
| 1420 |
'my::all_users': [ |
| 1421 |
{'name': 'John', 'age': 33}, |
| 1422 |
], |
| 1423 |
} |
| 1424 |
|
| •1425 |
def assertions(): |
| 1426 |
assert that(something).deep_equals({ |
| 1427 |
'my::all_users': [ |
| 1428 |
{'name': 'John', 'age': 30, 'foo': 'bar'}, |
| 1429 |
], |
| 1430 |
}) |
| 1431 |
|
| 1432 |
assert that(assertions).raises( |
| 1433 |
AssertionError, compat_repr( |
| 1434 |
"given\n" |
| 1435 |
"X = {{'my::all_users': [{{'age': 33, 'name': 'John'}}]}}\n" |
| 1436 |
" and\n" |
| 1437 |
"Y = {{'my::all_users': [{{'age': 30, 'foo': 'bar', 'name': 'John'}}]}}\n" |
| 1438 |
"X['my::all_users'][0] does not have the key \"{0}\" whereas Y['my::all_users'][0] has it" |
| 1439 |
).format(safe_repr('foo')) |
| 1440 |
) |
| 1441 |
|
| 1442 |
|
| •1443 |
def test_deep_equals_dict_level3_fails_extra_key(self): |
| 1444 |
"that() deep_equals(dict) failing on level 3 when has an extra key" |
| 1445 |
|
| •1446 |
something = { |
| 1447 |
'my::all_users': [ |
| 1448 |
{'name': 'John', 'age': 33, 'foo': 'bar'}, |
| 1449 |
], |
| 1450 |
} |
| 1451 |
|
| •1452 |
def assertions(): |
| 1453 |
assert that(something).deep_equals({ |
| 1454 |
'my::all_users': [ |
| 1455 |
{'name': 'John', 'age': 30}, |
| 1456 |
], |
| 1457 |
}) |
| 1458 |
|
| •1459 |
assert that(assertions).raises( |
| 1460 |
AssertionError, compat_repr( |
| 1461 |
"given\n" |
| 1462 |
"X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" |
| 1463 |
" and\n" |
| 1464 |
"Y = {{'my::all_users': [{{'age': 30, 'name': 'John'}}]}}\n" |
| 1465 |
"X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" |
| 1466 |
).format(safe_repr('foo'))) |
| 1467 |
|
| 1468 |
|
| •1469 |
def test_deep_equals_dict_level3_fails_different_key(self): |
| 1470 |
"that() deep_equals(dict) failing on level 3 when has an extra key" |
| 1471 |
|
| •1472 |
something = { |
| 1473 |
'my::all_users': [ |
| 1474 |
{'name': 'John', 'age': 33, 'foo': 'bar'}, |
| 1475 |
], |
| 1476 |
} |
| 1477 |
|
| •1478 |
def assertions(): |
| 1479 |
assert that(something).deep_equals({ |
| 1480 |
'my::all_users': [ |
| 1481 |
{'name': 'John', 'age': 33, 'bar': 'foo'}, |
| 1482 |
], |
| 1483 |
}) |
| 1484 |
|
| •1485 |
assert that(assertions).raises( |
| 1486 |
AssertionError, compat_repr( |
| 1487 |
"given\n" |
| 1488 |
"X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" |
| 1489 |
" and\n" |
| 1490 |
"Y = {{'my::all_users': [{{'age': 33, 'bar': 'foo', 'name': 'John'}}]}}\n" |
| 1491 |
"X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" |
| 1492 |
).format(safe_repr('foo'))) |
| 1493 |
|
| 1494 |
|
| 1495 |
def test_deep_equals_list_level2_fail_by_length_x_gt_y(self): |
| 1496 |
"that(list) deep_equals(list) failing by length (len(X) > len(Y))" |
| 1497 |
|
| 1498 |
something = {'iterable': ['one', 'yeah', 'awesome!']} |
| 1499 |
|
| 1500 |
def assertions(): |
| 1501 |
assert that(something).deep_equals({'iterable': ['one', 'yeah']}) |
| 1502 |
|
| 1503 |
assert that(assertions).raises( |
| 1504 |
AssertionError, compat_repr( |
| 1505 |
"given\n" \ |
| 1506 |
"X = {'iterable': ['one', 'yeah', 'awesome!']}\n" \ |
| 1507 |
" and\n" \ |
| 1508 |
"Y = {'iterable': ['one', 'yeah']}\n" \ |
| 1509 |
"X has 3 items whereas Y has only 2", |
| 1510 |
)) |
| 1511 |
|
| 1512 |
|
| 1513 |
def test_deep_equals_list_level2_fail_by_length_y_gt_x(self): |
| 1514 |
"that(list) deep_equals(list) failing by length (len(Y) > len(X))" |
| 1515 |
|
| 1516 |
something = ['one', 'yeah'] |
| 1517 |
|
| 1518 |
def assertions(): |
| 1519 |
assert that(something).deep_equals(['one', 'yeah', 'damn']) |
| 1520 |
|
| 1521 |
assert that(assertions).raises( |
| 1522 |
AssertionError, compat_repr( |
| 1523 |
"given\n" \ |
| 1524 |
"X = ['one', 'yeah']\n" \ |
| 1525 |
" and\n" \ |
| 1526 |
"Y = ['one', 'yeah', 'damn']\n" \ |
| 1527 |
"Y has 3 items whereas X has only 2", |
| 1528 |
)) |
| 1529 |
|
| 1530 |
|
| 1531 |
def test_function_decorated_with_wip_should_set_a_flag(self): |
| 1532 |
"@sure.work_in_progress should set an internal flag into `sure`" |
| 1533 |
|
| 1534 |
@sure.work_in_progress |
| 1535 |
def this_was_called(): |
| 1536 |
assert sure.it('is_running') |
| 1537 |
return True |
| 1538 |
|
| 1539 |
assert not sure._registry['is_running'] |
| 1540 |
assert this_was_called() |
| 1541 |
assert not sure._registry['is_running'] |
| 1542 |
|
| 1543 |
|
| 1544 |
def test_that_equals_fails(self): |
| 1545 |
"that() equals(string) when it's supposed to fail" |
| 1546 |
|
| 1547 |
something = "else" |
| 1548 |
|
| 1549 |
def fail(): |
| 1550 |
assert that('something').equals(something) |
| 1551 |
|
| 1552 |
assert that(fail).raises( |
| 1553 |
AssertionError, compat_repr( |
| 1554 |
"given\n" \ |
| 1555 |
"X = 'something'\n" \ |
| 1556 |
" and\n" \ |
| 1557 |
"Y = 'else'\n" \ |
| 1558 |
"X is 'something' whereas Y is 'else'", |
| 1559 |
)) |
| 1560 |
|
| 1561 |
|
| 1562 |
def test_deep_equals_weird(self): |
| 1563 |
part1 = [ |
| 1564 |
('Bootstraping Redis role', []), |
| 1565 |
('Restart scalarizr', []), |
| 1566 |
('Rebundle server', ['rebundle']), |
| 1567 |
('Use new role', ['rebundle']), |
| 1568 |
('Restart scalarizr after bundling', ['rebundle']), |
| 1569 |
('Bundling data', []), |
| 1570 |
('Modifying data', []), |
| 1571 |
('Reboot server', []), |
| 1572 |
('Backuping data on Master', []), |
| 1573 |
('Setup replication', []), |
| 1574 |
('Restart scalarizr in slave', []), |
| 1575 |
('Slave force termination', []), |
| 1576 |
('Slave delete EBS', ['ec2']), |
| 1577 |
('Setup replication for EBS test', ['ec2']), |
| 1578 |
('Writing on Master, reading on Slave', []), |
| 1579 |
('Slave -> Master promotion', []), |
| 1580 |
('Restart farm', ['restart_farm']), |
| 1581 |
] |
| 1582 |
|
| 1583 |
part2 = [ |
| 1584 |
('Bootstraping Redis role', ['rebundle', 'rebundle', 'rebundle']), |
| 1585 |
('Restart scalarizr', []), |
| 1586 |
('Rebundle server', ['rebundle']), |
| 1587 |
('Use new role', ['rebundle']), |
| 1588 |
('Restart scalarizr after bundling', ['rebundle']), |
| 1589 |
('Bundling data', []), |
| 1590 |
('Modifying data', []), |
| 1591 |
('Reboot server', []), |
| 1592 |
('Backuping data on Master', []), |
| 1593 |
('Setup replication', []), |
| 1594 |
('Restart scalarizr in slave', []), |
| 1595 |
('Slave force termination', []), |
| 1596 |
('Slave delete EBS', ['ec2']), |
| 1597 |
('Setup replication for EBS test', ['ec2']), |
| 1598 |
('Writing on Master, reading on Slave', []), |
| 1599 |
('Slave -> Master promotion', []), |
| 1600 |
('Restart farm', ['restart_farm']), |
| 1601 |
] |
| 1602 |
|
| •1603 |
expect(that(part1).equals).when.called_with(part2).should.throw("") |