<commit_before>[![Build Status](https://travis-ci.org/snudler6/time-travel.svg?branch=master)](https://travis-ci.org/snudler6/time-travel)

# time-travel
python time libraries mocking

### time-travel is the fun and easy way to unit-test time sensetive modules.
### Can write determenistic test for long and even infinite time scenarios.

## Usage

`TimeTravel` is context manager patching all time modules currently patched
in a single line.

### Examples

#### Skip timeouts

Tests are determenistic and take no time with time travel.

```python
with TimeTravel():  
    assert time.time() == 0
    time.sleep(3600)
    assert time.time() == 3600    
```

```python
with TimeTravel():
    assert datetime.today() == datetime.fromtimestamp(0)
    time.sleep(3600)
    assert datetime.today() == datetime.fromtimestamp(3600)
```

#### Patching event based modules

Can Patch and determine future events for event based modules using select:

```python
with TimeTravel() as t:
    event = mock.MagicMock()
    t.events_pool.add_future_event(2, event, t.events_types.select.WRITE)
    assert select.select([], [event], []) == ([], [event], [])
    assert time.time() == 2
```


## List of currently patched modules and functions

- time.time
- time.sleep
- datetime.datetime.today
- select.select
- select.poll

### Add your own patches to time-travel

time-travel uses python "entry points" to add external patches to it.<commit_msg>#20 - Some more documentation for snudler6's satisfaction<commit_after>[![Build Status](https://travis-ci.org/snudler6/time-travel.svg?branch=master)](https://travis-ci.org/snudler6/time-travel)

# time-travel
python time libraries mocking

### time-travel is the fun and easy way to unit-test time sensetive modules.
### Can write determenistic test for long and even infinite time scenarios.

## Usage

`TimeTravel` is context manager patching all time modules currently patched
in a single line.

### Examples

#### Skip timeouts

Tests are determenistic and take no time with time travel.

```python
with TimeTravel():  
    assert time.time() == 0
    time.sleep(3600)
    assert time.time() == 3600    
```

```python
with TimeTravel():
    assert datetime.today() == datetime.fromtimestamp(0)
    time.sleep(3600)
    assert datetime.today() == datetime.fromtimestamp(3600)
```

#### Patching event based modules

Can Patch and determine future events for event based modules using select:

```python
with TimeTravel() as t:
    event = mock.MagicMock()
    t.events_pool.add_future_event(2, event, t.events_types.select.WRITE)
    assert select.select([], [event], []) == ([], [event], [])
    assert time.time() == 2
```

Or using ``poll``:

```python
with TimeTravel() as t:
    fd = mock.MagicMock()
    t.events_pool.add_future_event(2, fd, select.POLLIN)

    poll = select.poll()
    poll.register(fd, select.POLLIN | select.POLLOUT)

    assert poll.poll() == [(fd, select.POLLIN)]
    assert time.time() == 2
```

## List of currently patched modules and functions

- time.time
- time.sleep
- datetime.datetime.today
- select.select
- select.poll

### Add your own patches to time-travel

time-travel uses python "entry points" to add external patches to it.
{'fraction_non_alphanumeric': 0.13069245165315035, 'fraction_numerical': 0.015283842794759825, 'mean_word_length': 4.0109375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4166464', 'n_tokens_mistral': 1041, 'n_tokens_neox': 962, 'n_words': 310}