예제 #1
0
    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))
예제 #2
0
    def test_restore_over_restore(self):
        domain_id = _create_domain()
        graph = dump_base(s, "/base/contractbridge")
        restore(s, graph, domain_id)

        # now we restore again... it should raise a MetawebError
        self.assertRaises(MetawebError, restore, s, graph, domain_id)
 def test_restore_over_restore(self):
     domain_id = _create_domain()
     graph = dump_base(s, "/base/contractbridge")   
     restore(s, graph, domain_id)
     
     # now we restore again... it should raise a MetawebError
     self.assertRaises(MetawebError, restore, s, graph, domain_id)
 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))
예제 #5
0
def cmd_restore(fb, newlocation, graphfile):
    """restore a graph object to the graph
    %prog restore newlocation graphfile

    Restore a graph object to the newlocation
    """
    fh = open(graphfile, "r")
    graph = json.loads(fh.read())
    fh.close()
    return restore(fb.mss, graph, newlocation, ignore_types=None)
예제 #6
0
def cmd_restore(fb, newlocation, graphfile):
    """restore a graph object to the graph
    %prog restore newlocation graphfile

    Restore a graph object to the newlocation
    """
    fh = open(graphfile, "r")
    graph = json.loads(fh.read())
    fh.close()
    return restore(fb.mss, graph, newlocation, ignore_types=None)
 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])
예제 #8
0
def fb_restore():
    op = OptionParser(usage='%prog [options] new_location graph_output_from_dump*_command')
    
    op.disable_interspersed_args()

    op.add_option('-s', '--service', dest='service_host',
                    metavar='HOST',
                    help='Freebase HTTP service address:port')
    
    op.add_option('-S', '--sandbox', dest='use_sandbox',
                    default=False, action='store_true',
                    help='shortcut for --service=sandbox-freebase.com (default)')
    
    op.add_option('-F', '--freebase', dest='use_freebase',
                    default=False, action='store_true',
                    help='shortcut for --service=freebase.com (not default)')

    op.add_option('-u', '--username', dest='username',
                    action='store',
                    help='username for freebase service')

    op.add_option('-p', '--password', dest='password',
                    action='store',
                    help='password for freebase service')

                    

    options,args = op.parse_args()

    if (options.username and not options.password) or (not options.username and options.password):
        op.error("You must supply both a username and password")

    if options.use_sandbox and options.use_freebase:
        op.error("You can't use both freebase and sandbox!")
    
    if options.service_host and (options.use_sandbox or options.use_freebase):
        op.error("You can't specify both --service and --freebase or --sandbox")
    
    if not options.service_host and not options.use_sandbox and not options.use_freebase:
        op.error("You have to specify to upload to sandbox or production (freebase)")
    
    service_host = options.service_host
    if options.use_sandbox:
        service_host = "sandbox-freebase.com"
    if options.use_freebase:
        service_host = "freebase.com"

    s = login(service_host, username=options.username, password=options.password)
    
    newlocation = args[0]
    if len(args) == 1:
        graphfile = "-" #stdin
    else: graphfile = args[1]
    if graphfile != "-":
        fg = open(graphfile, "r")
        graph = json.load(fg)
        fg.close()
    if graphfile == "-": # use stdin
        graph = json.load(sys.stdin)
    print "loaded graph", graph
    restore(s, graph, newlocation, ignore_types=None, debug=False)
예제 #9
0
    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])