def test_save(self, get_mock_db): class Artist(RedisModel): model_name = 'artist' model_fields = ('genre', 'location') model_sets = ('albums',) mock_db = get_mock_db() mock_pipe = create_autospec(StrictPipeline) mock_db.smembers.side_effect = [set()] mock_db.pipeline.return_value = mock_pipe data = {'genre': 'Alternative'} artist = Artist('Paramore', data.copy()) location, albums = 'Franklin', {'All We Know Is Falling', 'Riot'} artist.location, artist.albums = location, albums database.save(artist) mock_pipe.hmset.assert_called_once_with( 'artist:Paramore', {'location': 'Franklin'} ) mock_pipe.sadd.assert_has_calls([ call('artist:Paramore:albums', album) for album in albums ], any_order=True) self.assertTrue(mock_pipe.execute.called) # Make sure the model's fields weren't changed by the save method. self.assertEqual(artist.genre, data['genre']) self.assertEqual(artist.location, location) self.assertEqual(artist.albums, albums)
def test_save_sets(self, get_mock_db): class Artist(RedisModel): model_name = 'artist' model_sets = ('members', ) mock_db = get_mock_db() mock_pipe = create_autospec(StrictPipeline) members = { 'Hayley Williams', 'Jeremy Davis', 'Josh Farro', 'Zac Farro' } mock_db.smembers.side_effect = [members] mock_db.pipeline.return_value = mock_pipe artist = Artist('Paramore', {'members': members.copy()}) added, removed = {'Taylor York', 'Justin York'}, {'Josh Farro', 'Zac Farro'} artist.members.update(added) artist.members.difference_update(removed) database.save(artist) mock_pipe.sadd.assert_has_calls( [call('artist:Paramore:members', member) for member in added], any_order=False) mock_pipe.srem.assert_has_calls( [call('artist:Paramore:members', member) for member in removed], any_order=False) self.assertTrue(mock_pipe.execute.called)
def test_save(self, get_mock_db): class Artist(RedisModel): model_name = 'artist' model_fields = ('genre', 'location') model_sets = ('albums', ) mock_db = get_mock_db() mock_pipe = create_autospec(StrictPipeline) mock_db.smembers.side_effect = [set()] mock_db.pipeline.return_value = mock_pipe data = {'genre': 'Alternative'} artist = Artist('Paramore', data.copy()) location, albums = 'Franklin', {'All We Know Is Falling', 'Riot'} artist.location, artist.albums = location, albums database.save(artist) mock_pipe.hmset.assert_called_once_with('artist:Paramore', {'location': 'Franklin'}) mock_pipe.sadd.assert_has_calls( [call('artist:Paramore:albums', album) for album in albums], any_order=True) self.assertTrue(mock_pipe.execute.called) # Make sure the model's fields weren't changed by the save method. self.assertEqual(artist.genre, data['genre']) self.assertEqual(artist.location, location) self.assertEqual(artist.albums, albums)
def authenticate(state, code): r = get_reddit_oauth_session() if database.get('authentication_state', state) is None: raise AuthenticationFailure("Invalid state token") database.delete('authentication_state', state) access_information = r.get_access_information(code) authenticated_user = r.get_me() username = authenticated_user.name user = User(username) user.username = username update_access_information(user, access_information) database.save(user) login.set_remember_token(user) return user
def test_save_sets(self, get_mock_db): class Artist(RedisModel): model_name = 'artist' model_sets = ('members',) mock_db = get_mock_db() mock_pipe = create_autospec(StrictPipeline) members = { 'Hayley Williams', 'Jeremy Davis', 'Josh Farro', 'Zac Farro' } mock_db.smembers.side_effect = [members] mock_db.pipeline.return_value = mock_pipe artist = Artist('Paramore', {'members': members.copy()}) added, removed = {'Taylor York', 'Justin York'}, {'Josh Farro', 'Zac Farro'} artist.members.update(added) artist.members.difference_update(removed) database.save(artist) mock_pipe.sadd.assert_has_calls([ call('artist:Paramore:members', member) for member in added ], any_order=False) mock_pipe.srem.assert_has_calls([ call('artist:Paramore:members', member) for member in removed ], any_order=False) self.assertTrue(mock_pipe.execute.called)