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