def test_base_form_catches_is_literate_and_witness(self): subject_consent = mommy.prepare_recipe( "edc_consent.subjectconsent", consent_datetime=self.study_open_datetime, dob=self.dob, is_literate=NO, witness_name="", first_name="ERIK", last_name="THEPLEEB", ) subject_consent.initials = (subject_consent.first_name[0] + subject_consent.last_name[0]) form = SubjectConsentForm(subject_consent.__dict__) self.assertFalse(form.is_valid()) subject_consent = mommy.prepare_recipe( "edc_consent.subjectconsent", consent_datetime=self.study_open_datetime, dob=self.dob, is_literate=NO, witness_name="BOND, JAMES", first_name="ERIK", last_name="THEPLEEB", ) subject_consent.initials = (subject_consent.first_name[0] + subject_consent.last_name[0]) form = SubjectConsentForm(subject_consent.__dict__) self.assertTrue(form.is_valid())
def test_blog_tag_filter(rf): """ Alice is a user that wants to read our blog. She opens the /blog/ webpage and choose a particular tag filter. She expects to find only a subset of posts, according to the chosen tag. - the Blog page contains three posts - two posts shares one tag while the other one has a different tag - Alice navigates to /blog/ page choosing the common tag - Alice expects to see two posts """ # create a blog page with three posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') posts = mommy.prepare_recipe('tests.recipes.post', _quantity=3) link_page(root_page, blog) link_page(blog, posts) # append the proper tags tag_1 = mommy.make('blog.PostTag', content_object=posts[0]).tag tag_2 = mommy.make('blog.PostTag', content_object=posts[1]).tag # the third post is tagged with tag_1 mommy.make('blog.PostTag', tag=tag_1, content_object=posts[2]) # create a fake request with a tag attribute request = add_site_to_request(rf.get(blog.slug, {'tag': tag_1.name})) context = blog.get_context(request) assert len(context['articles']) == 2 assert context['current_tag'] == tag_1.name
def test_blog_exposes_articles_tags_are_order(): """ The Blog model can retrieve a list of all available tags. The list of tags is ordered according to the usage so that more used tags are listed first. - the Blog page is a child of the root page - the Blog page contains two posts - post_1 has two tags, post_2 has one tags - a tag is used twice while the other only once - blog.tags must return two tags; the first one should be the most common """ # create a blog page with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') posts = mommy.prepare_recipe('tests.recipes.post', _quantity=2) link_page(root_page, blog) link_page(blog, posts) # append two tags to two different posts tag_1 = mommy.make('blog.PostTag', content_object=posts[0]).tag tag_2 = mommy.make('blog.PostTag', content_object=posts[1]).tag # one tag is used twice mommy.make('blog.PostTag', tag=tag_2, content_object=posts[0]) assert len(blog.tags) == 2 assert blog.tags[0] == tag_2 assert blog.tags[1] == tag_1
def test_base_form_catches_gender_of_consent(self): site_consents.registry = {} self.consent_factory( start=self.study_open_datetime, end=self.study_open_datetime + timedelta(days=50), version='1.0', gender=[MALE]) subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime, dob=self.dob, gender=MALE) form = SubjectConsentForm(subject_consent.__dict__) subject_consent.initials = subject_consent.first_name[ 0] + subject_consent.last_name[0] self.assertTrue(form.is_valid()) subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime, dob=self.dob, gender=FEMALE) form = SubjectConsentForm(subject_consent.__dict__) subject_consent.initials = subject_consent.first_name[ 0] + subject_consent.last_name[0] self.assertFalse(form.is_valid())
def test_save_related_instances_on_prepare_recipe(self): dog = mommy.prepare_recipe('test.generic.homeless_dog') self.assertIsNone(dog.id) self.assertIsNone(dog.owner.id) dog = mommy.prepare_recipe('test.generic.homeless_dog', _save_related=True) self.assertIsNone(dog.id) self.assertTrue(dog.owner.id)
def test_save_related_instances_on_prepare_recipe(self): dog = mommy.prepare_recipe("test.generic.homeless_dog") self.assertIsNone(dog.id) self.assertIsNone(dog.owner.id) dog = mommy.prepare_recipe("test.generic.homeless_dog", _save_related=True) self.assertIsNone(dog.id) self.assertTrue(dog.owner.id)
def go_to_subject_dashboard(self): self.login() url = reverse( settings.DASHBOARD_URL_NAMES.get('screening_listboard_url')) self.selenium.get('%s%s' % (self.live_server_url, url)) self.selenium.implicitly_wait(3) self.selenium.find_element_by_id('subjectscreening_add').click() self.selenium.implicitly_wait(10) # add a subject screening form obj = mommy.prepare_recipe(self.subject_screening_model) model_obj = self.fill_form( model=self.subject_screening_model, obj=obj, exclude=['subject_identifier', 'report_datetime']) # add a subject consent for the newly screening subject self.selenium.find_element_by_id( f'subjectconsent_add_{model_obj.screening_identifier}').click() obj = mommy.prepare_recipe( self.subject_consent_model, **{ 'screening_identifier': model_obj.screening_identifier, 'dob': model_obj.estimated_dob, 'gender': model_obj.gender }) obj.initials = f'{obj.first_name[0]}{obj.last_name[0]}' model_obj = self.fill_form(model=self.subject_consent_model, obj=obj, exclude=[ 'subject_identifier', 'citizen', 'legal_marriage', 'marriage_certificate', 'subject_type', 'gender', 'study_site' ], verbose=True) # set appointment in progress subject_identifier = model_obj.subject_identifier appointment = Appointment.objects.filter( subject_identifier=subject_identifier).order_by('timepoint')[0] self.selenium.find_element_by_id( f'start_btn_{appointment.visit_code}_' f'{appointment.visit_code_sequence}').click() model_obj = self.fill_form(model=self.appointment_model, obj=appointment, values={ 'appt_status': IN_PROGRESS_APPT, 'appt_reason': SCHEDULED_APPT }, exclude=[ 'subject_identifier', 'timepoint_datetime', 'timepoint_status', 'facility_name' ], verbose=True) return subject_identifier
def test_save_related_instances_on_prepare_recipe(self): dog = mommy.prepare_recipe('tests.generic.homeless_dog') assert not dog.id assert not dog.owner.id dog = mommy.prepare_recipe('tests.generic.homeless_dog', _save_related=True) assert not dog.id assert dog.owner.id
def test_do_not_create_related_model(self): """ It should not attempt to create other object when passing the object as argument """ person = mommy.make_recipe('test.generic.person') self.assertEqual(Person.objects.count(), 1) mommy.make_recipe('test.generic.dog', owner=person) self.assertEqual(Person.objects.count(), 1) mommy.prepare_recipe('test.generic.dog', owner=person) self.assertEqual(Person.objects.count(), 1)
def test_gets_most_recent_assignment_date(self): assignment1 = mommy.prepare_recipe('devices.assignment_recipe') assignment2 = mommy.prepare_recipe('devices.assignment_recipe') old_date = datetime.date.today() - datetime.timedelta(days=14) assignment1.save() assignment2.save() self.device.save() mommy.make('DeviceAssignment', device=self.device, assignment=assignment1, assignment_date=old_date) mommy.make('DeviceAssignment', device=self.device, assignment=assignment2) result = self.device.get_last_assignment_date() assert_equal(result, assignment2.assignment_date())
def _assert_text_field_has_maximum_length_of_characters(self, mommy_recipe, field, max_length): """ Helper method for asserting a field has a maximum length of characters """ test_object1 = mommy.prepare_recipe(mommy_recipe, **{field: 'a'*(max_length+1)}) exclude_fields = test_object1._meta.get_all_field_names() exclude_fields.remove(field) test_object1.full_clean.when.called_with(exclude=exclude_fields).should\ .throw(ValidationError, "{'%s': [u'%s must be %s characters or less']}" % (field, field, max_length)) test_object2 = mommy.prepare_recipe(mommy_recipe, **{field: 'a'*max_length}) test_object2.full_clean.when.called_with(exclude=exclude_fields).should_not.throw(Exception)
def test_model_with_foreign_key(self): dog = mommy.make_recipe('test.generic.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None) dog = mommy.prepare_recipe('test.generic.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertIsNone(dog.owner.id) dog = mommy.prepare_recipe('test.generic.dog', _save_related=True) self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertIsNotNone(dog.owner.id)
def test_lcavailed_post_save_without_lc_number_validation(self): """Test saving LcAvailed with lc_number validation switched off.""" lc = mommy.prepare_recipe('lcavail.lcavailed') # calling save() on the LcAvailed instance will raise ValidationError # because the lc_number will not return True when we call # adhocmodels.models.ValidTransactionRef.is_valid_trxn_ref( # lc_number) with nt.assert_raises(ValidationError): lc.save() # we assert that no LcAvailed object has been created because the # previous call raised ValidationError nt.eq_(LcAvailed.objects.count(), 0) # we set the dont_validate property for lc_number lc.dont_validate = { 'lc_number': True, } # calling save( now will not raise ValidationError) lc.save() # we check that an LcAvailed object has indeed been created nt.eq_(LcAvailed.objects.count(), 1)
def test_blog_articles(): """ Test the Blog model so that users may retrieve all published articles. - the Blog page is a child of the root page - the Blog page contains two posts - blog.articles must return two (live) posts """ # create a blog post with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') posts = mommy.prepare_recipe('tests.recipes.post', _quantity=2) link_page(root_page, blog) link_page(blog, posts) assert len(blog.articles) == 2
def test_blog_exposes_empty_tags_as_default(): """ The Blog model can retrieve a list of all available tags. By default, a Post doesn't have a Tag. - the Blog page is a child of the root page - the Blog page contains a post - blog.tags must return an empty list """ # create a blog post with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') post = mommy.prepare_recipe('tests.recipes.post') link_page(root_page, blog) link_page(blog, post) assert len(blog.tags) == 0
def setup(self): self.project = models.Project(name='Selene') self.device = mommy.prepare_recipe('devices.non_asset_device_recipe') self.device.sequence = None self.device.code = None self.device.save() self.project.save()
def test_can_get_last_assignment_date(self): assignment = mommy.prepare_recipe('devices.assignment_recipe') assignment.save() self.device.save() mommy.make('DeviceAssignment', device=self.device, assignment=assignment) result = self.device.get_last_assignment_date() assert_equal(result, assignment.assignment_date())
def test_increment_after_override_definition_field(self): person = mommy.make_recipe('test.generic.serial_person', name='tom') self.assertEqual(person.name, 'tom') person = mommy.make_recipe('test.generic.serial_person') self.assertEqual(person.name, 'joe1') person = mommy.prepare_recipe('test.generic.serial_person') self.assertEqual(person.name, 'joe2')
def test_increment_after_override_definition_field(self): person = mommy.make_recipe("test.generic.serial_person", name="tom") self.assertEqual(person.name, "tom") person = mommy.make_recipe("test.generic.serial_person") self.assertEqual(person.name, "joe1") person = mommy.prepare_recipe("test.generic.serial_person") self.assertEqual(person.name, "joe2")
def test_increment_for_strings(self): person = mommy.make_recipe('test.generic.serial_person') self.assertEqual(person.name, 'joe1') person = mommy.prepare_recipe('test.generic.serial_person') self.assertEqual(person.name, 'joe2') person = mommy.make_recipe('test.generic.serial_person') self.assertEqual(person.name, 'joe3')
def test_increment_after_override_definition_field(self): person = mommy.make_recipe('tests.generic.serial_person', name='tom') assert person.name == 'tom' person = mommy.make_recipe('tests.generic.serial_person') assert person.name == 'joe4' person = mommy.prepare_recipe('tests.generic.serial_person') assert person.name == 'joe5'
def test_do_query_lookup_for_recipes_prepare_method(self): """ It should not attempt to create other object when using query lookup syntax """ dog = mommy.prepare_recipe('test.generic.dog', owner__name='James') self.assertEqual(dog.owner.name, 'James')
def test_increment_for_strings(self): person = mommy.make_recipe("test.generic.serial_person") self.assertEqual(person.name, "joe1") person = mommy.prepare_recipe("test.generic.serial_person") self.assertEqual(person.name, "joe2") person = mommy.make_recipe("test.generic.serial_person") self.assertEqual(person.name, "joe3")
def test_increment_for_strings(self): person = mommy.make_recipe('tests.generic.serial_person') assert person.name == 'joe1' person = mommy.prepare_recipe('tests.generic.serial_person') assert person.name == 'joe2' person = mommy.make_recipe('tests.generic.serial_person') assert person.name == 'joe3'
def test_do_query_lookup_for_recipes_prepare_method(self): """ It should not attempt to create other object when using query lookup syntax """ dog = mommy.prepare_recipe("test.generic.dog", owner__name="James") self.assertEqual(Person.objects.count(), 1) self.assertEqual(dog.owner.name, "James")
def test_should_not_set_life_start_date_on_device_for_non_laptop(self): device_type = models.DeviceType.objects.get_or_create(code='M', name='Mouse')[0] device_type.save() device = mommy.prepare_recipe('devices.non_asset_device_recipe') device.device_type = device_type device.save() self.get_response(self.build_request(devices=[device.id])) device = models.DeviceAssignment.objects.filter(device__device_type__name='Mouse')[0].device assert_is_none(device.life_start_date)
def test_base_form_catches_consent_datetime_before_study_open(self): subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime + relativedelta(days=1), dob=self.dob) subject_consent.initials = subject_consent.first_name[ 0] + subject_consent.last_name[0] consent_form = SubjectConsentForm(data=subject_consent.__dict__) self.assertTrue(consent_form.is_valid()) self.assertIsNone(consent_form.errors.get('consent_datetime')) subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime - relativedelta(days=1), dob=self.dob) data = subject_consent.__dict__ data['initials'] = data['first_name'][0] + data['last_name'][0] consent_form = SubjectConsentForm(data=data) self.assertFalse(consent_form.is_valid())
def test_should_return_assignment_date_for_device_without_lifetime(self): assignment = mommy.prepare_recipe('devices.assignment_recipe') assignment.save() self.device.device_type.life_time = None self.device.assign() self.device.save() mommy.make('DeviceAssignment', device=self.device, assignment=assignment) result = self.device.life_start_date_or_assignment_date() assert_equal(result, assignment.assignment_date())
def test_base_form_catches_dob_upper(self): subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime, dob=self.dob - relativedelta(years=100)) subject_consent.initials = subject_consent.first_name[ 0] + subject_consent.last_name[0] consent_form = SubjectConsentForm(subject_consent.__dict__) self.assertFalse(consent_form.is_valid())
def test_model_with_foreign_key(self): dog = mommy.make_recipe('test.generic.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None) dog = mommy.prepare_recipe('test.generic.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None)
def test_model_with_foreign_key_as_unicode(self): dog = mommy.make_recipe('test.generic.other_dog_unicode') self.assertEqual(dog.breed, 'Basset') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None) dog = mommy.prepare_recipe('test.generic.other_dog_unicode') self.assertEqual(dog.breed, 'Basset') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None)
def test_model_with_foreign_key_as_unicode(self): dog = mommy.make_recipe('tests.generic.other_dog_unicode') assert dog.breed == 'Basset' assert isinstance(dog.owner, Person) assert dog.owner.id dog = mommy.prepare_recipe('tests.generic.other_dog_unicode') assert dog.breed == 'Basset' assert isinstance(dog.owner, Person) assert dog.owner.id is None
def test_base_form_identity_mismatch(self): subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', consent_datetime=self.study_open_datetime, dob=self.dob) subject_consent.confirm_identity = '1' subject_consent.initials = subject_consent.first_name[ 0] + subject_consent.last_name[0] consent_form = SubjectConsentForm(data=subject_consent.__dict__) self.assertFalse(consent_form.is_valid())
def test_prepare_recipe_with_quantity_parameter_respection_model_args( self): people = mommy.prepare_recipe('tests.generic.person', _quantity=3, name='Dennis Ritchie', age=70) assert len(people) == 3 for person in people: assert person.name == 'Dennis Ritchie' assert person.age == 70
def test_model_with_foreign_key(self): dog = mommy.make_recipe('model_mommy.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None) dog = mommy.prepare_recipe('model_mommy.dog') self.assertEqual(dog.breed, 'Pug') self.assertIsInstance(dog.owner, Person) self.assertNotEqual(dog.owner.id, None)
def test_prepare_recipe_with_quantity_parameter_respection_model_args( self): people = mommy.prepare_recipe('test.generic.person', _quantity=3, name='Dennis Ritchie', age=70) self.assertEqual(len(people), 3) for person in people: self.assertEqual(person.name, 'Dennis Ritchie') self.assertEqual(person.age, 70)
def test_should_set_device_status_to_assigned(self): device = mommy.prepare_recipe('devices.non_asset_device_recipe') uid = 'Hack to id the record: we should tear down!' device.model = uid device.sequence = None device.code = None device.save() self.get_response(self.build_request(devices=[device.id])) assert_equal((models.Device.objects.get(model=uid)).device_status.name, (models.DeviceStatus.objects.get(name=models.DeviceStatus.ASIGNADO)).name)
def test_blog_index_context(rf): """ Alice is a user that wants to read our blog. She opens the /blog/ webpage and expects to find two new posts. - the Blog page contains two posts - Alice navigates to /blog/ page - Alice expects to see two posts """ # create a blog page with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') posts = mommy.prepare_recipe('tests.recipes.post', _quantity=2) link_page(root_page, blog) link_page(blog, posts) # create a fake request request = add_site_to_request(rf.get(blog.slug)) context = blog.get_context(request) assert len(context['articles']) == 2 assert context['current_tag'] is None
def test_action_item(self): action_type = ActionType.objects.get(name=AE_INITIAL_ACTION) for _ in range(0, 5): self.add_consented_subject() subject_identifier = self.consent_model_cls.objects.all( )[0].subject_identifier appointment = Appointment.objects.filter( subject_identifier=subject_identifier).order_by('timepoint')[0] appointment.appt_status = IN_PROGRESS_APPT appointment.appt_reason = SCHEDULED_APPT appointment.save() self.login() url = reverse( settings.DASHBOARD_URL_NAMES.get('subject_listboard_url')) self.selenium.get('%s%s' % (self.live_server_url, url)) self.selenium.find_element_by_id( f'btn-subject-dashboard-{subject_identifier}').click() self.selenium.find_element_by_id('prn-panel-title').click() # add action item action_item = self.add_action_item( subject_identifier=subject_identifier, name=action_type.name) # show action item popover self.selenium.find_element_by_id( f'actionitem-{action_item.action_identifier}').click() self.selenium.implicitly_wait(10) # fill reference model for link on popover self.selenium.find_element_by_id( f'referencemodel-change-{action_item.action_identifier}').click() obj = mommy.prepare_recipe(action_item.reference_model) self.fill_form(model=action_item.reference_model, obj=obj, exclude=[ 'subject_identifier', 'action_identifier', 'tracking_identifier' ]) assert f'actionitem-{action_item.action_identifier}' not in self.selenium.page_source model_cls = django_apps.get_model(self.action_item_model) action_item = model_cls.objects.get( parent_reference_identifier=action_item.action_identifier) # parent_action_item=action_item) assert f'actionitem-{action_item.action_identifier}' in self.selenium.page_source self.selenium.implicitly_wait(10)
def test_blog_articles_only_live(): """ Test the Blog model so that users may retrieve all published articles, while not published articles are hidden to users. - the Blog page is a child of the root page - the Blog page contains two posts - post_1 is published, post_2 is not published - blog.articles must return [post_1] """ # create a blog post with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') link_page(root_page, blog) post_1 = mommy.prepare_recipe('tests.recipes.post') link_page(blog, post_1) post_2 = mommy.prepare_recipe('tests.recipes.post', live=False) link_page(blog, post_2) assert post_1 in blog.articles assert post_2 not in blog.articles
def test_base_form_is_valid(self): """Asserts mommy defaults validate. """ subject_consent = mommy.prepare_recipe( 'edc_consent.subjectconsent', dob=self.dob, consent_datetime=self.study_open_datetime) subject_consent.initials = (subject_consent.first_name[0] + subject_consent.last_name[0]) consent_form = SubjectConsentForm(data=subject_consent.__dict__) self.assertTrue(consent_form.is_valid())
def test_blog_articles_order(): """ Test the Blog model so that users may retrieve all published articles ordered by date (newest first). - the Blog page is a child of the root page - the Blog page contains two posts - post_1 is an old post, post_2 is a new post - blog.articles must return [post_2, post_1] """ # create a blog post with two posts root_page = get_root_page() blog = mommy.prepare_recipe('tests.recipes.blog') link_page(root_page, blog) post_1 = mommy.prepare_recipe('tests.recipes.post', date=datetime(2000, 1, 1)) link_page(blog, post_1) post_2 = mommy.prepare_recipe('tests.recipes.post', date=datetime(2000, 1, 2)) link_page(blog, post_2) assert blog.articles[0] == post_2 assert blog.articles[1] == post_1
def setUp(self): # Linked to geographic units ( county, ward, constituency) that # do not have boundaries; intended to test validation self.test_fac = mommy.make_recipe( 'mfl_gis.tests.facility_recipe', ward=mommy.make_recipe('mfl_gis.tests.ward_recipe')) self.test_ward = self.test_fac.ward self.test_constituency = self.test_ward.constituency self.test_county = self.test_constituency.county self.test_coords = mommy.prepare_recipe( 'mfl_gis.tests.facility_coordinates_recipe', facility=self.test_fac)
def test_model_with_foreign_key(self): dog = mommy.make_recipe('tests.generic.dog') assert dog.breed == 'Pug' assert isinstance(dog.owner, Person) assert dog.owner.id dog = mommy.prepare_recipe('tests.generic.dog') assert dog.breed == 'Pug' assert isinstance(dog.owner, Person) assert dog.owner.id is None dog = mommy.prepare_recipe('tests.generic.dog', _save_related=True) assert dog.breed == 'Pug' assert isinstance(dog.owner, Person) assert dog.owner.id dogs = mommy.make_recipe('tests.generic.dog', _quantity=2) owner = dogs[0].owner for dog in dogs: assert dog.breed == 'Pug' assert dog.owner == owner