Skip to content

Instantly share code, notes, and snippets.

@thiagoferreiraw
Created November 25, 2021 19:22
Show Gist options
  • Save thiagoferreiraw/63a71ff083ee3f924a229ebd9118094a to your computer and use it in GitHub Desktop.
Save thiagoferreiraw/63a71ff083ee3f924a229ebd9118094a to your computer and use it in GitHub Desktop.
united tests
import cairosvg
@mock.patch("cairosvg.svg2png", wraps=cairosvg.svg2png)
def test_create_png_from_svg_template(self, spy_svg2png):
with BytesIO() as output_file:
image.create_png_from_svg_template(
"utils/tests/svg_template.svg",
{"_WIDTH": 159, "_HEIGHT": 161, "_TEXT": "Hello World!"},
output_file,
)
self.assertTrue(spy_svg2png.called)
generated_svg = spy_svg2png.call_args[0][0]
self.assertEqual(
generated_svg,
'<svg xmlns="http://www.w3.org/2000/svg" pointer-events="none" '
' width="159" height="161"> <rect width="159" height="161"></rect> '
' <text text-anchor="middle" y="50%" x="50%" dy="0.35em" '
'pointer-events="auto" fill="#ffffff" font-family="{font-family}"> '
"Hello World! </text>"
"</svg>",
)
@patch("cairosvg.svg2png", wraps=cairosvg.svg2png)
def test_create_promotional_image(self, spy_svg2png):
self.assertFalse(self.coupon.marketing_image) # initial state
create_promotional_image(self.coupon.id)
# Checking the image was created
self.coupon.refresh_from_db()
self.assertTrue(self.coupon.marketing_image)
self.assertIn(
f"coupons/{self.coupon.id}/marketing_image/",
self.coupon.marketing_image.url,
)
# Also checking we have the coupon code inside the svg:
generated_svg = spy_svg2png.call_args[0][0]
self.assertIn(self.coupon.code, generated_svg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment