def main():
    parser = argparse.ArgumentParser(
        description='import CSV data into Topogram')
    parser.add_argument('filename',
                        action="store",
                        default=None,
                        help='CSV file path')
    parser.add_argument('--base-url',
                        default='http://localhost:3000',
                        help='Base URL for Topogram API endpoint')
    parser.add_argument('--user', default=None, help='Username')
    parser.add_argument('--password', default=None, help='Password')
    parser.add_argument('--title',
                        default="Test",
                        help='Title of the document')
    parser.add_argument('--verbose', default=False, help='Log')

    args = parser.parse_args()

    print args.base_url
    client = TopogramAPIClient(args.base_url,
                               args.user,
                               args.password,
                               debug=args.verbose)

    #check if the file exists
    if not os.path.isfile(args.filename):
        raise ValueError("CSV File '%s' doesn't exist" % args.filename)

    # parse data
    data = parse_data(args.filename)
 def test_has_active_connection(self):
     self.assertRaises(
         ConnectionError,
         lambda: TopogramAPIClient("http://localhost:5000", debug=True))
 def setUpClass(self):
     # init client
     self.client = TopogramAPIClient(test_server_url, debug=True)
     self.session = session()
class TestTopogramAPIClient(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # init client
        self.client = TopogramAPIClient(test_server_url, debug=True)
        self.session = session()

    def setUp(self):

        # clean the database
        drop_database()

        # create a new user
        self.client.create_user("*****@*****.**", "password")

        # log in
        r = self.client.user_login("*****@*****.**", "password")
        print r["data"]["authToken"], r["data"]["userId"]

    def test_make_url(self):
        """URLs should be conformed, errors should be raised when passing wrong paths """

        self.assertRaises(ValueError,
                          lambda: self.client.make_url("/start-with-slash"))
        self.assertRaises(ValueError,
                          lambda: self.client.make_url("http://myapp.com/api"))
        self.assertEqual(self.client.make_url("lalalala"),
                         test_server_url + "/api/lalalala")

    def test_has_active_connection(self):
        self.assertRaises(
            ConnectionError,
            lambda: TopogramAPIClient("http://*****:*****@tester.com", "password")
    #     self.assertEqual(True, False)

    def test_user_login(self):
        r = self.client.create_user("*****@*****.**", "password")
        _id = r["data"]["_id"]
        self.assertEqual(type(_id), unicode)

        # need to be logged in to delete a user
        self.assertRaises(ValueError, lambda: self.client.delete_user(_id))

    def test_create_topogram(self):
        # create with only a name
        r = self.client.create_topogram("test topogram")
        self.assertEqual(r["status"], "success")
        self.assertTrue(r["status_code"] == 200)
        self.assertEqual(r["data"]["name"], "test topogram")

    def test_make_public(self):
        r = self.client.create_topogram("test topogram")
        _id = r["data"]["_id"]
        r = self.client.makePublic(_id)
        self.assertEqual(r["data"]["sharedPublic"], 1)
        r = self.client.makePrivate(_id)
        self.assertEqual(r["data"]["sharedPublic"], 0)

    def test_get_public_topograms_list(self):
        r = self.client.create_topogram("test topogram")
        _id = r["data"]["_id"]
        r = self.client.makePublic(_id)
        r = self.client.get_public_topograms()
        self.assertEqual(len(r), 1)

    def test_get_private_topograms_list(self):
        self.client.create_topogram("wonderful isn't it?")
        self.client.create_topogram("yes, absolutely stunning")
        self.client.create_topogram("well, not so bad")
        r = self.client.get_topograms()
        self.assertEqual(len(r["data"]), 3)

    def test_get_topogram(self):
        r = self.client.create_topogram("哈哈哈")
        _id = r["data"]["_id"]
        r = self.client.get_topogram(_id)
        self.assertEqual(r["data"]["name"].encode('utf-8'), "哈哈哈")

    def test_delete_topogram(self):
        r = self.client.create_topogram("to be deleted")
        _id = r["data"]["_id"]
        r = self.client.delete_topogram(_id)
        # TODO : assert that nodes and edges have correctly disappear
        r = self.client.get_topograms()
        self.assertEqual(len(r["data"]), 0)

    def test_create_node(self):
        r = self.client.create_topogram("ok")
        topogramId = r["data"]["_id"]
        r = self.client.create_node(topogramId,
                                    id="jojo",
                                    x=1,
                                    y=2,
                                    data={"lat": 3})
        self.assertEqual(r["data"]["data"]["id"], "jojo")
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)
        self.assertEqual(r["data"]["data"]["lat"], 3)

    def test_get_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        r = self.client.get_node(_id)
        self.assertEqual(r["data"]["data"]["id"], "jojo")
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)

    def test_update_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)
        r = self.client.update_node(_id, x=3, y=4, data={"starred": True})
        self.assertEqual(r["data"]["position"]["x"], 3)
        self.assertEqual(r["data"]["position"]["y"], 4)
        self.assertEqual(r["data"]["data"]["starred"], True)

    def test_delete_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        r = self.client.delete_node(_id)
        r = self.client.get_node(_id)
        self.assertEqual(r["status"], "fail")
        self.assertIn("not found", r["message"])

    def test_create_edge(self):
        topogramId = "sth"
        r = self.client.create_edge(topogramId,
                                    "bla",
                                    "bli",
                                    data={"lat": 2},
                                    name="kokoko")
        # _id = r["data"]["_id"]
        self.assertEqual(r["data"]["data"]["source"], "bla")
        self.assertEqual(r["data"]["data"]["target"], "bli")
        self.assertEqual(r["data"]["data"]["lat"], 2)
        self.assertEqual(r["data"]["id"], "kokoko")

    def test_update_edge(self):
        topogramId = "sth"
        r = self.client.create_edge(topogramId,
                                    "bla",
                                    "bli",
                                    data={"lat": 2},
                                    name="kokoko")
        _id = r["data"]["_id"]
        self.assertEqual(r["data"]["data"]["source"], "bla")
        self.assertEqual(r["data"]["data"]["target"], "bli")
        self.assertEqual(r["data"]["data"]["lat"], 2)
        self.assertEqual(r["data"]["id"], "kokoko")

        r = self.client.create_edge(_id,
                                    source="blu",
                                    target="blo",
                                    data={"lat": 3},
                                    name="kikiki")
        self.assertEqual(r["data"]["data"]["source"], "blu")
        self.assertEqual(r["data"]["data"]["target"], "blo")
        self.assertEqual(r["data"]["data"]["lat"], 3)
        self.assertEqual(r["data"]["id"], "kikiki")

    def test_create_nodes(self):
        topogramId = "sth"
        nodes = [{
            "x": 1,
            "y": 2,
            "id": "love",
            "name": "hihi"
        }, {
            "x": 3,
            "y": 4,
            "id": "hate",
            "name": "hoho"
        }, {
            "x": 5,
            "y": 6,
            "id": "indifference",
            "name": "haha"
        }]

        r = self.client.create_nodes(topogramId, nodes)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["data"]["id"] for d in r["data"]]),
                         set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["position"]["x"] for d in r["data"]]),
                         set([1, 3, 5]))
        self.assertEqual(set([d["position"]["y"] for d in r["data"]]),
                         set([2, 4, 6]))
        self.assertEqual(set([d["data"]["name"] for d in r["data"]]),
                         set(["hihi", "haha", "hoho"]))

    def test_create_edges(self):
        topogramId = "sth"
        edges = [{
            "source": 1,
            "target": 2,
            "id": "love",
            "name": "hihi"
        }, {
            "source": 3,
            "target": 4,
            "id": "hate",
            "name": "haha"
        }, {
            "source": 5,
            "target": 6,
            "id": "indifference",
            "name": "hoho"
        }]

        r = self.client.create_edges(topogramId, edges)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["id"] for d in r["data"]]),
                         set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["data"]["source"] for d in r["data"]]),
                         set([1, 3, 5]))
        self.assertEqual(set([d["data"]["target"] for d in r["data"]]),
                         set([2, 4, 6]))
        self.assertEqual(set([d["data"]["name"] for d in r["data"]]),
                         set(["hihi", "haha", "hoho"]))

    def test_get_edges(self):
        topogramId = "sth"
        edges = [{
            "source": 1,
            "target": 2,
            "id": "love"
        }, {
            "source": 3,
            "target": 4,
            "id": "hate"
        }, {
            "source": 5,
            "target": 6,
            "id": "indifference"
        }]
        self.client.create_edges(topogramId, edges)

        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["id"] for d in r["data"]]),
                         set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["data"]["source"] for d in r["data"]]),
                         set([1, 3, 5]))
        self.assertEqual(set([d["data"]["target"] for d in r["data"]]),
                         set([2, 4, 6]))

    def test_get_nodes(self):
        topogramId = "sth"
        nodes = [{
            "x": 1,
            "y": 2,
            "id": "love"
        }, {
            "x": 3,
            "y": 4,
            "id": "hate"
        }, {
            "x": 5,
            "y": 6,
            "id": "indifference"
        }]
        self.client.create_nodes(topogramId, nodes)

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["data"]["id"] for d in r["data"]]),
                         set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["position"]["x"] for d in r["data"]]),
                         set([1, 3, 5]))
        self.assertEqual(set([d["position"]["y"] for d in r["data"]]),
                         set([2, 4, 6]))

    def test_delete_nodes(self):
        topogramId = "sth"
        nodes = [{
            "x": 1,
            "y": 2,
            "id": "love"
        }, {
            "x": 3,
            "y": 4,
            "id": "hate"
        }, {
            "x": 5,
            "y": 6,
            "id": "indifference"
        }]
        r = self.client.create_nodes(topogramId, nodes)
        ids = [d["_id"] for d in r["data"]]

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 3)

        self.client.delete_nodes(ids)

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 0)

    def test_delete_edges(self):
        topogramId = "sth"
        edges = [{
            "source": 1,
            "target": 2,
            "id": "love"
        }, {
            "source": 3,
            "target": 4,
            "id": "hate"
        }, {
            "source": 5,
            "target": 6,
            "id": "indifference"
        }]

        r = self.client.create_edges(topogramId, edges)
        ids = [d["_id"] for d in r["data"]]

        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 3)

        self.client.delete_edges(ids)
        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 0)
 def setUpClass(self):
     # init client
     self.client = TopogramAPIClient(test_server_url, debug=True)
     self.session = session()
