def test_setitem(): cards = CardDB(index='id') cards[3] = {'id': 2, 'a': 1, 'b': 'c'} assert cards[3] == {'id': 3, 'a': 1, 'b': 'c'} cards[3] = {'c': 4.5} assert len(cards) == 1 assert cards[3] == {'id': 3, 'a': 1, 'b': 'c', 'c': 4.5}
def test_get_move_card_ghargs(): card = { 'url': 'https://api.github.com/projects/columns/cards/18001901', 'id': 18001901, 'note': 'https://github.com/NCAR/xdev-bot-testing/issues/76', 'column_url': 'https://api.github.com/projects/columns/4507386', 'column_id': 4507386, 'created_at': '2019-02-22T20:42:18Z', 'updated_at': '2019-02-22T20:42:18Z', 'creator': 'xdev-bot', 'sender': 'xdev-bot', 'column_name': 'to_do' } cards = CardDB(card, index='note') with open(os.path.join(PWD, 'payload_examples/issue_closed.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="issues", delivery_id="12345") ghargs = GHArgs('/projects/columns/cards/18001901/moves', data={ 'position': 'top', 'column_id': 4_507_393 }, accept='application/vnd.github.inertia-preview+json') assert get_move_card_ghargs(event, column='done', database=cards) == ghargs
def test_edit_card(): card = { 'url': 'https://api.github.com/projects/columns/cards/20333934', 'id': 20333934, 'note': 'Add NetCDF4 (parallel) support to IOR for I/O testing...\r\n\r\n(https://github.com/hpc/ior/pull/142)', 'column_url': 'https://api.github.com/projects/columns/4507386', 'column_id': 4507386, 'created_at': '2019-04-16T13:05:44Z', 'updated_at': '2019-04-16T13:06:00Z', 'creator': 'kmpaul', 'sender': 'kmpaul', 'column_name': 'to_do' } cards = CardDB(card, index='note') assert len(cards) == 1 with open(os.path.join( PWD, 'payload_examples/card_edited_w_changes.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") card = get_card_from_card_event(event) edit_card(event, database=cards) assert len(cards) == 1 assert cards[card['note']] == card
def test_remove_not_found(): cards = CardDB(index='id') with pytest.raises(KeyError): cards.remove({'id': 2, 'a': 1, 'b': 'c'}) with pytest.raises(KeyError): cards.remove({'a': 1, 'b': 'c'})
def test_add_overwrite_element(): cards = CardDB(index='id') card2 = {'id': 2, 'a': 1, 'b': 'c'} cards.add(card2) assert len(cards) == 1 assert cards[2] == card2 cards.add({'id': 2, 'a': 2}) assert cards[2] == {'id': 2, 'a': 2, 'b': 'c'}
def test_add_nonunique(): cards = CardDB(index='id') card2a = {'id': 2, 'a': 1, 'b': 'c'} card2b = {'id': 2, 'a': 2, 'b': 'f'} cards.add(card2a) cards.add(card2b) assert len(cards) == 1 assert cards[2] == card2b
def test_save(s3fn): cards = CardDB(index='id', s3filename=s3fn) assert not S3FS.exists(s3fn) card0 = {'id': 0, 'a': 1, 'b': 'c', 'd': 4.5} card3 = {'id': 3, 'a': 2, 'b': 'e', 'd': 8.5} card7 = {'id': 7, 'a': 2, 'b': 'c', 'd': -4.2} cards.add(card0) cards.add(card3) cards.add(card7) assert len(cards) == 3 cards.remove(card3) cards.save() assert S3FS.exists(s3fn) with S3FS.open(s3fn, 'r') as f: df = pd.read_csv(f) assert cards._df.equals(df) cards2 = CardDB(index='id', s3filename=s3fn) assert cards._df.equals(cards2._df)
def test_getitem_index_not_found(): card = {'a': 1, 'b': 'c'} cards = CardDB(index='id') cards._df = cards._df.append(card, ignore_index=True) assert cards._idxcol == 'id' assert cards._idxcol not in cards._df with pytest.raises(IndexError): cards[3]
def test_get_update_status_ghargs_other(): card = {'note': 'Just some text', 'column_name': 'in_progress'} cards = CardDB(card, index='note') with open(os.path.join(PWD, 'payload_examples/card_created_other.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") assert get_update_status_ghargs(event, database=cards) is None
def test_getitem(): card2 = {'id': 2, 'a': 1, 'b': 'c'} card3 = {'id': 3, 'a': 2, 'b': 'c'} card7 = {'id': 7, 'a': 2, 'b': 'f'} cards = CardDB(card2, card3, card7, index='id') assert cards[2] == card2 assert cards[3] == card3 assert cards[7] == card7 assert cards[0] is None assert cards[1] is None
def test_remove_only_matching_index(): card2 = {'id': 2, 'a': 1, 'b': 'c'} card3 = {'id': 3, 'a': 2, 'b': 'c'} card7 = {'id': 7, 'a': 2, 'b': 'f'} cards = CardDB(card2, card3, card7, index='id') assert len(cards) == 3 cards.remove({'id': 3}) assert len(cards) == 2 assert cards[2] == card2 assert cards[3] is None assert cards[7] == card7
def test_get_move_card_not_found_ghargs(): cards = CardDB(index='note') with open(os.path.join(PWD, 'payload_examples/issue_closed.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="issues", delivery_id="12345") ghargs = GHArgs('/projects/columns/4507393/cards', data={'note': payload['issue']['html_url']}, accept='application/vnd.github.inertia-preview+json') assert get_move_card_ghargs(event, column='done', database=cards) == ghargs
def test_read_no_s3_file(s3fn): aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY'] aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID'] del os.environ['AWS_SECRET_ACCESS_KEY'] del os.environ['AWS_ACCESS_KEY_ID'] S3FS.touch(s3fn) with pytest.raises(Exception): CardDB(index='id', s3filename=s3fn) os.environ['AWS_ACCESS_KEY_ID'] = aws_access_key_id os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_access_key
def test_search(): card2 = {'id': 2, 'a': 1, 'b': 'c'} card3 = {'id': 3, 'a': 2, 'b': 'c'} card7 = {'id': 7, 'a': 2, 'b': 'f'} cards = CardDB(card2, card3, card7, index='id') assert cards.search(z=42) == [] assert cards.search(a=1) == [card2] cards_a_2 = cards.search(a=2) assert card2 not in cards_a_2 assert card3 in cards_a_2 assert card7 in cards_a_2
def test_save_merged_status_merged(): cards = CardDB(index='note') with open(os.path.join(PWD, 'payload_examples/pull_request_merged.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="pull_request", delivery_id="12345") note = event.data['pull_request']['html_url'] save_merged_status(event, database=cards) assert len(cards) == 1 assert cards[note]['merged']
def test_get_update_status_ghargs_not_found(): cards = CardDB(index='note') with open(os.path.join(PWD, 'payload_examples/card_moved_to_done.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") ghargs = GHArgs( 'https://api.github.com/repos/NCAR/xdev-bot-testing/issues/75', data={'state': 'closed'}, func='patch') assert get_update_status_ghargs(event, database=cards) == ghargs
def test_get_update_status_ghargs_no_change(): card = { 'note': 'https://github.com/NCAR/xdev-bot-testing/pull/75', 'column_name': 'done', 'merged': None } cards = CardDB(card, index='note') with open(os.path.join(PWD, 'payload_examples/card_moved_to_done.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") assert get_update_status_ghargs(event, database=cards) is None
def test_save_new_card_and_remove(): cards = CardDB(index='note') with open(os.path.join(PWD, 'payload_examples/card_created_issue.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") card = get_card_from_card_event(event) save_card(event, database=cards) assert len(cards) == 1 assert cards[card['note']] == card remove_card(event, database=cards) assert len(cards) == 0
def test_get_update_status_ghargs(): card = { 'note': 'https://github.com/NCAR/xdev-bot-testing/issues/11', 'column_name': 'done' } cards = CardDB(card, index='note') with open(os.path.join(PWD, 'payload_examples/card_created_issue.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") ghargs = GHArgs( 'https://api.github.com/repos/NCAR/xdev-bot-testing/issues/11', data={'state': 'open'}, func='patch') assert get_update_status_ghargs(event, database=cards) == ghargs
def test_get_update_status_ghargs_merged_pr(): card = { 'note': 'https://github.com/NCAR/xdev-bot-testing/pull/75', 'column_name': 'done', 'merged': True } cards = CardDB(card, index='note') with open( os.path.join( PWD, 'payload_examples/card_moved_to_in_progress.json')) as f: payload = json.load(f) event = sansio.Event(payload, event="project_card", delivery_id="12345") ghargs = GHArgs('/projects/columns/cards/18727793/moves', data={ 'position': 'top', 'column_id': 4_507_393 }, accept='application/vnd.github.inertia-preview+json') assert get_update_status_ghargs(event, database=cards) == ghargs
def test_init_without_index_raises_index_error(): with pytest.raises(IndexError): CardDB()
def test_init_database_with_invalid_index(): card = {'a': 1, 'b': 'c'} with pytest.raises(IndexError): CardDB(card, index='id')
def test_len(): cards = CardDB(index='id') assert len(cards) == 0
def test_add(): card2 = {'id': 2, 'a': 1, 'b': 'c'} cards = CardDB(index='id') cards.add(card2) assert len(cards) == 1
def test_add_invalid_index(): card2 = {'i': 2, 'a': 1, 'b': 'c'} cards = CardDB(index='id') with pytest.raises(IndexError): cards.add(card2)
def test_init_single(): card2 = {'id': 2, 'a': 1, 'b': 'c'} cards = CardDB(card2, index='id') assert len(cards) == 1
def test_init_empty(): cards = CardDB(index='id') assert isinstance(cards, CardDB)