"""Tests for get_user_headers.py"""
# pylint: disable=protected-access

from __future__ import (absolute_import, division, print_function,
                        with_statement, unicode_literals)

__author__ = "Stephan Sokolow (deitarion/SSokolow)"
__license__ = "MIT"

import datetime, math

import get_user_headers

def check_randomize_delay(base_delay, results):
    """Code shared between default and nondefault randomize_delay tests."""
    assert min(results) >= base_delay * 0.5
    assert max(results) <= base_delay * 1.5

def check_randomize_stddev(base_delay, results):
    """Code shared between default and non-default std. deviation tests."""
    mean = sum(results) / len(results)
    variance = sum((x - mean) ** 2 for x in results) / len(results)
    stddev = math.sqrt(variance)

    # TODO: Stop just guessing at the proper values for these thresholds
    min_val, max_val = (base_delay * 0.25), (base_delay * 0.5)
    assert min_val <= stddev <= max_val, (
        "not({} <= {} >= {})".format(min_val, stddev, max_val))

def check_timestamp_roundtrip(timestamp):
    dt = datetime.datetime.fromtimestamp(timestamp)
    ts = get_user_headers._timestamp(dt)
    dt_new = datetime.datetime.fromtimestamp(ts)
    assert ts == timestamp, '{} != {}'.format(ts, timestamp)
    assert dt_new == dt, '{} != {}'.format(dt_new, dt)

def test_default_randomize_delay():
    """randomize_delay(): 1 <= randomize_delay() <= 1.5"""
    results = [get_user_headers.randomize_delay() for _ in range(0, 10000)]
    check_randomize_delay(get_user_headers.DEFAULT_BASE_DELAY, results)

def test_nondefault_randomize_delay():
    """randomize_delay: 2.5 <= randomize_delay(5) <= 7.5"""
    base = 5
    results = [get_user_headers.randomize_delay(base) for _ in range(0, 10000)]
    check_randomize_delay(base, results)

def test_randomize_delay_distrib():
    """randomize_delay: Standard deviation is acceptable

    NOTE: This will need to be changed when I change the distribution of the
          random numbers to better simulate real human behaviour.

          (This is really just a poor proxy for doing proper statistical
          analysis on the function's output.)"""
    results = [get_user_headers.randomize_delay() for _ in range(0, 10000)]
    check_randomize_stddev(2, results)
    results = [get_user_headers.randomize_delay(5) for _ in range(0, 10000)]
    check_randomize_stddev(5, results)

# TODO: How difficult would it be to have a testcase which statistically
#       analyzes the generated delays for similarity to test data collected
#       from actual human activity?

def test_timestamp_epoch():
    """_timestamp(): round-trips correctly at the epoch"""
    check_timestamp_roundtrip(0)

def test_timestamp_recent():
    """_timestamp(): round-trips correctly at a typical time"""
    check_timestamp_roundtrip(1468673923)

@@ -1081,18 +1081,82 @@
 p):%0A    
-dt
+%22%22%22Code shared between timestamp round-tripping tests%22%22%22%0A    dtime
  = datet
@@ -1199,16 +1199,20 @@
 )%0A    ts
+tamp
  = get_u
@@ -1240,16 +1240,19 @@
 p(dt
+ime
 )%0A    dt
 _new
@@ -1247,16 +1247,19 @@
 )%0A    dt
+ime
 _new = d
@@ -1291,16 +1291,20 @@
 stamp(ts
+tamp
 )%0A    as
@@ -1310,16 +1310,20 @@
 ssert ts
+tamp
  == time
@@ -1349,16 +1349,20 @@
 ormat(ts
+tamp
 , timest
@@ -1379,16 +1379,19 @@
 ssert dt
+ime
 _new == 
@@ -1392,16 +1392,19 @@
 ew == dt
+ime
 , '%7B%7D !=
@@ -1421,16 +1421,19 @@
 t(dt
+ime
 _new, dt
 )%0A%0Ad
@@ -1428,16 +1428,19 @@
 _new, dt
+ime
 )%0A%0Adef t

Fix PyLint variable naming nitpicks in check_timestamp_roundtrip