Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/filterhits/test/db_tests.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Display friendly message if processing script can't connect to DB. Created March 2, 2015, 11:14 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sitescripts/filterhits/test/common_tests.py ('k') | sitescripts/filterhits/test/geometrical_mean_tests.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/filterhits/test/db_tests.py
diff --git a/sitescripts/filterhits/test/db_tests.py b/sitescripts/filterhits/test/db_tests.py
new file mode 100644
index 0000000000000000000000000000000000000000..3f220579f3beebb08d383b3098c814ae13dc08f6
--- /dev/null
+++ b/sitescripts/filterhits/test/db_tests.py
@@ -0,0 +1,68 @@
+# coding: utf-8
+
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2015 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import unittest, MySQLdb
+from datetime import datetime
+
+from sitescripts.filterhits import db
+
+class DbTestCase(unittest.TestCase):
+ longMessage = True
+ maxDiff = None
+
+ def clear_rows(self):
+ if self.db:
+ db.write(self.db, (("DELETE FROM filters",),))
+
+ def setUp(self):
+ try:
+ db.testing = True
+ self.db = db.connect()
+ except MySQLdb.Error:
+ self.db = None
+ self.clear_rows()
+
+ def tearDown(self):
+ if self.db:
+ self.clear_rows()
+ self.db.close()
+ self.db = None
+
+ def test_query_and_write(self):
+ if not self.db:
+ raise unittest.SkipTest("Not connected to test DB.")
+
+ insert_sql = """INSERT INTO `filters` (filter, sha1)
+ VALUES (%s, UNHEX(SHA1(filter)))"""
+ select_sql = "SELECT filter FROM filters ORDER BY filter ASC"
+
+ # Table should be empty to start with
+ self.assertEqual(db.query(self.db, select_sql), ())
+ # Write some data and query it back
+ db.write(self.db, ((insert_sql, "something"),))
+ self.assertEqual(db.query(self.db, select_sql), ((u"something",),))
+ # Write an array of SQL strings
+ db.write(self.db, ((insert_sql, "a"), (insert_sql, "b"), (insert_sql, "c")))
+ self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), (u"something",)))
+ # Write a sequence of SQL but roll back when a problem arrises
+ with self.assertRaises(MySQLdb.ProgrammingError):
+ db.write(self.db, ((insert_sql, "f"), (insert_sql, "g"), (insert_sql, "h"),
+ ("GFDGks",)))
+ self.assertEqual(db.query(self.db, select_sql), ((u"a",), (u"b",), (u"c",), (u"something",)))
+
+if __name__ == '__main__':
+ unittest.main()
« no previous file with comments | « sitescripts/filterhits/test/common_tests.py ('k') | sitescripts/filterhits/test/geometrical_mean_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld