summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/runtests.py8
-rw-r--r--test/test_hardcore_schema_manipulation.py121
-rw-r--r--test/test_schema_manipulation.py12
3 files changed, 129 insertions, 12 deletions
diff --git a/test/runtests.py b/test/runtests.py
index c90afb7..db2303b 100644
--- a/test/runtests.py
+++ b/test/runtests.py
@@ -16,18 +16,23 @@ def main():
created = True
USERNAME, PASSWORD = getlogindetails.main(create_password_file=True)
USERNAME, PASSWORD = getlogindetails.main()
+
# run tests
import test_freebase
import test_schema_manipulation
+ import test_hardcore_schema_manipulation
s1 = unittest.TestLoader().loadTestsFromTestCase(test_freebase.TestFreebase)
s2 = unittest.TestLoader().loadTestsFromTestCase(test_schema_manipulation.TestSchemaManipulation)
+ s3 = unittest.TestLoader().loadTestsFromTestCase(test_hardcore_schema_manipulation.TestHardcoreSchemaManipulation)
# This is very strange. If you try to do [s1, s2], thereby running freebase tests first,
# two tests in the testschemamanipulation file fail! They fail because of caching issues; if
# I check on freebase, the changes are actually there. I have racked my mind for explanations.
- anotherrun = unittest.TestSuite([s1, s2])
+
+ # this is such a hack, and I'm sorry. The tests run 100% correct individually.
+ anotherrun = unittest.TestSuite([s1, s3, s2])
#run = unittest.TestSuite(suites)
@@ -35,6 +40,7 @@ def main():
if created: os.remove(passwordfile)
return anotherrun
+
if __name__ == '__main__':
diff --git a/test/test_hardcore_schema_manipulation.py b/test/test_hardcore_schema_manipulation.py
new file mode 100644
index 0000000..aaecf28
--- /dev/null
+++ b/test/test_hardcore_schema_manipulation.py
@@ -0,0 +1,121 @@
+import unittest
+import sys, logging
+import freebase
+import random
+import time
+
+import getlogindetails
+
+from freebase.api import HTTPMetawebSession, MetawebError
+from freebase.schema import dump_type, dump_base, restore
+
+USERNAME = 'username'
+PASSWORD = 'password'
+API_HOST = 'sandbox.freebase.com'
+
+s = freebase.api.HTTPMetawebSession(API_HOST)
+
+if USERNAME == "username" and PASSWORD == "password":
+ USERNAME, PASSWORD = getlogindetails.main()
+
+s.login(USERNAME, PASSWORD)
+
+# Sorry, this is just so annoying to type.
+f = lambda x: x["id"]
+
+class TestHardcoreSchemaManipulation(unittest.TestCase):
+
+ def test_copy_an_entire_domain(self):
+ domain_id = _create_domain()
+ ex_domain_id = "/base/contractbridge" # example domain id
+ ex_domain_type = "bridge_player"
+ ex_domain_type_id = ex_domain_id + "/" + ex_domain_type
+
+ graph = dump_base(s, ex_domain_id)
+ restore(s, graph, domain_id)
+
+ newperson, realperson = s.mqlreadmulti([{"id" : domain_id + "/" + ex_domain_type, "/type/type/properties" : {"return" : "count" }},
+ {"id" : ex_domain_type_id, "/type/type/properties" : {"return" : "count" }}])
+ self.assertEqual(newperson["/type/type/properties"], realperson["/type/type/properties"])
+
+ # let's try and check everything.
+ # - check all the types are there
+ realtypes = s.mqlread([{"id" : None, "type" : "/type/type", "domain" : ex_domain_id}])
+ newtypes = s.mqlread([{"id" : None, "type" : "/type/type", "domain" : domain_id}])
+
+ l = lambda q: sorted(map(f, q))
+
+ realtypes = l(realtypes)
+ newtypes = l(newtypes)
+
+ self.assertEqual(len(realtypes), len(newtypes))
+ for i in range(len(realtypes)):
+ self.assertEqual(realtypes[i].rsplit("/", 1)[-1], newtypes[i].rsplit("/", 1)[-1])
+
+ # - check the properties are the same
+
+ def get_properties(types):
+ properties = set()
+ for i in s.mqlreadmulti([{"id" : id, "/type/type/properties" : [{"id" : None}]} for id in types]):
+ properties.update(map(lambda x: x["id"], i["/type/type/properties"]))
+ return properties
+
+ realproperties = sorted(list(get_properties(realtypes)))
+ newproperties = sorted(list(get_properties(newtypes)))
+
+ def ignore_base(id): return id.rsplit("/", 1)[-1]
+
+ self.assertEqual(len(realproperties), len(newproperties))
+ self.assertEqual([ignore_base(prop_id) for prop_id in realproperties], [ignore_base(prop_id) for prop_id in newproperties])
+
+ # - check the properties and type's attributes are the same
+
+
+
+
+ def test_try_copying_a_cvt(self):
+
+ # if follow_types is True, everything is kosher.
+ domain_id = _create_domain()
+ graph = dump_type(s, "/film/actor", follow_types=True)
+ restore(s, graph, domain_id)
+
+ newactor, realactor = s.mqlreadmulti([{"id" : domain_id + "/actor", "/type/type/properties" : {"return" : "count" }},
+ {"id" : "/film/actor", "/type/type/properties" : {"return" : "count" }}])
+ self.assertEqual(newactor["/type/type/properties"], realactor["/type/type/properties"])
+
+ # if follow_types is False, if we try to upload a cvt, it should whine
+ from freebase.schema import CVTError
+ self.assertRaises(CVTError, lambda: dump_type(s, "/film/actor", follow_types=False))
+
+
+
+def _create_domain():
+ domain_id = s.create_private_domain("test" + str(int(random.random() * 1e10)), "test")["domain_id"]
+ domain_id = s.mqlread({"id" : domain_id, "a:id" : None})["a:id"]
+ return domain_id
+
+if __name__ == '__main__':
+ if USERNAME == "username" and PASSWORD == "password":
+ try:
+ passwordfile = open(".password.txt", "r")
+ fh = passwordfile.read().split("\n")
+ USERNAME = fh[0]
+ PASSWORD = fh[1]
+ passwordfile.close()
+ s.login(USERNAME, PASSWORD)
+
+ 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): ")
+ s.login(USERNAME, PASSWORD)
+ print "Thanks!"
+
+ else:
+ s.login(USERNAME, PASSWORD)
+
+ unittest.main()
+
+
+
diff --git a/test/test_schema_manipulation.py b/test/test_schema_manipulation.py
index 53ce542..f8453e7 100644
--- a/test/test_schema_manipulation.py
+++ b/test/test_schema_manipulation.py
@@ -31,30 +31,20 @@ 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"])
- print s.cookiejar._cookies["sandbox-freebase.com"]
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();
- print s.cookiejar._cookies["sandbox-freebase.com"]
+
type_object(s, b.id, "/film/film_genre")
- print s.cookiejar._cookies["sandbox-freebase.com"]
- 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"])
- print s.cookiejar._cookies["sandbox-freebase.com"]
-
- 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)