class CaseType: checker_cls = lambda_fixture(params=CHECKER_CLASSES + (util.Collection, )) compatible_collection_types = lambda_fixture( lambda checker_cls: set(CHECKER_COLLECTION_TYPES.values()) if checker_cls is util.Collection else {CHECKER_COLLECTION_TYPES[checker_cls]}) incompatible_collection_types = lambda_fixture( lambda compatible_collection_types: set( CHECKER_COLLECTION_TYPES.values( )) - compatible_collection_types) def it_compares_true_to_compatible_instances( self, checker_cls, compatible_collection_types): checker = checker_cls expected = compatible_collection_types actual = { cls for cls in CHECKER_COLLECTION_TYPES.values() if checker == cls() } assert expected == actual def it_doesnt_compare_true_to_incompatible_instances( self, checker_cls, incompatible_collection_types): checker = checker_cls expected = incompatible_collection_types actual = { cls for cls in CHECKER_COLLECTION_TYPES.values() if checker != cls() } assert expected == actual
class DescribeRetrieve( UsesGetMethod, UsesDetailEndpoint, Returns200, ): # NOTE: autouse=True is not used, because the detail_url requests this # fixture key_value = lambda_fixture(lambda: KeyValue.objects.create( key='apple', value='π', )) def it_returns_key_value(self, key_value, json): expected = express_key_value(key_value) actual = json assert expected == actual
class TestStuff: caret = lambda_fixture('caret') def test_it(self, caret, my_toplevel_static_fixture): assert caret a = caret b = my_toplevel_static_fixture def it_does_things(self, caret): pass def it_does_stuff(self, caret): pass def how_do_one_do_dis(self, caret): pass
class DescribeHeaders( APIViewTest, UsesGetMethod, ): # NOTE: this view simply returns the request's headers as the response url = lambda_fixture(lambda: url_for('views-headers')) # This fixture supports passing headers (e.g. `Authorization: Api-Key 123`) in the request headers = static_fixture({ 'Custom-Header': 'abc', 'Head': 'Shoulders, Knees, Toes', }) def it_passes_headers(self, json, headers): expected = headers actual = json assert_dict_is_subset(expected, actual)
class DescribeData( APIViewTest, UsesPostMethod, ): # NOTE: this view simply returns the request's POST data as the response url = lambda_fixture(lambda: url_for('views-data')) # This fixture supports passing POST data in the request data = static_fixture({ 'post': 'malone', 'fizzbuzz': 'zibbzuff', }) def it_posts_data(self, json, data): expected = data actual = json assert expected == actual
class DescribeList( UsesGetMethod, UsesListEndpoint, Returns200, ReturnsPageNumberPagination, ): movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title="Alfred Hitchcock's The Byrds: A Biopic", year=1975), Movie(title='Forty-Two Monkeys', year=1997), ]), autouse=True, ) def it_returns_movies(self, movies, results): expected = express_movies(movies) actual = results assert expected == actual class ContextSearch( Returns200, ): matching_movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title='Forty-Two Monkeys', year=1997), ])) non_matching_movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title="Alfred Hitchcock's The Byrds: A Biopic", year=1975), ])) movies = lambda_fixture( lambda matching_movies, non_matching_movies: (matching_movies + non_matching_movies), autouse=True, ) query_params = static_fixture({ 'q': 'monkey', }) def it_returns_only_matching_movies(self, matching_movies, results): expected = express_movies(matching_movies) actual = results assert expected == actual
class DescribeQueryParams( APIViewTest, UsesGetMethod, ): # NOTE: this view simply returns the request's query params as the response url = lambda_fixture(lambda: url_for('views-query-params')) # This fixture supports passing query params (e.g. ?key=val) with the requested URL query_params = static_fixture({ 'key': 'val', 'param': 'value', 'pink': 'floyd', }) def it_passes_query_params(self, json, query_params): expected = query_params actual = json assert expected == actual
class DescribeList( UsesGetMethod, UsesListEndpoint, Returns200, ): # Here, we create some rows in the DB to play with. We set autouse=True, # so the fixture is evaluated even though nothing explicitly requests it. # The @pytest.mark.late (from pytest-fixture-order) mark ensures our # http request is run *after* all autouse fixtures. key_values = lambda_fixture( lambda: (KeyValue.objects.create_batch( alpha='beta', delta='gamma', )), autouse=True, ) def it_returns_key_values_rows(self, key_values, results): expected = express_key_values(key_values) actual = results assert expected == actual
class DescribeUserInfo( APIViewTest, UsesGetMethod, ): # NOTE: this view returns the username, first_name, last_name, and email of # the authenticated user. url = lambda_fixture(lambda: url_for('authentication-user-info')) class CaseAlice(AsUser('alice')): def it_returns_alices_info(self, alice, json): expected = { 'username': alice.username, 'first_name': alice.first_name, 'last_name': alice.last_name, 'email': alice.email, } actual = json assert expected == actual class CaseBob(AsUser('bob')): def it_returns_bobs_info(self, bob, json): expected = { 'username': bob.username, 'first_name': bob.first_name, 'last_name': bob.last_name, 'email': bob.email, } actual = json assert expected == actual class CaseAnonymous: def it_returns_no_info(self, json): # NOTE: AnonymousUser does not define first_name, last_name, or email. expected = {'username': ''} actual = json assert expected == actual
class AsAnonymousUser: client = lambda_fixture('unauthed_client')
my_toplevel_static_fixture = static_fixture(123) @pytest.fixture def caret(my_toplevel_static_fixture): return 20 @pytest.mark.parametrize('v', [ pytest.param(1), ]) def test_stuff(v): a = v a = lambda_fixture(lambda : 123) class TestStuff: caret = lambda_fixture('caret') def test_it(self, caret, my_toplevel_static_fixture): assert caret a = caret b = my_toplevel_static_fixture def it_does_things(self, caret): pass def it_does_stuff(self, caret): pass
class DescribeKeyValueViewSet(ViewSetTest): list_url = lambda_fixture(lambda: url_for('views-key-values-list')) detail_url = lambda_fixture( lambda key_value: url_for('views-key-values-detail', pk=key_value.pk)) class DescribeList( UsesGetMethod, UsesListEndpoint, Returns200, ): # Here, we create some rows in the DB to play with. We set autouse=True, # so the fixture is evaluated even though nothing explicitly requests it. # The @pytest.mark.late (from pytest-fixture-order) mark ensures our # http request is run *after* all autouse fixtures. key_values = lambda_fixture( lambda: (KeyValue.objects.create_batch( alpha='beta', delta='gamma', )), autouse=True, ) def it_returns_key_values_rows(self, key_values, results): expected = express_key_values(key_values) actual = results assert expected == actual class DescribeCreate( UsesPostMethod, UsesListEndpoint, Returns201, ): data = static_fixture({ 'key': 'apple', 'value': 'π', }) ### # precondition_fixture uses the pytest dependency graph to ensure that, # if requested, this fixture is *always* evaluated before our HTTP request # is made. # # Here, we record the existing KeyValue IDs, so we can verify that a # new row was indeed created by our endpoint. # initial_key_value_ids = precondition_fixture( lambda: set(KeyValue.objects.values_list('pk', flat=True))) def it_creates_key_value(self, initial_key_value_ids, json): expected = initial_key_value_ids | {json['id']} actual = set(KeyValue.objects.values_list('pk', flat=True)) assert expected == actual def it_returns_key_value(self, json): key_value = KeyValue.objects.get(pk=json['id']) expected = express_key_value(key_value) actual = json assert expected == actual def it_sets_model_fields(self, data, json): key_value = KeyValue.objects.get(pk=json['id']) expected = data assert_model_attrs(key_value, expected) class DescribeRetrieve( UsesGetMethod, UsesDetailEndpoint, Returns200, ): # NOTE: autouse=True is not used, because the detail_url requests this # fixture key_value = lambda_fixture(lambda: KeyValue.objects.create( key='apple', value='π', )) def it_returns_key_value(self, key_value, json): expected = express_key_value(key_value) actual = json assert expected == actual class DescribeUpdate( UsesPatchMethod, UsesDetailEndpoint, Returns200, ): # NOTE: autouse=True is not used, because the detail_url requests this # fixture key_value = lambda_fixture(lambda: KeyValue.objects.create( key='apple', value='π', )) data = static_fixture({ 'key': 'banana', 'value': 'ρ', }) ### # precondition_fixture uses the pytest dependency graph to ensure that, # if requested, this fixture is *always* evaluated before our HTTP request # is made. # # Here, we record the existing KeyValue IDs, so we can verify that no # new rows are created by our endpoint. We request the `key_value` fixture, # to ensure it's included in this set. # initial_key_value_ids = precondition_fixture(lambda key_value: set( KeyValue.objects.values_list('pk', flat=True))) def it_updates_key_value(self, key_value, data): # After updating, refreshing our DB row is vital — otherwise, it # will appear as though our endpoint is not doing its job. key_value.refresh_from_db() expected = data assert_model_attrs(key_value, expected) def it_returns_key_value(self, key_value, json): # After updating, refreshing our DB row is vital — otherwise, it # will appear as though our endpoint is not doing its job. key_value.refresh_from_db() expected = express_key_value(key_value) actual = json assert expected == actual def it_doesnt_create_or_destroy_rows(self, initial_key_value_ids): expected = initial_key_value_ids actual = set(KeyValue.objects.values_list('pk', flat=True)) assert expected == actual class DescribeDestroy( UsesDeleteMethod, UsesDetailEndpoint, Returns204, ): # NOTE: autouse=True is not used, because the detail_url requests this # fixture key_value = lambda_fixture(lambda: KeyValue.objects.create( key='apple', value='π', )) ### # precondition_fixture uses the pytest dependency graph to ensure that, # if requested, this fixture is *always* evaluated before our HTTP request # is made. # # Here, we record the existing KeyValue IDs, so we can verify that our # endpoint actually deletes the row # initial_key_value_ids = precondition_fixture( lambda: set(KeyValue.objects.values_list('pk', flat=True))) def it_deletes_key_value(self, key_value, initial_key_value_ids): expected = initial_key_value_ids - {key_value.id} actual = set(KeyValue.objects.values_list('pk', flat=True)) assert expected == actual
class DescribeStatusCode( APIViewTest, UsesGetMethod, ): # Status code to be returned from view. # This will be overridden in child test contexts. status_code = not_implemented_fixture() url = lambda_fixture( lambda status_code: url_for('status-code', code=status_code)) class Case200(Returns200): status_code = static_fixture(200) class Case201(Returns201): status_code = static_fixture(201) class Case202(Returns202): status_code = static_fixture(202) class Case204(Returns204): status_code = static_fixture(204) class Case301(Returns301): status_code = static_fixture(301) class Case302(Returns302): status_code = static_fixture(302) class Case304(Returns304): status_code = static_fixture(304) class Case307(Returns307): status_code = static_fixture(307) class Case308(Returns308): status_code = static_fixture(308) class Case400(Returns400): status_code = static_fixture(400) class Case401(Returns401): status_code = static_fixture(401) class Case403(Returns403): status_code = static_fixture(403) class Case404(Returns404): status_code = static_fixture(404) class Case405(Returns405): status_code = static_fixture(405) class Case409(Returns409): status_code = static_fixture(409) class Case422(Returns422): status_code = static_fixture(422) class Case429(Returns429): status_code = static_fixture(429) class Case500(Returns500): status_code = static_fixture(500) class Case503(Returns503): status_code = static_fixture(503) class Case504(Returns504): status_code = static_fixture(504) class CaseArbitrary(ReturnsStatus(599)): status_code = static_fixture(599)
class UsesDetailEndpoint: url = lambda_fixture('detail_url')
import logging import pytest from pytest_lambda import lambda_fixture, static_fixture from linked_list import BasicLinearLinkedList logger = logging.getLogger(__name__) nodes = lambda_fixture(lambda single_link_node_factory: single_link_node_factory.create_batch(size=10)) empty_list = lambda_fixture(lambda: BasicLinearLinkedList()) items_to_add = static_fixture([(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]) @pytest.fixture def list_with_nodes(nodes): for current, next_node in zip(nodes, nodes[1:]): current.next = next_node linear_linked_list = BasicLinearLinkedList() linear_linked_list.head = nodes[0] linear_linked_list.length = 10 return linear_linked_list class TestBasicLinearLinkedList: class ContextValidKey:
'random_init': None, 'random_params': (None, None) } # todo random init as testcase ? test_rand_initializer = { 'interval': None, 'random_init': None, 'random_params': (None, None) } # profile_parameter test_amp = 20 test_bias = 10 test_freq = 2 # simple triangular profile as example test_speed_profile = lambda_fixture( lambda t, amp, freq, bias: amp * signal.sawtooth(2 * np.pi * freq * t, width=0.5) + bias) @pytest.fixture def defaultMechanicalLoad(): """ pytest fixture that returns a default MechanicalLoad object :return: MechanicalLoad object initialized with default values """ return MechanicalLoad() @pytest.fixture def concreteMechanicalLoad(): """
from pytest_lambda import lambda_fixture, static_fixture unique = lambda_fixture(lambda: 'unique') def it_processes_toplevel_lambda_fixture(unique): expected = 'unique' actual = unique assert expected == actual unique_static = static_fixture('unique') def it_processes_toplevel_static_fixture(unique_static): expected = 'unique' actual = unique_static assert expected == actual unique_alias = lambda_fixture('unique_static') def it_processes_toplevel_aliased_lambda_fixture(unique_alias): expected = 'unique' actual = unique_alias assert expected == actual a = static_fixture('a') b = static_fixture('b')
from django.contrib.auth.models import User from pytest_lambda import lambda_fixture from pytest_drf import APIViewTest, AsUser, UsesGetMethod from pytest_drf.util import url_for alice = lambda_fixture(lambda: User.objects.create( username='******', first_name='Alice', last_name='Innchains', email='*****@*****.**', )) bob = lambda_fixture(lambda: User.objects.create( username='******', first_name='Bob', last_name='Loblaw', email='*****@*****.**', )) class DescribeUserInfo( APIViewTest, UsesGetMethod, ): # NOTE: this view returns the username, first_name, last_name, and email of # the authenticated user. url = lambda_fixture(lambda: url_for('authentication-user-info'))
class DescribeMovieViewSet(ViewSetTest): list_url = lambda_fixture(lambda: url_for('movies-list')) detail_url = lambda_fixture( lambda movie: url_for('movies-detail', pk=movie.pk)) class DescribeList( UsesGetMethod, UsesListEndpoint, Returns200, ReturnsPageNumberPagination, ): movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title="Alfred Hitchcock's The Byrds: A Biopic", year=1975), Movie(title='Forty-Two Monkeys', year=1997), ]), autouse=True, ) def it_returns_movies(self, movies, results): expected = express_movies(movies) actual = results assert expected == actual class ContextSearch( Returns200, ): matching_movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title='Forty-Two Monkeys', year=1997), ])) non_matching_movies = lambda_fixture( lambda: Movie.objects.bulk_create([ Movie(title="Alfred Hitchcock's The Byrds: A Biopic", year=1975), ])) movies = lambda_fixture( lambda matching_movies, non_matching_movies: (matching_movies + non_matching_movies), autouse=True, ) query_params = static_fixture({ 'q': 'monkey', }) def it_returns_only_matching_movies(self, matching_movies, results): expected = express_movies(matching_movies) actual = results assert expected == actual class DescribeRetrieve( UsesGetMethod, UsesDetailEndpoint, Returns200, ): movie = lambda_fixture(lambda: Movie.objects.create( title='The Muffin Man', year=2038, )) def it_returns_movie(self, movie, json): expected = express_movie(movie) actual = json assert expected == actual
class DescribeCollectionValuesChecker: checker_cls = lambda_fixture(params=[ util.List, util.Set, util.Dict, util.Str, ]) collection_type = lambda_fixture( lambda checker_cls: checker_cls._collection_type) create_collection = lambda_fixture(lambda collection_type: partial( create_collection_of_type, collection_type)) class CaseType: checker_cls = lambda_fixture(params=CHECKER_CLASSES + (util.Collection, )) compatible_collection_types = lambda_fixture( lambda checker_cls: set(CHECKER_COLLECTION_TYPES.values()) if checker_cls is util.Collection else {CHECKER_COLLECTION_TYPES[checker_cls]}) incompatible_collection_types = lambda_fixture( lambda compatible_collection_types: set( CHECKER_COLLECTION_TYPES.values( )) - compatible_collection_types) def it_compares_true_to_compatible_instances( self, checker_cls, compatible_collection_types): checker = checker_cls expected = compatible_collection_types actual = { cls for cls in CHECKER_COLLECTION_TYPES.values() if checker == cls() } assert expected == actual def it_doesnt_compare_true_to_incompatible_instances( self, checker_cls, incompatible_collection_types): checker = checker_cls expected = incompatible_collection_types actual = { cls for cls in CHECKER_COLLECTION_TYPES.values() if checker != cls() } assert expected == actual class DescribeEmpty: def it_compares_true_to_empty_collection(self, checker_cls, create_collection): collection = create_collection() assert len(collection) == 0 # sanity check expected = checker_cls.empty() actual = collection assert expected == actual def it_compares_false_to_nonempty_collection(self, checker_cls, create_collection): collection = create_collection('a') assert len(collection) > 0 # sanity check expected = checker_cls.empty() actual = collection assert expected != actual class DescribeNotEmpty: def it_compares_true_to_nonempty_collection(self, checker_cls, create_collection): collection = create_collection('a') assert len(collection) > 0 # sanity check expected = checker_cls.not_empty() actual = collection assert expected == actual def it_compares_false_to_empty_collection(self, checker_cls, create_collection): collection = create_collection() assert len(collection) == 0 # sanity check expected = checker_cls.not_empty() actual = collection assert expected != actual class DescribeContaining: def it_compares_true_to_collection_containing_values( self, checker_cls, create_collection): expected = checker_cls.containing('b', 'c') actual = create_collection(*'abcz') assert expected == actual def it_accepts_values_from_generator(self, checker_cls, create_collection): expected = checker_cls.containing(c for c in 'bc') actual = create_collection(*'abcz') assert expected == actual def it_compares_false_to_collection_not_containing_values( self, checker_cls, create_collection): expected = checker_cls.containing('b', 'c') actual = create_collection(*'acz') assert expected != actual class DescribeNotContaining: def it_compares_true_to_collection_not_containing_values( self, checker_cls, create_collection): expected = checker_cls.not_containing('b', 'd') actual = create_collection(*'acz') assert expected == actual def it_accepts_values_from_generator(self, checker_cls, create_collection): expected = checker_cls.not_containing(c for c in 'bd') actual = create_collection(*'acz') assert expected == actual def it_compares_false_to_collection_containing_values( self, checker_cls, create_collection): expected = checker_cls.not_containing('b', 'c') actual = create_collection(*'abcz') assert expected != actual class DescribeContainingOnly: def it_compares_true_to_collection_containing_only_values( self, checker_cls, create_collection): expected = checker_cls.containing_only('b', 'c') actual = create_collection(*'bc') assert expected == actual def it_accepts_values_from_generator(self, checker_cls, create_collection): expected = checker_cls.containing_only(c for c in 'bc') actual = create_collection(*'bc') assert expected == actual def it_compares_false_to_collection_containing_extra_values( self, checker_cls, create_collection): expected = checker_cls.containing_only('b', 'c') actual = create_collection(*'abcz') assert expected != actual def it_compares_false_to_collection_not_containing_values( self, checker_cls, create_collection): expected = checker_cls.containing_only('b', 'c') actual = create_collection(*'acz') assert expected != actual class DescribeContainingExactly: def it_compares_true_to_collection_containing_exactly_values( self, checker_cls, create_collection): expected = checker_cls.containing_exactly('a', 'b') actual = create_collection(*'ab') assert expected == actual def it_accepts_values_from_generator(self, checker_cls, create_collection): expected = checker_cls.containing_exactly(c for c in 'ab') actual = create_collection(*'ab') assert expected == actual def it_compares_false_to_collection_not_containing_values( self, checker_cls, create_collection): expected = checker_cls.containing_exactly('a', 'b') actual = create_collection(*'ac') assert expected != actual def it_compares_false_to_collection_containing_additional_values( self, checker_cls, create_collection): expected = checker_cls.containing_exactly('a', 'b') actual = create_collection(*'abc') assert expected != actual def it_compares_false_to_collection_containing_dupe_values( self, checker_cls, create_collection, collection_type): if issubclass(collection_type, (dict, set)): pytest.xfail('dict and set do not support dupe values') expected = checker_cls.containing_exactly('a', 'b') actual = create_collection(*'aabb') assert expected != actual def it_compares_false_to_collection_containing_fewer_values( self, checker_cls, create_collection): expected = checker_cls.containing_exactly('a', 'b') actual = create_collection('a') assert expected != actual class DescribeRepr: def it_generates_string_repr_for_all_methods(self, checker_cls): checker = (checker_cls.empty().not_empty().containing( 'uniq1').containing_only('uniq2').not_containing('uniq5')) if issubclass(checker_cls, dict): checker = checker.containing_exactly(uniq3='uniq4') else: checker = checker.containing_exactly('uniq3', 'uniq4') checker_repr = repr(checker) expected = { 'empty', 'not_empty', 'containing', 'containing_only', 'containing_exactly', 'not_containing', 'uniq1', 'uniq2', 'uniq3', 'uniq4', 'uniq5', } actual = {s for s in expected if s in checker_repr} assert actual == expected
from django.contrib.auth.models import User from pytest_lambda import lambda_fixture from pytest_drf import ( APIViewTest, AsUser, ForbidsAnonymousUsers, Returns200, UsesGetMethod, ) from pytest_drf.util import url_for user = lambda_fixture(lambda: User.objects.create( username='******', first_name='Test', last_name='User', email='*****@*****.**', )) class DescribeLoginRequired( APIViewTest, UsesGetMethod, ForbidsAnonymousUsers, AsUser('user'), Returns200, ): # NOTE: this view simply returns 200, but requires an authenticated user # (i.e. it declares IsAuthenticated for its permission_classes) url = lambda_fixture(lambda: url_for('authorization-login-required'))
class UsesListEndpoint: url = lambda_fixture('list_url')