Exemple #1
0
class ProductsTest(TestBase):
    def setUp(self):
        super().setUp()
        self.products = Products()

    def test_require(self):
        self.products.require('foo')

        self.assertTrue(self.products.isrequired('foo'))
        self.assertFalse(self.products.isrequired('bar'))

        # require should not cross-contaminate require_data
        self.assertFalse(self.products.is_required_data('foo'))
        self.assertFalse(self.products.is_required_data('bar'))

    def test_get(self):
        foo_product_mapping1 = self.products.get('foo')
        foo_product_mapping2 = self.products.get('foo')

        self.assertIsInstance(foo_product_mapping1, Products.ProductMapping)
        self.assertIs(foo_product_mapping1, foo_product_mapping2)

    def test_get_does_not_require(self):
        self.assertFalse(self.products.isrequired('foo'))
        self.products.get('foo')
        self.assertFalse(self.products.isrequired('foo'))
        self.products.require('foo')
        self.assertTrue(self.products.isrequired('foo'))

    def test_require_data(self):
        self.products.require_data('foo')

        self.assertTrue(self.products.is_required_data('foo'))
        self.assertFalse(self.products.is_required_data('bar'))

        # require_data should not cross-contaminate require
        self.assertFalse(self.products.isrequired('foo'))
        self.assertFalse(self.products.isrequired('bar'))

    def test_get_data(self):
        self.assertIsNone(self.products.get_data('foo'))

        data1 = self.products.get_data('foo', dict)
        data2 = self.products.get_data('foo', dict)

        self.assertIsInstance(data1, dict)
        self.assertIs(data1, data2)

    def test_get_data_does_not_require_data(self):
        self.assertFalse(self.products.is_required_data('foo'))
        self.products.get_data('foo')
        self.assertFalse(self.products.is_required_data('foo'))
        self.products.require_data('foo')
        self.assertTrue(self.products.is_required_data('foo'))

    def test_register_data(self):
        data = {}
        self.assertIs(data, self.products.register_data('foo', data))
        with self.assertRaises(ProductError):
            self.products.register_data('foo', data)

    def test_empty_products(self):
        foo_product_mapping = self.products.get('foo')
        self.assertFalse(foo_product_mapping)

    @contextmanager
    def add_products(self, context_products, product_type, target, *products):
        product_mapping = context_products.get(product_type)
        with temporary_dir() as outdir:

            def create_product(product):
                with safe_open(os.path.join(outdir, product), mode='w') as fp:
                    fp.write(product)
                return product

            product_mapping.add(
                target, outdir,
                [create_product(product) for product in products])
            yield temporary_dir

    def test_non_empty_products(self):
        target = self.make_target('c')
        with self.add_products(self.products, 'foo', target, 'a.class'):
            foo_product_mapping = self.products.get('foo')
            self.assertTrue(foo_product_mapping)

    def test_empty_data(self):
        foo_product_mapping = self.products.get_data('foo')
        self.assertFalse(foo_product_mapping)

    @contextmanager
    def add_data(self, context_products, data_type, target, *products):
        make_products = lambda: defaultdict(MultipleRootedProducts)
        data_by_target = context_products.get_data(data_type, make_products)
        with temporary_dir() as outdir:

            def create_product(product):
                abspath = os.path.join(outdir, product)
                with safe_open(abspath, mode='w') as fp:
                    fp.write(product)
                return abspath

            data_by_target[target].add_abs_paths(
                outdir, [create_product(product) for product in products])
            yield temporary_dir

    def test_non_empty_data(self):
        target = self.make_target('c')
        with self.add_data(self.products, 'foo', target, 'a.class'):
            foo_product_mapping = self.products.get_data('foo')
            self.assertTrue(foo_product_mapping)
