コード例 #1
0
 def setUp(self, with_data=True, use_mysql=True):
     super(EhrReceiptDaoTest, self).setUp(with_data=with_data,
                                          use_mysql=use_mysql)
     self.setup_fake()
     self.calendar_dao = CalendarDao()
     self.org_dao = OrganizationDao()
     self.hpo_dao = HPODao()
     self.participant_dao = ParticipantDao()
     self.summary_dao = ParticipantSummaryDao()
     self.ehr_receipt_dao = EhrReceiptDao()
     self._setup_initial_data()
コード例 #2
0
    def setUp(self, **kwargs):
        super(MetricsEhrApiTestBase, self).setUp(use_mysql=True, **kwargs)
        self.dao = ParticipantDao()
        self.ps_dao = ParticipantSummaryDao()
        self.ehr_receipt_dao = EhrReceiptDao()
        self.ps = ParticipantSummary()
        self.calendar_dao = CalendarDao()
        self.site_dao = SiteDao()
        self.hpo_dao = HPODao()
        self.org_dao = OrganizationDao()

        self.hpo_test = self._make_hpo(hpoId=TEST_HPO_ID,
                                       name=TEST_HPO_NAME,
                                       displayName='Test',
                                       organizationType=OrganizationType.UNSET)

        self.hpo_foo = self._make_hpo(hpoId=10, name='FOO', displayName='Foo')
        self.hpo_bar = self._make_hpo(hpoId=11, name='BAR', displayName='Bar')

        self.org_foo_a = self._make_org(organizationId=10,
                                        externalId='FOO_A',
                                        displayName='Foo A',
                                        hpoId=self.hpo_foo.hpoId)
        self.org_bar_a = self._make_org(organizationId=11,
                                        externalId='BAR_A',
                                        displayName='Bar A',
                                        hpoId=self.hpo_bar.hpoId)
コード例 #3
0
    def setUp(self, **kwargs):
        super(MetricsEhrMultiEndpointTest, self).setUp(**kwargs)

        #
        # insert some test data
        #
        for date in iter_dates(datetime.date(2017, 12, 30),
                               datetime.date(2018, 2, 1)):
            calendar_day = Calendar(day=date)
            CalendarDao().insert(calendar_day)
        # noinspection PyArgumentList
        participant_1 = Participant(participantId=1, biobankId=4)
        summary_1 = self._make_participant(
            participant_1,
            'Alice',
            'Aardvark',
            self.hpo_foo,
            self.org_foo_a,
            time_int=datetime.datetime(2018, 1, 2),
            time_mem=datetime.datetime(2018, 1, 3),
            time_fp=datetime.datetime(2018, 1, 4))
        # noinspection PyArgumentList
        participant_2 = Participant(participantId=2, biobankId=5)
        summary_2 = self._make_participant(
            participant_2,
            'Bo',
            'Badger',
            self.hpo_bar,
            self.org_bar_a,
            time_int=datetime.datetime(2018, 1, 3),
            time_mem=datetime.datetime(2018, 1, 4),
            time_fp=datetime.datetime(2018, 1, 5))
        self._update_ehr(summary_1, update_time=datetime.datetime(2018, 1, 5))
        self._update_ehr(summary_2, update_time=datetime.datetime(2018, 1, 6))
  def setUp(self):
    super(ParticipantCountsOverTimeApiTest, self).setUp(use_mysql=True)
    self.dao = ParticipantDao()
    self.ps_dao = ParticipantSummaryDao()
    self.ps = ParticipantSummary()
    self.calendar_dao = CalendarDao()
    self.hpo_dao = HPODao()

    # Needed by ParticipantCountsOverTimeApi
    self.hpo_dao.insert(HPO(hpoId=TEST_HPO_ID, name=TEST_HPO_NAME, displayName='Test',
                       organizationType=OrganizationType.UNSET))

    self.time1 = datetime.datetime(2017, 12, 31)
    self.time2 = datetime.datetime(2018, 1, 1)
    self.time3 = datetime.datetime(2018, 1, 2)
    self.time4 = datetime.datetime(2018, 1, 3)

    # Insert 2 weeks of dates
    curr_date = datetime.date(2017, 12, 22)
    for _ in xrange(0, 14):
      calendar_day = Calendar(day=curr_date )
      CalendarDao().insert(calendar_day)
      curr_date = curr_date + datetime.timedelta(days=1)
