From ad9936e3f527e3bdb6fe02db85091d2e8bec9fe3 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 17 Mar 2012 13:27:42 +0100 Subject: [PATCH] Add python tests --- .gitignore | 1 + library/tests/Makefile.am | 12 ++++- library/tests/test-javascript.js | 14 ------ library/tests/test-lookup-password.py | 61 +++++++++++++++++++++++ library/tests/test-remove-password.py | 71 +++++++++++++++++++++++++++ library/tests/test-store-password.py | 61 +++++++++++++++++++++++ 6 files changed, 205 insertions(+), 15 deletions(-) delete mode 100644 library/tests/test-javascript.js create mode 100644 library/tests/test-lookup-password.py create mode 100644 library/tests/test-remove-password.py create mode 100644 library/tests/test-store-password.py diff --git a/.gitignore b/.gitignore index e9f73af..ce462df 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ .cproject .libs .project +.pydevproject aclocal.m4 autom4te.cache compile diff --git a/library/tests/Makefile.am b/library/tests/Makefile.am index 9490f65..f4ccf4b 100644 --- a/library/tests/Makefile.am +++ b/library/tests/Makefile.am @@ -65,7 +65,12 @@ JS_ENV = \ LD_LIBRARY_PATH=$(builddir)/.libs \ GI_TYPELIB_PATH=$(builddir)/..:$(builddir) -test: test-c test-js +PY_TESTS = \ + test-lookup-password.py \ + test-remove-password.py \ + test-store-password.py + +PY_ENV = $(JS_ENV) test-c: $(TEST_PROGS) @gtester --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS) @@ -73,6 +78,11 @@ test-c: $(TEST_PROGS) test-js: @for js in $(JS_TESTS); do echo "TEST: $$js"; $(JS_ENV) gjs $$js; done +test-py: + @for py in $(PY_TESTS); do echo "TEST: $$py"; $(PY_ENV) python $$py; done + +test: test-c test-py test-js + # ------------------------------------------------------------------ # INTROSPECTION diff --git a/library/tests/test-javascript.js b/library/tests/test-javascript.js deleted file mode 100644 index 95b3c3f..0000000 --- a/library/tests/test-javascript.js +++ /dev/null @@ -1,14 +0,0 @@ - -const Mock = imports.gi.MockService; -const Secret = imports.gi.Secret; - -Mock.start("mock-service-normal.py"); - -var schema = new Secret.Schema.new("org.test", - Secret.SchemaFlags.NONE, - { "blah": Secret.SchemaAttributeType.STRING }); -log(schema.identifier); -log(schema.flags); -/* log(schema.attributes); */ - -Mock.stop(); \ No newline at end of file diff --git a/library/tests/test-lookup-password.py b/library/tests/test-lookup-password.py new file mode 100644 index 0000000..27b6d2c --- /dev/null +++ b/library/tests/test-lookup-password.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +import unittest + +from gi.repository import MockService as Mock +from gi.repository import Secret, GLib + +Mock.start("mock-service-normal.py") + +STORE_SCHEMA = Secret.Schema.new("org.mock.type.Store", + Secret.SchemaFlags.NONE, + { + "number": Secret.SchemaAttributeType.INTEGER, + "string": Secret.SchemaAttributeType.STRING, + "even": Secret.SchemaAttributeType.BOOLEAN, + } +) + +class TestLookup(unittest.TestCase): + def setUp(self): + Mock.start("mock-service-normal.py") + + def tearDown(self): + Mock.stop() + + def testSynchronous(self): + password = Secret.password_lookup_sync (STORE_SCHEMA, { "number": "1", "even": "false" }, None) + self.assertEqual("111", password) + + def testSyncNotFound(self): + password = Secret.password_lookup_sync (STORE_SCHEMA, { "number": "5", "even": "true" }, None) + self.assertEqual(None, password) + + def testAsynchronous(self): + loop = GLib.MainLoop(None, False) + + def on_result_ready(source, result, unused): + loop.quit() + password = Secret.password_lookup_finish(result) + self.assertEquals("222", password) + + Secret.password_lookup (STORE_SCHEMA, { "number": "2", "string": "two" }, + None, on_result_ready, None) + + loop.run() + + def testAsyncNotFound(self): + loop = GLib.MainLoop(None, False) + + def on_result_ready(source, result, unused): + loop.quit() + password = Secret.password_lookup_finish(result) + self.assertEquals(None, password) + + Secret.password_lookup (STORE_SCHEMA, { "number": "7", "string": "five" }, + None, on_result_ready, None) + + loop.run() + +if __name__ == '__main__': + unittest.main() diff --git a/library/tests/test-remove-password.py b/library/tests/test-remove-password.py new file mode 100644 index 0000000..06ac1bb --- /dev/null +++ b/library/tests/test-remove-password.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +import unittest + +from gi.repository import MockService as Mock +from gi.repository import Secret, GLib + +Mock.start("mock-service-normal.py") + +STORE_SCHEMA = Secret.Schema.new("org.mock.type.Store", + Secret.SchemaFlags.NONE, + { + "number": Secret.SchemaAttributeType.INTEGER, + "string": Secret.SchemaAttributeType.STRING, + "even": Secret.SchemaAttributeType.BOOLEAN, + } +) + +class TestRemove(unittest.TestCase): + def setUp(self): + Mock.start("mock-service-normal.py") + + def tearDown(self): + Mock.stop() + + def testSynchronous(self): + attributes = { "number": "1", "string": "one", "even": "false" } + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual("111", password) + + deleted = Secret.password_remove_sync(STORE_SCHEMA, attributes, None) + self.assertEqual(True, deleted) + + def testSyncNotFound(self): + attributes = { "number": "11", "string": "one", "even": "true" } + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual(None, password) + + deleted = Secret.password_remove_sync(STORE_SCHEMA, attributes, None) + self.assertEqual(False, deleted) + + def testAsynchronous(self): + loop = GLib.MainLoop(None, False) + + def on_result_ready(source, result, unused): + loop.quit() + deleted = Secret.password_remove_finish(result) + self.assertEquals(True, deleted) + + Secret.password_remove(STORE_SCHEMA, { "number": "2", "string": "two" }, + None, on_result_ready, None) + + loop.run() + + def testAsyncNotFound(self): + loop = GLib.MainLoop(None, False) + + def on_result_ready(source, result, unused): + loop.quit() + deleted = Secret.password_remove_finish(result) + self.assertEquals(False, deleted) + + Secret.password_remove(STORE_SCHEMA, { "number": "7", "string": "five" }, + None, on_result_ready, None) + + loop.run() + +if __name__ == '__main__': + unittest.main() diff --git a/library/tests/test-store-password.py b/library/tests/test-store-password.py new file mode 100644 index 0000000..93d1f30 --- /dev/null +++ b/library/tests/test-store-password.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +import unittest + +from gi.repository import MockService as Mock +from gi.repository import Secret, GLib + +Mock.start("mock-service-normal.py") + +STORE_SCHEMA = Secret.Schema.new("org.mock.type.Store", + Secret.SchemaFlags.NONE, + { + "number": Secret.SchemaAttributeType.INTEGER, + "string": Secret.SchemaAttributeType.STRING, + "even": Secret.SchemaAttributeType.BOOLEAN, + } +) + +class TestStore(unittest.TestCase): + def setUp(self): + Mock.start("mock-service-normal.py") + + def tearDown(self): + Mock.stop() + + def testSynchronous(self): + attributes = { "number": "9", "string": "nine", "even": "false" } + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual(None, password) + + stored = Secret.password_store_sync(STORE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT, + "The number nine", "999", None) + self.assertEqual(True, stored); + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual("999", password) + + def testAsynchronous(self): + attributes = { "number": "888", "string": "eight", "even": "true" } + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual(None, password); + + loop = GLib.MainLoop(None, False) + + def on_result_ready(source, result, unused): + loop.quit() + stored = Secret.password_store_finish(result) + self.assertEquals(True, stored) + + Secret.password_store(STORE_SCHEMA, attributes, None, "The number eight", "888", + None, on_result_ready, None) + + loop.run() + + password = Secret.password_lookup_sync(STORE_SCHEMA, attributes, None) + self.assertEqual("888", password) + +if __name__ == '__main__': + unittest.main()