Exemple #2
0
class ProductsTest(BaseTest):
  def setUp(self):
    super(ProductsTest, self).setUp()
    self.products = Products()

  def test_require(self):
    self.products.require('foo')

    self.assertTrue(self.products.isrequired('foo'))
    self.assertFalse(self.products.isrequired('bar'))

    # require should not cross-contaminate require_data
    self.assertFalse(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

  def test_get(self):
    foo_product_mapping1 = self.products.get('foo')
    foo_product_mapping2 = self.products.get('foo')

    self.assertIsInstance(foo_product_mapping1, Products.ProductMapping)
    self.assertIs(foo_product_mapping1, foo_product_mapping2)

  def test_get_does_not_require(self):
    self.assertFalse(self.products.isrequired('foo'))
    self.products.get('foo')
    self.assertFalse(self.products.isrequired('foo'))
    self.products.require('foo')
    self.assertTrue(self.products.isrequired('foo'))

  def test_require_data(self):
    self.products.require_data('foo')

    self.assertTrue(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

    # require_data should not cross-contaminate require
    self.assertFalse(self.products.isrequired('foo'))
    self.assertFalse(self.products.isrequired('bar'))

  def test_get_data(self):
    self.assertIsNone(self.products.get_data('foo'))

    data1 = self.products.get_data('foo', dict)
    data2 = self.products.get_data('foo', dict)

    self.assertIsInstance(data1, dict)
    self.assertIs(data1, data2)

  def test_get_data_does_not_require_data(self):
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.get_data('foo')
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.require_data('foo')
    self.assertTrue(self.products.is_required_data('foo'))

  def test_empty_products(self):
    foo_product_mapping = self.products.get('foo')
    self.assertFalse(foo_product_mapping)

  def test_non_empty_products(self):
    target = self.make_target('c')
    with self.add_products(self.products, 'foo', target, 'a.class'):
      foo_product_mapping = self.products.get('foo')
      self.assertTrue(foo_product_mapping)

  def test_empty_data(self):
    foo_product_mapping = self.products.get_data('foo')
    self.assertFalse(foo_product_mapping)

  def test_non_empty_data(self):
    target = self.make_target('c')
    with self.add_data(self.products, 'foo', target, 'a.class'):
      foo_product_mapping = self.products.get_data('foo')
      self.assertTrue(foo_product_mapping)
Exemple #3
0
class ProductsTest(BaseTest):
    def setUp(self):
        super(ProductsTest, self).setUp()
        self.products = Products()

    def test_require(self):
        self.products.require('foo')

        self.assertTrue(self.products.isrequired('foo'))
        self.assertFalse(self.products.isrequired('bar'))

        # require should not cross-contaminate require_data
        self.assertFalse(self.products.is_required_data('foo'))
        self.assertFalse(self.products.is_required_data('bar'))

    def test_get(self):
        foo_product_mapping1 = self.products.get('foo')
        foo_product_mapping2 = self.products.get('foo')

        self.assertIsInstance(foo_product_mapping1, Products.ProductMapping)
        self.assertIs(foo_product_mapping1, foo_product_mapping2)

    def test_get_does_not_require(self):
        self.assertFalse(self.products.isrequired('foo'))
        self.products.get('foo')
        self.assertFalse(self.products.isrequired('foo'))
        self.products.require('foo')
        self.assertTrue(self.products.isrequired('foo'))

    def test_require_data(self):
        self.products.require_data('foo')

        self.assertTrue(self.products.is_required_data('foo'))
        self.assertFalse(self.products.is_required_data('bar'))

        # require_data should not cross-contaminate require
        self.assertFalse(self.products.isrequired('foo'))
        self.assertFalse(self.products.isrequired('bar'))

    def test_get_data(self):
        self.assertIsNone(self.products.get_data('foo'))

        data1 = self.products.get_data('foo', dict)
        data2 = self.products.get_data('foo', dict)

        self.assertIsInstance(data1, dict)
        self.assertIs(data1, data2)

    def test_get_data_does_not_require_data(self):
        self.assertFalse(self.products.is_required_data('foo'))
        self.products.get_data('foo')
        self.assertFalse(self.products.is_required_data('foo'))
        self.products.require_data('foo')
        self.assertTrue(self.products.is_required_data('foo'))

    def test_empty_products(self):
        foo_product_mapping = self.products.get('foo')
        self.assertFalse(foo_product_mapping)

    def test_non_empty_products(self):
        target = self.make_target('c')
        with self.add_products(self.products, 'foo', target, 'a.class'):
            foo_product_mapping = self.products.get('foo')
            self.assertTrue(foo_product_mapping)

    def test_empty_data(self):
        foo_product_mapping = self.products.get_data('foo')
        self.assertFalse(foo_product_mapping)

    def test_non_empty_data(self):
        target = self.make_target('c')
        with self.add_data(self.products, 'foo', target, 'a.class'):
            foo_product_mapping = self.products.get_data('foo')
            self.assertTrue(foo_product_mapping)
class ProductsTest(TestBase):
  def setUp(self):
    super(ProductsTest, self).setUp()
    self.products = Products()

  def test_require(self):
    self.products.require('foo')

    self.assertTrue(self.products.isrequired('foo'))
    self.assertFalse(self.products.isrequired('bar'))

    # require should not cross-contaminate require_data
    self.assertFalse(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

  def test_get(self):
    foo_product_mapping1 = self.products.get('foo')
    foo_product_mapping2 = self.products.get('foo')

    self.assertIsInstance(foo_product_mapping1, Products.ProductMapping)
    self.assertIs(foo_product_mapping1, foo_product_mapping2)

  def test_get_does_not_require(self):
    self.assertFalse(self.products.isrequired('foo'))
    self.products.get('foo')
    self.assertFalse(self.products.isrequired('foo'))
    self.products.require('foo')
    self.assertTrue(self.products.isrequired('foo'))

  def test_require_data(self):
    self.products.require_data('foo')

    self.assertTrue(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

    # require_data should not cross-contaminate require
    self.assertFalse(self.products.isrequired('foo'))
    self.assertFalse(self.products.isrequired('bar'))

  def test_get_data(self):
    self.assertIsNone(self.products.get_data('foo'))

    data1 = self.products.get_data('foo', dict)
    data2 = self.products.get_data('foo', dict)

    self.assertIsInstance(data1, dict)
    self.assertIs(data1, data2)

  def test_get_data_does_not_require_data(self):
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.get_data('foo')
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.require_data('foo')
    self.assertTrue(self.products.is_required_data('foo'))

  def test_register_data(self):
    data = {}
    self.assertIs(data, self.products.register_data('foo', data))
    with self.assertRaises(ProductError):
      self.products.register_data('foo', data)

  def test_empty_products(self):
    foo_product_mapping = self.products.get('foo')
    self.assertFalse(foo_product_mapping)

  @contextmanager
  def add_products(self, context_products, product_type, target, *products):
    product_mapping = context_products.get(product_type)
    with temporary_dir() as outdir:
      def create_product(product):
        with safe_open(os.path.join(outdir, product), mode='w') as fp:
          fp.write(product)
        return product
      product_mapping.add(target, outdir, [create_product(product) for product in products])
      yield temporary_dir

  def test_non_empty_products(self):
    target = self.make_target('c')
    with self.add_products(self.products, 'foo', target, 'a.class'):
      foo_product_mapping = self.products.get('foo')
      self.assertTrue(foo_product_mapping)

  def test_empty_data(self):
    foo_product_mapping = self.products.get_data('foo')
    self.assertFalse(foo_product_mapping)

  @contextmanager
  def add_data(self, context_products, data_type, target, *products):
    make_products = lambda: defaultdict(MultipleRootedProducts)
    data_by_target = context_products.get_data(data_type, make_products)
    with temporary_dir() as outdir:
      def create_product(product):
        abspath = os.path.join(outdir, product)
        with safe_open(abspath, mode='w') as fp:
          fp.write(product)
        return abspath
      data_by_target[target].add_abs_paths(outdir, [create_product(product) for product in products])
      yield temporary_dir

  def test_non_empty_data(self):
    target = self.make_target('c')
    with self.add_data(self.products, 'foo', target, 'a.class'):
      foo_product_mapping = self.products.get_data('foo')
      self.assertTrue(foo_product_mapping)
Exemple #5
0
class ProductsTest(unittest.TestCase):
  def setUp(self):
    self.products = Products()

  def test_require(self):
    self.products.require('foo')

    predicate = self.products.isrequired('foo')
    self.assertIsNotNone(predicate)
    self.assertFalse(predicate(42))

    self.assertIsNone(self.products.isrequired('bar'))

    # require should not cross-contaminate require_data
    self.assertFalse(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

  def test_require_predicate(self):
    self.products.require('foo', predicate=lambda x: x == 42)

    predicate = self.products.isrequired('foo')
    self.assertIsNotNone(predicate)
    self.assertTrue(predicate(42))
    self.assertFalse(predicate(0))

  def test_require_multiple_predicates(self):
    self.products.require('foo', predicate=lambda x: x == 1)
    self.products.require('foo', predicate=lambda x: x == 2)
    self.products.require('foo', predicate=lambda x: x == 3)

    predicate = self.products.isrequired('foo')
    self.assertIsNotNone(predicate)
    self.assertFalse(predicate(0))
    self.assertTrue(predicate(1))
    self.assertTrue(predicate(2))
    self.assertTrue(predicate(3))
    self.assertFalse(predicate(4))

  def test_get(self):
    foo_product_mapping1 = self.products.get('foo')
    foo_product_mapping2 = self.products.get('foo')

    self.assertIsInstance(foo_product_mapping1, Products.ProductMapping)
    self.assertIs(foo_product_mapping1, foo_product_mapping2)

  def test_get_does_not_require(self):
    self.assertIsNone(self.products.isrequired('foo'))
    self.products.get('foo')
    self.assertIsNone(self.products.isrequired('foo'))
    self.products.require('foo')
    self.assertIsNotNone(self.products.isrequired('foo'))

  def test_require_data(self):
    self.products.require_data('foo')

    self.assertTrue(self.products.is_required_data('foo'))
    self.assertFalse(self.products.is_required_data('bar'))

    # require_data should not cross-contaminate require
    self.assertIsNone(self.products.isrequired('foo'))
    self.assertIsNone(self.products.isrequired('bar'))

  def test_get_data(self):
    self.assertIsNone(self.products.get_data('foo'))

    data1 = self.products.get_data('foo', dict)
    data2 = self.products.get_data('foo', dict)

    self.assertIsInstance(data1, dict)
    self.assertIs(data1, data2)

  def test_get_data_does_not_require_data(self):
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.get_data('foo')
    self.assertFalse(self.products.is_required_data('foo'))
    self.products.require_data('foo')
    self.assertTrue(self.products.is_required_data('foo'))