def test_it_can_be_printed(): date1 = Date(2017) date2 = Date(2017, 1) date3 = Date(2017, 1, 2) assert repr(date1) == "<Date '2017'>" assert repr(date2) == "<Date '2017-01'>" assert repr(date3) == "<Date '2017-01-02'>"
def test_it_casts_to_a_string(): date1 = Date(2017) date2 = Date(2017, 1) date3 = Date(2017, 1, 2) assert str(date1) == '2017' assert str(date2) == '2017-01' assert str(date3) == '2017-01-02'
def test_it_must_be_a_valid_date(): with pytest.raises(ValueError): Date(2017, 0, 0) with pytest.raises(ValueError): Date(2017, 13, 31) with pytest.raises(ValueError): Date(2017, 12, 32) with pytest.raises(ValueError): Date(2017, 2, 29)
def test_it_may_have_a_department(): has = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017), department='Department') has_not = Affiliation('2', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert has.department == 'Department' assert has_not.department is None
def test_it_may_be_restricted(): has = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017), restricted=True) has_not = Affiliation('2', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert has.restricted is True assert has_not.restricted is False
def test_it_can_become_the_lowest_possible_datetime(): date1 = Date(2017) date2 = Date(2017, 2) date3 = Date(2017, 3, 4) assert date1.lowest_possible().isoformat() == '2017-01-01T00:00:00' assert date2.lowest_possible().isoformat() == '2017-02-01T00:00:00' assert date3.lowest_possible().isoformat() == '2017-03-04T00:00:00'
def test_it_removes_affiliations(): profile = Profile('12345678', Name('Name')) profile.add_affiliation( Affiliation('1', Address(countries.get('gb'), 'City 1'), 'Organisation 1', Date(2017))) orcid_record = {} update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 0
def _convert_orcid_date(orcid_date: dict) -> Optional[Date]: if 'year' in orcid_date: year = int(orcid_date['year']['value']) month = int( orcid_date['month']['value']) if orcid_date.get('month') else None day = int( orcid_date['day']['value']) if orcid_date.get('day') else None try: return Date(year, month, day) except ValueError as exception: LOGGER.error('%s: %s', exception, (year, month, day))
def test_it_can_have_affiliations(): profile = Profile('12345678', Name('foo'), '0000-0002-1825-0097') affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert len(profile.affiliations) == 0 profile.add_affiliation(affiliation) assert len(profile.affiliations) == 1 profile.remove_affiliation(affiliation.id) assert len(profile.affiliations) == 0 with pytest.raises(AffiliationNotFound): profile.get_affiliation('1')
def test_it_can_be_compared(): date1 = Date(2017) date2 = Date(2017) date3 = Date(2017, 1) date4 = Date(2017, 1) date5 = Date(2017, 1, 2) date6 = Date(2017, 1, 2) assert date1 == date2 assert date1 != date3 assert date3 == date4 assert date3 != date1 assert date5 == date6 assert date5 != date1
def test_it_only_sends_one_event_if_multiple_changes_are_detected(mock_publisher: MagicMock, profile: Profile, session: scoped_session, commit: Callable[[], None] ) -> None: event_publisher = send_update_events(publisher=mock_publisher) models_committed.connect(receiver=event_publisher) affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) profile.add_affiliation(affiliation) session.add(profile) commit() assert mock_publisher.publish.call_count == 1 assert mock_publisher.publish.call_args[0][0] == {'id': '12345678', 'type': 'profile'}
def test_it_updates_affiliations(): profile = Profile('12345678', Name('Name')) profile.add_affiliation( Affiliation('1', Address(countries.get('gb'), 'City 1'), 'Organisation 1', Date(2017))) orcid_record = { 'activities-summary': { 'employments': { 'employment-summary': [ { 'put-code': 1, 'department-name': 'Department 2', 'organization': { 'name': 'Organisation 2', 'address': { 'city': 'City 2', 'region': 'Region 2', 'country': 'US' } }, 'start-date': { 'year': { 'value': '2016' }, 'month': { 'value': '12' }, 'day': { 'value': '31' } }, 'end-date': { 'year': { 'value': '2018' }, 'month': { 'value': '02' }, 'day': { 'value': '03' } }, 'visibility': 'LIMIT' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 1 assert profile.affiliations[0].department == 'Department 2' assert profile.affiliations[0].organisation == 'Organisation 2' assert profile.affiliations[0].address.city == 'City 2' assert profile.affiliations[0].address.region == 'Region 2' assert profile.affiliations[0].address.country == countries.get('US') assert profile.affiliations[0].starts == Date(2016, 12, 31) assert profile.affiliations[0].ends == Date(2018, 2, 3) assert profile.affiliations[0].restricted is True assert profile.affiliations[0].position == 0
def test_it_must_have_a_month_to_have_a_day(): with pytest.raises(ValueError): Date(2017, None, 2)
def test_it_may_have_a_day(): has = Date(2016, 2, 29) has_not = Date(2017, 1) assert has.day == 29 assert has_not.day is None
def test_it_must_have_a_valid_month(): with pytest.raises(ValueError): Date(2017, 0) with pytest.raises(ValueError): Date(2017, 13)
def test_it_may_have_a_month(): has = Date(2017, 1) has_not = Date(2017) assert has.month == 1 assert has_not.month is None
def test_it_can_become_the_highest_possible_datetime(): date1 = Date(2017) date2 = Date(2017, 2) date3 = Date(2016, 2) date4 = Date(2017, 3, 4) assert date1.highest_possible().isoformat() == '2017-12-31T00:00:00' assert date2.highest_possible().isoformat() == '2017-02-28T00:00:00' assert date3.highest_possible().isoformat() == '2016-02-29T00:00:00' assert date4.highest_possible().isoformat() == '2017-03-04T00:00:00'
def test_it_has_an_organisation(): affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert affiliation.organisation == 'Organisation'
def test_it_can_be_printed(): affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert repr(affiliation) == "<Affiliation '1'>"
def test_it_can_be_created_for_yesterday(): date = Date.yesterday() assert date == Date(2017, 1, 1)
def test_it_can_be_created_from_a_datetime(): date = Date.from_datetime(datetime.now()) assert date == Date(2017, 1, 2)
def tomorrow(): return Date.tomorrow()
def test_it_can_be_created_for_today(): date = Date.today() assert date == Date(2017, 1, 2)
def yesterday(): return Date.yesterday()
def test_it_can_be_created_for_tomorrow(): date = Date.tomorrow() assert date == Date(2017, 1, 3)
def test_it_has_a_year(): date = Date(2017) assert date.year == 2017
def test_exception_is_handled_by_catch_exception_decorator(mock_publisher: MagicMock, profile: Profile, session: scoped_session, commit: Callable[[], None]) -> None: mock_publisher.publish.side_effect = Exception('Some Exception') event_publisher = send_update_events(publisher=mock_publisher) models_committed.connect(receiver=event_publisher) affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) profile.add_affiliation(affiliation) session.add(profile) commit() assert mock_publisher.publish.call_count == 1
def test_it_adds_affiliations_with_a_partial_end_dates(): profile = Profile('12345678', Name('Name')) orcid_record = { 'activities-summary': { 'employments': { 'employment-summary': [ { 'put-code': 1, 'start-date': { 'year': { 'value': '2015' }, 'month': { 'value': '1' }, 'day': { 'value': '1' } }, 'end-date': { 'year': { 'value': '2016' } }, 'organization': { 'name': 'Organisation 1', 'address': { 'city': 'City 1', 'country': 'GB' } }, 'visibility': 'PUBLIC' }, { 'put-code': 2, 'start-date': { 'year': { 'value': '2015' }, 'month': { 'value': '1' }, 'day': { 'value': '1' } }, 'end-date': { 'year': { 'value': '2017' }, 'month': { 'value': '12' } }, 'department-name': 'Department 2', 'organization': { 'name': 'Organisation 2', 'address': { 'city': 'City 2', 'region': 'Region 2', 'country': 'US' } }, 'visibility': 'LIMIT' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 2 assert profile.affiliations[0].id == '1' assert profile.affiliations[0].restricted is False assert profile.affiliations[0].position == 0 assert profile.affiliations[0].ends == Date(2016) assert profile.affiliations[1].id == '2' assert profile.affiliations[1].restricted is True assert profile.affiliations[1].position == 1 assert profile.affiliations[1].ends == Date(2017, 12)