Skip to content

Commit

Permalink
InterposedTestCase allows Interposer subclassing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeking3 committed Sep 4, 2020
1 parent b065c3e commit ee456c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.6.1] - 2019-09-04

### Changed

- `InterposedTestCase` now allows Interposer to be subclassed.

## [0.6.0] - 2019-09-04

### Added

- `InterposedTestCase` was added to make testing even easier.

### Changed

- Updated the README.
- Provided an example.

## [0.5.0] - 2019-09-01

Initial Release.
22 changes: 12 additions & 10 deletions interposer/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,35 @@ class will record what they do, depending on what is patched in as a
wrapper.
"""

def setUp(self, *args, **kwargs) -> None:
def setUp(self, recordings: Path, cls: Interposer = Interposer) -> None:
"""
Prepare for recording or playback based on the test name.
Arguments:
cls (Interposer): allows subclassing Interposer
recordings (Path): the location of the recordings
"""
tapedir = kwargs.pop("recordings", None)
super().setUp(*args, **kwargs)
super().setUp()

assert tapedir, "recordings location must be specified"
assert isinstance(tapedir, Path), "recordings location must be a pathlib.Path"
assert recordings, "recordings location must be specified"
assert isinstance(
recordings, Path
), "recordings location must be a pathlib.Path"

self.mode = Mode.Recording if os.environ.get("RECORDING") else Mode.Playback
self.tape = tapedir / f"{self.id()}.db"
self.tape = recordings / f"{self.id()}.db"
if self.mode == Mode.Playback:
# decompress the recording
with gzip.open(str(self.tape) + ".gz", "rb") as fin:
with self.tape.open("wb") as fout:
fout.write(fin.read())
else:
tapedir.mkdir(parents=True, exist_ok=True)
recordings.mkdir(parents=True, exist_ok=True)

self.interposer = Interposer(self.tape, self.mode)
self.interposer = cls(self.tape, self.mode)
self.interposer.open()

def tearDown(self, *args, **kwargs) -> None:
def tearDown(self) -> None:
"""
Finalize recording or playback based on the test name.
"""
Expand All @@ -62,4 +64,4 @@ def tearDown(self, *args, **kwargs) -> None:
# self.tape is the uncompressed file - do not leave it around
self.tape.unlink()

super().tearDown(*args, **kwargs)
super().tearDown()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
description = "A code intercept wrapper with recording and playback options."
major = 0
minor = 6
patch = 0
patch = 1

# Everything below should be cookie-cutter

Expand Down

0 comments on commit ee456c6

Please sign in to comment.