def test_get_target_path(self):
    tester = self
    test_bundle_spec = 'test_bundle_spec'
    test_uuid = 'test_uuid'
    test_data_hash = 'test_data_hash'
    test_location = 'test_location'
    test_path = 'test_path'
    target = (test_bundle_spec, test_path)

    class MockBundleModel(object):
      def get_bundle(self, uuid):
        tester.assertEqual(uuid, test_uuid)
        return self._bundle
    test_model = MockBundleModel()

    class MockBundleStore(object):
      def get_location(self, data_hash):
        tester.assertEqual(data_hash, test_data_hash)
        return test_location
    bundle_store = MockBundleStore()

    def get_spec_uuid(model, bundle_spec):
      self.assertEqual(model, test_model)
      self.assertEqual(bundle_spec, test_bundle_spec)
      return test_uuid

    with mock.patch('codalab.lib.canonicalize.get_spec_uuid', get_spec_uuid):
      test_model._bundle = type('MockBundle', (object,), {
        'state': State.CREATED,
        'data_hash': None,
      })
      self.assertRaises(UsageError, lambda: canonicalize.get_target_path(
        bundle_store,
        test_model,
        target,
      ))
      test_model._bundle.data_hash = test_data_hash
      result = canonicalize.get_target_path(bundle_store, test_model, target)
      self.assertEqual(result, os.path.join(test_location, test_path))
      # Test for invalid path (file not contained in bundle)
      self.assertRaises(UsageError, lambda: canonicalize.get_target_path(
        bundle_store,
        test_model,
        (test_bundle_spec, os.path.join("..", "..", test_path)),
      ))
    def test_get_target_path(self):
        tester = self
        test_bundle_spec = 'test_bundle_spec'
        test_uuid = 'test_uuid'
        test_data_hash = 'test_data_hash'
        test_location = 'test_location'
        test_path = 'test_path'
        target = (test_uuid, test_path)

        class MockBundleModel(object):
            def get_bundle(self, uuid):
                tester.assertEqual(uuid, test_uuid)
                return self._bundle

        test_model = MockBundleModel()

        class MockBundleStore(object):
            def get_temp_location(self, identifier):
                return os.path.join('temp', identifier)

            def get_location(self, data_hash):
                tester.assertEqual(data_hash, test_data_hash)
                return test_location

        bundle_store = MockBundleStore()

        def get_bundle_uuid(model, bundle_spec):
            self.assertEqual(model, test_model)
            self.assertEqual(bundle_spec, test_bundle_spec)
            return test_uuid

        with mock.patch('codalab.lib.canonicalize.get_bundle_uuid',
                        get_bundle_uuid):
            test_model._bundle = type('MockBundle', (object, ), {
                'state': State.CREATED,
                'data_hash': None,
            })
            test_model._bundle.data_hash = test_data_hash
            result = canonicalize.get_target_path(bundle_store, test_model,
                                                  target)
            self.assertEqual(result, os.path.join(test_location, test_path))
  def test_get_target_path(self):
    tester = self
    test_bundle_spec = 'test_bundle_spec'
    test_uuid = 'test_uuid'
    test_data_hash = 'test_data_hash'
    test_location = 'test_location'
    test_path = 'test_path'
    target = (test_uuid, test_path)

    class MockBundleModel(object):
      def get_bundle(self, uuid):
        tester.assertEqual(uuid, test_uuid)
        return self._bundle
    test_model = MockBundleModel()

    class MockBundleStore(object):
      def get_temp_location(self, identifier):
        return os.path.join('temp', identifier)
      def get_location(self, data_hash):
        tester.assertEqual(data_hash, test_data_hash)
        return test_location
    bundle_store = MockBundleStore()

    def get_bundle_uuid(model, bundle_spec):
      self.assertEqual(model, test_model)
      self.assertEqual(bundle_spec, test_bundle_spec)
      return test_uuid

    with mock.patch('codalab.lib.canonicalize.get_bundle_uuid', get_bundle_uuid):
      test_model._bundle = type('MockBundle', (object,), {
        'state': State.CREATED,
        'data_hash': None,
      })
      test_model._bundle.data_hash = test_data_hash
      result = canonicalize.get_target_path(bundle_store, test_model, target)
      self.assertEqual(result, os.path.join(test_location, test_path))
示例#4
0
 def get_target_path(self, target):
     return canonicalize.get_target_path(self.bundle_store, self.model, target)