def test_merge_date_ranges_back_to_back():
    """Test that if two date ranges abut each other, they are merged."""
    date_range_1 = DateRange(datetime(2017, 5, 2), datetime(2017, 5, 5))
    date_range_2 = DateRange(datetime(2017, 5, 6), datetime(2017, 5, 6))

    merged_ranges = DateRange.merge_date_ranges([date_range_1, date_range_2])

    assert merged_ranges == [DateRange(datetime(2017, 5, 2), datetime(2017, 5, 6))]
def test_date_range_overlap():
    """Test if two DateRanges overlap."""
    date_range = DateRange(datetime(2017, 1, 4), datetime(2017, 1, 1))
    date_range_overlap = DateRange(datetime(2017, 1, 4), datetime(2017, 1, 8))
    date_range_no_overlap = DateRange(datetime(2017, 1, 5), datetime(2017, 1, 8))

    assert DateRange.date_range_overlap(date_range, date_range_overlap)
    assert not DateRange.date_range_overlap(date_range, date_range_no_overlap)
def test_contains_date():
    """Test DateRange contains_date."""
    date_range = DateRange(datetime(2017, 1, 1), datetime(2017, 1, 4))
    in_date = datetime(2017, 1, 1)
    out_date = datetime(2017, 1, 7)

    assert date_range.contains_date(in_date)
    assert not date_range.contains_date(out_date)
    def test_find_episode_id(self):
        """Test _find_episode_id."""
        date_ranges = [DateRange(date(2017, 1, 1), date(2017, 1, 1))]
        assert self.measure._find_episode_id(self.bene_1_claim_1,
                                             date_ranges) == 0

        date_ranges = [
            DateRange(date(2016, 1, 1), date(2016, 1, 1)),
            DateRange(date(2017, 1, 1), date(2017, 1, 1))
        ]
        assert self.measure._find_episode_id(self.bene_1_claim_1,
                                             date_ranges) == 1
def test_merge_date_ranges_overlap():
    """Merge a list of DateRanges."""
    date_range_1 = DateRange(datetime(2017, 1, 1), datetime(2017, 1, 2))
    date_range_2 = DateRange(datetime(2017, 1, 1), datetime(2017, 1, 3))
    date_range_3 = DateRange(datetime(2017, 1, 3), datetime(2017, 1, 8))

    date_ranges = [date_range_1, date_range_2, date_range_3]

    merged_ranges = DateRange.merge_date_ranges(date_ranges)

    expected = [DateRange(datetime(2017, 1, 1), datetime(2017, 1, 8))]

    assert merged_ranges == expected
def test_date_range_init():
    """Test DateRange init."""
    earlier = datetime(2017, 1, 1)
    later = datetime(2017, 1, 4)
    date_range = DateRange(earlier, later)
    assert earlier == date_range.start
    assert later == date_range.end
