def setUp(self): """ """ self.redis = fakeredis.FakeRedis() fixtures.npc.import_fixtures(self) fixtures.phobia.import_fixtures(self) fixtures.motivation.import_fixtures(self)
def set_up(self): self.mock_environment_fetcher.get_all_environments.return_value = ([ '/path/to/default.config.yaml' ], []) self.mock_message_router.push_message.side_effect = self._push_message self.mock_at_exit.side_effect = self._register_at_exit self.message = None self.at_exit = None self.mock_run_job_klass.return_when(self.mock_run_job, self.mock_message_router, self.pipeline_context) self.mock_complete_job_klass.return_when(self.mock_complete_job, self.mock_message_router, self.pipeline_context) self.mock_failed_job_klass.return_when(self.mock_failed_job, self.mock_message_router, self.pipeline_context, self.exception_data) self.mock_config_listing_klass.return_when(self.mock_config_listing, 'execution') self.patch('foundations_contrib.global_state.redis_connection', fakeredis.FakeRedis()) sys.excepthook = sys.__excepthook__
def config_loaded_redis(request: FixtureRequest): redis_conn = fakeredis.FakeRedis() redis_conn.hset('CONFIG', 'REDIS_ADDRESS', 'redis://localhost:6379') redis_conn.hset('CONFIG', 'REDIS_DB', 1) redis_conn.hset('CONFIG', 'ENABLED_CHANNEL_COMMANDS', json.dumps({ 'qa-talk': '*', '*': ['help', 'list'], })) redis_conn.hset( 'CONFIG', 'STATUS_COLORS', json.dumps({ 'integration': colors.ORANGE, 'staging': colors.YELLOW, 'released': colors.GREEN, 'queued': colors.BLUE, '*': colors.BLUE, })) redis_conn.hset('CONFIG', 'SLACK_VERIFICATION_TOKEN', 'slacktoken123') redis_conn.hset('CONFIG', 'PIVOTAL_API_TOKEN', 'pivotaltoken123') redis_conn.hset('CONFIG', 'PIVOTAL_PROJECT_IDS', json.dumps([ 1234567, 2345678, 3456789, ])) redis_conn.hset('CONFIG', 'GITHUB_ACCESS_TOKEN', 'githubtoken123')
def test_lock_is_removed_after_func_is_finished(self): redis_params = make_redis_params( file_path='test_dir/test_file.pkl', unique_id='123abc', redis_host='0.0.0.0', redis_port=12345, ) server = fakeredis.FakeServer() with patch('gokart.redis_lock.redis.Redis') as redis_mock: redis_mock.return_value = fakeredis.FakeRedis( server=server, host=redis_params.redis_host, port=redis_params.redis_port) mock_func = MagicMock() resulted = with_lock(func=mock_func, redis_params=redis_params)(123, b='abc') mock_func.assert_called_once() called_args, called_kwargs = mock_func.call_args self.assertTupleEqual(called_args, (123, )) self.assertDictEqual(called_kwargs, dict(b='abc')) self.assertEqual(resulted, mock_func()) fake_redis = fakeredis.FakeStrictRedis(server=server) with self.assertRaises(KeyError): fake_redis[redis_params.redis_key]
def test_kathy_hit_thirtyone_has_no_cards_left_and_others_do(self): """ Kathy just hit 31, and has no cards left. Tom has a card left Expected: no new points for kathy, and its now Tom's turn with a fresh pegging area """ fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': [], 'tom': ['95f92b2f0c'] }, 'pegging': { 'cards': ['75e734d054', '60575e1068', '1d5eb77128'], 'last_played': 'kathy', 'passed': [], 'run': [], 'total': 31 }, 'players': { 'tom': 0, 'kathy': 2 }, 'turn': 'kathy', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis bev.next_player('test') g = json.loads(fake_redis.get('test')) assert g['players']['kathy'] == 2 assert g['turn'] == 'tom' assert g['pegging']['total'] == 0
def test_init_connector( mock_cognitoidp_client, mock_user_pool, ): """Test the creation of a connector and share it with the rest the test suite Args: mock_cognitoidp_client (object): ab object mocking the connection with Cognito IDP Returns: AWSConnector: an instance of HTC Grid connector """ mock_agent_config = { "grid_storage_service": "REDIS", "s3_bucket": "test_bucket", "redis_url": "redis_url.eu-west-1.amazonaws.com", "public_api_gateway_url": "https://publicapi.eu-west-1.amazonaws.com", "private_api_gateway_url": "https://privateapi.eu-west-1.amazonaws.com", "api_gateway_key": "mocked_api_key_for_private_api", "user_pool_id": mock_user_pool["UserPoolId"], "cognito_userpool_client_id": mock_user_pool["UserPoolClientId"], "dynamodb_results_pull_interval_sec": 1, "task_input_passed_via_external_storage": 1 } connector = AWSConnector() connector.init(mock_agent_config, username="******", password="******", cognitoidp_client=mock_cognitoidp_client, redis_custom_connection=fakeredis.FakeRedis()) return connector
def setUp(self) -> None: self.redis = fakeredis.FakeRedis() path = GcsfsFilePath.from_absolute_path( "gs://test_bucket/us_id/ingest_view/2021/01/11/unprocessed_2021-01-11T00:00:00:000000_ingest_view_test_view.csv" ) self.cache = mock.MagicMock() self.delegate = CacheIngestFileAsParquetDelegate(self.cache, path)
def test_next_must_play(self): """ Kathy and Tom each have aces. Tom just played and the total is at 30 Expected: It is now kathy's turn and she must play """ fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': ['6d95c18472'], 'tom': ['ace1293f8a'] }, 'pegging': { 'cards': ['75e734d054', '60575e1068', '1d5eb77128'], 'last_played': 'tom', 'passed': [], 'run': [], 'total': 30 }, 'players': { 'tom': 0, 'kathy': 0 }, 'state': 'PLAY', 'turn': 'tom', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis bev.next_player('test') g = json.loads(fake_redis.get('test')) assert g['turn'] == 'kathy' assert g['pegging']['total'] == 30 assert bev.get_player_action('test', g['turn']) == 'PLAY'
def setUp(self) -> None: self.test_app = Flask(__name__) blueprint = Blueprint("data_discovery_test", __name__) self.test_client = self.test_app.test_client() self.fakeredis = fakeredis.FakeRedis() self.fs = FakeGCSFileSystem() self.gcs_factory_patcher = patch( "recidiviz.admin_panel.routes.data_discovery.GcsfsFactory.build") self.gcs_factory_patcher.start().return_value = self.fs self.project_number_patcher = patch( "recidiviz.utils.metadata.project_number", return_value=999) self.requires_gae_auth_patcher = patch( "recidiviz.admin_panel.routes.data_discovery.requires_gae_auth", side_effect=lambda route: route, ) self.redis_patcher = patch("redis.Redis", return_value=self.fakeredis) self.project_number_patcher.start() self.redis_patcher.start() self.requires_gae_auth_patcher.start() add_data_discovery_routes(blueprint) self.test_app.register_blueprint(blueprint)
def mock_redis(): """Monkey patch service cache with mocked redis.""" from renku.service.cache.base import BaseCache from renku.service.cache.models.user import User from renku.service.cache.models.job import Job from renku.service.cache.models.file import File from renku.service.cache.models.project import Project from renku.service.jobs.queues import WorkerQueues monkey_patch = MonkeyPatch() with monkey_patch.context() as m: fake_redis = fakeredis.FakeRedis() fake_model_db = Database(connection_pool=fake_redis.connection_pool) m.setattr(WorkerQueues, 'connection', fake_redis) m.setattr(BaseCache, 'cache', fake_redis) m.setattr(BaseCache, 'model_db', fake_model_db) m.setattr(Job, '__database__', fake_model_db) m.setattr(User, '__database__', fake_model_db) m.setattr(File, '__database__', fake_model_db) m.setattr(Project, '__database__', fake_model_db) yield monkey_patch.undo()
def app(): os.environ["CTF_CONFIG"] = "tests/configs/good.json" app = create_app() app.redis = fakeredis.FakeRedis() app.secret_key = 'my secret key' app.debug = True return app
def setUp(self): """ """ self.redis = fakeredis.FakeRedis() fixtures.bond.import_fixtures(self) fixtures.npc.import_fixtures(self) fixtures.phobia.import_fixtures(self) fixtures.motivation.import_fixtures(self) self.redis.lpush('npc_race', 'gnome')
def set_up(self): import fakeredis from foundations_internal.pipeline import Pipeline from foundations_internal.pipeline_context import PipelineContext self._pipeline_context = PipelineContext() self._pipeline = Pipeline(self._pipeline_context) self.patch('foundations_rest_api.global_state.redis_connection', fakeredis.FakeRedis())
def project_set_up(self): import fakeredis self._find_project = self.patch( "foundations_contrib.models.project_listing.ProjectListing.find_project" ) self._redis = self.patch( "foundations_rest_api.global_state.redis_connection", fakeredis.FakeRedis())
def setUp(self): # Setup fake redis for testing. self.r = fakeredis.FakeRedis() self.r.set('top_10', json.dumps(TOP_10_SAMPLE)) self.r.set('top_10_percentage', json.dumps(TOP_10_PERCENTAGE_SAMPLE)) self.r.set('top_10_per_author_percentage', json.dumps({'somename': TOP_10_PERCENTAGE_SAMPLE})) self.r.set('top_10_per_author', json.dumps({'somename': TOP_10_SAMPLE}))
def test_thirtyone(self): """ Verify two points for 31 """ fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': ['6d95c18472', 'c6f4900f82'], 'tom': ['ace1293f8a'] }, 'pegging': { 'cards': ['4de6b73ab8', 'f6571e162f', 'c88523b677'], # eight, ten, six 'last_played': 'tom', 'passed': [], 'run': [], 'total': 24 }, 'players': { 'tom': 0, 'kathy': 0 }, 'played_cards': { 'kathy': ['f6571e162f'], 'tom': ['4de6b73ab8', 'c88523b677'] }, 'scoring_stats': { 'tom': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 }, 'kathy': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 } }, 'state': 'PLAY', 'turn': 'kathy', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis seven_of_clubs = 'c6f4900f82' just_won, points, points_source = bev.score_play( 'test', 'kathy', seven_of_clubs) assert not just_won bev.record_play('test', 'kathy', seven_of_clubs) g = json.loads(fake_redis.get('test')) assert set(g['pegging']['cards']) == set( ['4de6b73ab8', 'f6571e162f', 'c88523b677', 'c6f4900f82']) assert g['hands']['kathy'] == ['6d95c18472'] assert g['players']['kathy'] == 2 assert g['pegging']['total'] == 31
def test_run_of_three(self): """ test run of three scores three points """ fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': ['6d95c18472', 'c6f4900f82'], 'tom': ['ace1293f8a'] }, 'pegging': { 'cards': ['4de6b73ab8', 'c88523b677'], # eight, six 'last_played': 'tom', 'passed': [], 'run': [], 'total': 14 }, 'players': { 'tom': 0, 'kathy': 0 }, 'played_cards': { 'kathy': ['32f7615119'], 'tom': ['4f99bf15e5', 'def8effef6'] }, 'scoring_stats': { 'tom': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 }, 'kathy': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 } }, 'state': 'PLAY', 'turn': 'kathy', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis seven_of_clubs = 'c6f4900f82' just_won, points, points_source = bev.score_play( 'test', 'kathy', seven_of_clubs) assert not just_won bev.record_play('test', 'kathy', seven_of_clubs) g = json.loads(fake_redis.get('test')) assert set(g['pegging']['cards']) == set( ['4de6b73ab8', 'c88523b677', 'c6f4900f82']) # all the sevens assert g['hands']['kathy'] == ['6d95c18472'] assert g['pegging']['total'] == 21 assert g['players']['kathy'] == 3
def make_database(reset, lock): r = RedisManager(fakeredis.FakeRedis()) r.delete_all() list = json.dumps({"A":"a", "B":["b", "c"]}) list2 = json.dumps({"D":"d", "E":["e", "f"]}) list3 = json.dumps({"G":"g", "H":["h", "i"]}) r.add_to_queue(list) r.add_to_queue(list2) r.add_to_progress(list3) return r
def get_connection(): """Helper method to get redis connection configured by settings""" import redis import fakeredis from .settings import diffs_settings if not diffs_settings['test_mode']: return redis.Redis(**diffs_settings['redis']) else: return fakeredis.FakeRedis()
def setUp(self): app.config['redis'] = fakeredis.FakeRedis() app.testing = True # Keys used in redis for different types app.config['dbkeys'] = _set_db_keys() # capnp schemas used to serialize type into database app.config['capnp'] = _load_capnp_schemas() self.app = app.test_client()
def test_four_of_a_kind(self): fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': ['6d95c18472', 'c6f4900f82'], 'tom': ['ace1293f8a'] }, 'pegging': { 'cards': ['32f7615119', '4f99bf15e5', 'def8effef6'], # seven of spades, diamonds, hearts 'last_played': 'tom', 'passed': [], 'run': [], 'total': 21 }, 'players': { 'tom': 6, 'kathy': 2 }, 'played_cards': { 'kathy': ['32f7615119'], 'tom': ['4f99bf15e5', 'def8effef6'] }, 'scoring_stats': { 'tom': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 }, 'kathy': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 } }, 'state': 'PLAY', 'turn': 'kathy', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis seven_of_clubs = 'c6f4900f82' just_won, points, points_source = bev.score_play( 'test', 'kathy', seven_of_clubs) assert not just_won bev.record_play('test', 'kathy', seven_of_clubs) g = json.loads(fake_redis.get('test')) assert set(g['pegging']['cards']) == set( ['32f7615119', '4f99bf15e5', 'def8effef6', 'c6f4900f82']) # all the sevens assert g['hands']['kathy'] == ['6d95c18472'] assert g['pegging']['total'] == 28 assert g['players']['kathy'] == 14
def setUp(self): """ """ self.redis = fakeredis.FakeRedis() fixtures.organization.import_fixtures(self) fixtures.npc.import_fixtures(self) fixtures.leader.import_fixtures(self) fixtures.phobia.import_fixtures(self) fixtures.motivation.import_fixtures(self) fixtures.region.import_fixtures(self) fixtures.country.import_fixtures(self) self.redis.lpush('npc_race', 'gnome')
def connection(self): """ A mocked connection to Redis """ connection = fakeredis.FakeRedis() connection.set("sample-string", "sample") connection.lpush("sample-list", 1, 2, 3) connection.hset("sample-hash", "a", 1) connection.sadd("sample-set", "a", "b", "c") return connection
def bind(self, url): if url: self.client = redis.Redis.from_url(url, decode_responses=True) else: warnings.warn( "Redorm was not provided a Redis URL and will fall back to the in-built incredibly slow fake redis. " "Set REDORM_URL environment variable to fix.", RuntimeWarning, ) # Fake redis for developement self.client = fakeredis.FakeRedis(decode_responses=True) self.setup_scripts()
def test_deal_unique_cards(self, three_player_game_unstarted): fake_redis = fakeredis.FakeRedis() fake_redis.set('cheers', json.dumps(three_player_game_unstarted)) bev.cache = fake_redis bev.start_game('cheers') bev.deal_hands('cheers') g = json.loads(fake_redis.get('cheers')) assert g['state'] == 'DISCARD' for player in three_player_game_unstarted['players'].keys(): assert player in g['hands'].keys() assert len(g['hands'][player]) == g['hand_size']
def __post_init__(self): if self.fake: self.pool = fakeredis.FakeRedis(encoding='utf-8', decode_responses=True) else: primary_endpoint = self.config["redis_primary_endpoint"] if "encoding" in self.config: self.pool = redis.Redis(host=primary_endpoint, encoding=self.config["encoding"], decode_responses=True) else: self.pool = redis.Redis(host=primary_endpoint)
def test_fifteen_two(self): fake_redis = fakeredis.FakeRedis() game_dict = { 'cards': CARDS, 'hands': { 'kathy': ['6d95c18472', 'c6f4900f82'], 'tom': ['ace1293f8a'] }, 'pegging': { 'cards': ['4de6b73ab8'], # eight of hearts 'last_played': 'tom', 'passed': [], 'run': [], 'total': 8 }, 'players': { 'tom': 0, 'kathy': 0 }, 'played_cards': { 'kathy': [], 'tom': [] }, 'scoring_stats': { 'tom': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 }, 'kathy': { 'a_play': 0, 'b_hand': 0, 'c_crib': 0 } }, 'state': 'PLAY', 'turn': 'kathy', 'winning_score': 121, } fake_redis.set('test', json.dumps(game_dict)) bev.cache = fake_redis seven_of_clubs = 'c6f4900f82' just_won, points, points_source = bev.score_play( 'test', 'kathy', seven_of_clubs) assert not just_won bev.record_play('test', 'kathy', seven_of_clubs) g = json.loads(fake_redis.get('test')) assert g['players']['kathy'] == 2 assert g['pegging']['total'] == 15 assert set(g['pegging']['cards']) == set(['4de6b73ab8', 'c6f4900f82']) assert g['hands']['kathy'] == ['6d95c18472']
def test_first_discard(self, two_player_game_unstarted): fake_redis = fakeredis.FakeRedis() fake_redis.set('cheers', json.dumps(two_player_game_unstarted)) bev.cache = fake_redis bev.start_game('cheers') bev.deal_hands('cheers') g = json.loads(fake_redis.get('cheers')) player_done, all_done = bev.discard('cheers', 'sam', g['hands']['sam'][0]) assert not player_done assert not all_done assert g['state'] == 'DISCARD'
def setUp(self): """ """ # TODO see if testconfiguration can put a prefix on redis keys to prevent overlap self.redis = fakeredis.FakeRedis() fixtures.business.import_fixtures(self) fixtures.npc.import_fixtures(self) fixtures.region.import_fixtures(self) fixtures.motivation.import_fixtures(self) fixtures.phobia.import_fixtures(self) fixtures.city.import_fixtures(self) fixtures.organization.import_fixtures(self) fixtures.leader.import_fixtures(self) fixtures.country.import_fixtures(self)
def test_start_with_two_players(self, two_player_game_unstarted): fake_redis = fakeredis.FakeRedis() fake_redis.set('cheers', json.dumps(two_player_game_unstarted)) bev.cache = fake_redis bev.start_game('cheers') g = json.loads(fake_redis.get('cheers')) assert g['state'] == 'DEAL' assert g['hand_size'] == 6 assert g['dealer'] != g['cutter'] assert g['dealer'] != g['first_to_score'] assert g['turn'] == g['dealer'] assert g['winning_score'] == 121 assert not g['jokers']