コード例 #5
0
  def test_consented_counts(self):
    # Set up data
    for date in iter_dates(
      datetime.date(2017, 12, 30),
      datetime.date(2018, 2, 1)
    ):
      calendar_day = Calendar(day=date)
      CalendarDao().insert(calendar_day)

    # noinspection PyArgumentList
    participant_1 = Participant(participantId=1, biobankId=4)
    self._make_participant(
      participant_1, 'Alice', 'Aardvark', self.hpo_foo, self.org_foo_a,
      time_int=datetime.datetime(2018, 1, 2),
      time_mem=datetime.datetime(2018, 1, 3),
      time_study=datetime.datetime(2018, 1, 3),
      time_fp=datetime.datetime(2018, 1, 4),
    )

    # noinspection PyArgumentList
    participant_2 = Participant(participantId=2, biobankId=5)
    self._make_participant(
      participant_2, 'Bo', 'Badger', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_study=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )

    # Begin testing
    response = self.send_get('MetricsEHR')
    self.assertEqual(response['metrics']['EHR_CONSENTED'], 2)
    self.assertEqual(
      response['metrics']['EHR_CONSENTED'],
      sum([o['total_ehr_consented'] for o in response['organization_metrics'].values()])
    )

    # test with organization filtering
    response = self.send_get('MetricsEHR', query_string=urllib.urlencode({
      'organization': 'foo_a',
    }))
    self.assertEqual(response['metrics']['EHR_CONSENTED'], 1)
    self.assertEqual(
      response['metrics']['EHR_CONSENTED'],
      sum([o['total_ehr_consented'] for o in response['organization_metrics'].values()])
    )
コード例 #6
0
 def get_participant_ehr_metrics_over_time_data(self,
                                                start_date,
                                                end_date,
                                                interval,
                                                organization_ids=None):
     """
 EHR Consented vs EHR Received over time
 """
     interval_query = CalendarDao.get_interval_query(start=start_date,
                                                     end=end_date,
                                                     interval_key=interval)
     ehr_query = self._get_participant_ehr_metrics_over_time_query(
         interval_query, organization_ids)
     with self.session() as session:
         ehr_cursor = session.execute(ehr_query)
     return [{
         'date': row_dict['start_date'],
         'metrics': {
             'EHR_CONSENTED': row_dict['consented_count'],
             'EHR_RECEIVED': row_dict['received_count'],
         }
     } for row_dict in
             [dict(zip(ehr_cursor.keys(), row)) for row in ehr_cursor]]
コード例 #7
0
    def get_active_organization_counts_in_interval(self,
                                                   start_date,
                                                   end_date,
                                                   interval,
                                                   organization_ids=None):
        """
    Get number of receipts per organization received in specific time intervals

    :param start_date: query min date
    :param end_date: query max date
    :param interval: time interval (one of the INTERVAL constants)
    :param organization_ids: (optional) filter results to matching organizations
    :return: dictionary of organizationId:list(dict(date=X, count=Y), ...)
    :rtype: dict
    """
        interval_query = CalendarDao.get_interval_query(start=start_date,
                                                        end=end_date,
                                                        interval_key=interval,
                                                        include_end_date=True)
        active_org_count_conditions = (
            (EhrReceipt.receiptTime >= interval_query.c.start_date)
            & (EhrReceipt.receiptTime < interval_query.c.end_date))
        if organization_ids:
            active_org_count_conditions &= EhrReceipt.organizationId.in_(
                organization_ids)
        active_org_count_query = (select([
            func.count(EhrReceipt.organizationId.distinct())
        ]).where(active_org_count_conditions))
        query = select([
            interval_query.c.start_date,
            interval_query.c.end_date,
            active_org_count_query.label('active_organization_count'),
        ])
        with self.session() as session:
            cursor = session.execute(query)
        return [dict(zip(cursor.keys(), row)) for row in cursor]
