From 637b2c5788657535ab05b6610f3b02d56ab957ec Mon Sep 17 00:00:00 2001 From: nitromaster101 Date: Thu, 9 Jul 2009 01:36:29 +0000 Subject: add upload/revert schema manipulation, more tests, and other changes of the past few days git-svn-id: http://freebase-python.googlecode.com/svn/trunk@172 5914aa95-5b3a-0410-a3b5-7b719e7fe9b2 --- test/__init__.py | 0 test/runtests.py | 67 +++++++++ test/test_all.py | 302 -------------------------------------- test/test_freebase.py | 307 +++++++++++++++++++++++++++++++++++++++ test/test_schema_manipulation.py | 150 +++++++++++++++++++ 5 files changed, 524 insertions(+), 302 deletions(-) create mode 100644 test/__init__.py create mode 100644 test/runtests.py delete mode 100755 test/test_all.py create mode 100755 test/test_freebase.py create mode 100644 test/test_schema_manipulation.py (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/runtests.py b/test/runtests.py new file mode 100644 index 0000000..add2380 --- /dev/null +++ b/test/runtests.py @@ -0,0 +1,67 @@ +import unittest + +import os +import os.path + +import freebase + +def main(): + created = False + passwordfile = "test/.password.txt" + + # setup password stuff + if not os.path.isfile(passwordfile): + created = True + USERNAME, PASSWORD = "", "" + print "RUNTESTSIn order to run the tests, we need to use a valid freebase username and password" + USERNAME = raw_input("Please enter your username: ") + PASSWORD = raw_input("Please enter your password (it'll appear in cleartext): ") + + freebase.login(USERNAME, PASSWORD) + + print "Thanks!" + fh = open(passwordfile, "w") + fh.write(USERNAME + "\n" + PASSWORD) + fh.close() + + # run tests + import test_freebase + import test_schema_manipulation + + # test_freebase.TestFreebase, + testcases = [test_schema_manipulation.TestSchemaManipulation, test_freebase.TestFreebase] + testcases_dumb = [test_freebase.TestFreebase, test_schema_manipulation.TestSchemaManipulation] + + + suites = [unittest.TestLoader().loadTestsFromTestCase(x) + for x in testcases] + + + suites_dumb = [unittest.TestLoader().loadTestsFromTestCase(x) + for x in testcases_dumb] + + print sorted(suites) + print sorted(suites_dumb) + print sorted(suites) == sorted(suites_dumb) + + print + + print "SUITES", suites + + + s1 = unittest.TestLoader().loadTestsFromTestCase(test_freebase.TestFreebase) + s2 = unittest.TestLoader().loadTestsFromTestCase(test_schema_manipulation.TestSchemaManipulation) + + anotherrun = unittest.TestSuite([s1, s2]) + + #run = unittest.TestSuite(suites) + + # delete password stuff + + if created: os.remove(passwordfile) + + return anotherrun + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/test/test_all.py b/test/test_all.py deleted file mode 100755 index cfe83f8..0000000 --- a/test/test_all.py +++ /dev/null @@ -1,302 +0,0 @@ -#!/usr/bin/python -# ======================================================================== -# Copyright (c) 2007, Metaweb Technologies, Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following -# disclaimer in the documentation and/or other materials provided -# with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL METAWEB TECHNOLOGIES BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR -# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE -# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN -# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# ======================================================================== - - -import unittest -import sys, logging -import freebase -import random - -from freebase.api import HTTPMetawebSession, MetawebError - - -USERNAME = 'username' -PASSWORD = 'password' -API_HOST = 'sandbox.freebase.com' -TEST_QUERY = {'id': 'null', 'name': 'Sting'} - -class TestFreebase(unittest.TestCase): - - def test_freebase_dot_login_logout(self): - freebase.login(username=USERNAME, password=PASSWORD) - self.assertNotEqual(freebase.user_info(), None) - self.assertEqual(freebase.loggedin(), True) - freebase.logout() - self.assertRaises(MetawebError, freebase.user_info) - self.assertEqual(freebase.loggedin(), False) - - def test_login_logout(self): - mss = HTTPMetawebSession(API_HOST, username=USERNAME, - password=PASSWORD) - mss.login() - - user_info = mss.user_info() - self.assertNotEqual(None, user_info) - self.assertEqual(user_info.code, "/api/status/ok") - self.assertEqual(mss.loggedin(), True) - - mss.logout() - self.assertRaises(MetawebError, mss.user_info) - self.assertEqual(mss.loggedin(), False) - - - def test_freebase_dot_read(self): - query = {'type':'/music/artist','guid':[{}],'name':'Sting', 'album':[{}]} - - result = freebase.mqlread(query) - - self.assertNotEqual(None, result) - self.assert_(result.has_key('guid')) - self.assert_(result.has_key('type')) - self.assert_(result.has_key('name')) - self.assert_(result.has_key('album')) - self.assertEqual(type([]), type(result['album'])) - self.assert_(len(result['album']) > 0) - self.assertEqual( 'Sting', result['name']) - self.assertEqual('#9202a8c04000641f8000000000092a01', result['guid'][0]['value']) - - def test_freebase_dot_write(self): - read_query = {'type':'/music/artist','name':'Yanni\'s Cousin Tom', 'id':{}} - - freebase.sandbox.login(username=USERNAME, password=PASSWORD) - result = freebase.sandbox.mqlread(read_query) - self.assertEqual(None, result) - - write_query = {'create':'unless_exists', 'type':'/music/artist','name':'Yanni'} - - write_result = freebase.sandbox.mqlwrite(write_query) - self.assertNotEqual(None, write_result) - self.assert_(write_result.has_key('create')) - self.assert_(write_result.has_key('type')) - self.assert_(write_result.has_key('name')) - self.assertEqual('existed', write_result['create']) - self.assertEqual('Yanni', write_result['name']) - self.assertEqual('/music/artist', write_result['type']) - - def test_read(self): - query = {'type':'/music/artist','guid':[{}],'name':'Sting', 'album':[{}]} - - mss = HTTPMetawebSession(API_HOST) - - result = mss.mqlread(query) - - self.assertNotEqual(None, result) - self.assert_(result.has_key('guid')) - self.assert_(result.has_key('type')) - self.assert_(result.has_key('name')) - self.assert_(result.has_key('album')) - self.assertEqual(type([]), type(result['album'])) - self.assert_(len(result['album']) > 0) - self.assertEqual( 'Sting', result['name']) - self.assertEqual('#9202a8c04000641f8000000000092a01', result['guid'][0]['value']) - - def test_ridiculously_long_write(self): - - q = [{ - "id":None, - "id|=":["/guid/9202a8c04000641f80000000000" + str(a) for a in range(10000,10320)] - }] - - self.assert_(len(str(q)), 1024) - self.assertNotEqual(len(freebase.mqlread(q)), 0) - - - def test_write(self): - read_query = {'type':'/music/artist','name':'Yanni\'s Cousin Tom', 'id':{}} - mss = HTTPMetawebSession(API_HOST, username=USERNAME, - password=PASSWORD) - result = mss.mqlread(read_query) - self.assertEqual(None, result) - - write_query = {'create':'unless_exists', 'type':'/music/artist','name':'Yanni'} - - mss.login() - write_result = mss.mqlwrite(write_query) - self.assertNotEqual(None, write_result) - self.assert_(write_result.has_key('create')) - self.assert_(write_result.has_key('type')) - self.assert_(write_result.has_key('name')) - self.assertEqual('existed', write_result['create']) - self.assertEqual('Yanni', write_result['name']) - self.assertEqual('/music/artist', write_result['type']) - - def test_trans_blurb(self): - kurt = "/en/kurt_vonnegut" - - blurb = freebase.blurb(kurt) - self.assert_(blurb.startswith("Kurt Vonnegut")) - self.assertNotEqual(len(blurb), 0) - - blurb14 = freebase.blurb(kurt, maxlength=14) - blurb57 = freebase.blurb(kurt, maxlength=57) - self.assertNotEqual(len(blurb14), len(blurb57)) - - blurbpar = freebase.blurb(kurt, break_paragraphs=True, maxlength=20000) - blurbnopar = freebase.blurb(kurt, break_paragraphs=False, maxlength=20000) - # self.assertNotEqual(blurbpar, blurbnopar) this doesn't work unless I get a good example - # of an article with paragraphs. - - def test_trans_raw(self): - kurt = "/en/kurt_vonnegut" - - self.assertRaises(MetawebError, lambda: freebase.raw(kurt)) - - r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) - raw = freebase.raw(r["/common/topic/article"][0].id) - self.assertNotEqual(len(raw), 0) - - # trans should also work - trans = freebase.trans(r["/common/topic/article"][0].id) - self.assertEqual(trans, raw) - - def test_unsafe(self): - kurt = "/en/kurt_vonnegut" - - self.assertRaises(MetawebError, lambda: freebase.unsafe(kurt)) - - r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) - unsafe = freebase.unsafe(r["/common/topic/article"][0].id) - self.assertNotEqual(len(unsafe), 0) - - # we need an example of getting unsafe data - # ... - - - def test_trans_image_thumb(self): - kurt = "/en/kurt_vonnegut" - - r = freebase.mqlread({"id":kurt, "/common/topic/image":[{"id":None, "optional":True, "limit":1}]}) - imageid = r["/common/topic/image"][0].id - rawimage = freebase.raw(imageid) - - thumbedimage = freebase.image_thumb(imageid, maxheight=99) - self.assertNotEqual(rawimage, thumbedimage) - - def test_upload(self): - my_text = "Kurt Vonnegut was an author! " + str(random.random()) - - freebase.sandbox.login(USERNAME, PASSWORD) - response = freebase.sandbox.upload(my_text, "text/plain") - - self.assertEqual(freebase.sandbox.raw(response.id), my_text) - # since it's text/plain, blurb should also be equal - self.assertEqual(freebase.sandbox.blurb(response.id), my_text) - - - def is_kurt_there(self, results): - for result in results: - if result.name == "Kurt Vonnegut": - return True - return False - - - def test_search(self): - r0 = freebase.search("Kurt V") - self.assertEqual(self.is_kurt_there(r0), True) - - r1 = freebase.search("Kurt V", type=["/location/citytown"]) - self.assertEqual(self.is_kurt_there(r1), False) - - r2 = freebase.search("Kurt V", type=["/location/citytown", "/music/artist"]) - self.assertEqual(self.is_kurt_there(r2), False) - - self.assertNotEqual(len(r0), len(r1)) - self.assertNotEqual(len(r0), len(r2)) - self.assertNotEqual(len(r1), len(r2)) - - def test_touch(self): - # this one's hard to test... let's just make sure it works. - freebase.touch() - - - def test_geosearch(self): - - self.assertRaises(Exception, freebase.geosearch) - - r0 = freebase.geosearch(location="/en/california") - self.assertNotEqual(len(r0), 0) - - json = freebase.geosearch(location="/en/san_francisco", format="json") - kml = freebase.geosearch(location="/en/san_francisco", format="kml") - self.assertNotEqual(json, kml) - - - def test_uri_submit(self): - # test a pdf - r = freebase.sandbox.uri_submit("http://www.jcbl.or.jp/game/nec/necfest07/nec2007_data/HayashiMiyake.pdf", content_type="application/pdf") - self.assertEqual(r['/type/content/media_type'], 'application/pdf') - - # test an image - r = freebase.sandbox.uri_submit("http://datamob.org/media/detail_freebase.png") - self.assertEqual(r['/type/content/media_type'], 'image/png') - - def test_version(self): - r = freebase.version() - self.assertNotEqual(len(r), 0) - - def test_status(self): - r = freebase.status() - self.assertNotEqual(len(r), 0) - self.assertEqual(r["status"], u"200 OK") - - - def test_private_domains(self): - freebase.sandbox.login(username=USERNAME, password=PASSWORD) - r = freebase.sandbox.create_private_domain("superfly" + str(int(random.random() * 1e10)), "Superfly!") - - q = {"id" : r["domain_id"], "*" : None} - info = freebase.sandbox.mqlread(q) - self.assertEqual(info["type"], ["/type/domain"]) - self.assertNotEqual(len(info["key"]), 0) - self.assertEqual(info["attribution"], info["creator"]) - - freebase.sandbox.delete_private_domain(info["key"][0]) - deleted = freebase.sandbox.mqlread(q) - self.assertEqual(len(deleted["key"]), 0) - self.assertEqual(len(deleted["type"]), 0) - self.assertEqual(deleted["name"], None) - self.assertEqual(deleted["creator"], info["attribution"]) - - -if __name__ == '__main__': - if USERNAME == "username" and PASSWORD == "password": - - try: - passwordfile = open("test/.password.txt", "r") - fh = passwordfile.read().split("\n") - USERNAME = fh[0] - PASSWORD = fh[1] - passwordfile.close() - - except Exception, e: - print "In order to run the tests, we need to use a valid freebase username and password" - USERNAME = raw_input("Please enter your username: ") - PASSWORD = raw_input("Please enter your password (it'll appear in cleartext): ") - - unittest.main() - diff --git a/test/test_freebase.py b/test/test_freebase.py new file mode 100755 index 0000000..32517fc --- /dev/null +++ b/test/test_freebase.py @@ -0,0 +1,307 @@ +#!/usr/bin/python +# ======================================================================== +# Copyright (c) 2007, Metaweb Technologies, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL METAWEB TECHNOLOGIES BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ======================================================================== + + +import unittest +import sys, logging +import freebase +import random + +from freebase.api import HTTPMetawebSession, MetawebError + + +USERNAME = 'username' +PASSWORD = 'password' +API_HOST = 'sandbox.freebase.com' +TEST_QUERY = {'id': 'null', 'name': 'Sting'} + +s = HTTPMetawebSession(API_HOST) + +if USERNAME == "username" and PASSWORD == "password": + try: + passwordfile = open("test/.password.txt", "r") + fh = passwordfile.read().split("\n") + USERNAME = fh[0] + PASSWORD = fh[1] + passwordfile.close() + s.login(USERNAME, PASSWORD) + + except Exception, e: + print "FREEBASEIn order to run the tests, we need to use a valid freebase username and password" + USERNAME = raw_input("Please enter your username: ") + PASSWORD = raw_input("Please enter your password (it'll appear in cleartext): ") + s.login(USERNAME, PASSWORD) + print "Thanks!" + +else: + s.login(USERNAME, PASSWORD) + +class TestFreebase(unittest.TestCase): + def test_freebase_dot_login_logout(self): + freebase.login(username=USERNAME, password=PASSWORD) + self.assertNotEqual(freebase.user_info(), None) + self.assertEqual(freebase.loggedin(), True) + freebase.logout() + self.assertRaises(MetawebError, freebase.user_info) + self.assertEqual(freebase.loggedin(), False) + + def test_login_logout(self): + mss = HTTPMetawebSession(API_HOST, username=USERNAME, + password=PASSWORD) + mss.login() + + user_info = mss.user_info() + self.assertNotEqual(None, user_info) + self.assertEqual(user_info.code, "/api/status/ok") + self.assertEqual(mss.loggedin(), True) + + mss.logout() + self.assertRaises(MetawebError, mss.user_info) + self.assertEqual(mss.loggedin(), False) + + + def test_freebase_dot_read(self): + query = {'type':'/music/artist','guid':[{}],'name':'Sting', 'album':[{}]} + + result = freebase.mqlread(query) + + self.assertNotEqual(None, result) + self.assert_(result.has_key('guid')) + self.assert_(result.has_key('type')) + self.assert_(result.has_key('name')) + self.assert_(result.has_key('album')) + self.assertEqual(type([]), type(result['album'])) + self.assert_(len(result['album']) > 0) + self.assertEqual( 'Sting', result['name']) + self.assertEqual('#9202a8c04000641f8000000000092a01', result['guid'][0]['value']) + + def test_freebase_dot_write(self): + read_query = {'type':'/music/artist','name':'Yanni\'s Cousin Tom', 'id':{}} + + freebase.sandbox.login(username=USERNAME, password=PASSWORD) + result = freebase.sandbox.mqlread(read_query) + self.assertEqual(None, result) + + write_query = {'create':'unless_exists', 'type':'/music/artist','name':'Yanni'} + + write_result = freebase.sandbox.mqlwrite(write_query) + self.assertNotEqual(None, write_result) + self.assert_(write_result.has_key('create')) + self.assert_(write_result.has_key('type')) + self.assert_(write_result.has_key('name')) + self.assertEqual('existed', write_result['create']) + self.assertEqual('Yanni', write_result['name']) + self.assertEqual('/music/artist', write_result['type']) + + def test_read(self): + query = {'type':'/music/artist','guid':[{}],'name':'Sting', 'album':[{}]} + + mss = HTTPMetawebSession(API_HOST) + + result = mss.mqlread(query) + + self.assertNotEqual(None, result) + self.assert_(result.has_key('guid')) + self.assert_(result.has_key('type')) + self.assert_(result.has_key('name')) + self.assert_(result.has_key('album')) + self.assertEqual(type([]), type(result['album'])) + self.assert_(len(result['album']) > 0) + self.assertEqual( 'Sting', result['name']) + self.assertEqual('#9202a8c04000641f8000000000092a01', result['guid'][0]['value']) + + def test_ridiculously_long_write(self): + + q = [{ + "id":None, + "id|=":["/guid/9202a8c04000641f80000000000" + str(a) for a in range(10000,10320)] + }] + + self.assert_(len(str(q)), 1024) + self.assertNotEqual(len(freebase.mqlread(q)), 0) + + + def test_write(self): + read_query = {'type':'/music/artist','name':'Yanni\'s Cousin Tom', 'id':{}} + mss = HTTPMetawebSession(API_HOST, username=USERNAME, password=PASSWORD) + result = mss.mqlread(read_query) + self.assertEqual(None, result) + + write_query = {'create':'unless_exists', 'type':'/music/artist','name':'Yanni'} + + mss.login() + write_result = mss.mqlwrite(write_query) + self.assertNotEqual(None, write_result) + self.assert_(write_result.has_key('create')) + self.assert_(write_result.has_key('type')) + self.assert_(write_result.has_key('name')) + self.assertEqual('existed', write_result['create']) + self.assertEqual('Yanni', write_result['name']) + self.assertEqual('/music/artist', write_result['type']) + + def test_trans_blurb(self): + kurt = "/en/kurt_vonnegut" + + blurb = freebase.blurb(kurt) + self.assert_(blurb.startswith("Kurt Vonnegut")) + self.assertNotEqual(len(blurb), 0) + + blurb14 = freebase.blurb(kurt, maxlength=14) + blurb57 = freebase.blurb(kurt, maxlength=57) + self.assertNotEqual(len(blurb14), len(blurb57)) + + blurbpar = freebase.blurb(kurt, break_paragraphs=True, maxlength=20000) + blurbnopar = freebase.blurb(kurt, break_paragraphs=False, maxlength=20000) + # self.assertNotEqual(blurbpar, blurbnopar) this doesn't work unless I get a good example + # of an article with paragraphs. + + def test_trans_raw(self): + kurt = "/en/kurt_vonnegut" + + self.assertRaises(MetawebError, lambda: freebase.raw(kurt)) + + r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) + raw = freebase.raw(r["/common/topic/article"][0].id) + self.assertNotEqual(len(raw), 0) + + # trans should also work + trans = freebase.trans(r["/common/topic/article"][0].id) + self.assertEqual(trans, raw) + + def test_unsafe(self): + kurt = "/en/kurt_vonnegut" + + self.assertRaises(MetawebError, lambda: freebase.unsafe(kurt)) + + r = freebase.mqlread({"id":kurt, "/common/topic/article":[{"id":None, "optional":True, "limit":1}]}) + unsafe = freebase.unsafe(r["/common/topic/article"][0].id) + self.assertNotEqual(len(unsafe), 0) + + # we need an example of getting unsafe data + # ... + + + def test_trans_image_thumb(self): + kurt = "/en/kurt_vonnegut" + + r = freebase.mqlread({"id":kurt, "/common/topic/image":[{"id":None, "optional":True, "limit":1}]}) + imageid = r["/common/topic/image"][0].id + rawimage = freebase.raw(imageid) + + thumbedimage = freebase.image_thumb(imageid, maxheight=99) + self.assertNotEqual(rawimage, thumbedimage) + + def test_upload(self): + my_text = "Kurt Vonnegut was an author! " + str(random.random()) + + freebase.sandbox.login(USERNAME, PASSWORD) + response = freebase.sandbox.upload(my_text, "text/plain") + + self.assertEqual(freebase.sandbox.raw(response.id), my_text) + # since it's text/plain, blurb should also be equal + self.assertEqual(freebase.sandbox.blurb(response.id), my_text) + + + def is_kurt_there(self, results): + for result in results: + if result.name == "Kurt Vonnegut": + return True + return False + + + def test_search(self): + r0 = freebase.search("Kurt V") + self.assertEqual(self.is_kurt_there(r0), True) + + r1 = freebase.search("Kurt V", type=["/location/citytown"]) + self.assertEqual(self.is_kurt_there(r1), False) + + r2 = freebase.search("Kurt V", type=["/location/citytown", "/music/artist"]) + self.assertEqual(self.is_kurt_there(r2), False) + + self.assertNotEqual(len(r0), len(r1)) + self.assertNotEqual(len(r0), len(r2)) + self.assertNotEqual(len(r1), len(r2)) + + def test_touch(self): + # this one's hard to test... let's just make sure it works. + freebase.touch() + + + def test_geosearch(self): + + self.assertRaises(Exception, freebase.geosearch) + + r0 = freebase.geosearch(location="/en/california") + self.assertNotEqual(len(r0), 0) + + json = freebase.geosearch(location="/en/san_francisco", format="json") + kml = freebase.geosearch(location="/en/san_francisco", format="kml") + self.assertNotEqual(json, kml) + + + def test_uri_submit(self): + # test a pdf + r = freebase.sandbox.uri_submit("http://www.jcbl.or.jp/game/nec/necfest07/nec2007_data/HayashiMiyake.pdf", content_type="application/pdf") + self.assertEqual(r['/type/content/media_type'], 'application/pdf') + + # test an image + r = freebase.sandbox.uri_submit("http://datamob.org/media/detail_freebase.png") + self.assertEqual(r['/type/content/media_type'], 'image/png') + + def test_version(self): + r = freebase.version() + self.assertNotEqual(len(r), 0) + + def test_status(self): + r = freebase.status() + self.assertNotEqual(len(r), 0) + self.assertEqual(r["status"], u"200 OK") + + + def test_private_domains(self): + freebase.sandbox.login(username=USERNAME, password=PASSWORD) + r = freebase.sandbox.create_private_domain("superfly" + str(int(random.random() * 1e10)), "Superfly!") + + q = {"id" : r["domain_id"], "*" : None} + info = freebase.sandbox.mqlread(q) + self.assertEqual(info["type"], ["/type/domain"]) + self.assertNotEqual(len(info["key"]), 0) + self.assertEqual(info["attribution"], info["creator"]) + + freebase.sandbox.delete_private_domain(info["key"][0]) + deleted = freebase.sandbox.mqlread(q) + self.assertEqual(len(deleted["key"]), 0) + self.assertEqual(len(deleted["type"]), 0) + self.assertEqual(deleted["name"], None) + self.assertEqual(deleted["creator"], info["attribution"]) + + +if __name__ == '__main__': + unittest.main() + diff --git a/test/test_schema_manipulation.py b/test/test_schema_manipulation.py new file mode 100644 index 0000000..968e429 --- /dev/null +++ b/test/test_schema_manipulation.py @@ -0,0 +1,150 @@ +import unittest +import sys, logging +import freebase +import random +import time + +from freebase.api import HTTPMetawebSession, MetawebError +from freebase.schema import create_type, reciprocate_property, delegate_property +from freebase.schema import create_property, type_object, copy_property, move_property + +USERNAME = 'username' +PASSWORD = 'password' +API_HOST = 'sandbox.freebase.com' + +s = freebase.api.HTTPMetawebSession(API_HOST) +domain_id = None + +if USERNAME == "username" and PASSWORD == "password": + try: + passwordfile = open("test/.password.txt", "r") + fh = passwordfile.read().split("\n") + USERNAME = fh[0] + PASSWORD = fh[1] + passwordfile.close() + s.login(USERNAME, PASSWORD) + + except Exception, e: + print "SCHEMAIn order to run the tests, we need to use a valid freebase username and password" + USERNAME = raw_input("Please enter your username: ") + PASSWORD = raw_input("Please enter your password (it'll appear in cleartext): ") + s.login(USERNAME, PASSWORD) + print "Thanks!" + +else: + s.login(USERNAME, PASSWORD) + +r = s.create_private_domain("test" + str(int(random.random() * 1e10)), "test")["domain_id"] +domain_id = s.mqlread({"id" : r, "a:id" : None})["a:id"] + +# Sorry, this is just so annoying to type. +f = lambda x: x["id"] + +class TestSchemaManipulation(unittest.TestCase): + + def test_make_and_type_object(self): + print domain_id + a = s.create_object("A", path=domain_id + "/a") + self.assertEqual(a.create, "created") + + b = s.create_object("B", path=domain_id + "/b", included_types=["/people/person"]) + q = { "id" : b.id, "type" : [{"id" : None}] } + types = map(f, s.mqlread(q)["type"]) + self.assertEqual("/common/topic" in types, True) + self.assertEqual("/people/person" in types, True) + self.assertEqual("/film/actor" in types, False) + s.touch(), time.sleep(2), s.touch(); + type_object(s, b.id, "/film/film_genre") + s.touch(), time.sleep(2), s.touch(); + s.mqlwrite({"id" : b.id, "/film/film_genre/films_in_this_genre" : {"id" : "/en/the_taking_of_pelham_1_2_3", "connect" : "insert"}}) + types = map(f, s.mqlread(q)["type"]) + s.touch(), time.sleep(2), s.touch(); + print types + + self.assertEqual("/film/film_genre" in types, True) + self.assertEqual("/media_common/media_genre" in types, True) + + def test_move_object(self): + print domain_id + old = s.create_object("old", domain_id + "/old") + s.move_object(domain_id + "/old", domain_id + "/new") + is_old = { "id" : domain_id + "/old", "key" : [{"value" : None}]} + is_new = { "id" : domain_id + "/new", "key" : [{"value" : None}]} + s.touch() + o = s.mqlread(is_old) + n = s.mqlread(is_new) + # we don't just check to see if the id exists, as freebase caches id -> guid lookups + if o: + self.assertEqual(len([b for b in o["key"] if b["value"] == "old"]), 0) + self.assertEqual(len([b for b in n["key"]]), 1) + + def test_property_moving(self): + person = create_type(s, "Person", "person", domain_id) + date_of_birth = create_property(s, "DB", "db", domain_id + "/person", "/type/datetime", False, tip="Date of Birth of a Person") + + goblin = create_type(s, "Goblin", "goblin", domain_id) + self.assertEqual(s.mqlread({"id" : domain_id + "/goblin/db"}), None) + copy_property(s, domain_id + "/person/db", domain_id + "/goblin/db") + self.assertNotEqual(s.mqlread({"id" : domain_id + "/goblin/db"}), None) + + self.assertEqual(s.mqlread({"id" : domain_id + "/goblin/db", "/freebase/documented_object/tip" : None})["/freebase/documented_object/tip"], "Date of Birth of a Person") + + # But we don't actually want that to be the tip, we should be able to change it + dragon = create_type(s, "Dragon", "dragon", domain_id) + copy_property(s, domain_id + "/person/db", domain_id + "/dragon/db", **{"/freebase/documented_object/tip": "Date of Birth of a Dragon!"}) + self.assertEqual(s.mqlread({"id" : domain_id + "/dragon/db", "/freebase/documented_object/tip" : None})["/freebase/documented_object/tip"], "Date of Birth of a Dragon!") + + # let's test with a slightly move obtuse property, unit! + create_property(s, "Magnetic Moment", "mmoment", domain_id + "/person", "/type/float", True, + extra={"unit" : {"connect" : "insert", "id" : "/en/nuclear_magneton"}}) + copy_property(s, domain_id + "/person/mmoment", domain_id + "/goblin/mmoment") + self.assertEqual(s.mqlread({"id" : domain_id + "/goblin/mmoment", "/type/property/unit" : {"id" : None}})["/type/property/unit"]["id"], "/en/nuclear_magneton") + + + def test_rename_property(self): + grendel = create_type(s, "Grendel", "grendel", domain_id) + # a mistake + date_of_bbirth = create_property(s, "Date of Bbirth", "dbb", domain_id + "/grendel", "/type/datetime", False, tip="Date of Bbirth of Grendel") + self.assertEqual(s.mqlread({"id" : domain_id + "/grendel/dbb", "name" : None})["name"], "Date of Bbirth") + + # let's fix it + move_property(s, domain_id + "/grendel/dbb", domain_id + "/grendel/db", name="Date of Birth", **{"/freebase/documented_object/tip":"Date of Birth of Grendel"}) + self.assertEqual(s.mqlread({"id" : domain_id + "/grendel/db", "name" : None})["name"], "Date of Birth") + # sometimes freebase still thinks /grendel/dbb exists... i hate id -> guid caches. + self.assertEqual(s.mqlread({"id" : domain_id + "/grendel/dbb", "key" : [{"value" : None}]}), None) + + def test_recreate_contractbridge_base(self): + + base = domain_id + + create_type(s, "Bridge Player", "bridge_player", base, tip="A bridge player", included=["/people/person", "/common/topic"]) + create_type(s, "Bridge Tournament", "bridge_tournament", base, tip="A bridge tournament", included=["/event/event", "/common/topic"]) + create_type(s, "Bridge Tournament Standings", "bridge_tournament_standings", base, cvt=True, tip="Bridge Tournamen Results") + + player = base + "/bridge_player" + tourney = base + "/bridge_tournament" + standing = base + "/bridge_tournament_standings" + + # tournament standings + # ideas: create a cvt could be a function, disambig always True, same schema, iono, whatever + create_property(s, "Year", "year", standing, "/type/datetime", True, disambig=True, tip="Year") + create_property(s, "First Place", "first_place", standing, player, False, disambig=True) + create_property(s, "Second Place", "second_place", standing, player, False, disambig=True) + create_property(s, "Tournament", "tournament", standing, tourney, True, disambig=True) + + + # tournament + reciprocate_property(s, "Standing", "standing", standing + "/tournament", False, disambig=True) + create_property(s, "Location", "location", tourney, "/location/citytown", False, disambig=True) + + # bridge player + # standings must be reverses! + reciprocate_property(s, "First Place Finish", "first_place_finish", standing + "/first_place", False, False) + reciprocate_property(s, "Second Place Finish", "second_place_finish", standing + "/second_place", False, False) + + #delegator property test + delegate_property(s, "/people/person/date_of_birth", player, "Date of Birth", "db") + +if __name__ == '__main__': + unittest.main() + -- cgit v1.3.1