Ejemplo n.º 1
0
class TestImage:
    @pytest.fixture(scope='function', autouse=True)
    def init_image(self):
        self.image = Image(
            Name='foobar',
            URL='baz',
            Sol=42,
            CameraID=1,
            ProductTypeID=1,
            DetatchedLabel=False,
        )

    def test_to_dict(self, image_session):
        image_session.add(self.image)
        image_session.commit()
        im = Image.query.filter().first()
        im_dict = im.to_dict()
        assert im_dict['Name'] == 'foobar'
        assert im_dict['ID'] == 1
        assert im_dict['Active']
        assert im_dict['Sol'] == 42
        assert im_dict['ProductTypeID'] == 1
        assert im_dict['CameraID'] == 1
        assert im_dict['product_type']['Name'] == 'bar'
        assert im_dict['camera']['Name'] == 'foo'
        assert not im_dict['DetatchedLabel']

    def test_from_dict(self):
        im = Image.from_dict({
            'Name': 'foobar',
            'URL': 'baz',
            'Sol': '42',
            'CameraID': 1,
            'ProductTypeID': 1,
            'fake': 'value',
            'DetatchedLabel': True,
        })
        assert im.Name == 'foobar'
        assert im.URL == 'baz'
        assert im.Sol == 42
        assert im.CameraID == 1
        assert im.ProductTypeID == 1
        assert im.DetatchedLabel

    def test_update_from_dict(self):
        self.image.update_from_dict({
            'Name': 'agentp',
            'URL': 'url',
            'spam': 'baz',
            'Sol': 24,
            'CameraID': 2,
            'ProductTypeID': 3,
            'DetatchedLabel': False,
        })
        assert self.image.Name == 'agentp'
        assert self.image.URL == 'url'
        assert self.image.Sol == 24
        assert self.image.CameraID == 2
        assert self.image.ProductTypeID == 3
        assert not self.image.DetatchedLabel

    def test_delete(self, image_session):
        image_session.add(self.image)
        image_session.commit()
        im = Image.query.filter().first()
        assert im.Active
        im.delete()
        assert not im.Active

    def test_name_is_unique(self, image_session):
        image_session.add(self.image)
        image_session.commit()
        im1 = Image.query.first()
        with pytest.raises(IntegrityError):
            new_im = Image.from_dict(im1.to_dict())
            image_session.add(new_im)
            image_session.commit()

    def test_many_to_one_relationships(self, image_session):
        image_session.add(self.image)
        image_session.commit()
        im1 = Image.query.first()
        # Also have the same product type id and camera id
        new_im = Image.from_dict(im1.to_dict())
        new_im.Name = 'agentp'
        image_session.add(new_im)
        image_session.commit()
        ims = Image.query.filter().all()
        assert len(ims) == 2
        im2 = ims[1]
        assert im1.ProductTypeID == 1
        assert im1.CameraID == 1
        assert im2.ProductTypeID == 1
        assert im2.CameraID == 1

        # Check the one to many relationships as well
        pt = ProductType.query.first()
        assert len(pt.images) == 2
        cam = Camera.query.first()
        assert len(cam.images) == 2