コード例 #8
0
    def test_organization_counts(self):
        # Set up data
        for date in iter_dates(datetime.date(2017, 12, 30),
                               datetime.date(2018, 2, 1)):
            calendar_day = Calendar(day=date)
            CalendarDao().insert(calendar_day)

        # noinspection PyArgumentList
        participant_1 = Participant(participantId=1, biobankId=4)
        summary_1 = self._make_participant(
            participant_1,
            'A',
            'Aardvark',
            self.hpo_foo,
            self.org_foo_a,
            time_int=datetime.datetime(2018, 1, 2),
            time_mem=datetime.datetime(2018, 1, 3),
            time_fp=datetime.datetime(2018, 1, 4))
        self._update_ehr(summary_1, update_time=datetime.datetime(2018, 1, 5))

        # noinspection PyArgumentList
        participant_2 = Participant(participantId=2, biobankId=5)
        summary_2 = self._make_participant(
            participant_2,
            'B',
            'Badger',
            self.hpo_foo,
            self.org_foo_a,
            time_int=datetime.datetime(2018, 1, 2),
            time_mem=datetime.datetime(2018, 1, 3),
            time_fp=datetime.datetime(2018, 1, 4))
        self._update_ehr(summary_2, update_time=datetime.datetime(2018, 1, 5))

        # noinspection PyArgumentList
        participant_3 = Participant(participantId=3, biobankId=6)
        summary_3 = self._make_participant(
            participant_3,
            'C',
            'Chicken',
            self.hpo_foo,
            self.org_foo_a,
            time_int=datetime.datetime(2018, 1, 3),
            time_mem=datetime.datetime(2018, 1, 4),
            time_fp=datetime.datetime(2018, 1, 5))
        self._update_ehr(summary_3, update_time=datetime.datetime(2018, 1, 6))

        # noinspection PyArgumentList
        participant_4 = Participant(participantId=4, biobankId=7)
        summary_4 = self._make_participant(
            participant_4,
            'D',
            'Dog',
            self.hpo_bar,
            self.org_bar_a,
            time_int=datetime.datetime(2018, 1, 3),
            time_mem=datetime.datetime(2018, 1, 4),
            time_fp=datetime.datetime(2018, 1, 5))
        self._update_ehr(summary_4, update_time=datetime.datetime(2018, 1, 6))
        self._update_ehr(summary_4, update_time=datetime.datetime(2018, 1, 7))

        # Begin testing
        response = self.send_get('MetricsEHR',
                                 query_string=urllib.urlencode({
                                     'start_date':
                                     '2018-01-01',
                                     'end_date':
                                     '2018-01-08',
                                     'interval':
                                     'day'
                                 }))
        counts_by_date = {
            day['date']: day['metrics']['ORGANIZATIONS_ACTIVE']
            for day in response['metrics_over_time']
        }
        self.assertEqual(
            counts_by_date, {
                u'2018-01-01': 0,
                u'2018-01-02': 0,
                u'2018-01-03': 0,
                u'2018-01-04': 0,
                u'2018-01-05': 1,
                u'2018-01-06': 2,
                u'2018-01-07': 1,
                u'2018-01-08': 0,
            })

        # test with organization filtering
        response = self.send_get('MetricsEHR',
                                 query_string=urllib.urlencode({
                                     'start_date':
                                     '2018-01-01',
                                     'end_date':
                                     '2018-01-08',
                                     'interval':
                                     'day',
                                     'organization':
                                     'FOO_A',
                                 }))
        counts_by_date = {
            day['date']: day['metrics']['ORGANIZATIONS_ACTIVE']
            for day in response['metrics_over_time']
        }
        self.assertEqual(
            counts_by_date, {
                u'2018-01-01': 0,
                u'2018-01-02': 0,
                u'2018-01-03': 0,
                u'2018-01-04': 0,
                u'2018-01-05': 1,
                u'2018-01-06': 1,
                u'2018-01-07': 0,
                u'2018-01-08': 0,
            })

        # test organization filter multiple
        response = self.send_get('MetricsEHR',
                                 query_string=urllib.urlencode({
                                     'start_date':
                                     '2018-01-01',
                                     'end_date':
                                     '2018-01-08',
                                     'interval':
                                     'day',
                                     'organization':
                                     'FOO_A,BAR_A',
                                 }))
        counts_by_date = {
            day['date']: day['metrics']['ORGANIZATIONS_ACTIVE']
            for day in response['metrics_over_time']
        }
        self.assertEqual(
            counts_by_date, {
                u'2018-01-01': 0,
                u'2018-01-02': 0,
                u'2018-01-03': 0,
                u'2018-01-04': 0,
                u'2018-01-05': 1,
                u'2018-01-06': 2,
                u'2018-01-07': 1,
                u'2018-01-08': 0,
            })
