|
1 | 1 | from pathlib import Path |
2 | 2 |
|
3 | | -from codeflash.code_utils.code_extractor import add_needed_imports_from_module |
| 3 | +from codeflash.code_utils.code_extractor import add_needed_imports_from_module, find_preexisting_objects |
| 4 | +from codeflash.code_utils.code_replacer import replace_functions_and_add_imports |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_add_needed_imports_from_module0() -> None: |
@@ -121,3 +122,230 @@ def belongs_to_function(name: Name, function_name: str) -> bool: |
121 | 122 | project_root = Path("/home/roger/repos/codeflash") |
122 | 123 | new_module = add_needed_imports_from_module(src_module, dst_module, src_path, dst_path, project_root) |
123 | 124 | assert new_module == expected |
| 125 | + |
| 126 | +def test_duplicated_imports() -> None: |
| 127 | + optim_code = '''from dataclasses import dataclass |
| 128 | +from recce.adapter.base import BaseAdapter |
| 129 | +from typing import Dict, List, Optional |
| 130 | +
|
| 131 | +@dataclass |
| 132 | +class DbtAdapter(BaseAdapter): |
| 133 | +
|
| 134 | + def build_parent_map(self, nodes: Dict, base: Optional[bool] = False) -> Dict[str, List[str]]: |
| 135 | + manifest = self.curr_manifest if base is False else self.base_manifest |
| 136 | + |
| 137 | + try: |
| 138 | + parent_map_source = manifest.parent_map |
| 139 | + except AttributeError: |
| 140 | + parent_map_source = manifest.to_dict()["parent_map"] |
| 141 | +
|
| 142 | + node_ids = set(nodes) |
| 143 | + parent_map = {} |
| 144 | + for k, parents in parent_map_source.items(): |
| 145 | + if k not in node_ids: |
| 146 | + continue |
| 147 | + parent_map[k] = [parent for parent in parents if parent in node_ids] |
| 148 | +
|
| 149 | + return parent_map |
| 150 | +''' |
| 151 | + |
| 152 | + original_code = '''import json |
| 153 | +import logging |
| 154 | +import os |
| 155 | +import uuid |
| 156 | +from contextlib import contextmanager |
| 157 | +from copy import deepcopy |
| 158 | +from dataclasses import dataclass, fields |
| 159 | +from errno import ENOENT |
| 160 | +from functools import lru_cache |
| 161 | +from pathlib import Path |
| 162 | +from typing import ( |
| 163 | + Any, |
| 164 | + Callable, |
| 165 | + Dict, |
| 166 | + Iterator, |
| 167 | + List, |
| 168 | + Literal, |
| 169 | + Optional, |
| 170 | + Set, |
| 171 | + Tuple, |
| 172 | + Type, |
| 173 | + Union, |
| 174 | +) |
| 175 | +
|
| 176 | +from recce.event import log_performance |
| 177 | +from recce.exceptions import RecceException |
| 178 | +from recce.util.cll import CLLPerformanceTracking, cll |
| 179 | +from recce.util.lineage import ( |
| 180 | + build_column_key, |
| 181 | + filter_dependency_maps, |
| 182 | + find_downstream, |
| 183 | + find_upstream, |
| 184 | +) |
| 185 | +from recce.util.perf_tracking import LineagePerfTracker |
| 186 | +
|
| 187 | +from ...tasks.profile import ProfileTask |
| 188 | +from ...util.breaking import BreakingPerformanceTracking, parse_change_category |
| 189 | +
|
| 190 | +try: |
| 191 | + import agate |
| 192 | + import dbt.adapters.factory |
| 193 | + from dbt.contracts.state import PreviousState |
| 194 | +except ImportError as e: |
| 195 | + print("Error: dbt module not found. Please install it by running:") |
| 196 | + print("pip install dbt-core dbt-<adapter>") |
| 197 | + raise e |
| 198 | +from watchdog.events import FileSystemEventHandler |
| 199 | +from watchdog.observers import Observer |
| 200 | +
|
| 201 | +from recce.adapter.base import BaseAdapter |
| 202 | +from recce.state import ArtifactsRoot |
| 203 | +
|
| 204 | +from ...models import RunType |
| 205 | +from ...models.types import ( |
| 206 | + CllColumn, |
| 207 | + CllData, |
| 208 | + CllNode, |
| 209 | + LineageDiff, |
| 210 | + NodeChange, |
| 211 | + NodeDiff, |
| 212 | +) |
| 213 | +from ...tasks import ( |
| 214 | + HistogramDiffTask, |
| 215 | + ProfileDiffTask, |
| 216 | + QueryBaseTask, |
| 217 | + QueryDiffTask, |
| 218 | + QueryTask, |
| 219 | + RowCountDiffTask, |
| 220 | + RowCountTask, |
| 221 | + Task, |
| 222 | + TopKDiffTask, |
| 223 | + ValueDiffDetailTask, |
| 224 | + ValueDiffTask, |
| 225 | +) |
| 226 | +from .dbt_version import DbtVersion |
| 227 | +
|
| 228 | +@dataclass |
| 229 | +class DbtAdapter(BaseAdapter): |
| 230 | +
|
| 231 | + def build_parent_map(self, nodes: Dict, base: Optional[bool] = False) -> Dict[str, List[str]]: |
| 232 | + manifest = self.curr_manifest if base is False else self.base_manifest |
| 233 | + manifest_dict = manifest.to_dict() |
| 234 | +
|
| 235 | + node_ids = nodes.keys() |
| 236 | + parent_map = {} |
| 237 | + for k, parents in manifest_dict["parent_map"].items(): |
| 238 | + if k not in node_ids: |
| 239 | + continue |
| 240 | + parent_map[k] = [parent for parent in parents if parent in node_ids] |
| 241 | +
|
| 242 | + return parent_map |
| 243 | +''' |
| 244 | + expected = '''import json |
| 245 | +import logging |
| 246 | +import os |
| 247 | +import uuid |
| 248 | +from contextlib import contextmanager |
| 249 | +from copy import deepcopy |
| 250 | +from dataclasses import dataclass, fields |
| 251 | +from errno import ENOENT |
| 252 | +from functools import lru_cache |
| 253 | +from pathlib import Path |
| 254 | +from typing import ( |
| 255 | + Any, |
| 256 | + Callable, |
| 257 | + Dict, |
| 258 | + Iterator, |
| 259 | + List, |
| 260 | + Literal, |
| 261 | + Optional, |
| 262 | + Set, |
| 263 | + Tuple, |
| 264 | + Type, |
| 265 | + Union, |
| 266 | +) |
| 267 | +
|
| 268 | +from recce.event import log_performance |
| 269 | +from recce.exceptions import RecceException |
| 270 | +from recce.util.cll import CLLPerformanceTracking, cll |
| 271 | +from recce.util.lineage import ( |
| 272 | + build_column_key, |
| 273 | + filter_dependency_maps, |
| 274 | + find_downstream, |
| 275 | + find_upstream, |
| 276 | +) |
| 277 | +from recce.util.perf_tracking import LineagePerfTracker |
| 278 | +
|
| 279 | +from ...tasks.profile import ProfileTask |
| 280 | +from ...util.breaking import BreakingPerformanceTracking, parse_change_category |
| 281 | +
|
| 282 | +try: |
| 283 | + import agate |
| 284 | + import dbt.adapters.factory |
| 285 | + from dbt.contracts.state import PreviousState |
| 286 | +except ImportError as e: |
| 287 | + print("Error: dbt module not found. Please install it by running:") |
| 288 | + print("pip install dbt-core dbt-<adapter>") |
| 289 | + raise e |
| 290 | +from watchdog.events import FileSystemEventHandler |
| 291 | +from watchdog.observers import Observer |
| 292 | +
|
| 293 | +from recce.adapter.base import BaseAdapter |
| 294 | +from recce.state import ArtifactsRoot |
| 295 | +
|
| 296 | +from ...models import RunType |
| 297 | +from ...models.types import ( |
| 298 | + CllColumn, |
| 299 | + CllData, |
| 300 | + CllNode, |
| 301 | + LineageDiff, |
| 302 | + NodeChange, |
| 303 | + NodeDiff, |
| 304 | +) |
| 305 | +from ...tasks import ( |
| 306 | + HistogramDiffTask, |
| 307 | + ProfileDiffTask, |
| 308 | + QueryBaseTask, |
| 309 | + QueryDiffTask, |
| 310 | + QueryTask, |
| 311 | + RowCountDiffTask, |
| 312 | + RowCountTask, |
| 313 | + Task, |
| 314 | + TopKDiffTask, |
| 315 | + ValueDiffDetailTask, |
| 316 | + ValueDiffTask, |
| 317 | +) |
| 318 | +from .dbt_version import DbtVersion |
| 319 | +
|
| 320 | +@dataclass |
| 321 | +class DbtAdapter(BaseAdapter): |
| 322 | +
|
| 323 | + def build_parent_map(self, nodes: Dict, base: Optional[bool] = False) -> Dict[str, List[str]]: |
| 324 | + manifest = self.curr_manifest if base is False else self.base_manifest |
| 325 | + |
| 326 | + try: |
| 327 | + parent_map_source = manifest.parent_map |
| 328 | + except AttributeError: |
| 329 | + parent_map_source = manifest.to_dict()["parent_map"] |
| 330 | +
|
| 331 | + node_ids = set(nodes) |
| 332 | + parent_map = {} |
| 333 | + for k, parents in parent_map_source.items(): |
| 334 | + if k not in node_ids: |
| 335 | + continue |
| 336 | + parent_map[k] = [parent for parent in parents if parent in node_ids] |
| 337 | +
|
| 338 | + return parent_map |
| 339 | +''' |
| 340 | + |
| 341 | + function_name: str = "DbtAdapter.build_parent_map" |
| 342 | + preexisting_objects: set[tuple[str, tuple[FunctionParent, ...]]] = find_preexisting_objects(original_code) |
| 343 | + new_code: str = replace_functions_and_add_imports( |
| 344 | + source_code=original_code, |
| 345 | + function_names=[function_name], |
| 346 | + optimized_code=optim_code, |
| 347 | + module_abspath=Path(__file__).resolve(), |
| 348 | + preexisting_objects=preexisting_objects, |
| 349 | + project_root_path=Path(__file__).resolve().parent.resolve(), |
| 350 | + ) |
| 351 | + assert new_code == expected |
0 commit comments