def test_connection(): data = request.get_json() logger.info("Data recieved: %s", data) if "name" not in data: msg = "No name provided for connection." logger.info(msg) return create_response(status=422, message=msg) if "user_id" not in data: msg = "No user_id provided for connection." logger.info(msg) return create_response(status=422, message=msg) if "comment" not in data: data["comment"] = "" if ("type" not in data): msg = "No type provided for connection." logger.info(msg) return create_response(status=422, message=msg) # create SQLAlchemy Object if data["type"] == "postgres": new_conn = Connection( name= data["name"], type= data["type"], params= data["params"], user_id = data["user_id"], comment = data["comment"] ) try: new_conn.start_connection() return create_response(message=f"Test OK!") except: return create_response(status=422, message="FAILED")
class TestConnectionModel(TestCase): def setUp(self): self.connection = Connection(name="Mysql", type="rdbms") self.connection.save() def test_connection_creation(self): self.assertEqual(Connection.objects.count(), 1) def test_connection_representation(self): self.assertEqual(self.connection.name, str(self.connection))
def create_connection(): data = request.get_json() logger.info("Data recieved: %s", data) if "name" not in data: msg = "No name provided for connection." logger.info(msg) return create_response(status=422, message=msg) if "user_id" not in data: msg = "No user_id provided for connection." logger.info(msg) return create_response(status=422, message=msg) if "comment" not in data: data["comment"] = "" if ("type" not in data): msg = "No type provided for connection." logger.info(msg) return create_response(status=422, message=msg) # create SQLAlchemy Object new_conn = Connection( name= data["name"], type= data["type"], params= data["params"], user_id = data["user_id"], comment = data["comment"] ) # commit it to database db.session.add(new_conn) db.session.commit() db.session.add(Access(user_id = data["user_id"], model = 'connection', model_id = new_postgres.id, level = 'admin')) db.session.commit() return create_response( message=f"Successfully created connection {new_conn.name} with id: {new_conn.id}" )
class TestNoteApi(APITestCase): def setUp(self): # create connection self.connection = Connection(name="MS sql", type="rdbms") self.connection.save() def test_connection_creation(self): response = self.client.post(reverse('connections'), { 'name': 'Mongo', 'type': "nosql" }) # assert new connection was added self.assertEqual(Connection.objects.count(), 2) # assert a created status code was returned self.assertEqual(201, response.status_code) def test_getting_connections(self): response = self.client.get(reverse('connections'), format="json") self.assertEqual(len(response.data), 1) def test_updating_connection(self): response = self.client.put(reverse('detail', kwargs={'pk': 1}), { 'name': 'redis', 'type': "nosql" }, format="json") # check info returned has the update self.assertEqual('redis', response.data['name']) def test_deleting_connection(self): response = self.client.delete(reverse('detail', kwargs={'pk': 1})) self.assertEqual(204, response.status_code)
def setUp(self): self.connection = Connection(name="Mysql", type="rdbms") self.connection.save()
def init_data(): query_params = request.args user_id = query_params.get('user') if (not(user_id)): msg = "No user provided for init." logger.info(msg) return create_response(status=422, message=msg) # return user_id # #Users # dummyusers =[] # dummyusers.append(User(name="Aragorn", email="*****@*****.**", password="******")) # dummyusers.append(User(name="Gandalf", email="*****@*****.**", password="******")) # db.session.add_all(dummyusers) # db.session.commit() #Connections dummyconnections =[] postgres1 = Connection( name="Aragorn's Connection 1", params={ 'host' : "AragornStrider.gondor.com", 'database' : "Gondor", 'username' : "userr", 'password' : "password", 'port' : 5432}, comment = "", type="postgres", user_id = user_id ) postgres2 = Connection( name = "Real Connection", params={ 'host' : "drona.db.elephantsql.com", 'database' : "uvqhwsnn", 'username' : "uvqhwsnn", 'password' : "mzwjhs6qcqZHTm-ecCXJkQ3FoLViB9RT", 'port' : 5432}, comment = "Conexión real! Base Northwind", type="postgres", user_id = user_id ) dummyconnections.append(postgres1) dummyconnections.append(postgres2) # dummyconnections.append(postgres3) db.session.add_all(dummyconnections) db.session.commit() #Sentences sentence1 = Sentence(name="Sentence 1", sql_query="SELECT * FROM people WHERE kingdom = 'Rohan'", connection_id = 1, comment = "Keeping tabs on the Rohirrim", visual_query_params = {'selected_tables': ['a']}) sentence2 = Sentence(name="Sentence 2", sql_query="SELECT * FROM Customers", connection_id = 2, comment = "Keeping tabs on Customers", visual_query_params = {'selected_tables': ['a']}) sentence3 = Sentence( name="Sentence 3 - using GROUP", sql_query="SELECT city, COUNT(*) AS customers, (COUNT(*)*1.0/2)::float AS customershalf FROM Customers GROUP BY city", connection_id = 2, comment = "Customers we have in each city", visual_query_params = { 'selected_tables': ['a'] } ) db.session.add(sentence1) db.session.add(sentence2) db.session.add(sentence3) # db.session.commit() #Visualizations visual1 = Visualization( name="Line Chart 1 - black", sentence_id = 3, type = "Line chart", comment = "Based on the real Sentence on the real Connection - 1", params= { 'columns':[ {'name': 'city'}, {'name': 'customers', 'color': '#FF0000', 'legend': 'Customers'} ], 'xaxis_label':'City', 'yaxis_label':'Number of Customers', 'legend':True } ) visual2 = Visualization( name="Bars, Bars, Bars!", type="Bar chart", sentence_id = 3, comment = "Based on the real Sentence on the real Connection", params= { 'columns':[ {'name': 'city'}, {'name': 'customers', 'color': 'MidnightBlue', 'legend': 'Active'} ], 'xaxis_label':'City', 'yaxis_label':'Number of Customers', 'legend':True } ) db.session.add(visual1) db.session.add(visual2) # db.session.commit() #Dashboards dash1 = Dashboard(name="Dashboard 1", comment = "No comment - 1") dash1.visualizations.append(DashboardsVisualizations(visualization = visual1, order = 2)) dash2 = Dashboard(name="Dashboard 2", comment = "Dashboard with no comment - 2") db.session.add(dash1) db.session.add(dash2) #Access access_load = [] a1 = Access(user_id=user_id, model='connection', model_id=1, level='admin') access_load.append(a1) access_load.append(Access(user_id=user_id, model='connection', model_id=2, level='admin')) access_load.append(Access(user_id=user_id, model='sentence', model_id=1, level='admin')) access_load.append(Access(user_id=user_id, model='sentence', model_id=2, level='admin')) access_load.append(Access(user_id=user_id, model='sentence', model_id=3, level='admin')) access_load.append(Access(user_id=user_id, model='visualization', model_id=1, level='admin')) access_load.append(Access(user_id=user_id, model='visualization', model_id=2, level='admin')) access_load.append(Access(user_id=user_id, model='dashboard', model_id=1, level='admin')) access_load.append(Access(user_id=user_id, model='dashboard', model_id=2, level='admin')) db.session.add_all(access_load) db.session.commit() msg = "Data has been initialized :D" return create_response(status=200, message=msg)
def setUp(self): # create connection self.connection = Connection(name="MS sql", type="rdbms") self.connection.save()