def test_source_collection(): # Create some test submissions and replies source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') submission = Submission(source=source, uuid="test", size=123, filename="2-test.doc.gpg") user = User('hehe') reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid='test') source.submissions = [submission] source.replies = [reply] # Now these items should be in the source collection in the proper order assert source.collection[0] == reply assert source.collection[1] == submission
def test_string_representation_of_source(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') source.__repr__()
def test_string_representation_of_reply(): user = User('hehe') source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') reply = Reply(source=source, journalist=user, filename="reply.gpg", size=1234, uuid='test') reply.__repr__()
def test_reply_content_not_downloaded(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') journalist = User('Testy mcTestface') reply = Reply(source=source, uuid="test", size=123, filename="test.docx", journalist=journalist) assert reply.content is None
def test_submission_content_not_downloaded(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') submission = Submission(source=source, uuid="test", size=123, filename="test.docx", download_url='http://test/test') assert submission.content is None
def test_string_representation_of_submission(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') submission = Submission(source=source, uuid="test", size=123, filename="test.docx", download_url='http://test/test') submission.__repr__()
def test_reply_content_downloaded(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') journalist = User('Testy mcTestface') reply = Reply(source=source, uuid="test", size=123, filename="test.docx", journalist=journalist) reply.is_downloaded = True with mock.patch('builtins.open', mock.mock_open(read_data="blah")): assert reply.content == "blah"
def test_submission_content_downloaded(): source = Source(journalist_designation="testy test", uuid="test", is_flagged=False, public_key='test', interaction_count=1, is_starred=False, last_updated='test') submission = Submission(source=source, uuid="test", size=123, filename="test.docx", download_url='http://test/test') submission.is_downloaded = True with mock.patch('builtins.open', mock.mock_open(read_data="blah")): assert submission.content == "blah"
def update_sources(remote_sources, local_sources, session): """ Given collections of remote sources, the current local sources and a session to the local database, ensure the state of the local database matches that of the remote sources: * Existing items are updated in the local database. * New items are created in the local database. * Local items not returned in the remote sources are deleted from the local database. """ local_uuids = {source.uuid for source in local_sources} for source in remote_sources: if source.uuid in local_uuids: # Update an existing record. local_source = [s for s in local_sources if s.uuid == source.uuid][0] local_source.journalist_designation = source.journalist_designation local_source.is_flagged = source.is_flagged local_source.public_key = source.key['public'] local_source.interaction_count = source.interaction_count local_source.is_starred = source.is_starred local_source.last_updated = parse(source.last_updated) # Removing the UUID from local_uuids ensures this record won't be # deleted at the end of this function. local_uuids.remove(source.uuid) logger.info('Updated source {}'.format(source.uuid)) else: # A new source to be added to the database. ns = Source(uuid=source.uuid, journalist_designation=source.journalist_designation, is_flagged=source.is_flagged, public_key=source.key['public'], interaction_count=source.interaction_count, is_starred=source.is_starred, last_updated=parse(source.last_updated)) session.add(ns) logger.info('Added new source {}'.format(source.uuid)) # The uuids remaining in local_uuids do not exist on the remote server, so # delete the related records. for deleted_source in [s for s in local_sources if s.uuid in local_uuids]: session.delete(deleted_source) logger.info('Deleted source {}'.format(deleted_source.uuid)) session.commit()