Exemple #1
0
    def test_serialize_config_to_json_file(self):
        """Given a configuration object, serialize that object to
        file as JSON.
        """
        # Set up for expected data.
        format = 'JPEG'
        filename = 'spam.json'
        framerate = None
        imagefile = 'spam.jpeg'
        location = [0, 0, 0]
        mode = 'RGB'
        size = [1, 1280, 720]
        conf = m.Image(**{
            'source': m.Layer(**{
                'source': s.Spot(**{
                    'radius': 128,
                    'ease': 'l',
                }),
                'location': location,
                'filters': [],
                'mask': None,
                'mask_filters': [],
                'blend': op.difference,
                'blend_amount': 1.0,
            }),
            'size': size,
            'filename': imagefile,
            'format': format,
            'mode': mode,
            'framerate': None
        })
        serialized_conf = {
            'Version': __version__,
            'Image': conf.asdict()
        }

        # Expected values.
        exp_json = json.dumps(serialized_conf, indent=4)
        exp_args = (filename, 'w')

        # Set up test data and state.
        open_mock = mock_open()
        with patch('pjinoise.io.open', open_mock, create=True):

            # Run test.
            io.save_conf(conf)

        # Extract actual values.
        act_json = open_mock.return_value.write.call_args[0][0]

        # Determine if test passed.
        self.assertEqual(exp_json, act_json)
        open_mock.assert_called_with(*exp_args)
Exemple #2
0
    def test_load_config_from_json_file(self):
        """Given the path of a configuration serialized as JSON,
        deserialize and return that configuration.
        """
        # Set up for expected data.
        format = 'JPEG'
        filename = 'spam.json'
        framerate = None
        imagefile = 'spam.jpeg'
        location = [0, 0, 0]
        mode = 'RGB'
        size = [1, 1280, 720]

        # Expected data.
        exp_conf = m.Image(**{
            'source': m.Layer(**{
                'source': s.Spot(**{
                    'radius': 128,
                    'ease': 'l',
                }),
                'location': location,
                'filters': [],
                'mask': None,
                'mask_filters': [],
                'blend': op.difference,
                'blend_amount': 1.0,
            }),
            'size': size,
            'filename': imagefile,
            'format': format,
            'mode': mode,
            'framerate': None
        })
        exp_args = (filename, 'r')

        # Set up test data and state.
        conf = {
            'Version': '0.2.0',
            'Image': exp_conf.asdict(),
        }
        text = json.dumps(conf)
        open_mock = mock_open()
        with patch('pjinoise.io.open', open_mock, create=True):
            open_mock.return_value.read.return_value = text

            # Run test.
            act_conf = io.load_conf(filename)

        # Determine if test passed.
        self.assertEqual(exp_conf, act_conf)
        open_mock.assert_called_with(*exp_args)
Exemple #3
0
    def test_load_config_cli_override_filename(self):
        """If a filename was passed to the CLI, override the filename
        in the loaded config with that filename.
        """
        # Expected value.
        exp = 'eggs.tiff'
        exp_format = 'TIFF'

        # Build test data and state.
        filename = 'spam.conf'
        args = _get_cli_args_mock()
        type(args).filename = PropertyMock(return_value=exp)
        type(args).load_config = PropertyMock(return_value=filename)
        image = m.Image(**{
            'source': m.Layer(**{
                'source': s.Spot(**{
                    'radius': 128,
                    'ease': 'l',
                }),
                'location': [0, 0, 0],
                'filters': [],
                'mask': None,
                'mask_filters': [],
                'blend': op.difference,
                'blend_amount': 1.0,
            }),
            'size': [1, 1280, 720],
            'filename': 'spam.jpeg',
            'format': 'JPEG',
            'mode': 'RGB',
            'framerate': None
        })
        conf = json.dumps({
            'Version': __version__,
            'Image': image.asdict()
        })
        open_mock = mock_open()
        with patch('pjinoise.io.open', open_mock, create=True):
            open_mock.return_value.read.return_value = conf

            # Run test.
            result = io.load_conf(filename, args)

        # Extract actual values from result.
        act = result.filename
        act_format = result.format

        # Determine if test passed.
        self.assertEqual(exp, act)
        self.assertEqual(exp_format, act_format)
