def test_identical_when_drivers_unmatched(self): driver1 = CodeResourceRevision() driver2 = CodeResourceRevision() user = User() m1 = Method(revision_name='A', driver=driver1, user=user) for i in range(2): inp = m1.inputs.create(dataset_name='a_in_{}'.format(i), dataset_idx=i + 1) inp.transformationinput = inp for i in range(3): out = m1.outputs.create(dataset_name='a_out_{}'.format(i), dataset_idx=i + 1) out.transformationoutput = out m2 = Method(revision_name='B', driver=driver2, user=user) for i in range(2): inp = m2.inputs.create(dataset_name='b_in_{}'.format(i), dataset_idx=i + 1) inp.transformationinput = inp for i in range(3): out = m2.outputs.create(dataset_name='b_in_{}'.format(i), dataset_idx=i + 1) out.transformationoutput = out self.assertFalse(m1.is_identical(m2))
def setUp(self): super(CodeResourceViewMockTests, self).setUp() patcher = mocked_relations(KiveUser, CodeResource, CodeResourceRevision, User, Group) patcher.start() self.addCleanup(patcher.stop) # noinspection PyUnresolvedReferences patchers = [patch.object(CodeResource._meta, 'default_manager', CodeResource.objects), patch.object(CodeResourceRevision._meta, 'default_manager', CodeResource.objects)] def dummy_save(r): r.id = id(r) # noinspection PyUnresolvedReferences patchers.append(patch.object(CodeResource, 'save', dummy_save)) patcher = PatcherChain(patchers) patcher.start() self.addCleanup(patcher.stop) self.client = self.create_client() self.dev_group = Group(pk=groups.DEVELOPERS_PK) self.everyone = Group(pk=groups.EVERYONE_PK) Group.objects.add(self.dev_group, self.everyone) self.user = kive_user() self.user.groups.add(self.dev_group) self.content_file = ContentFile('some text', 'existing.txt') self.code_resource = CodeResource(pk='99', user=self.user, name='existing', filename='existing.txt') self.code_resource._state.adding = False self.other_user = User(pk=5) self.other_code_resource = CodeResource(pk='150', user=self.other_user) CodeResource.objects.add(self.code_resource, self.other_code_resource) self.code_resource_revision = CodeResourceRevision( pk='199', user=self.user, content_file=self.content_file) self.code_resource_revision.coderesource = self.code_resource self.other_code_resource_revision = CodeResourceRevision( pk='200', user=self.other_user) self.other_code_resource_revision.coderesource = self.other_code_resource self.other_code_resource.revisions.add(self.other_code_resource_revision) CodeResourceRevision.objects.add(self.code_resource_revision, self.other_code_resource_revision) k = KiveUser(pk=users.KIVE_USER_PK) k.groups.add(self.dev_group) KiveUser.objects.add(k)
def test_no_outputs_checkOutputIndices_good(self): """Test output index check, one well-indexed output case.""" driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) foo.check_output_indices() foo.clean()
def setUp(self): patcher = mocked_relations(Method, MethodDependency, Transformation) patcher.start() self.addCleanup(patcher.stop) driver = CodeResourceRevision( coderesource=CodeResource(filename='driver.py')) self.method = Method(driver=driver, family=MethodFamily()) self.dependency = self.add_dependency('helper.py')
def test_method_revise_access_denied(self): """ Hides ungranted code revisions. """ revision1 = CodeResourceRevision(pk=101, revision_name='alpha', revision_number=1, user=self.user) revision2 = CodeResourceRevision(pk=102, revision_name='beta', revision_number=2, user=self.other_user) self.driver.coderesource.revisions.add(revision1, revision2) response = self.client.get(reverse('method_revise', kwargs=dict(pk='199'))) self.assertEqual(200, response.status_code) revisions = response.context['method_form']['driver_revisions'] self.assertEqual([('101', '1: alpha')], revisions.field.widget.choices)
def test_one_valid_output_checkOutputIndices_good(self): """Test output index check, one well-indexed output case.""" driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) out = foo.outputs.create(dataset_idx=1) out.transformationoutput = out foo.check_output_indices() foo.clean()
def test_many_valid_outputs_scrambled_checkOutputIndices_good(self): """Test output index check, well-indexed multi-output (scrambled order) case.""" driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) for i in (3, 1, 2): out = foo.outputs.create(dataset_idx=i) out.transformationoutput = out foo.check_output_indices() foo.clean()
def test_no_inputs_checkInputIndices_good(self): """ Method with no inputs defined should have check_input_indices() return with no exception. """ driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) # check_input_indices() should not raise a ValidationError foo.check_input_indices() foo.clean()
def test_single_valid_input_checkInputIndices_good(self): """ Method with a single, 1-indexed input should have check_input_indices() return with no exception. """ driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) inp = foo.inputs.create(dataset_idx=1) inp.transformationinput = inp # check_input_indices() should not raise a ValidationError foo.check_input_indices() foo.clean()
def test_many_ordered_valid_inputs_checkInputIndices_good(self): """ Test check_input_indices on a method with several inputs, correctly indexed and in order. """ driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) for i in range(3): inp = foo.inputs.create(dataset_idx=i + 1) inp.transformationinput = inp # check_input_indices() should not raise a ValidationError foo.check_input_indices() foo.clean()
def setUp(self): super(MethodViewMockTests, self).setUp() patcher = mocked_relations(KiveUser, MethodFamily, Method, CodeResource, CodeResourceRevision, CompoundDatatype, ContainerFamily, Container, Transformation, TransformationInput, TransformationOutput, User, Group) patcher.start() self.addCleanup(patcher.stop) # noinspection PyUnresolvedReferences patcher = patch.object(MethodFamily._meta, 'default_manager', MethodFamily.objects) patcher.start() self.addCleanup(patcher.stop) self.client = self.create_client() self.dev_group = Group(pk=groups.DEVELOPERS_PK) self.everyone = Group(pk=groups.EVERYONE_PK) Group.objects.add(self.dev_group, self.everyone) self.user = kive_user() self.user.groups.add(self.dev_group) self.other_user = User(pk=5) self.method_family = MethodFamily(pk='99', user=self.user) MethodFamily.objects.add(self.method_family) self.driver = CodeResourceRevision(user=self.user) self.driver.pk = 1337 # needed for viewing a method self.driver.coderesource = CodeResource() self.method = Method(pk='199', user=self.user) self.method.driver = self.driver self.method.family = self.method_family Method.objects.add(self.method) KiveUser.objects.add(KiveUser(pk=users.KIVE_USER_PK))
def test_one_invalid_output_checkOutputIndices_bad(self): """Test output index check, one badly-indexed output case.""" driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) out = foo.outputs.create(dataset_idx=4) out.transformationoutput = out self.assertRaisesRegexp( ValidationError, "Outputs are not consecutively numbered starting from 1", foo.check_output_indices) self.assertRaisesRegexp( ValidationError, "Outputs are not consecutively numbered starting from 1", foo.clean)
def test_many_nonconsective_inputs_scrambled_checkInputIndices_bad(self): """Test input index check, badly-indexed multi-input case.""" driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) for i in (2, 6, 1): inp = foo.inputs.create(dataset_idx=i) inp.transformationinput = inp self.assertRaisesRegexp( ValidationError, "Inputs are not consecutively numbered starting from 1", foo.check_input_indices) self.assertRaisesRegexp( ValidationError, "Inputs are not consecutively numbered starting from 1", foo.clean)
def create(self, validated_data): """ Create a CodeResourceRevision from the validated deserialized data. Note that no cleaning occurs here. That will fall to the calling method. """ crr_data = validated_data users_allowed = crr_data.pop( "users_allowed") if "users_allowed" in crr_data else [] groups_allowed = crr_data.pop( "groups_allowed") if "groups_allowed" in crr_data else [] with transaction.atomic(): crr = CodeResourceRevision(**crr_data) crr.clean() # this sets the MD5 crr.save() crr.users_allowed.add(*users_allowed) crr.groups_allowed.add(*groups_allowed) return crr
def test_one_invalid_input_checkInputIndices_bad(self): """ Test input index check, one badly-indexed input case. """ driver = CodeResourceRevision(coderesource=CodeResource()) foo = Method(driver=driver, family=MethodFamily()) inp = foo.inputs.create(dataset_idx=4) inp.transformationinput = inp # check_input_indices() should raise a ValidationError self.assertRaisesRegexp( ValidationError, "Inputs are not consecutively numbered starting from 1", foo.check_input_indices) self.assertRaisesRegexp( ValidationError, "Inputs are not consecutively numbered starting from 1", foo.clean)
def _make_crv(file_in_memory, creating_user, crv_form, parent_revision=None, code_resource=None): """ Helper that creates a CodeResourceRevision (and a CodeResource as well if appropriate). """ assert isinstance(crv_form, (CodeResourcePrototypeForm, CodeResourceRevisionForm)) # If parent_revision is specified, we are only making a CodeResourceRevision and not its parent CodeResource. assert not (parent_revision is None and isinstance(crv_form, CodeResourceRevision)) cr_filename = "" if file_in_memory is None else file_in_memory.name if code_resource is None and parent_revision is not None: code_resource = parent_revision.coderesource if code_resource is None: # crv_form is a CodeResourcePrototypeForm. code_resource = CodeResource( name=crv_form.cleaned_data['resource_name'], description=crv_form.cleaned_data['resource_desc'], filename=cr_filename, user=creating_user) try: code_resource.full_clean() # Skip the clean until later; after all, we're protected by a transaction here. code_resource.save() except ValidationError as e: crv_form.add_error('content_file', e.error_dict.get('filename', [])) crv_form.add_error('resource_name', e.error_dict.get('name', [])) crv_form.add_error('resource_desc', e.error_dict.get('description', [])) raise e code_resource.grant_from_json(crv_form.cleaned_data["permissions"]) rev_name = "Prototype" rev_desc = crv_form.cleaned_data["resource_desc"] else: rev_name = crv_form.cleaned_data["revision_name"] rev_desc = crv_form.cleaned_data["revision_desc"] # Modify actual filename prior to saving revision object. if file_in_memory is not None: file_in_memory.name += '_' + datetime.now().strftime('%Y%m%d%H%M%S') revision = CodeResourceRevision(revision_parent=parent_revision, revision_name=rev_name, revision_desc=rev_desc, coderesource=code_resource, content_file=file_in_memory, user=creating_user) # This sets the MD5. try: revision.clean() except ValidationError as e: crv_form.add_error(None, e) raise e revision.save() revision.grant_from_json(crv_form.cleaned_data["permissions"]) try: code_resource.full_clean() revision.full_clean() except ValidationError as e: crv_form.add_error(None, e) raise e return revision
def test_identical_self(self): """A Method should be identical to itself.""" m = Method(driver=CodeResourceRevision(), user=User()) self.assertTrue(m.is_identical(m))
def add_dependency(self, filename): helper = CodeResourceRevision( coderesource=CodeResource(filename=filename)) dependency = self.method.dependencies.create(requirement=helper) dependency.method = self.method return dependency