~emersion/public-inbox

Migrate tests from unittest to pytest v1 REJECTED

Rafael Castillo: 1
 Migrate tests from unittest to pytest

 2 files changed, 55 insertions(+), 68 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/9233/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Migrate tests from unittest to pytest Export this patch

---
 test/test_blocks.py | 43 +++++++++---------------
 test/test_parse.py  | 80 ++++++++++++++++++++++-----------------------
 2 files changed, 55 insertions(+), 68 deletions(-)

diff --git a/test/test_blocks.py b/test/test_blocks.py
index 61960dd..0480d94 100644
--- a/test/test_blocks.py
+++ b/test/test_blocks.py
@@ -1,36 +1,23 @@
from emailthreads import quotes
import unittest
from email.message import EmailMessage
import pytest

class ParseBlocksTestCase(unittest.TestCase):
	def _test(self, text, want):
		msg = EmailMessage()
		msg.set_content(text)

		got = quotes.parse_blocks(msg)
cases = [("a\nb\nc", [quotes.Text((0, 3), "a\nb\nc")]),
         (">a\n>  b\n> c", [quotes.Quote((0, 3), "a\nb\nc")]),
         ("a\n>b\nc\n> d\n> e\nf", [
             quotes.Text((0, 1), "a"),
             quotes.Quote((1, 2), "b"),
             quotes.Text((2, 3), "c"),
             quotes.Quote((3, 5), "d\ne"),
             quotes.Text((5, 6), "f")])]

		self.assertEqual(got, want)

	def test_only_text(self):
		text = "a\nb\nc"
		want = [quotes.Text((0, 3), text)]
		self._test(text, want)
@pytest.mark.parametrize('text,want', cases)
def test_parse_blocks(text, want):
    msg = EmailMessage()
    msg.set_content(text)

	def test_only_quote(self):
		text = ">a\n>  b\n> c"
		want = [quotes.Quote((0, 3), "a\nb\nc")]
		self._test(text, want)
    got = quotes.parse_blocks(msg)

	def test_with_quotes(self):
		text = "a\n>b\nc\n> d\n> e\nf"
		want = [
			quotes.Text((0, 1), "a"),
			quotes.Quote((1, 2), "b"),
			quotes.Text((2, 3), "c"),
			quotes.Quote((3, 5), "d\ne"),
			quotes.Text((5, 6), "f"),
		]
		self._test(text, want)

if __name__ == '__main__':
	unittest.main()
    assert got == want
diff --git a/test/test_parse.py b/test/test_parse.py
index cf7dfbe..b2822c3 100644
--- a/test/test_parse.py
+++ b/test/test_parse.py
@@ -1,55 +1,55 @@
import emailthreads
import os
import unittest
from email.message import EmailMessage
from email import message_from_file
import emailthreads


def normalize(output):
    output = output.strip()
    lines = "\n".split(output)
    lines = [l.strip() for l in lines]
    return "\n".join(lines)


def open_file(name):
    dir_path = os.path.dirname(os.path.realpath(__file__))
    return open(dir_path + "/data/" + name)


class ParseTestCase(unittest.TestCase):
	def _normalize(self, output):
		output = output.strip()
		lines = "\n".split(output)
		lines = [l.strip() for l in lines]
		return "\n".join(lines)
def load_msg_from_file(name):
    f = open_file(name)
    msg = message_from_file(f)
    f.close()
    return msg

	def _open_file(self, name):
		dir_path = os.path.dirname(os.path.realpath(__file__))
		return open(dir_path + "/data/" + name)

	def _load_msg_from_file(self, name):
		f = self._open_file(name)
		msg = message_from_file(f)
		f.close()
		return msg
def read_file(name):
    f = open_file(name)
    contents = f.read().strip()
    f.close()
    return normalize(contents)

	def _read_file(self, name):
		f = self._open_file(name)
		contents = f.read().strip()
		f.close()
		return self._normalize(contents)

	def test_with_scissor(self):
		patch = self._load_msg_from_file("scissor/patch.eml")
		reply = self._load_msg_from_file("scissor/reply.eml")
def test_with_scissor():
    patch = load_msg_from_file("scissor/patch.eml")
    reply = load_msg_from_file("scissor/reply.eml")

		thread = emailthreads.parse([patch, reply])
    thread = emailthreads.parse([patch, reply])

		got = self._normalize(str(thread))
		want = self._read_file("scissor/output.txt")
    got = normalize(str(thread))
    want = read_file("scissor/output.txt")

		self.assertEqual(got, want)
    assert got == want

	def test_with_multiple_replies(self):
		patch = self._load_msg_from_file("multiple-replies/patch.eml")
		reply1 = self._load_msg_from_file("multiple-replies/reply1.eml")
		reply2 = self._load_msg_from_file("multiple-replies/reply2.eml")
		reply3 = self._load_msg_from_file("multiple-replies/reply3.eml")

		thread = emailthreads.parse([patch, reply1, reply2, reply3])
def test_with_multiple_replies():
    patch = load_msg_from_file("multiple-replies/patch.eml")
    reply1 = load_msg_from_file("multiple-replies/reply1.eml")
    reply2 = load_msg_from_file("multiple-replies/reply2.eml")
    reply3 = load_msg_from_file("multiple-replies/reply3.eml")

		got = self._normalize(str(thread))
		want = self._read_file("multiple-replies/output3.txt")
    thread = emailthreads.parse([patch, reply1, reply2, reply3])

		self.assertEqual(got, want)
    got = normalize(str(thread))
    want = read_file("multiple-replies/output3.txt")

if __name__ == '__main__':
	unittest.main()
    assert got == want
-- 
2.24.0
But why?