summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornitromaster101 <nitromaster101@5914aa95-5b3a-0410-a3b5-7b719e7fe9b2>2009-07-09 01:36:29 +0000
committernitromaster101 <nitromaster101@5914aa95-5b3a-0410-a3b5-7b719e7fe9b2>2009-07-09 01:36:29 +0000
commit637b2c5788657535ab05b6610f3b02d56ab957ec (patch)
treee774b101c86e33e2fab7afaf9feec161b799edf5 /test
parent8e71e49ea3708c30a4a0d3688cc9d643665d68be (diff)
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
Diffstat (limited to 'test')
-rw-r--r--test/__init__.py0
-rw-r--r--test/runtests.py67
-rwxr-xr-xtest/test_freebase.py (renamed from test/test_all.py)41
-rw-r--r--test/test_schema_manipulation.py150
4 files changed, 240 insertions, 18 deletions
diff --git a/test/__init__.py b/test/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/__init__.py
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_freebase.py
index cfe83f8..32517fc 100755
--- a/test/test_all.py
+++ b/test/test_freebase.py
@@ -40,8 +40,28 @@ 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)
@@ -128,8 +148,7 @@ class TestFreebase(unittest.TestCase):
def test_write(self):
read_query = {'type':'/music/artist','name':'Yanni\'s Cousin Tom', 'id':{}}
- mss = HTTPMetawebSession(API_HOST, username=USERNAME,
- password=PASSWORD)
+ mss = HTTPMetawebSession(API_HOST, username=USERNAME, password=PASSWORD)
result = mss.mqlread(read_query)
self.assertEqual(None, result)
@@ -284,19 +303,5 @@ class TestFreebase(unittest.TestCase):
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_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()
+