def test_date_range_init_wrong_order():
    """Test init when dates in wrong order."""
    earlier = datetime(2017, 1, 1)
    later = datetime(2017, 1, 4)
    date_range = DateRange(later, earlier)
    assert earlier == date_range.start
    assert later == date_range.end
    def _get_mssa_date_ranges(self, claims):
        """
        Get mssa_date ranges by querying the IDR.

        Returns a dict of {bene_sk: [date_ranges]} that will need to be merged
        to keep only non-overlapping intervals.
        """
        bene_sks = {claim.bene_sk for claim in claims}
        start_date = config.get('calculation.start_date')
        end_date = config.get('calculation.end_date')
        mssa_query = idr_queries.get_mssa_query(
            bene_sks=bene_sks,
            encounter_codes=self.procedure_codes,
            start_date=start_date,
            end_date=end_date)

        rows = execute.execute(mssa_query)
        if not rows:
            logger.error('No MSSA date found despite provider '
                         'having submitted quality codes for Measure 407.')
            return {}

        mssa_date_ranges = collections.defaultdict(list)
        for row in rows:
            mssa_date_ranges[row['bene_sk']].append(
                DateRange(row['min_date'], row['max_date']))
        return mssa_date_ranges
    def test_merge_mssa_date_ranges(self):
        """Test dict merging of mssa_date_ranges."""
        mssa_date_ranges = {
            'bene_1': [
                DateRange(date(2017, 1, 1), date(2017, 1, 1)),
                DateRange(date(2017, 1, 1), date(2017, 1, 1))
            ],
            'bene_2': [
                DateRange(date(2017, 1, 1), date(2017, 1, 1)),
                DateRange(date(2017, 3, 1), date(2017, 3, 3))
            ]
        }

        reduced_mssa_date_ranges = self.measure._merge_mssa_date_ranges(
            mssa_date_ranges)

        expected = {
            'bene_1': [DateRange(date(2017, 1, 1), date(2017, 1, 1))],
            'bene_2': [
                DateRange(date(2017, 1, 1), date(2017, 1, 1)),
                DateRange(date(2017, 3, 1), date(2017, 3, 3))
            ]
        }

        assert reduced_mssa_date_ranges == expected
 def test_get_mssa_date_ranges_one_provider(self, execute):
     """Test _get_mssa_date_ranges."""
     execute.return_value = [{
         'bene_sk': 'bene_1',
         'min_date': date(2017, 1, 1),
         'max_date': date(2017, 1, 1)
     }]
     mssa_date_ranges = self.measure._get_mssa_date_ranges(self.claims)
     expected = {'bene_1': [DateRange(date(2017, 1, 1), date(2017, 1, 1))]}
     assert mssa_date_ranges == expected
    def _merge_mssa_date_ranges(mssa_date_ranges):
        """
        Reduce lists of ranges by merging overlapping date ranges.

        Returns a dict of {bene_sk: [date_ranges]}.
        """
        return {
            bene_sk: DateRange.merge_date_ranges(date_ranges)
            for bene_sk, date_ranges in mssa_date_ranges.items()
        }
    def test_get_mssa_episode_date_ranges(self, execute):
        """Test _get_mssa_episode_date_ranges."""
        execute.return_value = [{
            'bene_sk': 'bene_1',
            'min_date': date(2017, 1, 1),
            'max_date': date(2017, 1, 1)
        }, {
            'bene_sk': 'bene_1',
            'min_date': date(2017, 1, 1),
            'max_date': date(2017, 1, 5)
        }, {
            'bene_sk': 'bene_2',
            'min_date': date(2017, 1, 1),
            'max_date': date(2017, 1, 3)
        }]

        expected = {
            'bene_1': [DateRange(date(2017, 1, 1), date(2017, 1, 5))],
            'bene_2': [DateRange(date(2017, 1, 1), date(2017, 1, 3))]
        }

        assert self.measure._get_mssa_episode_date_ranges(
            self.claims) == expected
    def test_find_episode_id_claim_lines(self):
        """Test _find_episode_id if the start date doesn't overlap with date range."""
        date_ranges = [DateRange(date(2017, 5, 2), date(2017, 5, 6))]

        test_claim = claim.Claim({
            'bene_sk':
            'bene',
            'clm_ptnt_birth_dt':
            date(1954, 1, 1),
            'clm_bene_sex_cd':
            '1',
            'clm_from_dt':
            date(2017, 5, 1),
            'clm_thru_dt':
            date(2017, 5, 6),
            'dx_codes': [
                'A4101',
            ],
            'claim_lines': [{
                'clm_line_num': 3,
                'clm_line_hcpcs_cd': '93325',
                'mdfr_cds': ['26'],
                'clm_pos_code': '21',
                'clm_line_from_dt': date(2017, 5, 1),
                'clm_line_thru_dt': date(2017, 5, 1)
            }, {
                'clm_line_num': 2,
                'clm_line_hcpcs_cd': '93320',
                'mdfr_cds': ['26'],
                'clm_pos_code': '21',
                'clm_line_from_dt': date(2017, 5, 1),
                'clm_line_thru_dt': date(2017, 5, 1)
            }, {
                'clm_line_num': 4,
                'clm_line_hcpcs_cd': '99232',
                'mdfr_cds': [],
                'clm_pos_code': '21',
                'clm_line_from_dt': date(2017, 5, 6),
                'clm_line_thru_dt': date(2017, 5, 6)
            }, {
                'clm_line_num': 1,
                'clm_line_hcpcs_cd': '93312',
                'mdfr_cds': ['26'],
                'clm_pos_code': '21',
                'clm_line_from_dt': date(2017, 5, 1),
                'clm_line_thru_dt': date(2017, 5, 1)
            }]
        })
        assert self.measure._find_episode_id(test_claim, date_ranges) == 0
def test_string_representation():
    """Test that string representations of DateRange objects return expected output."""
    date_range = DateRange(datetime(2017, 1, 1), datetime(2017, 1, 2))
    assert date_range.__repr__() == 'DateRange(2017-01-01 00:00:00 to 2017-01-02 00:00:00)'
def test_contains_date_end():
    """Test DateRange contains_date."""
    date_range = DateRange(datetime(2017, 1, 1), datetime(2017, 1, 4))
    end_date = datetime(2017, 1, 4)

    assert date_range.contains_date(end_date)