diff options
| -rw-r--r-- | freebase/schema.py | 26 | ||||
| -rw-r--r-- | setup.py | 6 | ||||
| -rw-r--r-- | test/runtests.py | 17 | ||||
| -rwxr-xr-x | test/test_freebase.py | 22 | ||||
| -rw-r--r-- | test/test_schema_manipulation.py | 33 |
5 files changed, 38 insertions, 66 deletions
diff --git a/freebase/schema.py b/freebase/schema.py index 5be9b4a..62e5389 100644 --- a/freebase/schema.py +++ b/freebase/schema.py @@ -143,7 +143,8 @@ def create_type(s, name, key, ns, cvt=None, tip=None, included=None, extra=None) # Create Property def create_property(s, name, key, schema, expected, unique=False, disambig=False, tip=None, extra=None): if key_exists(s, schema + "/" + key): - raise Exception("The key \"%s\" already exists!" % (schema + "/" + key)) + # this isn't (neccesarily) a problem + # raise Exception("The key \"%s\" already exists!" % (schema + "/" + key)) return # validate parameters str @@ -251,25 +252,20 @@ def reciprocate_property(s, name, key, master, unique=False, disambig=False, tip # upload / restore types def dump_base(s, base_id): types = map(lambda x: x["id"], s.mqlread({"id" : base_id, "/type/domain/types":[{"id" : None}]})["/type/domain/types"]) - graph = _get_graph(types) + graph = _get_graph(s, types, True) graph["__follow_types"] = True return graph def dump_type(s, type_id, follow_types=True): types = [type_id] - graph = _get_graph(types, follow_types) + graph = _get_graph(s, types, follow_types) graph["__follow_types"] = follow_types - result = json.dumps(graph, indent=2) - - fh = open("junk.json", "w") - fh.write(result) - fh.close() return graph -def upload_type(s, graph, new_location, ignore_types=None, debug=False): +def restore(s, graph, new_location, ignore_types=None, debug=False): follow_types = graph.get("__follow_types", True) if debug: print "Following types:", follow_types @@ -279,7 +275,7 @@ def upload_type(s, graph, new_location, ignore_types=None, debug=False): if not tid.startswith("__"): typegraph[tid] = idres["__requires"] - type_deps = map(lambda (name, x): (len(x), name), typegraph.items()) + type_deps = map(lambda (name, x): (len(x), name), typegraph.iteritems()) type_deps.sort() if follow_types: types_to_create = create_what(type_deps, typegraph) @@ -294,7 +290,7 @@ def upload_type(s, graph, new_location, ignore_types=None, debug=False): for prop in idres["properties"]: propgraph[prop["id"]] = prop["__requires"] proptotype[prop["id"]] = tid - prop_deps = map(lambda (name, x): (len(x), name), propgraph.items()) + prop_deps = map(lambda (name, x): (len(x), name), propgraph.items()) #* prop_deps.sort() if follow_types: props_to_create = create_what(prop_deps, propgraph) @@ -355,7 +351,7 @@ def upload_type(s, graph, new_location, ignore_types=None, debug=False): if debug: print "--------------------------" - for prop in props_to_create: + for prop in props_to_create: #* prop_id info = graph[proptotype[prop]]["properties"] for i in info: if i["id"] == prop: @@ -367,8 +363,8 @@ def upload_type(s, graph, new_location, ignore_types=None, debug=False): if i["expected_type"]: expected = convert_name(i["expected_type"], base_id, domain_id, only_include) for k in i["key"]: - if k.namespace == proptotype[prop]: - key = k.value + if k["namespace"] == proptotype[prop]: + key = k["value"] if i["/freebase/documented_object/tip"]: tip = graph[type]["/freebase/documented_object/tip"] @@ -422,7 +418,7 @@ def upload_type(s, graph, new_location, ignore_types=None, debug=False): -def _get_graph(initial_types, follow_types): +def _get_graph(s, initial_types, follow_types): """ get the graph of dependencies of all the types involved, starting with a list supplied """ assert isinstance(initial_types, (list, tuple)) @@ -65,9 +65,9 @@ setup( entry_points = { 'console_scripts': [ 'fcl = freebase.fcl.fcl:main', - 'fb_save_base = freebase.schema.cmd:fb_save_base', - 'fb_save_type = freebase.schena.cmd:fb_save_type', - 'fb_restore = freebase.schema.cmd:fb_restore' + 'fb_save_base = freebase.schema_cmd:fb_save_base', + 'fb_save_type = freebase.schema_cmd:fb_save_type', + 'fb_restore = freebase.schema_cmd:fb_restore' ] }, test_suite = "test.runtests.main", diff --git a/test/runtests.py b/test/runtests.py index 65f02c9..c90afb7 100644 --- a/test/runtests.py +++ b/test/runtests.py @@ -5,6 +5,8 @@ import os.path import freebase +import getlogindetails + def main(): created = False passwordfile = "test/.password.txt" @@ -12,17 +14,8 @@ def main(): # 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() + USERNAME, PASSWORD = getlogindetails.main(create_password_file=True) + USERNAME, PASSWORD = getlogindetails.main() # run tests import test_freebase @@ -34,7 +27,7 @@ def main(): # 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([s2, s1]) + anotherrun = unittest.TestSuite([s1, s2]) #run = unittest.TestSuite(suites) diff --git a/test/test_freebase.py b/test/test_freebase.py index 32517fc..d448b39 100755 --- a/test/test_freebase.py +++ b/test/test_freebase.py @@ -32,34 +32,22 @@ import sys, logging import freebase import random +import getlogindetails + from freebase.api import HTTPMetawebSession, MetawebError USERNAME = 'username' PASSWORD = 'password' -API_HOST = 'sandbox.freebase.com' +API_HOST = 'http://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!" + USERNAME, PASSWORD = getlogindetails.main() -else: - s.login(USERNAME, PASSWORD) +s.login(USERNAME, PASSWORD) class TestFreebase(unittest.TestCase): def test_freebase_dot_login_logout(self): diff --git a/test/test_schema_manipulation.py b/test/test_schema_manipulation.py index 968e429..53ce542 100644 --- a/test/test_schema_manipulation.py +++ b/test/test_schema_manipulation.py @@ -4,35 +4,23 @@ import freebase import random import time +import getlogindetails + 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' +API_HOST = 'http://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!" + USERNAME, PASSWORD = getlogindetails.main() -else: - s.login(USERNAME, PASSWORD) +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"] @@ -48,16 +36,23 @@ class TestSchemaManipulation(unittest.TestCase): 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(); + 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"}}) + #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 |
