'''Tests of the statistics worker.'''
import json

import mock

import dnstwister.workers.stats


def test_delta_domain_retriever_returns_domains_list(f_httpretty, monkeypatch):
    given_worker_will_retrieve_json(f_httpretty, monkeypatch, json.dumps({
        'title': 'deltas datasource',
        'fields': ['domain'],
        'types': [25],
        'type_names': ['text'],
        'values': [
            ['mazon.com'],
            ['amazont.com'],
        ]
    }))

    domains = when_the_worker_is_called()

    assert set(domains) == set(('mazon.com', 'amazont.com'))


def test_delta_domain_retriever_filters_invalid_domains(f_httpretty, monkeypatch):
    given_worker_will_retrieve_json(f_httpretty, monkeypatch, json.dumps({
        'title': 'deltas datasource',
        'fields': ['domain'],
        'types': [25],
        'type_names': ['text'],
        'values': [
            [None],
            ['??:SDmazon.com'],
            ['amazont.com'],
        ]
    }))

    domains = when_the_worker_is_called()

    assert set(domains) == set(('amazont.com',))


# Givens

def given_worker_will_retrieve_json(f_httpretty, monkeypatch, response_json):
    f_httpretty.register_uri(
        f_httpretty.GET,
        'http://127.0.0.1:9876/out.json',
        body=response_json
    )
    monkeypatch.setenv('DELTAS_URL', 'http://127.0.0.1:9876/out.json')


# Whens

def when_the_worker_is_called():
    return dnstwister.workers.stats.get_delta_domains()

@@ -759,135 +759,8 @@
 (%7B%0D%0A
-        'title': 'deltas datasource',%0D%0A        'fields': %5B'domain'%5D,%0D%0A        'types': %5B25%5D,%0D%0A        'type_names': %5B'text'%5D,%0D%0A
     
@@ -768,32 +768,32 @@
    'values': %5B%0D%0A
+
             %5BNon
@@ -1081,67 +1081,17 @@
    f
-_httpretty.register_uri(%0D%0A        f_httpretty.GET,%0D%0A       
+ake_url =
  'ht
@@ -1119,26 +1119,73 @@
 ut.json'
-,
 %0D%0A    
-   
+f_httpretty.register_uri(f_httpretty.GET, fake_url,
  body=re
@@ -1195,22 +1195,16 @@
 nse_json
-%0D%0A    
 )%0D%0A    m
@@ -1239,40 +1239,16 @@
 L', 
-'http://127.0.0.1:9876/out.json'
+fake_url
 )%0D%0A%0D

Test refactoring