我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用vcr.use_cassette()。
def test_update_entry_field_that_was_undefined_in_the_webapp(self): entry = None with vcr.use_cassette('fixtures/entry/undefined_fields.yaml'): entry = CLIENT.entries(PLAYGROUND_SPACE).find('33uj74Wln2Oc02CAyEY8CK') self.assertEqual(entry.fields(), {}) with vcr.use_cassette('fixtures/entry/undefined_fields_write.yaml'): entry.name = 'foo' self.assertEqual(entry.fields()['name'], 'foo') entry.save() updated_entry = CLIENT.entries(PLAYGROUND_SPACE).find('33uj74Wln2Oc02CAyEY8CK') self.assertEqual(updated_entry.fields(), {'name': 'foo'})
def test_update_entry_field_with_field_that_was_not_present(self): entry = None with vcr.use_cassette('fixtures/entry/added_fields_1.yaml'): entry = CLIENT.entries(PLAYGROUND_SPACE).find('3fTNzlQsDmge6YQEikEuME') self.assertEqual(entry.fields(), {'name': 'A Name', 'other': 'Other Stuff'}) with vcr.use_cassette('fixtures/entry/added_fields_2.yaml'): entry.different = 'A Different Field' self.assertEqual(entry.different, 'A Different Field') self.assertEqual(entry.fields(), {'name': 'A Name', 'other': 'Other Stuff', 'different': 'A Different Field'}) self.assertEqual(entry.to_json()['fields']['different']['en-US'], 'A Different Field') entry.save() with vcr.use_cassette('fixtures/entry/added_fields_3.yaml'): entry = CLIENT.entries(PLAYGROUND_SPACE).find('3fTNzlQsDmge6YQEikEuME') self.assertEqual(entry.different, 'A Different Field')
def test_get_calories_ww_defined_food(session): with vcr.use_cassette("{}/{}.yaml".format(CASSETTES_HOME, 'egg_log.yaml')): # This object is actually quite more complicated. But I don't care food_log = {'portionId': '563c6669305d6e1834ab948d', 'sourceId': 58783, '_displayName': 'Egg(s)', 'sourcePortionName': 'serving(s)', 'points': 4, 'isCore': True, 'name': 'Egg(s)', 'entryId': 'ee1e26a0-4e37-11e6-8aa7-21442c64eff3', 'smartPointsPrecise': 2.0201, 'timeOfDay': 'morning', 'sourcePortionId': 9, 'versionId': '563c6669305d6e1834ab9485', 'smartPoints': 4, 'portionTypeId': 800, 'isActive': True, 'portionName': 'item(s)', 'portionSize': 2, 'isPowerFood': True, 'trackedDate': '2016-07-20', 'sourceType': WW_FOOD, '_servingDesc': '2 item(s)', '_id': '561dcbbae33175473413d475', 'pointsPrecise': 1.8347} food_detail = get_food_detail(session, food_log) result = get_nutrition(food_detail, food_log) assert result == { 'calories': 144.0, 'sodium': 142.0, 'saturatedFat': 3.12, 'carbs': 0.72, 'sugar': 0.36, 'fat': 9.5, 'protein': 12.56 }
def test_get_calories_ww_recipe(session): with vcr.use_cassette("{}/{}.yaml".format(CASSETTES_HOME, 'steak_recipe.yaml')): # This object is actually quite more complicated. But I don't care food_log = {'smartPoints': 4, '_servingDesc': '1 serving(s)', '_displayName': 'Coffee-Chili Rubbed Flank Steak with Peppers and Onions', 'trackedDate': '2016-07-20', 'pointsPrecise': 4.8926, 'portionSize': 1, 'isActive': True, 'entryId': '6fc42740-4e38-11e6-8237-3d072975d999', 'points': 5, 'sourceId': 523991, 'smartPointsPrecise': 4, 'portionName': 'serving(s)', 'name': 'Coffee-Chili Rubbed Flank Steak with Peppers and Onions', 'portionTypeId': None, 'versionId': '57516df7f9984a6a3682ac0d', '_id': '57516df7f9984a6a3682ac0c', 'timeOfDay': 'morning', 'sourcePortionName': 'serving(s)', 'sourcePortionId': None, 'sourceType': WW_RECIPE, 'portionId': None} result = get_nutrition(get_food_detail(session, food_log), food_log) assert result == { 'sodium': 1089.58948, 'protein': 26.27602775, 'fiber': 2.48177325, 'fat': 7.102034249999999, 'sugar': 3.51908025, 'saturatedFat': 2.4551232499999998, 'carbs': 7.944517, 'calories': 201.15632675 }
def test_send_schedule(self): talks = self.generate_mockup_talks() talk_like_numbers = {talk.id: 0 for talk in talks} liked_talk_ids = [] with vcr.use_cassette('vcr_cassettes/send_schedule.yaml'): response = messaging.send_schedule( self.access_token, self.user_id, talks, talk_like_numbers, liked_talk_ids ) self.assertTrue('recipient_id' in response) self.assertTrue('message_id' in response) self.assertEqual(response['recipient_id'], self.user_id)
def test_send_talk_info(self): talk = self.create_mockup_talk_with_speaker() with vcr.use_cassette('vcr_cassettes/send_more_talk_info.yaml'): response = messaging.send_talk_info(self.access_token, self.user_id, talk) self.assertTrue('recipient_id' in response) self.assertTrue('message_id' in response) self.assertEqual(response['recipient_id'], self.user_id)
def test_send_rate_menu(self): talk = self.create_mockup_talk() talk_liked = False with vcr.use_cassette('vcr_cassettes/send_rate_menu.yaml'): response = messaging.send_rate_menu(self.access_token, self.user_id, talk, talk_liked) self.assertTrue('recipient_id' in response) self.assertTrue('message_id' in response) self.assertEqual(response['recipient_id'], self.user_id)
def test_get_started_button_set_get_delete(self): payload = 'test payload' with vcr.use_cassette('vcr_cassettes/set_get_started_button.yaml'): response = messenger_profile.set_get_started_button(self.access_token, payload) self.assertEqual(response, {'result': 'success'}) with vcr.use_cassette('vcr_cassettes/get_get_started_button.yaml'): response = messenger_profile.get_field(self.access_token, 'get_started') self.assertEqual(response['data'][0]['get_started']['payload'], payload) with vcr.use_cassette('vcr_cassettes/delete_get_started_button.yaml'): response = messenger_profile.delete_field(self.access_token, 'get_started') self.assertEqual(response, {'result': 'success'})
def test_greeting_set_get_delete(self): greeting_text = 'test greeting' with vcr.use_cassette('vcr_cassettes/set_greeting.yaml'): response = messenger_profile.set_greeting(self.access_token, greeting_text) self.assertEqual(response, {'result': 'success'}) with vcr.use_cassette('vcr_cassettes/get_greeting.yaml'): response = messenger_profile.get_field(self.access_token, 'greeting') self.assertEqual(response['data'][0]['greeting'][0]['text'], greeting_text) with vcr.use_cassette('vcr_cassettes/delete_greeting.yaml'): response = messenger_profile.delete_field(self.access_token, 'greeting') self.assertEqual(response, {'result': 'success'})
def test_commit(): with vcr.use_cassette('test/fixtures/commit.yaml'): ret = on_message({"text": u"!commit"}, None) assert 'stuff' in ret
def test_basic(): with vcr.use_cassette('test/fixtures/wiki_basic.yaml'): ret = on_message({"text": u"!wiki dog"}, None) assert "member of the canidae family" in ret assert "http://en.wikipedia.org/wiki/Dog" in ret
def test_unicode(): with vcr.use_cassette('test/fixtures/wiki_unicode.yaml'): ret = on_message({"text": u"!wiki ?????"}, None) # not blowing up == success
def test_gif(): with vcr.use_cassette('test/fixtures/gif_bananas.yaml'): ret = on_message({"text": u"!gif bananas"}, None) assert ret in bananas_gifs, "{0} not in {1}".format(ret, bananas_gifs)
def test_unicode(): with vcr.use_cassette('test/fixtures/gif_unicode.yaml'): ret = on_message({"text": u"!gif Mötörhead"}, None) # not blowing up == success, for our purposes
def test_unicode(): with vcr.use_cassette('test/fixtures/weather_unicode.yaml'): ret = on_message({"text": u"!weather Provençal"}, None) # not blowing up == success
def test_apple(): with vcr.use_cassette('test/fixtures/stock_apple.yaml'): ret = on_message({"text": u"$aapl"}, None) assert ':chart_with_upwards_trend:' in ret assert 'Apple Inc.' in ret assert '130.41' in ret assert '+1.62' in ret
def test_nonexistent(): with vcr.use_cassette('test/fixtures/stock_none'): ret = on_message({"text": u"bana"}, None) eq_(ret, None)
def test_unicode(): with vcr.use_cassette('test/fixtures/stock_unicode.yaml'): ret = on_message({"text": u"$äapl"}, None) eq_(ret, None)
def test_multiple(): with vcr.use_cassette('test/fixtures/stock_multiple.yaml'): ret = on_message({"text": u"$goog $aapl"}, None) assert 'Google Inc' in ret
def test_calc(): with vcr.use_cassette('test/fixtures/calc_basic.yaml'): ret = on_message({"text": u"!calc 2469*5"}, None) assert '12,345' in ret
def test_unicode(): with vcr.use_cassette('test/fixtures/calc_unicode.yaml'): # ??? is the Thai Bhat (spelled in Thai, obvs) ret = on_message({"text": u"!calc 10 dollars in ???"}, None) # no exception == success
def test_basic(): with vcr.use_cassette('test/fixtures/mlb_basic.yaml'): ret = on_message({"text": u"!mlb Red Sox"}, None) assert "Boston Red Sox" in ret
def test_image(): with vcr.use_cassette('test/fixtures/image_bananas.yaml'): ret = on_message({"text": u"!image bananas"}, None) assert ret in bananas_images, "{0} not in {1}".format(ret, bananas_images)
def test_unicode(): with vcr.use_cassette('test/fixtures/image_unicode.yaml'): ret = on_message({"text": u"!image Mötörhead"}, None) # not blowing up == success, for our purposes
def test_basic(): with vcr.use_cassette('test/fixtures/stockphoto_basic.yaml'): ret = on_message({"text": u"!stock woman eating salad"}, None) assert ret in women_eating_salad
def test_unicode(): with vcr.use_cassette('test/fixtures/stockphoto_unicode.yaml'): ret = on_message({"text": u"!stock übermensch"}, None) # not blowing up == success
def test_basic(): with vcr.use_cassette('test/fixtures/youtube_basic.yaml'): ret = on_message({"text": u"!youtube live long and prosper"}, None) assert ret == "https://www.youtube.com/watch?v=DyiWkWcR86I"
def test_unicode(): with vcr.use_cassette('test/fixtures/youtube_unicode.yaml'): ret = on_message({"text": u"!youtube ???????"}, None) # not blowing up == success
def test_update_role(self): role = CLIENT.roles(PLAYGROUND_SPACE).find('1a6FSwjdLnKifppXvfELau') with vcr.use_cassette('fixtures/role/update.yaml'): role.name = 'Not Klingon' role.save() self.assertEqual(role.name, 'Not Klingon')
def test_delete_role(self): role = CLIENT.roles(PLAYGROUND_SPACE).find('1a6FSwjdLnKifppXvfELau') with vcr.use_cassette('fixtures/role/delete.yaml'): role.delete() with vcr.use_cassette('fixtures/role/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.roles(PLAYGROUND_SPACE).find('1a6FSwjdLnKifppXvfELau')
def test_update_content_type(self): content_type = CLIENT.content_types(PLAYGROUND_SPACE).find('foo') self.assertEqual(content_type.name, 'something else') with vcr.use_cassette('fixtures/content_type/update.yaml'): content_type.name = 'foo' content_type.save() self.assertEqual(content_type.name, 'foo')
def test_delete_content_type(self): content_type = CLIENT.content_types(PLAYGROUND_SPACE).find('45JdPK7wbCQwecKOAyqcw0') with vcr.use_cassette('fixtures/content_type/delete.yaml'): content_type.delete() with vcr.use_cassette('fixtures/content_type/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.content_types(PLAYGROUND_SPACE).find('45JdPK7wbCQwecKOAyqcw0')
def test_publish_content_type(self): content_type = CLIENT.content_types(PLAYGROUND_SPACE).find('1JzBeA5EcEcyKUaqGeqImy') published_counter = getattr(content_type, 'published_counter', 0) with vcr.use_cassette('fixtures/content_type/publish.yaml'): content_type.publish() self.assertEqual(content_type.published_counter, published_counter + 1) self.assertTrue(content_type.is_published)
def test_personal_access_token_revoke(self): token = CLIENT.personal_access_tokens().find('6ZKYaf1m2PyFU7Olcnw5jK') self.assertFalse(token.is_revoked) with vcr.use_cassette('fixtures/pat/revoke.yaml'): token = CLIENT.personal_access_tokens().revoke('6ZKYaf1m2PyFU7Olcnw5jK') self.assertTrue(token.is_revoked)
def test_editor_interface_update(self): editor_interface = CLIENT.editor_interfaces(PLAYGROUND_SPACE, 'foo').find() self.assertEqual(editor_interface.controls[0]['widgetId'], 'multiLine') with vcr.use_cassette('fixtures/editor_interface/update.yaml'): editor_interface.controls[0]['widgetId'] = 'singleLine' editor_interface.save() self.assertEqual(editor_interface.controls[0]['widgetId'], 'singleLine')
def test_update_api_key(self): api_key = CLIENT.api_keys(PLAYGROUND_SPACE).find('42sVZNadpFAje7EFwHOfVY') with vcr.use_cassette('fixtures/api_key/update.yaml'): api_key.name = 'Not Test Key' api_key.save() self.assertEqual(api_key.name, 'Not Test Key')
def test_delete_api_key(self): api_key = CLIENT.api_keys(PLAYGROUND_SPACE).find('42sVZNadpFAje7EFwHOfVY') with vcr.use_cassette('fixtures/api_key/delete.yaml'): api_key.delete() with vcr.use_cassette('fixtures/api_key/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.api_keys(PLAYGROUND_SPACE).find('42sVZNadpFAje7EFwHOfVY')
def test_delete_entry(self): entry = CLIENT.entries(PLAYGROUND_SPACE).find('4RToqNcBfW6MAK0UGU0qWc') with vcr.use_cassette('fixtures/entry/delete.yaml'): entry.delete() with vcr.use_cassette('fixtures/entry/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.entries(PLAYGROUND_SPACE).find('4RToqNcBfW6MAK0UGU0qWc')
def test_unpublish_entry(self): entry = CLIENT.entries(PLAYGROUND_SPACE).find('5gQdVmPHKwIk2MumquYwOu') with vcr.use_cassette('fixtures/entry/unpublish.yaml'): entry.unpublish() self.assertFalse(entry.is_published)
def test_update_entry(self): entry = CLIENT.entries(PLAYGROUND_SPACE).find('1dYTHwMZlU2wm88SA8I2Q0') self.assertEqual(entry.name, 'foobar') with vcr.use_cassette('fixtures/entry/update.yaml'): entry.name = 'something else' entry.save() self.assertEqual(entry.name, 'something else') # Issues
def test_update_entry_field_with_undefined_but_non_present_field(self): entry = None with vcr.use_cassette('fixtures/entry/undefined_fields.yaml'): entry = CLIENT.entries(PLAYGROUND_SPACE).find('33uj74Wln2Oc02CAyEY8CK') self.assertEqual(entry.fields(), {}) with vcr.use_cassette('fixtures/entry/undefined_fields_non_present.yaml'): entry.non_existent = 'foo' self.assertEqual(entry.non_existent, 'foo') self.assertEqual(entry.fields(), {})
def test_update_asset(self): asset = CLIENT.assets(PLAYGROUND_SPACE).find('file3') with vcr.use_cassette('fixtures/asset/update.yaml'): asset.file['fileName'] = 'demo app image' asset.save() self.assertEqual(asset.file['fileName'], 'demo app image')
def test_delete_asset(self): asset = CLIENT.assets(PLAYGROUND_SPACE).find('file6') with vcr.use_cassette('fixtures/asset/delete.yaml'): asset.delete() with vcr.use_cassette('fixtures/asset/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.assets(PLAYGROUND_SPACE).find('file6')
def test_publish_asset(self): asset = CLIENT.assets(PLAYGROUND_SPACE).find('file3') published_counter = getattr(asset, 'published_counter', 0) with vcr.use_cassette('fixtures/asset/publish.yaml'): asset.publish() self.assertEqual(asset.published_counter, published_counter + 1) self.assertTrue(asset.is_published)
def test_unpublish_asset(self): asset = CLIENT.assets(PLAYGROUND_SPACE).find('file3') with vcr.use_cassette('fixtures/asset/unpublish.yaml'): asset.unpublish() self.assertFalse(asset.is_published)
def test_archive_asset(self): asset = CLIENT.assets(PLAYGROUND_SPACE).find('file3') archived_version = getattr(asset, 'archived_version', asset.version) self.assertFalse(asset.is_archived) with vcr.use_cassette('fixtures/asset/archive.yaml'): asset.archive() self.assertEqual(asset.archived_version, archived_version) self.assertTrue(asset.is_archived)
def test_update_webhook(self): webhook = CLIENT.webhooks(PLAYGROUND_SPACE).find('2xzNZ8gOsq0sz4ueoytkeW') with vcr.use_cassette('fixtures/webhook/update.yaml'): webhook.name = 'Not Klingon' webhook.save() self.assertEqual(webhook.name, 'Not Klingon')
def test_delete_webhook(self): webhook = CLIENT.webhooks(PLAYGROUND_SPACE).find('2xzNZ8gOsq0sz4ueoytkeW') with vcr.use_cassette('fixtures/webhook/delete.yaml'): webhook.delete() with vcr.use_cassette('fixtures/webhook/not_found.yaml'): with self.assertRaises(NotFoundError): CLIENT.webhooks(PLAYGROUND_SPACE).find('2xzNZ8gOsq0sz4ueoytkeW')
def test_content_types_entries_proxy_delete(self): proxy = ContentTypeEntriesProxy(CLIENT, PLAYGROUND_SPACE, 'foo') proxy.delete('1zquSqZeokECU2Wike2cQi') with vcr.use_cassette('fixtures/entry/not_found_content_type.yaml'): with self.assertRaises(NotFoundError): proxy.find('1zquSqZeokECU2Wike2cQi')
def test_update_space(self): space = CLIENT.spaces().find('6sun6p8v2zr6') self.assertEqual(space.name, 'Create Test') with vcr.use_cassette('fixtures/space/update.yaml'): space.name = 'Update Test' space.save() self.assertEqual(space.name, 'Update Test')