summaryrefslogtreecommitdiff
path: root/service/test/unit
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-10-28 12:05:59 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-10-28 12:08:41 +0100
commit70c770635199cfa473608162ec7d31e030a11c5f (patch)
tree5bb5d9ff72a6edabf756256ca30eed0a21e180a4 /service/test/unit
parent41b462e9b29d62dc197be6d8a633c1b9a46688cf (diff)
Add markov-generate to load-mails
- Allows to generat mails based on a sample mails - use it with: pixelated-maintenance markov-generate --seed 21 --limit 10
Diffstat (limited to 'service/test/unit')
-rw-r--r--service/test/unit/fixtures/mbox17
-rw-r--r--service/test/unit/maintenance/test_commands.py24
-rw-r--r--service/test/unit/support/mail_generator_test.py42
-rw-r--r--service/test/unit/support/test_markov.py83
4 files changed, 164 insertions, 2 deletions
diff --git a/service/test/unit/fixtures/mbox b/service/test/unit/fixtures/mbox
new file mode 100644
index 00000000..c3506805
--- /dev/null
+++ b/service/test/unit/fixtures/mbox
@@ -0,0 +1,17 @@
+From guninski@guninski.com Thu Apr 9 23:42:05 2015
+Subject: Scott's Laws with a longer subject
+From: scott@try.pixelated-project.org
+To: alice@try.pixelated-project.org
+X-Pixelated-encryption-status: true
+Date: Tue, 09 Apr 2015 23:42:05 +0000 (UTC)
+
+Scott's First Law:
+ No matter what goes wrong, it will probably look right.
+
+Scott's Second Law:
+ When an error has been detected and corrected, it will be found
+ to have been wrong in the first place.
+Corollary:
+ After the correction has been found in error, it will be
+ impossible to fit the original quantity back into the
+ equation.
diff --git a/service/test/unit/maintenance/test_commands.py b/service/test/unit/maintenance/test_commands.py
index 52fe6ca2..a0fc58d6 100644
--- a/service/test/unit/maintenance/test_commands.py
+++ b/service/test/unit/maintenance/test_commands.py
@@ -80,8 +80,8 @@ class TestCommands(unittest.TestCase):
def test_load_mails_adds_mails(self):
# given
mail_root = pkg_resources.resource_filename('test.unit.fixtures', 'mailset')
- firstMailDeferred = defer.succeed(None)
- secondMailDeferred = defer.succeed(None)
+ firstMailDeferred = defer.succeed(MagicMock())
+ secondMailDeferred = defer.succeed(MagicMock())
self.mail_store.add_mail.side_effect = [firstMailDeferred, secondMailDeferred]
self.mail_store.add_mailbox.return_value = defer.succeed(None)
@@ -104,6 +104,26 @@ class TestCommands(unittest.TestCase):
return d
+ def test_load_mails_supports_mbox(self):
+ # given
+ mbox_file = pkg_resources.resource_filename('test.unit.fixtures', 'mbox')
+
+ d = load_mails(self.args, [mbox_file])
+
+ # then
+ def assert_mails_added(_):
+ self.assertTrue(self.mail_store.add_mail.called)
+ self.mail_store.add_mail.assert_any_call('INBOX', self._mail_content(mbox_file))
+
+ def error_callack(err):
+ print err
+ self.assertTrue(False)
+
+ d.addCallback(assert_mails_added)
+ d.addErrback(error_callack)
+
+ return d
+
def _mail_content(self, mail_file):
with open(mail_file, 'r') as fp:
m = email.message_from_file(fp)
diff --git a/service/test/unit/support/mail_generator_test.py b/service/test/unit/support/mail_generator_test.py
new file mode 100644
index 00000000..9d604378
--- /dev/null
+++ b/service/test/unit/support/mail_generator_test.py
@@ -0,0 +1,42 @@
+#
+# Copyright (c) 2015 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+from mailbox import mbox
+import unittest
+import pkg_resources
+import random
+from mock import patch
+
+from pixelated.support.mail_generator import MailGenerator
+
+
+class MailGeneratorTest(unittest.TestCase):
+
+ def test_generator(self):
+ mail = """Content-Type: text/plain; charset="us-ascii"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Scott\'s Laws with a longer subject Scott\'s Laws\nTo: alice@domain.test\nFrom: bob@domain.test\nDate: Sun, 18 Oct 2015 21:45:13 -0000\nX-Tags: \nX-Leap-Encryption: true\nX-Leap-Signature: valid\n\nFirst Law: No matter what goes wrong, it will probably look right. Scott\'s Second Law: When an error has been detected and corrected, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will probably look right. Scott\'s Second Law: When an error has been found in error, it will be impossible to fit the original quantity back into the \n\n First Law: No matter what goes wrong, it will be impossible to fit the original quantity back into the \n\n Scott\'s Second Law: When an error has been found in error, it will be found to have been wrong in the first place. After the correction has been found in error, it will be impossible to fit the original quantity back into the \n\n Second Law: When an error"""
+ receiver = 'alice'
+ domain_name = 'domain.test'
+ mbox_file = pkg_resources.resource_filename('test.unit.fixtures', 'mbox')
+ mails = mbox(mbox_file)
+ rnd = random.Random(0)
+
+ with patch('pixelated.support.mail_generator.time.time') as time_mock:
+ time_mock.return_value = 1446029232.636018
+
+ gen = MailGenerator(receiver, domain_name, mails, rnd)
+
+ result = gen.generate_mail()
+
+ self.assertEqual(mail, result.as_string())
diff --git a/service/test/unit/support/test_markov.py b/service/test/unit/support/test_markov.py
new file mode 100644
index 00000000..f0b0277d
--- /dev/null
+++ b/service/test/unit/support/test_markov.py
@@ -0,0 +1,83 @@
+#
+# Copyright (c) 2015 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+
+import unittest
+from pixelated.support.markov import MarkovGenerator
+import random
+
+
+SAMPLE_TEXT = 'One two three four'
+
+
+class MarkovGeneratorTest(unittest.TestCase):
+
+ def setUp(self):
+ self.random = random.Random(0)
+
+ def test_starts_with_capital_case_workd(self):
+ gen = MarkovGenerator(['lower Upper smaller Capital'], random=self.random)
+
+ result = gen.generate(1)
+
+ self.assertTrue(result.startswith('Upper'))
+
+ def test_aborts_if_no_upper_letter_word_found(self):
+ gen = MarkovGenerator(['all lower case'], random=self.random)
+
+ self.assertRaises(ValueError, gen.generate, 1)
+
+ def test_generate(self):
+ gen = MarkovGenerator([SAMPLE_TEXT], random=self.random)
+
+ result = gen.generate(3)
+
+ self.assertEqual('One two three', result)
+
+ def test_minimum_three_words(self):
+ self.assertRaises(ValueError, MarkovGenerator([]).generate, 1)
+ self.assertRaises(ValueError, MarkovGenerator, ['1'])
+ self.assertRaises(ValueError, MarkovGenerator, ['1', '2'])
+ self.assertRaises(ValueError, MarkovGenerator, ['1', '2', '3'])
+
+ def test_add_paragraph_on_empty_chain(self):
+ gen = MarkovGenerator([SAMPLE_TEXT], random=self.random, add_paragraph_on_empty_chain=True)
+
+ result = gen.generate(5)
+
+ self.assertEqual('One two three four \n\n One', result)
+
+ def test_multiple_inputs(self):
+ gen = MarkovGenerator([SAMPLE_TEXT, 'Five Six seven eight'], random=self.random)
+
+ result = gen.generate(3)
+
+ self.assertEqual('Five Six seven', result)
+
+ def test_add(self):
+ gen = MarkovGenerator([], random=self.random)
+
+ gen.add(SAMPLE_TEXT)
+ result = gen.generate(3)
+
+ self.assertEqual('One two three', result)
+
+ def test_multiple_word_occurences(self):
+ gen = MarkovGenerator(['One Two One Three One Two One Four'], random=self.random)
+
+ result = gen.generate(2)
+
+ self.assertEqual('Two One', result)