コード例 #9
0
    def test_consented_counts(self):
        # Set up data
        for date in iter_dates(datetime.date(2017, 12, 30),
                               datetime.date(2018, 2, 1)):
            calendar_day = Calendar(day=date)
            CalendarDao().insert(calendar_day)

        # noinspection PyArgumentList
        participant_1 = Participant(participantId=1, biobankId=4)
        self._make_participant(
            participant_1,
            'Alice',
            'Aardvark',
            self.hpo_foo,
            self.org_foo_a,
            time_int=datetime.datetime(2018, 1, 2),
            time_mem=datetime.datetime(2018, 1, 3),
            time_fp=datetime.datetime(2018, 1, 4),
        )

        # noinspection PyArgumentList
        participant_2 = Participant(participantId=2, biobankId=5)
        self._make_participant(participant_2,
                               'Bo',
                               'Badger',
                               self.hpo_bar,
                               self.org_bar_a,
                               time_int=datetime.datetime(2018, 1, 3),
                               time_mem=datetime.datetime(2018, 1, 4),
                               time_fp=datetime.datetime(2018, 1, 5))

        # Begin testing
        response = self.send_get('MetricsEHR',
                                 query_string=urllib.urlencode({
                                     'start_date':
                                     '2018-01-01',
                                     'end_date':
                                     '2018-01-06',
                                     'interval':
                                     'day'
                                 }))
        counts_by_date = {
            day['date']: day['metrics']['EHR_CONSENTED']
            for day in response['metrics_over_time']
        }
        self.assertEqual(
            counts_by_date, {
                u'2018-01-01': 0,
                u'2018-01-02': 0,
                u'2018-01-03': 1,
                u'2018-01-04': 2,
                u'2018-01-05': 2,
                u'2018-01-06': 2,
            })

        # test with organization filtering
        response = self.send_get('MetricsEHR',
                                 query_string=urllib.urlencode({
                                     'start_date':
                                     '2018-01-01',
                                     'end_date':
                                     '2018-01-06',
                                     'interval':
                                     'day',
                                     'organization':
                                     'foo_a',
                                 }))
        counts_by_date = {
            day['date']: day['metrics']['EHR_CONSENTED']
            for day in response['metrics_over_time']
        }
        self.assertEqual(
            counts_by_date, {
                u'2018-01-01': 0,
                u'2018-01-02': 0,
                u'2018-01-03': 1,
                u'2018-01-04': 1,
                u'2018-01-05': 1,
                u'2018-01-06': 1,
            })
