<commit_before># -*- coding: utf-8 -*-

from ambry.library import new_library
from ambry.library.warehouse import Warehouse

from test.factories import PartitionFactory
from test.test_base import TestBase

try:
    # py2, mock is external lib.
    from mock import patch, MagicMock
except ImportError:
    # py3, mock is included
    from unittest.mock import patch, MagicMock


class WarehouseTest(TestBase):
    def setUp(self):
        super(self.__class__, self).setUp()
        rc = self.get_rc()
        self.library = new_library(rc)
        self.warehouse = Warehouse(self.library)

    # __init__ tests
    def _test_uses_library_db(self):
        # FIXME:
        pass

    def _test_uses_db_from_config(self):
        # FIXME:
        pass

    # query tests
    def test_uses_library_driver_backend(self):
        # FIXME:
        pass

    @patch('ambry.library.warehouse.Warehouse._install')
    @patch('ambry.library.warehouse.Warehouse._get_table_name')
    def test_installs_each_ref_found_in_the_query(self, fake_get, fake_install):
        w = Warehouse(self.library)
        PartitionFactory._meta.sqlalchemy_session = self.library.database.session
        partition = PartitionFactory()
        fake_get.return_value = partition.vid
        w.query('SELECT * FROM {};'.format(partition.vid))
        fake_install.assert_called_once_with(partition.vid)

    # _get_table_name tests
    # FIXME:

    # _install tests
    @patch('ambry.library.warehouse.add_partition')
    @patch('ambry.library.Library.partition')
    def test_creates_virtual_table_for_given_partition(self, fake_partition, fake_add):
        w = Warehouse(self.library)
        PartitionFactory._meta.sqlalchemy_session = self.library.database.session
        partition = PartitionFactory()
        fake_partition.return_value = partition  # FIXME: This should be bundle partition.
        w._install(partition.vid)
        self.assertEqual(len(fake_add.mock_calls), 1)<commit_msg>Warehouse unit tests fixed. #140.<commit_after># -*- coding: utf-8 -*-

from ambry.library import new_library
from ambry.library.warehouse import Warehouse

from test.factories import PartitionFactory
from test.test_base import ConfigDatabaseTestBase

try:
    # py2, mock is external lib.
    from mock import patch
except ImportError:
    # py3, mock is included
    from unittest.mock import patch


class WarehouseTest(ConfigDatabaseTestBase):
    def setUp(self):
        super(self.__class__, self).setUp()
        rc = self.get_rc()
        self.library = new_library(rc)
        self.warehouse = Warehouse(self.library)

    # __init__ tests
    def _test_uses_library_db(self):
        # FIXME:
        pass

    def _test_uses_db_from_config(self):
        # FIXME:
        pass

    # query tests
    def test_uses_library_driver_backend(self):
        # FIXME:
        pass

    def test_sends_query_to_database_backend(self):
        w = Warehouse(self.library)
        PartitionFactory._meta.sqlalchemy_session = self.library.database.session
        partition = PartitionFactory()
        with patch.object(w._backend, 'query') as fake_query:
            query = 'SELECT * FROM {};'.format(partition.vid)
            w.query(query)

            # backend.query called once.
            self.assertEqual(len(fake_query.mock_calls), 1)

            # second argument of the call was the same query.
            self.assertEqual(fake_query.mock_calls[0][1][1], query)

    # install tests
    def test_finds_partition_by_refs_and_installs_partition_to_backend(self):
        w = Warehouse(self.library)
        PartitionFactory._meta.sqlalchemy_session = self.library.database.session
        partition = PartitionFactory()

        with patch.object(w._backend, 'install') as fake_install:
            w.install(partition.vid)

            # backend.query called once.
            self.assertEqual(len(fake_install.mock_calls), 1)

            # second argument of the is found partition.
            self.assertEqual(fake_install.mock_calls[0][1][1].ref, partition.ref)
{'fraction_non_alphanumeric': 0.09030347890451518, 'fraction_numerical': 0.0044411547002220575, 'mean_word_length': 2.9745098039215687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6633764', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1187, 'n_words': 249}