def test_write_subregion_simple(self, ks):
        """A simple test that ensures the appropriate keyspace data is written
        out."""
        # Create a simple keyspace and some instances of it
        keyspaces = [ks(x=1, y=1, p=31), ks(x=3, y=7, p=2), ]

        # Add two field instances, one to get the routing key the other to get
        # the mask.
        kf = KeyField(maps={'c': 'c'})
        mf = MaskField(tag='routing')

        # Create the region
        r = KeyspacesRegion(keyspaces, fields=[kf, mf],
                            prepend_num_keyspaces=True)

        # Write out the region, then check that the data corresponds to what
        # we'd expect.
        fp = tempfile.TemporaryFile()
        r.write_subregion_to_file(fp, slice(0, 10), c=5)

        fp.seek(0)
        assert fp.read(4) == b'\x02\x00\x00\x00'  # Number of keyspaces
        assert fp.read() == struct.pack('4I',
                                        keyspaces[0](c=5).get_value(),
                                        keyspaces[0].get_mask(tag='routing'),
                                        keyspaces[1](c=5).get_value(),
                                        keyspaces[1].get_mask(tag='routing'))
Exemple #2
0
    def test_write_subregion_simple(self, ks):
        """A simple test that ensures the appropriate keyspace data is written
        out."""
        # Create a simple keyspace and some instances of it
        keyspaces = [
            (Signal(ks), dict(x=1, y=1, p=31)),
            (Signal(ks(x=3, y=7, p=2)), {}),
        ]

        # Add two field instances, one to get the routing key the other to get
        # the mask.
        kf = KeyField(maps={'c': 'c'})
        mf = MaskField(tag='routing')

        # Create the region
        r = KeyspacesRegion(keyspaces,
                            fields=[kf, mf],
                            prepend_num_keyspaces=True)

        # Write out the region, then check that the data corresponds to what
        # we'd expect.
        fp = tempfile.TemporaryFile()
        r.write_subregion_to_file(fp, slice(0, 10), c=5)

        expected_ks = [s.keyspace(**kw) for s, kw in keyspaces]

        fp.seek(0)
        assert fp.read(4) == b'\x02\x00\x00\x00'  # Number of keyspaces
        assert fp.read() == struct.pack('4I', expected_ks[0](c=5).get_value(),
                                        expected_ks[0].get_mask(tag='routing'),
                                        expected_ks[1](c=5).get_value(),
                                        expected_ks[1].get_mask(tag='routing'))
    def test_write_subregion_calls_fields(self):
        """Check that writing a subregion to file calls the field functions
        with each key and that any extra arguments are passed along.
        """
        # Create some keyspaces
        keys = [(Signal(BitField(32)), {}) for _ in range(10)]

        # Create two fields
        fields = [mock.Mock() for _ in range(2)]
        fields[0].return_value = 0
        fields[1].return_value = 0

        # Create an UNPARTITIONED region and write out a slice, check that
        # field methods were called with EACH key and the kwargs.
        r = KeyspacesRegion(keys, fields)
        fp = tempfile.TemporaryFile()

        kwargs = {"spam": "and eggs", "green_eggs": "and ham"}
        r.write_subregion_to_file(fp, slice(0, 1), **kwargs)

        for f in fields:
            f.assert_has_calls([mock.call(k.keyspace, **kwargs) for
                                k, _ in keys])
            f.reset_mock()

        # Create a PARTITIONED region and write out a slice, check that
        # field methods were called with EACH key IN THE SLICE and the kwargs.
        r = KeyspacesRegion(keys, fields, partitioned_by_atom=True)

        for sl in (slice(0, 1), slice(2, 5)):
            fp = tempfile.TemporaryFile()

            kwargs = {"spam": "spam spam spam", "in_a_box": "with a fox"}
            r.write_subregion_to_file(fp, sl, **kwargs)

            for f in fields:
                f.assert_has_calls([mock.call(k.keyspace, **kwargs) for
                                    k, _ in keys[sl]])
                f.reset_mock()
Exemple #4
0
    def test_write_subregion_calls_fields(self):
        """Check that writing a subregion to file calls the field functions
        with each key and that any extra arguments are passed along.
        """
        # Create some keyspaces
        keys = [(Signal(BitField(32)), {}) for _ in range(10)]

        # Create two fields
        fields = [mock.Mock() for _ in range(2)]
        fields[0].return_value = 0
        fields[1].return_value = 0

        # Create an UNPARTITIONED region and write out a slice, check that
        # field methods were called with EACH key and the kwargs.
        r = KeyspacesRegion(keys, fields)
        fp = tempfile.TemporaryFile()

        kwargs = {"spam": "and eggs", "green_eggs": "and ham"}
        r.write_subregion_to_file(fp, slice(0, 1), **kwargs)

        for f in fields:
            f.assert_has_calls(
                [mock.call(k.keyspace, **kwargs) for k, _ in keys])
            f.reset_mock()

        # Create a PARTITIONED region and write out a slice, check that
        # field methods were called with EACH key IN THE SLICE and the kwargs.
        r = KeyspacesRegion(keys, fields, partitioned_by_atom=True)

        for sl in (slice(0, 1), slice(2, 5)):
            fp = tempfile.TemporaryFile()

            kwargs = {"spam": "spam spam spam", "in_a_box": "with a fox"}
            r.write_subregion_to_file(fp, sl, **kwargs)

            for f in fields:
                f.assert_has_calls(
                    [mock.call(k.keyspace, **kwargs) for k, _ in keys[sl]])
                f.reset_mock()