コード例 #10
0
class EhrReceiptDaoTest(SqlTestBase):
    def setUp(self, with_data=True, use_mysql=True):
        super(EhrReceiptDaoTest, self).setUp(with_data=with_data,
                                             use_mysql=use_mysql)
        self.setup_fake()
        self.calendar_dao = CalendarDao()
        self.org_dao = OrganizationDao()
        self.hpo_dao = HPODao()
        self.participant_dao = ParticipantDao()
        self.summary_dao = ParticipantSummaryDao()
        self.ehr_receipt_dao = EhrReceiptDao()
        self._setup_initial_data()

    @staticmethod
    def _iter_dates_in_range(start, end):
        current = start
        while current <= end:
            yield current
            current += datetime.timedelta(days=1)

    def _fill_calendar_range(self, start, end):
        for date in self._iter_dates_in_range(start, end):
            self.calendar_dao.insert(Calendar(day=date))

    def _make_hpo(self, int_id, string_id):
        hpo = HPO(hpoId=int_id, name=string_id)
        self.hpo_dao.insert(hpo)
        return hpo

    def _make_org(self, **kwargs):
        org = Organization(**kwargs)
        self.org_dao.insert(org)
        return org

    def _make_participant(self, org, int_id):
        participant = self._participant_with_defaults(participantId=int_id,
                                                      biobankId=int_id)
        participant.hpoId = org.hpoId
        participant.organizationId = org.organizationId
        self.participant_dao.insert(participant)
        summary = self.participant_summary(participant)
        summary.hpoId = participant.hpoId
        summary.organizationId = participant.organizationId
        self.summary_dao.insert(summary)
        return participant, summary

    def _update_ehr(self, participant_summary, update_time):
        self.summary_dao.update_ehr_status(participant_summary, update_time)
        self.summary_dao.update(participant_summary)

    def _save_ehr_receipt(self, org, receipt_time):
        receipt = EhrReceipt(organizationId=org.organizationId,
                             receiptTime=receipt_time)
        self.ehr_receipt_dao.insert(receipt)

    def _setup_initial_data(self):
        self.hpo_foo = self._make_hpo(int_id=10, string_id='hpo_foo')
        self.hpo_bar = self._make_hpo(int_id=11, string_id='hpo_bar')

        self.org_foo_a = self._make_org(organizationId=10,
                                        externalId='FOO_A',
                                        displayName='Foo A',
                                        hpoId=self.hpo_foo.hpoId)
        self.org_bar_a = self._make_org(organizationId=11,
                                        externalId='BAR_A',
                                        displayName='Bar A',
                                        hpoId=self.hpo_bar.hpoId)

        participant_and_summary_pairs = [
            self._make_participant(org=self.org_foo_a, int_id=11),
            self._make_participant(org=self.org_foo_a, int_id=12),
            self._make_participant(org=self.org_bar_a, int_id=13),
            self._make_participant(org=self.org_bar_a, int_id=14),
        ]
        self.participants = {
            participant.participantId: participant
            for participant, summary in participant_and_summary_pairs
        }
        self.summaries = {
            participant.participantId: summary
            for participant, summary in participant_and_summary_pairs
        }

    def test_get_active_organization_counts_in_interval_day(self):
        self._fill_calendar_range(datetime.date(2019, 1, 1),
                                  datetime.date(2019, 3, 1))

        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 2, 2))
        self._save_ehr_receipt(org=self.org_bar_a,
                               receipt_time=datetime.datetime(2019, 2, 2))
        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 2, 4))

        results = self.ehr_receipt_dao.get_active_organization_counts_in_interval(
            start_date=datetime.datetime(2019, 2, 1),
            end_date=datetime.datetime(2019, 2, 7),
            interval=INTERVAL_DAY)

        self.assertEqual([(r['start_date'], r['active_organization_count'])
                          for r in results], [
                              (datetime.date(2019, 2, 1), 0L),
                              (datetime.date(2019, 2, 2), 2L),
                              (datetime.date(2019, 2, 3), 0L),
                              (datetime.date(2019, 2, 4), 1L),
                              (datetime.date(2019, 2, 5), 0L),
                              (datetime.date(2019, 2, 6), 0L),
                              (datetime.date(2019, 2, 7), 0L),
                          ])

    def test_get_active_organization_counts_in_interval_week(self):
        self._fill_calendar_range(datetime.date(2019, 1, 1),
                                  datetime.date(2019, 3, 1))

        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 2, 4))
        self._save_ehr_receipt(org=self.org_bar_a,
                               receipt_time=datetime.datetime(2019, 2, 4))
        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 2, 18))

        results = self.ehr_receipt_dao.get_active_organization_counts_in_interval(
            start_date=datetime.datetime(2019, 2, 1),
            end_date=datetime.datetime(2019, 3, 1),
            interval=INTERVAL_WEEK)

        self.assertEqual([(r['start_date'], r['active_organization_count'])
                          for r in results], [
                              (datetime.date(2019, 1, 27), 0L),
                              (datetime.date(2019, 2, 3), 2L),
                              (datetime.date(2019, 2, 10), 0L),
                              (datetime.date(2019, 2, 17), 1L),
                              (datetime.date(2019, 2, 24), 0L),
                          ])

    def test_get_active_organization_counts_in_interval_month(self):
        self._fill_calendar_range(datetime.date(2018, 12, 1),
                                  datetime.date(2019, 7, 1))

        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 2, 1))
        self._save_ehr_receipt(org=self.org_bar_a,
                               receipt_time=datetime.datetime(2019, 2, 1))
        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 4, 1))

        results = self.ehr_receipt_dao.get_active_organization_counts_in_interval(
            start_date=datetime.datetime(2019, 1, 1),
            end_date=datetime.datetime(2019, 5, 1),
            interval=INTERVAL_MONTH)

        self.assertEqual([(r['start_date'], r['active_organization_count'])
                          for r in results], [
                              (datetime.date(2019, 1, 1), 0L),
                              (datetime.date(2019, 2, 1), 2L),
                              (datetime.date(2019, 3, 1), 0L),
                              (datetime.date(2019, 4, 1), 1L),
                              (datetime.date(2019, 5, 1), 0L),
                          ])

    def test_get_active_organization_counts_in_interval_quarter(self):
        self._fill_calendar_range(datetime.date(2018, 12, 1),
                                  datetime.date(2020, 1, 1))

        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 5, 1))
        self._save_ehr_receipt(org=self.org_bar_a,
                               receipt_time=datetime.datetime(2019, 5, 1))
        self._save_ehr_receipt(org=self.org_foo_a,
                               receipt_time=datetime.datetime(2019, 11, 1))

        results = self.ehr_receipt_dao.get_active_organization_counts_in_interval(
            start_date=datetime.datetime(2019, 1, 1),
            end_date=datetime.datetime(2020, 1, 1),
            interval=INTERVAL_QUARTER)

        self.assertEqual([(r['start_date'], r['active_organization_count'])
                          for r in results], [
                              (datetime.date(2019, 1, 1), 0L),
                              (datetime.date(2019, 4, 1), 2L),
                              (datetime.date(2019, 7, 1), 0L),
                              (datetime.date(2019, 10, 1), 1L),
                              (datetime.date(2020, 1, 1), 0L),
                          ])
