import pandas as pd
from unittest.mock import patch, call
from etl.land_registry.LandRegistryClient import LandRegistryClient


class TestLandRegistryClient:
    @patch('pandas.read_csv')
    @patch('fuzzywuzzy.fuzz.ratio')
    def test_read(self, mock_fuzz, mock_read_csv):
        # setup mocks
        mock_read_csv.return_value = pd.DataFrame([
            ['1', '815000', '2022-06-28', 'SW6 3JA', 'F', 'N', 'L', 'FLAT', 'GROUND FLOOR', '6 DYMOCK STREET', None,
             'LONDON', 'HAMMERSMITH AND FULHAM', 'GREATER LONDON', 'A', 'Y'],
            ['2', '200', '2023-03-04', 'N16 0EG', 'S', 'N', 'F', '25', None, 'DEFOE ROAD', None, 'LONDON', 'HACKNEY',
             'GREATER LONDON', 'B', 'Y'],
            ['3', '300', '2023-03-05', 'N16 0EG', 'O', 'Y', 'L', '26', None, 'DEFOE ROAD', None, 'LONDON', 'HACKNEY',
             'GREATER LONDON', 'A', 'N']
        ], columns=LandRegistryClient.COLUMN_NAMES)
        mock_fuzz.side_effect = [70, 100, 50]

        # setup client
        addresses = [{
            'postcode': 'SW6 3JA', 'address1': 'GROUND FLOOR', 'address2': 'FLAT 6 DYMOCK STREET', 'address3': 'FULHAM',
            'address': 'Ground Floor Flat, 6 Dymock Street, Fulham', 'uprn': '34063921'
        }]
        client = LandRegistryClient(['path_to_data.csv'], addresses)

        # call read
        result = client.read()

        # assertions
        mock_read_csv.assert_called_once_with('path_to_data.csv', header=None)

        assert mock_fuzz.call_args_list == [
            call('GROUND FLOOR FLAT 6 DYMOCK STREET ', 'GROUND FLOOR FLAT 6 DYMOCK STREET FULHAM'),
            call('GROUND FLOOR FLAT 6 DYMOCK STREET ', 'GROUND FLOOR FLAT 6 DYMOCK STREET FULHAM')
        ]

        pd.testing.assert_frame_equal(result, pd.DataFrame({
            'price': ["815000"],
            'date_of_transfer': ['2022-06-28'],
            'property_type': ['F'],
            'old_new': ['N'],
            'duration': ['L'],
            'ppd_category_type': ['A'],
            'record_status': ['Y'],
            'uprn': ['34063921'],
            'address': ['Ground Floor Flat, 6 Dymock Street, Fulham']
        }))
