def test_assert(self): test_list = namedlist('test_list', ['x', 'y']) test_list_instance = test_list(1, 2) with pytest.raises(KeyError) as e: change_namedlist(test_list_instance, {'z': 3}) assert 'Field with name z is not in list test_list(x=1, y=2)' in str( e.value)
def test_correct_change(self): test_list = namedlist('test_list', ['x', 'y']) test_list_instance = test_list(1, 2) assert test_list_instance.x == 1 assert test_list_instance.y == 2 test_list_instance2 = change_namedlist(test_list_instance, {'x': 3}) assert test_list_instance2.x == 3 assert test_list_instance2.y == 2
class TestOrganizationFormValidity: """ * - Not null fields: """ @pytest.mark.parametrize( ", ".join(fields), [ # correct scenarios minimal, # minimal is full :) # incorrect scenarios # institution_type # wrongo choice change_namedlist( validity_false, { 'title': 'Wrong institution type choice', 'institution_type': '12345' }), # postal_code # wrong postal_code format change_namedlist(validity_false, {'postal_code': '12345'}), # city change_namedlist(validity_false, { 'title': "Too long city name", 'city': 'a' * 201, }), # street_type change_namedlist(validity_false, { 'title': "Too long street_type", 'street_type': 'a' * 51, }), # street_number change_namedlist(validity_false, { 'title': "Too long street_number", 'street_type': 'a' * 201, }), # flat_number change_namedlist(validity_false, { 'title': "Too long flat_number", 'street_type': 'a' * 201, }), # email change_namedlist(validity_false, { 'title': "Too long email", 'email': 'a@' + 'a' * 300 + '.pl', }), change_namedlist(validity_false, { 'title': "Wrong email type", 'email': 'a', }), # regon change_namedlist(validity_false, { 'title': "Wrong regon", 'regon': '123456789', }), # website change_namedlist(validity_false, { 'title': "Wrong website", 'website': '123456789', }), ]) def test_dataset_form_validity( self, slug, title, institution_type, postal_code, city, street, street_number, flat_number, street_type, email, fax, tel, epuap, regon, website, status, validity, ): form = OrganizationForm( data={ 'slug': slug, 'title': title, 'institution_type': institution_type, 'postal_code': postal_code, 'city': city, 'street': street, 'street_number': street_number, 'flat_number': flat_number, 'street_type': street_type, 'email': email, 'fax': fax, 'tel': tel, 'epuap': epuap, 'regon': regon, 'website': website, 'status': status, }) assert form.is_valid() is validity def test_create_and_save(self): data = { 'title': "Organization title", 'slug': "organization-title-1", 'institution_type': 'local', 'postal_code': "00-001", 'city': "Warszawa", 'street': "Królewska", 'street_number': 1, 'flat_number': 1, 'street_type': "ul", 'email': "*****@*****.**", 'fax': "123123123", 'tel': "123123123", 'epuap': "123123123", 'regon': "123456785", 'website': "http://test.pl", 'status': 'draft', } form = OrganizationForm(data=data) assert form.is_valid() form.save() last_ds = Organization.objects.last() assert last_ds.title == data['title'] def test_save_add_users_to_existing_organization(self, valid_organization, editor_user): data = { 'title': "Organization title", 'slug': "organization-title-2", 'institution_type': 'local', 'postal_code': "00-001", 'city': "Warszawa", 'street': "Królewska", 'street_number': 1, 'flat_number': 1, 'street_type': "ul", 'email': "*****@*****.**", 'fax': "123123123", 'tel': "123123123", 'epuap': "123123123", 'regon': "123456785", 'website': "http://test.pl", 'status': 'draft', 'users': [ editor_user.id, ] } assert editor_user not in valid_organization.users.all() form = OrganizationForm(data=data, instance=valid_organization) assert form.instance.pk assert form.is_valid() saved_org = form.save(commit=False) assert editor_user in saved_org.users.all() def test_create_organization_and_add_users(self, editor_user): data = { 'title': "Organization title", 'slug': "organization-title-10", 'institution_type': 'local', 'postal_code': "00-001", 'city': "Warszawa", 'street': "Królewska", 'street_number': 1, 'flat_number': 1, 'street_type': "ul", 'email': "*****@*****.**", 'fax': "123123123", 'tel': "123123123", 'epuap': "123123123", 'regon': "123456785", 'website': "http://test.pl", 'status': 'draft', 'users': [ editor_user.id, ] } form = OrganizationForm(data=data) assert form.is_valid() saved_org = form.save() assert editor_user in saved_org.users.all() def test_add_users_to_unsaved_organization(self, editor_user): data = { 'title': "Organization title", 'slug': "organization-title", 'institution_type': 'local', 'postal_code': "00-001", 'city': "Warszawa", 'street': "Królewska", 'street_number': 1, 'flat_number': 1, 'street_type': "ul", 'email': "*****@*****.**", 'fax': "123123123", 'tel': "123123123", 'epuap': "123123123", 'regon': "123456785", 'website': "http://test.pl", 'status': 'draft', 'users': [ editor_user.id, ] } form = OrganizationForm(data=data) assert form.is_valid() saved_org = form.save(commit=False) with pytest.raises(ValueError): saved_org.users.all()
epuap=None, regon=None, website=None, status=None, validity=False) minimal = change_namedlist( empty, { 'title': "Organization title", 'slug': "organization-title", 'institution_type': 'local', 'postal_code': "00-001", 'city': "Warszawa", 'street': "Królewska", 'street_number': 1, 'flat_number': 1, 'street_type': "ul", 'email': "*****@*****.**", 'fax': "123123123", 'tel': "123123123", 'epuap': "123123123", 'regon': "123456785", 'website': "http://test.pl", 'status': 'draft', 'validity': True }) validity_false = change_namedlist(minimal, {'validity': False}) @pytest.mark.django_db class TestOrganizationFormValidity:
class TestDatasetFormValidity: """ """ @pytest.mark.parametrize( ", ".join(fields), [ # correct scenarios minimal, # full, # # incorect scenarios # title # no title change_namedlist(minimal, {'title': None, 'validity': False}), # too long change_namedlist(minimal, {'title': "T" * 301, 'validity': False}), # name * auto/manual - base on title # no name change_namedlist(minimal, {'title': 'no slug', 'slug': None, 'validity': True}), # too long name change_namedlist(minimal, {'title': 'too long name', 'slug': "T" * 101, 'validity': True}), # category # wrong category change_namedlist(minimal, {'category': 'xxx', 'validity': False}), # status * choices # No status choice change_namedlist(minimal, {'title': 'no status', 'status': None, 'validity': False}), # wrong choice value of status change_namedlist(minimal, {'title': 'wrong status', 'status': "XXX", 'validity': False}), # private # metadata_modified # creator_user_id * auto # url # wrong url format change_namedlist(minimal, {'title': 'wrong app_url format', 'url': "wrong format", 'validity': False}), ]) def test_dataset_form_validity(self, slug, title, url, notes, license_id, organization, customfields, license_condition_db_or_copyrighted, license_condition_modification, license_condition_original, license_condition_responsibilities, license_condition_source, update_frequency, category, status, validity, valid_organization ): form = DatasetForm(data={ "slug": slug, "title": title, "url": url, "notes": notes, "license_id": license_id, "organization": valid_organization.id, "customfields": customfields, "license_condition_db_or_copyrighted": license_condition_db_or_copyrighted, "license_condition_modification": license_condition_modification, "license_condition_original": license_condition_original, "license_condition_responsibilities": license_condition_responsibilities, "license_condition_source": license_condition_source, "update_frequency": update_frequency, "category": category, "status": status, }) assert form.is_valid() is validity if validity and title != "no name": form.save() assert Dataset.objects.last().title == title if title == "no name": with pytest.raises(ValidationError) as e: form.save() assert "'slug'" in str(e.value) def test_dataset_form_add_tags(self, valid_tag, valid_organization): form = DatasetForm(data={ 'title': "Test add tag", 'slug': "test-add-tag", 'status': 'published', 'organization': valid_organization.id, 'notes': "notes", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'tags': [valid_tag] }) assert form.is_valid() form.save() ap = Dataset.objects.last() assert ap.slug == "test-add-tag" assert valid_tag in ap.tags.all() def test_metadata_modified_is_not_null(self, valid_organization): data = { 'title': "test metadata_modified title", 'slug': "Test", 'organization': valid_organization.id, 'url': 'http://cos.tam.pl', 'update_frequency': "weekly", 'license_id': "other-pd", 'status': 'published', 'validity': True } form = DatasetForm(data=data) assert form.is_valid() form.save() last_ds = Dataset.objects.last() assert last_ds.title == data['title'] assert isinstance(last_ds.modified, datetime.datetime) def test_add_valid_category(self, valid_organization, valid_category): form = DatasetForm(data={ 'title': "Test add tag", 'slug': "test-add-tag", 'status': 'published', 'organization': valid_organization.id, 'notes': "notes", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'category': valid_category.id, }) form.is_valid() assert form.is_valid() form.save() d = Dataset.objects.last() assert d.slug == 'test-add-tag' assert d.category == valid_category
license_condition_db_or_copyrighted=None, license_condition_modification=None, license_condition_original=None, license_condition_responsibilities=None, license_condition_source=None, update_frequency=None, category=None, status=None, validity=False, ) minimal = change_namedlist(empty, { 'title': "Dataset title", 'slug': "test", 'url': 'http://cos.tam.pl', 'update_frequency': "weekly", 'organization': None, 'license_id': "other-pd", 'status': 'published', 'validity': True }) @pytest.mark.django_db class TestDatasetFormValidity: """ """ @pytest.mark.parametrize( ", ".join(fields), [ # correct scenarios
class TestApplicationFormValidity: """ * - Not null fields: fields of application: id * auto title * slug * auto/manual - base on title notes * author status * choices creator_user_id * auto url * # date - odchodzimy od tego na rzecz TimeStampedModel image """ @pytest.mark.parametrize( ", ".join(fields), [ # correct scenarios minimal, full, # # incorect scenarios # title # no title change_namedlist(minimal, {'title': None, 'validity': False}), # too long change_namedlist(minimal, {'title': "T" * 301, 'validity': False}), # name * auto/manual - base on title # no name change_namedlist(minimal, {'title': 'no name', 'slug': None, 'validity': True}), # too long name change_namedlist(minimal, {'title': 'too long name', 'slug': "T" * 601, 'validity': False}), # notes * change_namedlist(minimal, {'title': 'no notes', 'notes': None, 'validity': False}), # author # author too long change_namedlist(minimal, {'title': 'to long author', 'author': "a" * 51, 'validity': False}), # status * choices # No status choice change_namedlist(minimal, {'title': 'no status', 'status': None, 'validity': False}), # wrong choice value of status change_namedlist(minimal, {'title': 'wrong status', 'status': "XXX", 'validity': False}), # no app url change_namedlist(minimal, {'title': 'no url', 'url': None, 'validity': False}), # to long app_url change_namedlist(minimal, {'title': 'too long app url', 'url': "http://smth." + "a" * 300 + ".pl", 'validity': False}), # wrong url format change_namedlist(minimal, {'title': 'wrong url format', 'url': "wrong format", 'validity': False}), # date # wrong format # change_namedlist(minimal, {'title': 'wrong date format', 'validity': False}), ]) def test_application_form_validity(self, title, slug, notes, author, status, url, image, validity, ): form = ApplicationForm(data={ "title": title, "slug": slug, "notes": notes, "author": author, "status": status, "url": url, "image": image }) assert form.is_valid() is validity if validity and title != "no name": form.save() assert Application.objects.last().title == title def test_application_form_add_datasets(self, dataset): form = ApplicationForm(data={ 'title': "Test with dataset title", 'slug': "test-with-dataset-title", 'url': "http://test.pl", 'notes': 'tresc', 'status': 'published', 'datasets': [dataset] }) assert form.is_valid() is True form.save() ap = Application.objects.last() assert ap.title == "Test with dataset title" assert dataset in ap.datasets.all() def test_application_form_add_invalid_datasets(self): form = ApplicationForm(data={ 'title': "Test with dataset title", 'slug': "test-with-dataset-title", 'url': "http://test.pl", 'notes': 'tresc', 'status': 'published', 'datasets': 'aaaa' }) assert form.is_valid() is False assert form.errors == {'datasets': ['Podaj listę wartości.']} def test_application_form_add_tags(self, tag, tag_pl): data = { 'title': "Test add tag", 'slug': "test-add-tag", 'url': "http://test.pl", 'notes': 'tresc', 'status': 'published', 'tags_pl': [tag_pl.id], } form = ApplicationForm(data=data) assert form.is_valid() is True form.save() assert not form.errors ap = Application.objects.last() assert ap.slug == "test-add-tag" assert tag_pl in ap.tags.all()
class TestDatasetFormValidity: """ """ @pytest.mark.parametrize( ", ".join(fields), [ # correct scenarios minimal, # full, # # incorect scenarios # title # no title change_namedlist(minimal, {'title': None, 'validity': False}), # too long change_namedlist(minimal, {'title': "T" * 301, 'validity': False}), # name * auto/manual - base on title # no name change_namedlist(minimal, {'title': 'no slug', 'slug': None, 'validity': True}), # # too long name # change_namedlist(minimal, {'title': 'too long name', 'slug': "T" * 601, 'validity': True}), # category # wrong category change_namedlist(minimal, {'legacy_category': 'xxx', 'validity': False}), # status * choices # No status choice change_namedlist(minimal, {'title': 'no status', 'status': None, 'validity': False}), # wrong choice value of status change_namedlist(minimal, {'title': 'wrong status', 'status': "XXX", 'validity': False}), # private # metadata_modified # creator_user_id * auto # url # wrong url format change_namedlist(minimal, {'title': 'wrong app_url format', 'url': "wrong format", 'validity': False}), ]) def test_dataset_form_validity(self, slug, title, url, notes, license_id, organization, customfields, license_condition_db_or_copyrighted, license_condition_personal_data, license_condition_modification, license_condition_original, license_condition_responsibilities, license_condition_source, update_frequency, legacy_category, categories, status, validity, institution, tag, tag_pl ): data = { "slug": slug, "title": title, "url": url, "notes": notes, "license_id": license_id, "organization": institution.id, "customfields": customfields, "license_condition_db_or_copyrighted": license_condition_db_or_copyrighted, "license_condition_personal_data": license_condition_personal_data, "license_condition_modification": license_condition_modification, "license_condition_original": license_condition_original, "license_condition_responsibilities": license_condition_responsibilities, "license_condition_source": license_condition_source, "update_frequency": update_frequency, "category": legacy_category, "categories": [x.id for x in categories], "status": status, "tags_pl": [tag_pl.id], } if is_enabled('S41_resource_has_high_value_data.be'): data['has_high_value_data'] = False form = DatasetForm(data=data) assert form.is_valid() is validity if validity and title != "no name": form.save() assert Dataset.objects.last().title == title def test_dataset_form_add_tags(self, tag, tag_pl, tag_en, institution, categories): data = { 'title': "Test add tag", 'slug': "test-add-tag", 'status': 'published', 'organization': institution.id, 'notes': "more than 20 characters", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'tags_pl': [tag_pl], 'tags_en': [tag_en], 'categories': [x.id for x in categories], } if is_enabled('S41_resource_has_high_value_data.be'): data['has_high_value_data'] = False form = DatasetForm(data=data) assert form.is_valid() form.save() dataset = Dataset.objects.last() assert dataset.slug == "test-add-tag" assert tag_pl in dataset.tags.all() assert tag_en in dataset.tags.all() def test_dataset_form_add_categories(self, categories, tag, tag_pl, institution): data = { 'title': "Test add categories", 'slug': "test-add-categories", 'status': 'published', 'organization': institution.id, 'notes': "more than 20 characters", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'categories': [category.id for category in categories], 'tags_pl': [tag_pl], } if is_enabled('S41_resource_has_high_value_data.be'): data['has_high_value_data'] = False form = DatasetForm(data=data) assert form.is_valid() form.save() dataset = Dataset.objects.last() assert all(category in dataset.categories.all() for category in categories) def test_add_category(self, institution, legacy_category, tag, tag_pl, categories): data = { 'title': "Test add tag", 'slug': "test-add-tag", 'status': 'published', 'organization': institution.id, 'notes': "more than 20 characters", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'category': legacy_category.id, 'categories': [x.id for x in categories], 'tags_pl': [tag_pl.id], } if is_enabled('S41_resource_has_high_value_data.be'): data['has_high_value_data'] = False form = DatasetForm(data=data) form.is_valid() assert form.is_valid() form.save() d = Dataset.objects.last() assert d.slug == 'test-add-tag' assert d.category == legacy_category def test_is_valid_upload_image(self, institution, tag, tag_pl, small_image, categories): data = { 'title': "Test add tag", 'slug': "test-add-tag", 'status': 'published', 'organization': institution.id, 'notes': "more than 20 characters", "update_frequency": "weekly", 'url': "http://cos.tam.pl", 'tags_pl': [tag_pl.id], 'categories': [x.id for x in categories], } if is_enabled('S41_resource_has_high_value_data.be'): data['has_high_value_data'] = False form = DatasetForm(data=data, files={'image': small_image}) assert form.is_valid()