コード例 #11
0
 def setUp(self, with_data=False, use_mysql=True):
     super(MetricsEhrApiIntervalTest, self).setUp(with_data=with_data,
                                                  use_mysql=use_mysql)
     self.calendar_dao = CalendarDao()
コード例 #12
0
class MetricsEhrApiIntervalTest(SqlTestBase):
    def setUp(self, with_data=False, use_mysql=True):
        super(MetricsEhrApiIntervalTest, self).setUp(with_data=with_data,
                                                     use_mysql=use_mysql)
        self.calendar_dao = CalendarDao()

    def _iter_dates_in_range(self, start, end):
        current = start
        while current <= end:
            yield current
            current += datetime.timedelta(days=1)

    def _fill_calendar_range(self, start, end):
        for date in self._iter_dates_in_range(start, end):
            self.calendar_dao.insert(Calendar(day=date))

    def test_interval_day(self):
        self._fill_calendar_range(datetime.date(2017, 12, 1),
                                  datetime.date(2018, 2, 1))

        query = self.calendar_dao.get_interval_query(
            start=datetime.datetime(2018, 1, 1),
            end=datetime.datetime(2018, 1, 4),
            interval_key=INTERVAL_DAY,
            include_end_date=True)

        with self.calendar_dao.session() as session:
            cursor = session.execute(query)
        results = [dict(zip(cursor.keys(), row)) for row in cursor]

        self.assertEqual(results, [
            {
                'start_date': datetime.date(2018, 1, 1),
                'end_date': datetime.date(2018, 1, 2)
            },
            {
                'start_date': datetime.date(2018, 1, 2),
                'end_date': datetime.date(2018, 1, 3)
            },
            {
                'start_date': datetime.date(2018, 1, 3),
                'end_date': datetime.date(2018, 1, 4)
            },
            {
                'start_date': datetime.date(2018, 1, 4),
                'end_date': datetime.date(2018, 1, 5)
            },
        ])

    def test_interval_week(self):
        self._fill_calendar_range(datetime.date(2017, 12, 1),
                                  datetime.date(2018, 3, 1))

        query = self.calendar_dao.get_interval_query(
            start=datetime.datetime(2018, 1, 1),
            end=datetime.datetime(2018, 2, 1),
            interval_key=INTERVAL_WEEK,
            include_end_date=True)

        with self.calendar_dao.session() as session:
            cursor = session.execute(query)
        results = [dict(zip(cursor.keys(), row)) for row in cursor]

        self.assertEqual(results, [
            {
                'start_date': datetime.date(2017, 12, 31),
                'end_date': datetime.date(2018, 1, 7)
            },
            {
                'start_date': datetime.date(2018, 1, 7),
                'end_date': datetime.date(2018, 1, 14)
            },
            {
                'start_date': datetime.date(2018, 1, 14),
                'end_date': datetime.date(2018, 1, 21)
            },
            {
                'start_date': datetime.date(2018, 1, 21),
                'end_date': datetime.date(2018, 1, 28)
            },
            {
                'start_date': datetime.date(2018, 1, 28),
                'end_date': datetime.date(2018, 2, 4)
            },
        ])

    def test_interval_month(self):
        self._fill_calendar_range(datetime.date(2017, 11, 1),
                                  datetime.date(2018, 8, 1))

        query = self.calendar_dao.get_interval_query(
            start=datetime.datetime(2018, 1, 1),
            end=datetime.datetime(2018, 6, 1),
            interval_key=INTERVAL_MONTH,
            include_end_date=True)

        with self.calendar_dao.session() as session:
            cursor = session.execute(query)
        results = [dict(zip(cursor.keys(), row)) for row in cursor]

        self.assertEqual(results, [
            {
                'start_date': datetime.date(2018, 1, 1),
                'end_date': datetime.date(2018, 2, 1)
            },
            {
                'start_date': datetime.date(2018, 2, 1),
                'end_date': datetime.date(2018, 3, 1)
            },
            {
                'start_date': datetime.date(2018, 3, 1),
                'end_date': datetime.date(2018, 4, 1)
            },
            {
                'start_date': datetime.date(2018, 4, 1),
                'end_date': datetime.date(2018, 5, 1)
            },
            {
                'start_date': datetime.date(2018, 5, 1),
                'end_date': datetime.date(2018, 6, 1)
            },
            {
                'start_date': datetime.date(2018, 6, 1),
                'end_date': datetime.date(2018, 7, 1)
            },
        ])

    def test_interval_quarter(self):
        self._fill_calendar_range(datetime.date(2017, 01, 1),
                                  datetime.date(2019, 01, 1))

        query = self.calendar_dao.get_interval_query(
            start=datetime.datetime(2018, 1, 1),
            end=datetime.datetime(2018, 12, 31),
            interval_key=INTERVAL_QUARTER,
            include_end_date=True)

        with self.calendar_dao.session() as session:
            cursor = session.execute(query)
        results = [dict(zip(cursor.keys(), row)) for row in cursor]

        self.assertEqual(results, [
            {
                'start_date': datetime.date(2018, 1, 1),
                'end_date': datetime.date(2018, 4, 1)
            },
            {
                'start_date': datetime.date(2018, 4, 1),
                'end_date': datetime.date(2018, 7, 1)
            },
            {
                'start_date': datetime.date(2018, 7, 1),
                'end_date': datetime.date(2018, 10, 1)
            },
            {
                'start_date': datetime.date(2018, 10, 1),
                'end_date': datetime.date(2019, 1, 1)
            },
        ])
