/usr/lib/python3/dist-packages/testrepository/tests/test_repository.py is in python3-testrepository 0.0.20-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | #
# Copyright (c) 2009, 2010 Testrepository Contributors
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# license you chose for the specific language governing permissions and
# limitations under that license.
"""Tests for Repository support logic and the Repository contract."""
from datetime import (
datetime,
timedelta,
)
import doctest
from subunit import (
iso8601,
v2,
)
from testresources import TestResource
from testtools import (
clone_test_with_new_id,
PlaceHolder,
)
import testtools
from testtools.compat import _b
from testtools.testresult.doubles import (
ExtendedTestResult,
StreamResult,
)
from testtools.matchers import DocTestMatches, raises
from testrepository import repository
from testrepository.repository import file, memory
from testrepository.tests import (
ResourcedTestCase,
Wildcard,
)
from testrepository.tests.stubpackage import (
TempDirResource,
)
class RecordingRepositoryFactory(object):
"""Test helper for tests wanting to check repository factory callers."""
def __init__(self, calls, decorated):
self.calls = calls
self.factory = decorated
def initialise(self, url):
self.calls.append(('initialise', url))
return self.factory.initialise(url)
def open(self, url):
self.calls.append(('open', url))
return self.factory.open(url)
class DirtyTempDirResource(TempDirResource):
def __init__(self):
TempDirResource.__init__(self)
self._dirty = True
def isDirty(self):
return True
def _setResource(self, new_resource):
"""Set the current resource to a new value."""
self._currentResource = new_resource
self._dirty = True
class MemoryRepositoryFactoryResource(TestResource):
def make(self, dependency_resources):
return memory.RepositoryFactory()
# what repository implementations do we need to test?
repo_implementations = [
('file', {'repo_impl': file.RepositoryFactory(),
'resources': [('sample_url', DirtyTempDirResource())]
}),
('memory', {
'resources': [('repo_impl', MemoryRepositoryFactoryResource())],
'sample_url': 'memory:'}),
]
class Case(ResourcedTestCase):
"""Reference tests."""
def passing(self):
pass
def failing(self):
self.fail("oops")
def unexpected_success(self):
self.expectFailure("unexpected success", self.assertTrue, True)
def make_test(id, should_pass):
"""Make a test."""
if should_pass:
case = Case("passing")
else:
case = Case("failing")
return clone_test_with_new_id(case, id)
def run_timed(id, duration, result, enumeration=False):
"""Make and run a test taking duration seconds.
:param enumeration: If True, don't run, just enumerate.
"""
start = datetime.now(tz=iso8601.Utc())
if enumeration:
result.status(test_id=id, test_status='exists', timestamp=start)
else:
result.status(test_id=id, test_status='inprogress', timestamp=start)
result.status(test_id=id, test_status='success',
timestamp=start + timedelta(seconds=duration))
class TestRepositoryErrors(ResourcedTestCase):
def test_not_found(self):
url = 'doesntexistatall'
error = repository.RepositoryNotFound(url)
self.assertEqual(
'No repository found in %s. Create one by running "testr init".'
% url, str(error))
class TestRepositoryContract(ResourcedTestCase):
scenarios = repo_implementations
def get_failing(self, repo):
"""Analyze a failing stream from repo and return it."""
run = repo.get_failing()
analyzer = testtools.StreamSummary()
analyzer.startTestRun()
try:
run.get_test().run(analyzer)
finally:
analyzer.stopTestRun()
return analyzer
def get_last_run(self, repo):
"""Return the results from a stream."""
run = repo.get_test_run(repo.latest_id())
analyzer = testtools.StreamSummary()
analyzer.startTestRun()
try:
run.get_test().run(analyzer)
finally:
analyzer.stopTestRun()
return analyzer
def test_can_initialise_with_param(self):
repo = self.repo_impl.initialise(self.sample_url)
self.assertIsInstance(repo, repository.AbstractRepository)
def test_can_get_inserter(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
self.assertNotEqual(None, result)
def test_insert_stream_smoke(self):
# We can insert some data into the repository.
repo = self.repo_impl.initialise(self.sample_url)
class Case(ResourcedTestCase):
def method(self):
pass
case = Case('method')
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
case.run(legacy_result)
legacy_result.stopTestRun()
self.assertEqual(1, repo.count())
self.assertNotEqual(None, result.get_id())
def test_open(self):
self.repo_impl.initialise(self.sample_url)
self.repo_impl.open(self.sample_url)
def test_open_non_existent(self):
url = 'doesntexistatall'
self.assertThat(lambda: self.repo_impl.open(url),
raises(repository.RepositoryNotFound(url)))
def test_inserting_creates_id(self):
# When inserting a stream, an id is returned from stopTestRun.
# Note that this is no longer recommended - but kept for compatibility.
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
self.assertNotEqual(None, result.stopTestRun())
def test_count(self):
repo = self.repo_impl.initialise(self.sample_url)
self.assertEqual(0, repo.count())
result = repo.get_inserter()
result.startTestRun()
result.stopTestRun()
self.assertEqual(1, repo.count())
result = repo.get_inserter()
result.startTestRun()
result.stopTestRun()
self.assertEqual(2, repo.count())
def test_latest_id_empty(self):
repo = self.repo_impl.initialise(self.sample_url)
self.assertThat(repo.latest_id,
raises(KeyError("No tests in repository")))
def test_latest_id_nonempty(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
result.stopTestRun()
inserted = result.get_id()
self.assertEqual(inserted, repo.latest_id())
def test_get_failing_empty(self):
# repositories can return a TestRun with just latest failures in it.
repo = self.repo_impl.initialise(self.sample_url)
analyzed = self.get_failing(repo)
self.assertEqual(0, analyzed.testsRun)
def test_get_failing_one_run(self):
# repositories can return a TestRun with just latest failures in it.
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('passing', True).run(legacy_result)
make_test('failing', False).run(legacy_result)
legacy_result.stopTestRun()
analyzed = self.get_failing(repo)
self.assertEqual(1, analyzed.testsRun)
self.assertEqual(1, len(analyzed.errors))
self.assertEqual('failing', analyzed.errors[0][0].id())
def test_unexpected_success(self):
# Unexpected successes get forwarded too. (Test added because of a
# NameError in memory repo).
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
test = clone_test_with_new_id(Case('unexpected_success'), 'unexpected_success')
test.run(legacy_result)
legacy_result.stopTestRun()
analyzed = self.get_last_run(repo)
self.assertEqual(1, analyzed.testsRun)
self.assertEqual(1, len(analyzed.unexpectedSuccesses))
self.assertEqual('unexpected_success', analyzed.unexpectedSuccesses[0].id())
def test_get_failing_complete_runs_delete_missing_failures(self):
# failures from complete runs replace all failures.
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('passing', True).run(legacy_result)
make_test('failing', False).run(legacy_result)
make_test('missing', False).run(legacy_result)
legacy_result.stopTestRun()
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('passing', False).run(legacy_result)
make_test('failing', True).run(legacy_result)
legacy_result.stopTestRun()
analyzed = self.get_failing(repo)
self.assertEqual(1, analyzed.testsRun)
self.assertEqual(1, len(analyzed.errors))
self.assertEqual('passing', analyzed.errors[0][0].id())
def test_get_failing_partial_runs_preserve_missing_failures(self):
# failures from two runs add to existing failures, and successes remove
# from them.
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('passing', True).run(legacy_result)
make_test('failing', False).run(legacy_result)
make_test('missing', False).run(legacy_result)
legacy_result.stopTestRun()
result = repo.get_inserter(partial=True)
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('passing', False).run(legacy_result)
make_test('failing', True).run(legacy_result)
legacy_result.stopTestRun()
analyzed = self.get_failing(repo)
self.assertEqual(2, analyzed.testsRun)
self.assertEqual(2, len(analyzed.errors))
self.assertEqual(set(['passing', 'missing']),
set([test[0].id() for test in analyzed.errors]))
def test_get_test_run_missing_keyerror(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
result.stopTestRun()
inserted = result.get_id()
self.assertThat(lambda:repo.get_test_run(inserted - 1),
raises(KeyError))
def test_get_test_run(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
inserted = result.stopTestRun()
run = repo.get_test_run(inserted)
self.assertNotEqual(None, run)
def test_get_latest_run(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
inserted = result.stopTestRun()
run = repo.get_latest_run()
self.assertEqual(inserted, run.get_id())
def test_get_latest_run_empty_repo(self):
repo = self.repo_impl.initialise(self.sample_url)
self.assertRaises(KeyError, repo.get_latest_run)
def test_get_test_run_get_id(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
inserted = result.stopTestRun()
run = repo.get_test_run(inserted)
self.assertEqual(inserted, run.get_id())
def test_get_test_run_preserves_time(self):
self.skip('Fix me before releasing.')
# The test run outputs the time events that it received.
now = datetime(2001, 1, 1, 0, 0, 0, tzinfo=iso8601.Utc())
second = timedelta(seconds=1)
repo = self.repo_impl.initialise(self.sample_url)
test_id = self.getUniqueString()
test = make_test(test_id, True)
result = repo.get_inserter()
result.startTestRun()
result.status(timestamp=now, test_id=test_id, test_status='inprogress')
result.status(timestamp=(now + 1 * second), test_id=test_id, test_status='success')
inserted = result.stopTestRun()
run = repo.get_test_run(inserted)
result = ExtendedTestResult()
run.get_test().run(result)
self.assertEqual(
[('time', now),
('tags', set(), set()),
('startTest', Wildcard),
('time', now + 1 * second),
('addSuccess', Wildcard),
('stopTest', Wildcard),
('tags', set(), set()),
],
result._events)
def test_get_failing_get_id(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
result.startTestRun()
result.stopTestRun()
run = repo.get_failing()
self.assertEqual(None, run.get_id())
def test_get_failing_get_subunit_stream(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('testrepository.tests.test_repository.Case.method', False).run(legacy_result)
legacy_result.stopTestRun()
run = repo.get_failing()
as_subunit = run.get_subunit_stream()
stream = v2.ByteStreamToStreamResult(as_subunit)
log = StreamResult()
log.startTestRun()
try:
stream.run(log)
finally:
log.stopTestRun()
self.assertEqual(
log._events, [
('startTestRun',),
('status',
'testrepository.tests.test_repository.Case.method',
'inprogress',
None,
True,
None,
None,
False,
None,
None,
Wildcard),
('status',
'testrepository.tests.test_repository.Case.method',
None,
None,
True,
'traceback',
Wildcard,
True,
Wildcard,
None,
Wildcard),
('status',
'testrepository.tests.test_repository.Case.method',
'fail',
None,
True,
None,
None,
False,
None,
None,
Wildcard),
('stopTestRun',)
])
def test_get_subunit_from_test_run(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('testrepository.tests.test_repository.Case.method', True).run(legacy_result)
legacy_result.stopTestRun()
inserted = result.get_id()
run = repo.get_test_run(inserted)
as_subunit = run.get_subunit_stream()
stream = v2.ByteStreamToStreamResult(as_subunit)
log = StreamResult()
log.startTestRun()
try:
stream.run(log)
finally:
log.stopTestRun()
self.assertEqual(
log._events,
[
('startTestRun',),
('status',
'testrepository.tests.test_repository.Case.method',
'inprogress',
None,
True,
None,
None,
False,
None,
None,
Wildcard),
('status',
'testrepository.tests.test_repository.Case.method',
'success',
None,
True,
None,
None,
False,
None,
None,
Wildcard),
('stopTestRun',)
])
def test_get_test_from_test_run(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
make_test('testrepository.tests.test_repository.Case.method', True).run(legacy_result)
legacy_result.stopTestRun()
inserted = result.get_id()
run = repo.get_test_run(inserted)
test = run.get_test()
result = testtools.StreamSummary()
result.startTestRun()
try:
test.run(result)
finally:
result.stopTestRun()
self.assertEqual(1, result.testsRun)
def test_get_times_unknown_tests_are_unknown(self):
repo = self.repo_impl.initialise(self.sample_url)
test_ids = set(['foo', 'bar'])
self.assertEqual(test_ids, repo.get_test_times(test_ids)['unknown'])
def test_inserted_test_times_known(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
test_name = 'testrepository.tests.test_repository.Case.method'
run_timed(test_name, 0.1, legacy_result)
legacy_result.stopTestRun()
self.assertEqual({test_name: 0.1},
repo.get_test_times([test_name])['known'])
def test_inserted_exists_no_impact_on_test_times(self):
repo = self.repo_impl.initialise(self.sample_url)
result = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(result)
legacy_result.startTestRun()
test_name = 'testrepository.tests.test_repository.Case.method'
run_timed(test_name, 0.1, legacy_result)
legacy_result.stopTestRun()
result = repo.get_inserter()
result.startTestRun()
test_name = 'testrepository.tests.test_repository.Case.method'
run_timed(test_name, 0.2, result, True)
result.stopTestRun()
self.assertEqual({test_name: 0.1},
repo.get_test_times([test_name])['known'])
def test_get_test_ids(self):
repo = self.repo_impl.initialise(self.sample_url)
inserter = repo.get_inserter()
legacy_result = testtools.ExtendedToStreamDecorator(inserter)
legacy_result.startTestRun()
test_cases = [PlaceHolder(self.getUniqueString()) for r in range(5)]
for test_case in test_cases:
test_case.run(legacy_result)
legacy_result.stopTestRun()
run_id = inserter.get_id()
self.assertEqual(run_id, repo.latest_id())
returned_ids = repo.get_test_ids(run_id)
self.assertEqual([test.id() for test in test_cases], returned_ids)
|