def test_finder_missing_branch(self):
        '''
        Test that the finder can deal with a missing branch.
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 3)

        with self.assertRaises(ValueError):
            nodes = list(finder.find_nodes(query=FindQuery('*', None, None)))
    def test_finder_nested_dotted_nodes(self):
        '''
        Test that the finder can deal with nested nodes with dots
        (Since OpenTSDB allows that).
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 2)

        nodes = list(finder.find_nodes(query=FindQuery('*.*', None, None)))
        self.assertEqual(
            [node.name for node in nodes],
            ['leaf.with.dots'],
        )
    def test_finder_settings(self):
        '''
        Test that the finder can default to Django settings.
        '''

        finder = OpenTSDBFinder()
        self.assertEqual(finder.opentsdb_uri, 'http://localhost:9999')
        self.assertEqual(finder.opentsdb_tree, 999)
 def setUp(self):
     #self.settings_dict = copy.deepcopy(self.BASE_SETTINGS)
     self.finder = OpenTSDBFinder('http://localhost:4242/api/v1', 1)
     cache.clear()
class OpenTSDBFinderTestCase(test.TestCase):
    '''Test the finder class.'''

    def setUp(self):
        #self.settings_dict = copy.deepcopy(self.BASE_SETTINGS)
        self.finder = OpenTSDBFinder('http://*****:*****@mock.patch.object(app_settings, 'OPENTSDB_URI', 'http://*****:*****@mock.patch.object(app_settings, 'OPENTSDB_TREE', 999)
    def test_finder_settings(self):
        '''
        Test that the finder can default to Django settings.
        '''

        finder = OpenTSDBFinder()
        self.assertEqual(finder.opentsdb_uri, 'http://localhost:9999')
        self.assertEqual(finder.opentsdb_tree, 999)

    @with_httmock(mocked_urls)
    def test_finder_nodes(self):
        '''
        Test that the finder can find nodes.
        '''

        # Base query
        nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1', 'branch2', 'leaf'],
        )

        # One level down
        nodes = list(self.finder.find_nodes(query=FindQuery('*.leaf', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1.leaf', 'branch2.leaf']
        )

        nodes = list(self.finder.find_nodes(query=FindQuery('branch1.leaf', None, None)))

        self.assertEqual(
            [node.path for node in nodes],
            ['branch1.leaf'],
        )

    @with_httmock(mocked_urls)
    def test_finder_braces(self):
        '''
        Test that the finder can deal with brace expressions.
        '''

        nodes = list(self.finder.find_nodes(query=FindQuery('{branch1,leaf}', None, None)))
        self.assertEqual(
            [node.path for node in nodes],
            ['branch1', 'leaf'],
        )

    @with_httmock(mocked_urls)
    def test_finder_character_classes(self):
        '''
        Test that the finder can deal with character classes.
        '''

        nodes = list(self.finder.find_nodes(query=FindQuery('branch[1]', None, None)))
        self.assertEqual(
            [node.path for node in nodes],
            ['branch1'],
        )

    @with_httmock(mocked_urls)
    def test_finder_dotted_nodes(self):
        '''
        Test that the finder can deal with nodes with dots
        (Since OpenTSDB allows that).
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 2)

        nodes = list(finder.find_nodes(query=FindQuery('*', None, None)))
        self.assertEqual(
            [node.name for node in nodes],
            ['branch.with.dots'],
        )

    @with_httmock(mocked_urls)
    def test_finder_nested_dotted_nodes(self):
        '''
        Test that the finder can deal with nested nodes with dots
        (Since OpenTSDB allows that).
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 2)

        nodes = list(finder.find_nodes(query=FindQuery('*.*', None, None)))
        self.assertEqual(
            [node.name for node in nodes],
            ['leaf.with.dots'],
        )

    @with_httmock(mocked_urls)
    def test_finder_missing_branch(self):
        '''
        Test that the finder can deal with a missing branch.
        '''

        finder = OpenTSDBFinder('http://localhost:4242/api/v1/', 3)

        with self.assertRaises(ValueError):
            nodes = list(finder.find_nodes(query=FindQuery('*', None, None)))


    def test_caching(self):
        '''
        Test caching behaviour.
        '''
        with HTTMock(mocked_urls):
            nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

            self.assertEqual(
                [node.path for node in nodes],
                ['branch1', 'branch2', 'leaf'],
            )

        with HTTMock(bad_urls):
            nodes = list(self.finder.find_nodes(query=FindQuery('*', None, None)))

            self.assertEqual(
                [node.path for node in nodes],
                ['branch1', 'branch2', 'leaf'],
            )