def test_set_recents_for_label(monkeypatch): mock_redis_instance = MockRedis(None) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) input_values = [ TrackedKill(kill_id=8, kill_timestamp=datetime.datetime( 2017, 7, 30, 20, 0, 0, tzinfo=datetime.timezone.utc)), ] unit.set_recents_for_label('UNITTEST', input_values) assert 'zadd' == mock_redis_instance.method_called assert 1 == mock_redis_instance.called assert 'RECENT_UNITTEST' == mock_redis_instance.called_with_args[0] assert 1501444800 == mock_redis_instance.called_with_args[1] assert TrackedKill.to_json( input_values[0]) == mock_redis_instance.called_with_args[2] mock_redis_instance = MockRedis(None) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) input_values = [ TrackedKill(kill_id=8, kill_timestamp=datetime.datetime( 2017, 7, 30, 20, 0, 0, tzinfo=datetime.timezone.utc)), TrackedKill(kill_id=9, kill_timestamp=datetime.datetime( 2017, 7, 30, 20, 0, 1, tzinfo=datetime.timezone.utc)), ] unit.set_recents_for_label('UNITTEST', input_values) assert 2 == mock_redis_instance.called assert 1501444801 == mock_redis_instance.called_with_args[1] assert TrackedKill.to_json( input_values[1]) == mock_redis_instance.called_with_args[2]
def test_validate_tracked_kill(): with pytest.raises(TypeError): unit.validate_tracked_kill(None) with pytest.raises(ValueError): unit.validate_tracked_kill(TrackedKill()) unit.validate_tracked_kill(TrackedKill(kill_id=1))
def test_set_latest_for_label(monkeypatch): mock_redis_instance = MockRedis(None) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) unit.set_latest_for_label('DELETES', None) assert 'delete' == mock_redis_instance.method_called assert 'LATEST_DELETES' == mock_redis_instance.called_with_args[0] mock_redis_instance = MockRedis(None) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) a_kill = TrackedKill(kill_id=1000) unit.set_latest_for_label('SETS', a_kill) assert 'set' == mock_redis_instance.method_called assert 'LATEST_SETS' == mock_redis_instance.called_with_args[0] assert 1000 == TrackedKill.from_json( mock_redis_instance.called_with_args[1]).kill_id
def set_latest_for_label(label, value): conn = redis.get_redis_singleton() if value: value_as_json = TrackedKill.to_json(value) conn.set(LATEST_KEY_FORMAT.format(label), value_as_json) else: conn.delete(LATEST_KEY_FORMAT.format(label))
def get_latest_for_label(label): conn = redis.get_redis_singleton() response = conn.get(LATEST_KEY_FORMAT.format(label)) if response: return TrackedKill.from_json(response) else: return None
def set_recents_for_label(label, values): conn = redis.get_redis_singleton() key = RECENT_KEY_FORMAT.format(label) for value in values or []: score = unix_datetime.datetime_to_unix(value.kill_timestamp) value_as_json = TrackedKill.to_json(value) conn.zadd(key, score, value_as_json)
def test_get_recents_for_label(monkeypatch): mock_redis_instance = MockRedis([ TrackedKill.to_json(TrackedKill(kill_id=7)), TrackedKill.to_json(TrackedKill(kill_id=8)), ]) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) result = unit.get_recents_for_label( 'UNITTEST', datetime.datetime(2017, 7, 30, 20, 0, 0, tzinfo=datetime.timezone.utc)) assert 'zrevrangebyscore' == mock_redis_instance.method_called assert 'RECENT_UNITTEST' == mock_redis_instance.called_with_args[0] assert '+inf' == mock_redis_instance.called_with_args[1] assert 1501444800 == mock_redis_instance.called_with_args[2] assert isinstance(result, list) assert 2 == len(result) assert 7 == result[0].kill_id assert 8 == result[1].kill_id
def test_get_latest_for_label(monkeypatch): mock_redis_instance = MockRedis(None) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) result = unit.get_latest_for_label('RETURNS_NOTHING') assert 'get' == mock_redis_instance.method_called assert 'LATEST_RETURNS_NOTHING' == mock_redis_instance.called_with_args[0] assert result is None mock_redis_instance = MockRedis( TrackedKill.to_json(TrackedKill(kill_id=500))) monkeypatch.setattr(unit, 'redis', MockRedisDispenser(mock_redis_instance)) result = unit.get_latest_for_label('RETURNS_SOMETHING') assert 'get' == mock_redis_instance.method_called assert 'LATEST_RETURNS_SOMETHING' == mock_redis_instance.called_with_args[ 0] assert isinstance(result, TrackedKill) assert 500 == result.kill_id
def get_recents_for_label(label, start_date): conn = redis.get_redis_singleton() start_date_score = unix_datetime.datetime_to_unix(start_date) response = conn.zrevrangebyscore(RECENT_KEY_FORMAT.format(label), '+inf', start_date_score) or [] for index, value in enumerate(response): response[index] = TrackedKill.from_json(response[index]) return response
def test_set_for_tracking_label(monkeypatch): mock_repo = MockCacheRepository(None) monkeypatch.setattr(unit, 'cache_repository', mock_repo) unit.set_for_tracking_label('UNITTEST', None) assert 2 == mock_repo.called assert 'UNITTEST' == mock_repo.called_with_args[0] assert mock_repo.called_with_args[1] is None mock_repo = MockCacheRepository(None) monkeypatch.setattr(unit, 'cache_repository', mock_repo) input_values = [ TrackedKill(kill_id=1, kill_timestamp=datetime.datetime(2017, 1, 1)), TrackedKill(kill_id=2, kill_timestamp=datetime.datetime(2016, 12, 31)), ] unit.set_for_tracking_label('UNITTEST', input_values) assert 2 == mock_repo.called assert 'UNITTEST' == mock_repo.called_with_args[0] assert mock_repo.called_with_args[1] is input_values
def test_to_from_json(): result = Unit.to_json(None) assert result is None result = Unit.from_json(None) assert result is None a_kill = Unit(kill_id=50, kill_timestamp=datetime(2017, 7, 30), kill_tracking_label='UNITTEST') as_json = Unit.to_json(a_kill) assert isinstance(as_json, str) assert a_kill.kill_timestamp.isoformat() in as_json assert '50' in as_json assert 'UNITTEST' in as_json back_again = Unit.from_json(as_json) assert isinstance(back_again, Unit) assert a_kill.kill_id == back_again.kill_id assert a_kill.kill_timestamp == back_again.kill_timestamp assert a_kill.kill_tracking_label == back_again.kill_tracking_label
def test_validate_tracked_kill_list(monkeypatch): def mock_validate_tracked_kill_pass(*args, **kwargs): pass def mock_validate_tracked_kill_fail(*args, **kwargs): raise Exception('unit test') monkeypatch.setattr(unit, 'validate_tracked_kill', mock_validate_tracked_kill_fail) unit.validate_tracked_kill_list(None) with pytest.raises(TypeError): unit.validate_tracked_kill_list(1) with pytest.raises(Exception): unit.validate_tracked_kill_list([TrackedKill(kill_id=1)]) monkeypatch.setattr(unit, 'validate_tracked_kill', mock_validate_tracked_kill_pass) unit.validate_tracked_kill_list([TrackedKill(kill_id=1)]) unit.validate_tracked_kill_list([])
def convert_zk_response_to_tracked_kill(tracking_label, zk): """Convert zk response dict to a TrackedKill instance """ # Should this be an and? if not isinstance(tracking_label, str) or not isinstance(zk, dict): return None pk = zk['package'] tk = TrackedKill() tk.kill_tracking_label = tracking_label tk.kill_id = pk['killID'] tk.kill_timestamp = convert_zk_timestamp_to_datetime( pk['killmail']['killTime']) tk.ship_id = pk['killmail']['victim']['shipType']['id'] # structure kills do not have an associated character if 'character' in pk['killmail']['victim']: tk.character_id = pk['killmail']['victim']['character']['id'] tk.corporation_id = pk['killmail']['victim']['corporation']['id'] # players do not have to join an alliance if 'alliance' in pk['killmail']['victim']: tk.alliance_id = pk['killmail']['victim']['alliance']['id'] tk.total_value = pk['zkb']['totalValue'] tk.system_id = pk['killmail']['solarSystem']['id'] tk.more_info_href = 'https://zkillboard.com/kill/{}/'.format(pk['killID']) tk.full_response = json.dumps(zk) tk.character_name = None # deprecated tk.corporation_name = '' # deprecated tk.alliance_name = None # deprecated tk.ship_name = '' # deprecated tk.system_name = '' # deprecated return tk