def test_Return_uid_str_cace_subject_description(self, item): """add_item: subjectとdescriptionがあるケース、正常にクエリを投げuidを受け取ることができる""" actual = app.get_app_db().add_item( subject=item['subject'], description=item['description'], username=DEFAULT_USERNAME) assert type(actual) == str assert len(actual) == 36
def test_Return_uid_case_state_only(self, mock, item): """update_item: stateを更新するケース、更新したitemのuidを正常に返すことができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) actual = app.get_app_db().update_item( uid=item['uid'], state=item['state'], username=item['username']) assert actual == item['uid']
def test_Return_uid_case_description_only(self, mock, item): """update_item: descriptionを更新するケース、更新したitemのuidを正常に返すことができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) actual = app.get_app_db().update_item( uid=item['uid'], description=item['description']+"_updated", username=item['username']) assert actual == item['uid']
def test_Return_items_by_username(self, mock): """list_items: ユーザーdefaultのアイテムをすべて取得することができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) query = '' actual = app.get_app_db().list_items(query=query, username=DEFAULT_USERNAME) actual = sorted(actual, key=operator.itemgetter('uid')) expected = [item for item in TESTDATA_DDB_ITEMS if item['username'] == DEFAULT_USERNAME] expected = sorted(expected, key=operator.itemgetter('uid')) assert actual == expected
def test_Return_uid_case_all_attributes(self, mock, item): """update_item: すべての属性を更新するケース、更新したitemのuidを正常に返すことができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) actual = app.get_app_db().update_item( uid=item['uid'], subject=item['subject']+"_updated", description=item['description']+"_updated", state=item['state'], username=item['username']) assert actual == item['uid']
def test_Return_items_by_query(self, query, mock): """list_items: ユーザーdefaultのアイテムからクエリを満たすものをすべて取得することができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) actual = app.get_app_db().list_items(query=query, username=DEFAULT_USERNAME) actual.sort(key=operator.itemgetter('uid')) expected = [item for item in TESTDATA_DDB_ITEMS if item['username'] == DEFAULT_USERNAME] expected = [item for item in expected if query in item['subject'] or query in item['description']] expected = sorted(expected, key=operator.itemgetter('uid')) assert actual == expected
def test_Raise_NotFoundError_case_uid_not_exist(self, mock): """get_item: uidが存在しないケース、例外を発生させることができる""" with pytest.raises(NotFoundError): app.get_app_db().get_item("_NOT_EXIST_UID", username=DEFAULT_USERNAME)
def test_Return_item(self, mock, item): """get_item: uidが存在するケース、itemを正常に返すことができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) assert app.get_app_db().get_item( uid=item['uid'], username=item['username']) == item
def test_Raise_case_description_only(self): """add_item: descriptionのみのケース、例外を発生させることができる""" with pytest.raises(TypeError): app.get_app_db().add_item( description='', username=DEFAULT_USERNAME)
def test_Return_all_items(self, mock): """list_all_items: すべてのアイテムを取得することができる""" mock.table.put_items(TESTDATA_DDB_ITEMS) assert app.get_app_db().list_all_items() == TESTDATA_DDB_ITEMS
def test_Return_uid_str(self, mock, item): """delete_item: uidが存在するケース、削除したitemのuidを正常に返すことができる""" mock.table.put_items([item]) assert app.get_app_db().delete_item( item['uid'], username=item['username']) == item['uid']
def test_Return_DynamoDBTodo_class(self): """get_app_db: get_app_dbの返り値クラスと、元のクラスが一致する""" assert app.get_app_db().__class__ is DynamoDBTodo
def test_process_later_with_varying_queue_lengths( self, people_to_follow, users, mock_db, mocked_tweepy, mock_sqs_resource, test_client, mock_settings_env_vars, frozen_time, mock_message_as_object, ): stubbed_later_queue = mock_sqs_resource.create_queue( QueueName="test-later-queue" ) messages = map( json.dumps, [ {**mock_message_as_object, "follower_id": f"{i}"} for i in range(people_to_follow) ], ) if people_to_follow < 10: for message in messages: stubbed_later_queue.send_message(MessageBody=message) else: entries = [] for i, message in enumerate(messages, start=1): entries.append({"Id": str(i), "MessageBody": message}) if i % 10 == 0: stubbed_later_queue.send_messages(Entries=entries) entries.clear() if entries: stubbed_later_queue.send_messages(Entries=entries) mocked_queues = patch( "app.queues", return_value=[None, stubbed_later_queue, None] ) mocked_api = patch("app.tweepy.API", return_value=mocked_tweepy) with mocked_api, mocked_queues, Client(views.app) as client: event = client.events.generate_cw_event( source="test.aws.events", detail_type="Scheduled Event", detail={}, resources=[ "arn:aws:events:us-east-1:123456789012:rule/MyScheduledRule" ], region="eu-west-test-1", ) test_client.lambda_.invoke("process_later", event) if people_to_follow <= 400: assert views.get_app_db().get_item("app").get("count") == people_to_follow assert ( views.get_app_db().get_item(users[0]).get("count") == people_to_follow ) else: timed_out_until = datetime.datetime.strptime( "2021-05-01 00:00:00", "%Y-%m-%d %H:%M:%S" ) + datetime.timedelta(hours=24) assert mocked_tweepy.locked_until == timed_out_until.timestamp() assert views.get_app_db().get_item("app").get("count") <= 1000 for user in users: assert views.get_app_db().get_item(user).get("count") <= 400