import random
import time

from . import TestCase

from graphite_api.intervals import Interval, IntervalSet
from graphite_api.node import LeafNode, BranchNode
from graphite_api.storage import Store


class FinderTest(TestCase):
    def test_custom_finder(self):
        store = Store([DummyFinder()])
        nodes = list(store.find("foo"))
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].path, 'foo')

        nodes = list(store.find('bar.*'))
        self.assertEqual(len(nodes), 10)
        node = nodes[0]
        self.assertEqual(node.path.split('.')[0], 'bar')

        time_info, series = node.fetch(100, 200)
        self.assertEqual(time_info, (100, 200, 10))
        self.assertEqual(len(series), 10)

    def test_multi_finder(self):
        store = Store([DummyFinder(), DummyFinder()])
        nodes = list(store.find("foo"))
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].path, 'foo')

        nodes = list(store.find('bar.*'))
        self.assertEqual(len(nodes), 10)
        node = nodes[0]
        self.assertEqual(node.path.split('.')[0], 'bar')

        time_info, series = node.fetch(100, 200)
        self.assertEqual(time_info, (100, 200, 10))
        self.assertEqual(len(series), 10)


class DummyReader(object):
    __slots__ = ('path',)

    def __init__(self, path):
        self.path = path

    def fetch(self, start_time, end_time):
        npoints = (end_time - start_time) // 10
        return (start_time, end_time, 10), [
            random.choice([None, 1, 2, 3]) for i in range(npoints)
        ]

    def get_intervals(self):
        return IntervalSet([Interval(time.time() - 3600, time.time())])


class DummyFinder(object):
    def find_nodes(self, query):
        if query.pattern == 'foo':
            yield BranchNode('foo')

        elif query.pattern == 'bar.*':
            for i in range(10):
                path = 'bar.{0}'.format(i)
                yield LeafNode(path, DummyReader(path))

@@ -1,12 +1,22 @@
+import os%0A
 import rando
@@ -52,17 +52,63 @@
 TestCase
-%0A
+, WHISPER_DIR%0A%0Afrom graphite_api.app import app
 %0Afrom gr
@@ -247,16 +247,57 @@
 t Store%0A
+from graphite_api._vendor import whisper%0A
 %0A%0Aclass 
@@ -2082,8 +2082,1479 @@
 (path))%0A
+%0A%0Aclass WhisperFinderTest(TestCase):%0A    _listdir_counter = 0%0A    _original_listdir = os.listdir%0A%0A    def test_whisper_finder(self):%0A        for db in (%0A            ('whisper_finder', 'foo.wsp'),%0A            ('whisper_finder', 'foo', 'bar', 'baz.wsp'),%0A            ('whisper_finder', 'bar', 'baz', 'baz.wsp'),%0A        ):%0A            db_path = os.path.join(WHISPER_DIR, *db)%0A            if not os.path.exists(os.path.dirname(db_path)):%0A                os.makedirs(os.path.dirname(db_path))%0A            whisper.create(db_path, %5B(1, 60)%5D)%0A%0A        def listdir_mock(d):%0A            self._listdir_counter += 1%0A            return self._original_listdir(d)%0A%0A        try:%0A            os.listdir = listdir_mock%0A            store = app.config%5B'GRAPHITE'%5D%5B'store'%5D%0A            print(%22store = %25s%22 %25 store)%0A%0A            self._listdir_counter = 0%0A            nodes = store.find('whisper_finder.foo')%0A            self.assertEqual(len(list(nodes)), 2)%0A            self.assertEqual(self._listdir_counter, 2)%0A%0A            self._listdir_counter = 0%0A            nodes = store.find('whisper_finder.foo.bar.baz')%0A            self.assertEqual(len(list(nodes)), 1)%0A            self.assertEqual(self._listdir_counter, 4)%0A%0A            self._listdir_counter = 0%0A            nodes = store.find('whisper_finder.*.ba?.%7Bbaz,foo%7D')%0A            self.assertEqual(len(list(nodes)), 2)%0A            self.assertEqual(self._listdir_counter, 6)%0A%0A        finally:%0A            os.listdir = self._original_listdir%0A

Add unittests for WhisperFinder