class TestTopogramAPIClient(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # init client
        self.client = TopogramAPIClient(test_server_url, debug=True)
        self.session = session()

    def setUp(self):

        # clean the database
        drop_database()

        # create a new user
        self.client.create_user("*****@*****.**", "password")

        # log in
        r = self.client.user_login("*****@*****.**", "password")
        print r["data"]["authToken"], r["data"]["userId"]

    def test_make_url(self):
        """URLs should be conformed, errors should be raised when passing wrong paths """

        self.assertRaises(ValueError, lambda: self.client.make_url("/start-with-slash"))
        self.assertRaises(ValueError, lambda: self.client.make_url("http://myapp.com/api"))
        self.assertEqual(self.client.make_url("lalalala"), test_server_url + "/api/lalalala")

    def test_has_active_connection(self):
        self.assertRaises(ConnectionError, lambda: TopogramAPIClient("http://*****:*****@tester.com", "password")
    #     self.assertEqual(True, False)

    def test_user_login(self):
        r = self.client.create_user("*****@*****.**", "password")
        _id = r["data"]["_id"]
        self.assertEqual(type(_id), unicode)

        # need to be logged in to delete a user
        self.assertRaises(ValueError, lambda: self.client.delete_user(_id))

    def test_create_topogram(self):
        # create with only a name
        r = self.client.create_topogram("test topogram")
        self.assertEqual(r["status"], "success")
        self.assertTrue(r["status_code"] == 200)
        self.assertEqual(r["data"]["name"], "test topogram")

    def test_make_public(self):
        r = self.client.create_topogram("test topogram")
        _id = r["data"]["_id"]
        r = self.client.makePublic(_id)
        self.assertEqual(r["data"]["sharedPublic"], 1)
        r = self.client.makePrivate(_id)
        self.assertEqual(r["data"]["sharedPublic"], 0)

    def test_get_public_topograms_list(self):
        r = self.client.create_topogram("test topogram")
        _id = r["data"]["_id"]
        r = self.client.makePublic(_id)
        r = self.client.get_public_topograms()
        self.assertEqual(len(r), 1)

    def test_get_private_topograms_list(self):
        self.client.create_topogram("wonderful isn't it?")
        self.client.create_topogram("yes, absolutely stunning")
        self.client.create_topogram("well, not so bad")
        r = self.client.get_topograms()
        self.assertEqual(len(r["data"]), 3)

    def test_get_topogram(self):
        r = self.client.create_topogram("哈哈哈")
        _id = r["data"]["_id"]
        r = self.client.get_topogram(_id)
        self.assertEqual(r["data"]["name"].encode("utf-8"), "哈哈哈")

    def test_delete_topogram(self):
        r = self.client.create_topogram("to be deleted")
        _id = r["data"]["_id"]
        r = self.client.delete_topogram(_id)
        # TODO : assert that nodes and edges have correctly disappear
        r = self.client.get_topograms()
        self.assertEqual(len(r["data"]), 0)

    def test_create_node(self):
        r = self.client.create_topogram("ok")
        topogramId = r["data"]["_id"]
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2, data={"lat": 3})
        self.assertEqual(r["data"]["data"]["id"], "jojo")
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)
        self.assertEqual(r["data"]["data"]["lat"], 3)

    def test_get_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        r = self.client.get_node(_id)
        self.assertEqual(r["data"]["data"]["id"], "jojo")
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)

    def test_update_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        self.assertEqual(r["data"]["position"]["x"], 1)
        self.assertEqual(r["data"]["position"]["y"], 2)
        r = self.client.update_node(_id, x=3, y=4, data={"starred": True})
        self.assertEqual(r["data"]["position"]["x"], 3)
        self.assertEqual(r["data"]["position"]["y"], 4)
        self.assertEqual(r["data"]["data"]["starred"], True)

    def test_delete_node(self):
        topogramId = "sth"
        r = self.client.create_node(topogramId, id="jojo", x=1, y=2)
        _id = r["data"]["_id"]
        r = self.client.delete_node(_id)
        r = self.client.get_node(_id)
        self.assertEqual(r["status"], "fail")
        self.assertIn("not found", r["message"])

    def test_create_edge(self):
        topogramId = "sth"
        r = self.client.create_edge(topogramId, "bla", "bli", data={"lat": 2}, name="kokoko")
        # _id = r["data"]["_id"]
        self.assertEqual(r["data"]["data"]["source"], "bla")
        self.assertEqual(r["data"]["data"]["target"], "bli")
        self.assertEqual(r["data"]["data"]["lat"], 2)
        self.assertEqual(r["data"]["id"], "kokoko")

    def test_update_edge(self):
        topogramId = "sth"
        r = self.client.create_edge(topogramId, "bla", "bli", data={"lat": 2}, name="kokoko")
        _id = r["data"]["_id"]
        self.assertEqual(r["data"]["data"]["source"], "bla")
        self.assertEqual(r["data"]["data"]["target"], "bli")
        self.assertEqual(r["data"]["data"]["lat"], 2)
        self.assertEqual(r["data"]["id"], "kokoko")

        r = self.client.create_edge(_id, source="blu", target="blo", data={"lat": 3}, name="kikiki")
        self.assertEqual(r["data"]["data"]["source"], "blu")
        self.assertEqual(r["data"]["data"]["target"], "blo")
        self.assertEqual(r["data"]["data"]["lat"], 3)
        self.assertEqual(r["data"]["id"], "kikiki")

    def test_create_nodes(self):
        topogramId = "sth"
        nodes = [
            {"x": 1, "y": 2, "id": "love", "name": "hihi"},
            {"x": 3, "y": 4, "id": "hate", "name": "hoho"},
            {"x": 5, "y": 6, "id": "indifference", "name": "haha"},
        ]

        r = self.client.create_nodes(topogramId, nodes)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["data"]["id"] for d in r["data"]]), set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["position"]["x"] for d in r["data"]]), set([1, 3, 5]))
        self.assertEqual(set([d["position"]["y"] for d in r["data"]]), set([2, 4, 6]))
        self.assertEqual(set([d["data"]["name"] for d in r["data"]]), set(["hihi", "haha", "hoho"]))

    def test_create_edges(self):
        topogramId = "sth"
        edges = [
            {"source": 1, "target": 2, "id": "love", "name": "hihi"},
            {"source": 3, "target": 4, "id": "hate", "name": "haha"},
            {"source": 5, "target": 6, "id": "indifference", "name": "hoho"},
        ]

        r = self.client.create_edges(topogramId, edges)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["id"] for d in r["data"]]), set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["data"]["source"] for d in r["data"]]), set([1, 3, 5]))
        self.assertEqual(set([d["data"]["target"] for d in r["data"]]), set([2, 4, 6]))
        self.assertEqual(set([d["data"]["name"] for d in r["data"]]), set(["hihi", "haha", "hoho"]))

    def test_get_edges(self):
        topogramId = "sth"
        edges = [
            {"source": 1, "target": 2, "id": "love"},
            {"source": 3, "target": 4, "id": "hate"},
            {"source": 5, "target": 6, "id": "indifference"},
        ]
        self.client.create_edges(topogramId, edges)

        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["id"] for d in r["data"]]), set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["data"]["source"] for d in r["data"]]), set([1, 3, 5]))
        self.assertEqual(set([d["data"]["target"] for d in r["data"]]), set([2, 4, 6]))

    def test_get_nodes(self):
        topogramId = "sth"
        nodes = [{"x": 1, "y": 2, "id": "love"}, {"x": 3, "y": 4, "id": "hate"}, {"x": 5, "y": 6, "id": "indifference"}]
        self.client.create_nodes(topogramId, nodes)

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 3)
        self.assertEqual(set([d["data"]["id"] for d in r["data"]]), set(["love", "hate", "indifference"]))
        self.assertEqual(set([d["position"]["x"] for d in r["data"]]), set([1, 3, 5]))
        self.assertEqual(set([d["position"]["y"] for d in r["data"]]), set([2, 4, 6]))

    def test_delete_nodes(self):
        topogramId = "sth"
        nodes = [{"x": 1, "y": 2, "id": "love"}, {"x": 3, "y": 4, "id": "hate"}, {"x": 5, "y": 6, "id": "indifference"}]
        r = self.client.create_nodes(topogramId, nodes)
        ids = [d["_id"] for d in r["data"]]

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 3)

        self.client.delete_nodes(ids)

        r = self.client.get_nodes(topogramId)
        self.assertEqual(len(r["data"]), 0)

    def test_delete_edges(self):
        topogramId = "sth"
        edges = [
            {"source": 1, "target": 2, "id": "love"},
            {"source": 3, "target": 4, "id": "hate"},
            {"source": 5, "target": 6, "id": "indifference"},
        ]

        r = self.client.create_edges(topogramId, edges)
        ids = [d["_id"] for d in r["data"]]

        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 3)

        self.client.delete_edges(ids)
        r = self.client.get_edges(topogramId)
        self.assertEqual(len(r["data"]), 0)
    if len(existing_edges):
        topogram.delete_edges([n["_id"] for n in existing_edges])
        print "%s edges deleted" % len(existing_edges)

    r = topogram.create_nodes(topogram_ID, nodes)
    print r
    print "%s nodes created." % len(r["data"])
    r = topogram.create_edges(topogram_ID, edges)
    print "%s edges created." % len(r["data"])

    print "done. Topogram has been updated. Check it at %s/topograms/%s" % (
        TOPOGRAM_URL, topogram_ID)


# connect to the topogram instance (pass debug=True params for more info )
topogram = TopogramAPIClient(TOPOGRAM_URL)  #, debug=True)

# create a new user
try:
    topogram.create_user(USER, PASSWORD)
except ValueError:
    print "> User has already been created."

# login a new user if needed
resp_user_login = topogram.user_login(USER, PASSWORD)
print resp_user_login

assert (resp_user_login["status"] == "success")
assert (resp_user_login["status_code"] == 200)

create_topogram("Geo-time network", my_nodes, my_edges)