Exemple #4
0
    def test_load_config_cli_override_location(self):
        """If an image location was passed to the CLI, offset the
        locations in the loaded config with that location.
        """
        # Expected value.
        exp = [10, 10, 10]

        # Build test data and state.
        filename = 'spam.conf'
        location = [4, 5, 6]
        offset = [6, 5, 4]
        args = _get_cli_args_mock()
        type(args).location = PropertyMock(return_value=offset[::-1])
        type(args).load_config = PropertyMock(return_value=filename)
        image = m.Image(**{
            'source': m.Layer(**{
                'source': [
                    m.Layer(**{
                        'source': s.Spot(**{
                            'radius': 128,
                            'ease': 'l',
                        }),
                        'location': location,
                        'filters': [],
                        'mask': None,
                        'mask_filters': [],
                        'blend': op.replace,
                        'blend_amount': 1.0,
                    }),
                    m.Layer(**{
                        'source': s.Spot(**{
                            'radius': 128,
                            'ease': 'l',
                        }),
                        'location': location,
                        'filters': [],
                        'mask': None,
                        'mask_filters': [],
                        'blend': op.difference,
                        'blend_amount': 1.0,
                    }),
                ],
                'location': location,
                'filters': [],
                'mask': None,
                'mask_filters': [],
                'blend': op.replace,
                'blend_amount': 1.0,
            }),
            'size': [1, 1280, 720],
            'filename': 'spam.jpeg',
            'format': 'JPEG',
            'mode': 'RGB',
            'framerate': None
        })
        conf = json.dumps({
            'Version': __version__,
            'Image': image.asdict()
        })
        open_mock = mock_open()
        with patch('pjinoise.io.open', open_mock, create=True):
            open_mock.return_value.read.return_value = conf

            # Run test.
            result = io.load_conf(filename, args)

        # Extract actual values from result.
        def find_location(item):
            result = []
            if 'location' in vars(item):
                result.append(item.location)
            if '_source' in vars(item):
                if isinstance(item._source, Sequence):
                    for obj in item._source:
                        result.extend(find_location(obj))
                else:
                    result.extend(find_location(item._source))
            return result
        acts = find_location(result)
        for act in acts:

            # Determine if test passed.
            self.assertListEqual(exp, act)
Exemple #5
0
    def test_create_single_layer_image(self):
        """Given the proper CLI options, cli.build_config should
        return the config as a model.Image object.
        """
        # Back up initial state.
        argv_bkp = sys.argv
        try:

            # Set up data for expected values.
            format = 'JPEG'
            filename = 'spam.json'
            framerate = None
            imagefile = 'spam.jpeg'
            location = [5, 0, 0]
            mode = 'L'
            size = [1, 1280, 720]

            # Expected values.
            exp = m.Image(
                **{
                    'source':
                    m.Layer(
                        **{
                            'source': s.Spot(**{
                                'radius': 128,
                                'ease': 'l',
                            }),
                            'location': location,
                            'filters': [
                                f.BoxBlur(5),
                                f.Skew(.1),
                            ],
                            'mask': None,
                            'mask_filters': [],
                            'blend': op.difference,
                            'blend_amount': .5,
                        }),
                    'size':
                    size,
                    'filename':
                    imagefile,
                    'format':
                    format,
                    'mode':
                    mode,
                    'framerate':
                    None
                })

            # Set up test data and state.
            sys.argv = [
                'python3.8 -m pjinoise.pjinoise',
                '-s',
                str(size[-1]),
                str(size[-2]),
                str(size[-3]),
                '-n',
                'spot_128:l_0:0:5_boxblur:5+skew:.1_difference:.5',
                '-o',
                imagefile,
                '-m',
                mode,
            ]

            # Run tests.
            args = cli.parse_cli_args()
            act = cli.build_config(args)

            # Determine if test passed.
            try:
                self.assertEqual(exp, act)
            except AssertionError as e:
                exp = exp.asdict()
                act = act.asdict()
                result = map_compare(exp, act)
                if result is not True:
                    msg = f'Path to bad key(s): {result}'
                    raise ValueError(msg)
                else:
                    raise e

        # Restore initial state.
        finally:
            sys.argv = argv_bkp