コード例 #13
0
  def setUp(self, **kwargs):
    super(MetricsEhrMultiEndpointTest, self).setUp(**kwargs)

    #
    # insert some test data
    #
    for date in iter_dates(
      datetime.date(2017, 12, 30),
      datetime.date(2018, 2, 1)
    ):
      calendar_day = Calendar(day=date)
      CalendarDao().insert(calendar_day)
    # noinspection PyArgumentList
    participant_1 = Participant(participantId=1, biobankId=1)
    summary_1 = self._make_participant(
      participant_1, 'Alice', 'Aardvark', self.hpo_foo, self.org_foo_a,
      time_int=datetime.datetime(2018, 1, 2),
      time_mem=datetime.datetime(2018, 1, 3),
      time_study=datetime.datetime(2018, 1, 3),
      time_fp=datetime.datetime(2018, 1, 4)
    )
    self._update_ehr(
      summary_1,
      update_time=datetime.datetime(2018, 1, 5)
    )

    # noinspection PyArgumentList
    participant_2 = Participant(participantId=2, biobankId=2)
    summary_2 = self._make_participant(
      participant_2, 'Bo', 'Badger', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_study=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )
    self._update_ehr(
      summary_2,
      update_time=datetime.datetime(2018, 1, 6)
    )

    # noinspection PyArgumentList
    participant_3 = Participant(participantId=3, biobankId=3)
    self._make_participant(
      participant_3, 'Cheryl', 'Caterpillar', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_study=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )
    participant_3.withdrawalStatus = WithdrawalStatus.NO_USE
    self.dao.update(participant_3)

    # noinspection PyArgumentList
    participant_4 = Participant(participantId=4, biobankId=4)
    self._make_participant(
      participant_4, 'Dan', 'Donkey', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_study=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )
    participant_4.isGhostId = True
    self.dao.update(participant_4)

    # noinspection PyArgumentList
    participant_5 = Participant(participantId=5, biobankId=5)
    self._make_participant(
      participant_5, 'Elia', 'Elephant', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_study=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )

    # noinspection PyArgumentList
    participant_6 = Participant(participantId=6, biobankId=6)
    self._make_participant(
      participant_6, 'Elia', 'Elephant', self.hpo_bar, self.org_bar_a,
      time_int=datetime.datetime(2018, 1, 3),
      time_mem=datetime.datetime(2018, 1, 4),
      time_fp=datetime.datetime(2018, 1, 5)
    )