Пример #1
0
 def test_method_mapping(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     table_map = "test_map_method"
     test_map_method = Mock()
     channel_import.test_map_method = test_map_method
     mapper = channel_import.generate_table_mapper(table_map=table_map)
     self.assertEqual(mapper, test_map_method)
Пример #2
0
 def test_base_table_mapper(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     class_mock = Mock()
     [record for record in channel_import.base_table_mapper(class_mock)]
     channel_import.destination.assert_has_calls(
         [call.session.query(class_mock),
          call.session.query().all()])
Пример #3
0
 def test_no_column_mapping(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     mappings = {"test_attr": "test_attr_mapped"}
     mapper = channel_import.generate_row_mapper(mappings=mappings)
     record = Mock(spec=["test_attr"])
     with self.assertRaises(AttributeError):
         mapper(record, "test_attr")
Пример #4
0
 def test_column_name_mapping(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     mappings = {"test_attr": "test_attr_mapped"}
     mapper = channel_import.generate_row_mapper(mappings=mappings)
     record = MagicMock()
     record.test_attr_mapped = "test_val"
     self.assertEqual(mapper(record, "test_attr"), "test_val")
Пример #5
0
 def test_method_mapping(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     mappings = {"test_attr": "test_map_method"}
     mapper = channel_import.generate_row_mapper(mappings=mappings)
     record = {}
     test_map_method = Mock()
     test_map_method.return_value = "test_val"
     channel_import.test_map_method = test_map_method
     self.assertEqual(mapper(record, "test_attr"), "test_val")
Пример #6
0
 def test_no_models_unflushed_rows_passed_through(self, apps_mock,
                                                  tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     record_mock = MagicMock(spec=["__table__"])
     channel_import.destination.get_class.return_value = record_mock
     self.assertEqual(
         0,
         channel_import.table_import(MagicMock(), lambda x, y: None,
                                     lambda x: [], 0),
     )
Пример #7
0
 def test_destination_tree_ids(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     class_mock = Mock()
     channel_import.destination.get_class.return_value = class_mock
     channel_import.get_all_destination_tree_ids()
     channel_import.destination.assert_has_calls([
         call.session.query(class_mock.tree_id),
         call.session.query().distinct(),
         call.session.query().distinct().all(),
     ])
Пример #8
0
 def test_no_merge_records_bulk_insert_flush(self, apps_mock, tree_id_mock,
                                             BridgeMock):
     channel_import = ChannelImport("test")
     record_mock = MagicMock(spec=["__table__"])
     record_mock.__table__.columns.items.return_value = [("test_attr",
                                                          MagicMock())]
     channel_import.destination.get_class.return_value = record_mock
     channel_import.table_import(MagicMock(), lambda x, y: "test_val",
                                 lambda x: [{}] * 10000, 0)
     channel_import.destination.session.flush.assert_called_once_with()
Пример #9
0
 def test_get_config(self, db_path_mock, apps_mock, tree_id_mock,
                     BridgeMock):
     ChannelImport("test")
     apps_mock.assert_has_calls([
         call.get_app_config("content"),
         call.get_app_config().get_models(include_auto_created=True),
     ])
Пример #10
0
    def test_merge_records_merge_flush(self, apps_mock, tree_id_mock,
                                       BridgeMock):
        from kolibri.core.content.utils.channel_import import merge_models

        channel_import = ChannelImport("test")
        record_mock = Mock(spec=["__table__"])
        record_mock.__table__.columns.items.return_value = [("test_attr",
                                                             MagicMock())]
        channel_import.destination.get_class.return_value = record_mock
        model_mock = Mock()
        model_mock._meta.pk.name = "test_attr"
        merge_models.append(model_mock)
        channel_import.merge_record = Mock()
        channel_import.table_import(model_mock, lambda x, y: "test_val",
                                    lambda x: [{}] * 10000, 0)
        channel_import.destination.session.flush.assert_called_once_with()
Пример #11
0
 def test_two_bridges(self, db_path_mock, apps_mock, tree_id_mock,
                      BridgeMock):
     db_path_mock.return_value = "test"
     ChannelImport("test")
     BridgeMock.assert_has_calls([
         call(sqlite_file_path="test"),
         call(app_name="content", schema_version=CONTENT_SCHEMA_VERSION),
     ])
Пример #12
0
 def test_import_channel_methods_called(self, apps_mock, tree_id_mock,
                                        BridgeMock):
     channel_import = ChannelImport("test")
     model_mock = Mock(spec=["__name__"])
     channel_import.content_models = [model_mock]
     mapping_mock = Mock()
     channel_import.schema_mapping = {model_mock: mapping_mock}
     with patch.object(channel_import, "generate_row_mapper"), patch.object(
             channel_import, "generate_table_mapper"), patch.object(
                 channel_import, "table_import"), patch.object(
                     channel_import, "check_and_delete_existing_channel"):
         channel_import.import_channel_data()
         channel_import.generate_row_mapper.assert_called_once_with(
             mapping_mock.get("per_row"))
         channel_import.generate_table_mapper.assert_called_once_with(
             mapping_mock.get("per_table"))
         channel_import.table_import.assert_called_once()
         channel_import.check_and_delete_existing_channel.assert_called_once(
         )
         channel_import.destination.session.commit.assert_called_once_with()
Пример #13
0
 def test_channel_id(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     self.assertEqual(channel_import.channel_id, "test")
Пример #14
0
 def test_base_mapper(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     mapper = channel_import.generate_table_mapper()
     self.assertEqual(mapper, channel_import.base_table_mapper)
Пример #15
0
 def test_no_column_mapping(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     table_map = "test_map_method"
     with self.assertRaises(AttributeError):
         channel_import.generate_table_mapper(table_map=table_map)
Пример #16
0
 def test_end(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     channel_import.end()
     channel_import.destination.end.assert_has_calls([call(), call()])
Пример #17
0
 def test_tree_id(self, apps_mock, tree_id_mock, BridgeMock):
     ChannelImport("test")
     tree_id_mock.assert_called_once_with()
Пример #18
0
 def test_one_two(self, apps_mock, tree_ids_mock, BridgeMock):
     tree_ids_mock.return_value = [2]
     channel_import = ChannelImport("test")
     self.assertEqual(channel_import.find_unique_tree_id(), 1)
Пример #19
0
 def test_base_mapper(self, apps_mock, tree_id_mock, BridgeMock):
     channel_import = ChannelImport("test")
     mapper = channel_import.generate_row_mapper()
     record = MagicMock()
     record.test_attr = "test_val"
     self.assertEqual(mapper(record, "test_attr"), "test_val")