def testCreateTabledef(self): """Checks if our TestModel can be converted into a valid SQL table.""" helpers.create_tables([TestModel()], self.connection) cursor = self.connection.cursor() cursor.execute( "INSERT INTO TestModel(string_text,int64_number) " "VALUES ('test', 13)")
def testQueryOnNonExistentColumn(self): helpers.create_tables([TestModel()], self.connection) model = TestModel(text='t1', number=13) model.put() data = TestModel.gql( 'WHERE text2=:1 and number=:2 order by text desc', 't1', 13).fetch(5) self.assertEquals(0, len(data))
def testGetOrInsert(self): """tests if get_or_insert works correctly""" helpers.create_tables([TestModel()], self.connection) model1 = TestModel(number=1) model1.put() model2 = TestModel.get_or_insert('foo', number=13, text='t') self.assertEquals(13, model2.number) fetched = TestModel.get_by_key_name('foo') self.assertEquals(13, fetched.number)
def testTypeChange(self): """Tests what happens if a field changes its type.""" helpers.create_tables([TestModel()], self.connection) class Mutation(db.Model): @classmethod def kind(cls): return 'TestModel' text = db.IntegerProperty(default=42) model = Mutation(text=23) model.put()
def testAddedField(self): """Tests what happens if a field gets added to a model.""" helpers.create_tables([TestModel()], self.connection) class Mutation(TestModel): @classmethod def kind(cls): return 'TestModel' text2 = db.StringProperty(default='some more text') model = Mutation(text='Text 1', text2='Text 2') model.put()
def testSimpleQuery(self): helpers.create_tables([TestModel()], self.connection) model = TestModel(text='t1', number=13) model.put() data = TestModel.gql( 'WHERE text=:1 and number=:2 order by text desc', 't1', 13).fetch(5) self.assertEquals(1, len(data)) self.assertEquals('t1', data[0].text) self.assertEquals(13, data[0].number)
def init_database(conn): """ Passing in a connection, initialize the database with somewhat randomized data. Arguments: - conn - postgres connection object """ schema = input("Input a schema name to be created: ") create_schema(conn, schema) create_tables(conn, schema) generate_data(conn, schema)
def testGetSingleElementByCustomKey(self): """Gets a single model from the datastore with a string key.""" helpers.create_tables([TestModel()], self.connection) model1 = TestModel(number=1) model2 = TestModel(key_name='custom', number=2, text='#2') model3 = TestModel(number=3) key1 = model1.put() key2 = model2.put() key3 = model3.put() fetched = TestModel.get_by_key_name('custom') self.assertEquals(2, fetched.number) self.assertEquals('#2', fetched.text)
def main(organization: str, repository: str, start_date: str, end_date: str) -> None: """ end_date takes in additional timedelta(hours = 24), since we're interested in end_date's results as well (inclusively). """ if start_date > end_date: raise ValueError("Start date needs to be earlier than End date") config = configparser.ConfigParser() config.read('configs.cfg') commits_info, authors_info = get_data(organization, repository, start_date, end_date) # Connect to Postgres conn = psycopg2.connect( "host={} dbname={} user={} password={} port={}".format( *config['POSTGRES'].values())) cur = conn.cursor() drop_tables(cur, conn) create_tables(cur, conn) print(f"Commits count: {len(commits_info)}") insert_to_table(cur, conn, authors_table_insert, authors_info) # type: ignore insert_to_table(cur, conn, commits_table_insert, commits_info) # type: ignore insight_1_results = generate_insight(cur, conn, insight_1_query) print(f'Insight 1 (Top 3 Authors):\n \ 1st: {insight_1_results[0][1]}({insight_1_results[0][0]}) -> {insight_1_results[0][2]} commits\n \ 2nd: {insight_1_results[1][1]}({insight_1_results[1][0]}) -> {insight_1_results[1][2]} commits\n \ 3rd: {insight_1_results[2][1]}({insight_1_results[2][0]}) -> {insight_1_results[2][2]} commits\n' ) insight_2_results = generate_insight(cur, conn, insight_2_query) print(f'Insight 2 (Author with longest contribution window):\n \ {insight_2_results[0][0]} -> {insight_2_results[0][1]} hours\n') insight_3_results = generate_insight(cur, conn, insight_3_query) insight_3_temp_df = pd.DataFrame(insight_3_results, columns=['date']) print( f'Insight 3 (Heatmap of Github commits): Heatmap image generate to /output directory.' ) insight_3_df = get_insight_3_df(insight_3_temp_df) insight_3_df_pivot = insight_3_df.pivot("day", "time_interval", "total_commits") # Generate heatmap generate_heatmap(insight_3_df_pivot, organization, repository)
def testGetSchema(self): helpers.create_tables([TestModel()], self.connection) prm = datastore_sqlite_stub.PRMHelper(None, None) schema = prm.getSchema(self.connection, 'TestModel') assert schema self.assertEquals({ 'text': ['string_text'], 'float': ['double_float'], 'cond1': ['boolean_cond1'], 'cond2': ['boolean_cond2'], 'number': ['int64_number']}, schema) no_schema = prm.getSchema(self.connection, 'No_Model') assert no_schema is None
def testGetMultipleElements(self): """Gets a several models from the datastore.""" helpers.create_tables([TestModel()], self.connection) model1 = TestModel(number=1) model2 = TestModel(number=2) model3 = TestModel(number=3) key1 = model1.put() key2 = model2.put() key3 = model3.put() fetched = TestModel.get([key2, key1]) self.assertEquals(2, len(fetched)) self.assertEquals(2, fetched[0].number) self.assertEquals(1, fetched[1].number)
def testWriteDouble(self): """Writes a value into the database twice.""" helpers.create_tables([TestModel()], self.connection) model = TestModel() key = model.put() id = key._Key__reference.path().element_list()[-1].id() key = model.put() id2 = key._Key__reference.path().element_list()[-1].id() self.assertEquals(id, id2) cursor = self.connection.cursor() cursor.execute( 'SELECT COUNT(*) FROM TestModel WHERE pk_int=%s' % id) result = cursor.fetchone() self.assertEquals(1, result[0])
def testGetSingleElement(self): """Gets a single model from the datastore.""" helpers.create_tables([TestModel()], self.connection) model1 = TestModel(number=1) model2 = TestModel(number=2, text='#2') model3 = TestModel(number=3) key1 = model1.put() key2 = model2.put() key3 = model3.put() fetched = TestModel.get(key2) self.assertEquals(2, fetched.number) self.assertEquals('#2', fetched.text) self.assertEquals(3.14, fetched.float) self.assertEquals(True, fetched.cond1) self.assertEquals(False, fetched.cond2)
def testWriteSingle(self): """Writes a single model to the database retrieves it.""" helpers.create_tables([TestModel()], self.connection) model = TestModel() key = model.put() id = key._Key__reference.path().element_list()[-1].id() cursor = self.connection.cursor() cursor.execute( 'SELECT string_text, int64_number, double_float, ' 'boolean_cond1, boolean_cond2 FROM TestModel ' 'WHERE pk_int=%s' % id) result = cursor.fetchone() self.assertEquals('some text', result[0]) self.assertEquals(42, result[1]) self.assertEquals(3.14, result[2]) self.assertEquals(1, result[3]) self.assertEquals(0, result[4])
if not os.getenv("DATABASE_URL"): raise RuntimeError("DATABASE_URL is not set") # Configure session to use filesystem app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" app.config['JSON_SORT_KEYS'] = False # Set up database connection engine = create_engine(os.getenv("DATABASE_URL"), connect_args={"application_name": "application.py", "sslmode":"require"}) db = scoped_session(sessionmaker(bind=engine)) # Create tables if not exists create_tables(db) # NOTE: Run import.py to populate books table # TODO: Add timestamp column to reviews table # TODO: Optional: Handle search routing with query parameters # Routing @app.route("/api/<string:isbn>", methods=["GET"]) def isbn(isbn): # Select book from database book_statement = text("SELECT isbn, title, author, year FROM books WHERE isbn = :isbn") book_statement = book_statement.bindparams(isbn=isbn) # Assume that ISBN is unique
def setUp(self): create_tables() super().setUp()