File size: 3,222 Bytes
ce4bc73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff73b92
ce4bc73
 
 
 
 
ff73b92
ce4bc73
 
 
 
ff73b92
ce4bc73
 
 
 
 
ff73b92
ce4bc73
 
 
 
 
 
ff73b92
ce4bc73
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Configuration for end-to-end tests.

This file contains pytest fixtures and configuration for end-to-end tests.
"""

import logging
import os

import pandas as pd
import pytest

from src.folio.portfolio import process_portfolio_data

# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


@pytest.fixture(scope="class")
def portfolio_data():
    """Load portfolio data from CSV file.

    Returns:
        pandas.DataFrame: The loaded data, or None if no data is available.
    """
    # Define paths to test data
    private_test_path = "private-data/test/test-portfolio.csv"
    sample_path = "sample-data/sample-portfolio.csv"

    # Try to load private test data first
    if os.path.exists(private_test_path):
        logger.info(f"Loading test data from {private_test_path}")
        return pd.read_csv(private_test_path)

    # Fall back to sample data
    if os.path.exists(sample_path):
        logger.info(f"Loading test data from {sample_path}")
        return pd.read_csv(sample_path)

    # No data available
    logger.warning("No test data available")
    return None


@pytest.fixture(scope="class")
def processed_portfolio(portfolio_data):
    """Process portfolio data using the same functions as the UI.

    Args:
        portfolio_data: The portfolio data loaded from CSV.

    Returns:
        tuple: A tuple containing (groups, summary, summary_dict).
    """
    if portfolio_data is None:
        pytest.skip("No portfolio data available for testing")

    logger.info("Processing portfolio data...")
    result = process_portfolio_data(portfolio_data)

    # Check the structure of the result
    if isinstance(result, tuple):
        if len(result) == 3:
            # Newer version: (groups, summary, cash_like_positions)
            groups, summary, cash_like_positions = result
        elif len(result) == 2:
            # Possible alternative: (groups, cash_like_positions)
            groups, cash_like_positions = result
            from src.folio.portfolio import calculate_portfolio_summary

            summary = calculate_portfolio_summary(groups, cash_like_positions, 0.0)
    else:
        # If result is not a tuple, it's likely just the groups
        groups = result
        from src.folio.portfolio import calculate_portfolio_summary

        summary = calculate_portfolio_summary(groups, [], 0.0)
        cash_like_positions = []

    # Ensure we have a valid summary object
    if not hasattr(summary, "to_dict"):
        logger.error("Error: summary object does not have to_dict method")
        logger.error(f"Type of summary: {type(summary)}")
        # Create a minimal summary for testing
        # Import here to avoid circular imports
        from src.folio.data_model import ExposureBreakdown, PortfolioSummary

        empty_exposure = ExposureBreakdown()
        summary = PortfolioSummary(
            net_market_exposure=0.0,
            portfolio_beta=0.0,
            long_exposure=empty_exposure,
            short_exposure=empty_exposure,
            options_exposure=empty_exposure,
        )

    # Convert summary to dictionary for use in tests
    summary_dict = summary.to_dict()

    return groups, summary, summary_dict