def init_database(): ''' 若数据库还未初始化, 则初始化数据库, 数据库中应人为保证没有名为roles和users的表, 才能完成初始化 若数据库已经初始化, 则什么都不会发生 ''' from manage import db db.reflect() all_table = { table_obj.name: table_obj for table_obj in db.get_tables_for_bind() } try: str(all_table['roles']) str(all_table['users']) except KeyError: from db_models.user_model import User from db_models.role_model import Role db.drop_all() db.create_all() role1 = Role(name='admin') role2 = Role(name='ordinary_user') db.session.add_all([role1, role2]) db.session.commit() user1 = User(name='hshs', password='******', role_id=role1.id) user2 = User(name='hkhk', password='******', role_id=role1.id) user3 = User(name='郑某人', password='******', role_id=role2.id) user4 = User(name='王维', password='******', role_id=role2.id) db.session.add_all([user1, user2, user3, user4]) db.session.commit() print('新建了roles和users表') else: print('已存在roles和users表')
def setUp(self): password = generate_password_hash('password') challonge_api_key = xor_crypt_string('challonge123', encode=True) test_user = User('testuser', password, '*****@*****.**', 'testuser', challonge_api_key) self.test_user = test_user db.drop_all() db.create_all() db.session.add(self.test_user) db.session.commit() valid_credentials = base64.b64encode(b'testuser:password').decode( 'utf-8') response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(response.data) tk_valid_user = returned['token'] tk_invalid = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTV9.LZaaRkg7THSCD-8VDtjX43Wxn5gKktR6m8DJQDH2SpM' self.headers = { 'Content-Type': 'application/json', 'x-access-token': tk_valid_user } self.badheaders = { 'Content-Type': 'application/json', 'x-access-token': tk_invalid }
def test_adding_augmented_copa_record_to_db_no_category_matches(self): copa_split_csv = os.path.join(IFTestBase.resource_directory, 'copa_scraped_split.csv') df = pd.read_csv(copa_split_csv) db.drop_all() db.create_all(bind=COPA_DB_BIND_KEY) augmented = Augment().get_augmented_copa_data(copa_split_csv) assert_frame_equal(augmented, df) assert len(augmented) == len(df)
def setUp(self): password = generate_password_hash('password') test_user = User('testuser', password, '*****@*****.**', 'matchuptesting', challonge_api_key) self.test_user = test_user db.drop_all() db.create_all() db.session.add(self.test_user) db.session.commit()
def test_copa_scrape_integration(self, get_copa_data_demographics): with patch.object(StorageFactory, 'get_storage') as storage_mock, \ patch('invisible_flow.app.scrape_data') as scrape_mock: scrape_mock.return_value = get_copa_data_demographics storage_mock.return_value = LocalStorage() db.session.close() db.drop_all() db.create_all(COPA_DB_BIND_KEY) self.initialize_database(db) copa_scrape() match_data_file_contents = LocalStorage().get('match_data.csv', "COPA_SCRAPE-2019-03-25_05-30-50") new_allegation_file_contents = LocalStorage().get('new_allegation_data.csv', "COPA_SCRAPE-2019-03-25_05-30-50") new_officer_unknown_file_contents = LocalStorage().get('new_officer_unknown.csv', "COPA_SCRAPE-2019-03-25_05-30-50") new_officer_allegation_file_contents = LocalStorage().get('new_officer_allegation.csv', "COPA_SCRAPE-2019-03-25_05-30-50") expected_match_data_file_contents = open(os.path.join(IFTestBase.resource_directory, 'expected_match_copa_data.csv')).read() expected_new_allegation_data = open(os.path.join(IFTestBase.resource_directory, 'expected_new_allegation_data.csv')).read() expected_new_officer_unknown_data = open(os.path.join(IFTestBase.resource_directory, 'expected_new_officer_unknown.csv')).read() expected_new_officer_allegation_data = open(os.path.join(IFTestBase.resource_directory, 'expected_new_officer_allegation.csv')).read() entry_from_db = DataAllegation.query.get('1087387') number_of_rows_in_db = DataAllegation.query.count() # tests > helpers > resources (can find csv contents used in these tests # first two asserts; beat_ids were added to expected files in resource folder # new_data & match_data should show up in local_upload_folder assert (match_data_file_contents == expected_match_data_file_contents) assert (new_allegation_file_contents == expected_new_allegation_data) assert (new_officer_unknown_file_contents == expected_new_officer_unknown_data) assert (new_officer_allegation_file_contents == expected_new_officer_allegation_data) assert (entry_from_db is not None) assert (number_of_rows_in_db == 151) # ^was bumped up +1 due to added entry in copa_scraped_demographics.csv when checking test with new data local_upload_dir = LocalStorage().local_upload_directory os.remove(os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'match_data.csv')) os.remove(os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'new_allegation_data.csv')) os.remove(os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'new_officer_allegation.csv')) os.remove(os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'new_officer_unknown.csv')) os.rmdir(os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50"))
def test_copa_scrape_integration(self, get_copa_data_demographics): with patch.object(StorageFactory, 'get_storage') as storage_mock, \ patch('invisible_flow.app.scrape_data') as scrape_mock: scrape_mock.return_value = get_copa_data_demographics storage_mock.return_value = LocalStorage() db.session.close() db.drop_all() db.create_all(COPA_DB_BIND_KEY) self.initialize_database(db) copa_scrape() match_data_file_contents = LocalStorage().get( 'match_data.csv', "COPA_SCRAPE-2019-03-25_05-30-50") new_data_file_contents = LocalStorage().get( 'new_data.csv', "COPA_SCRAPE-2019-03-25_05-30-50") expected_match_data_file_contents = open( os.path.join(IFTestBase.resource_directory, 'expected_match_copa_data.csv')).read() expected_new_data_file_contents = open( os.path.join(IFTestBase.resource_directory, 'expected_new_copa_data.csv')).read() entry_from_db = DataAllegation.query.get('1087387') number_of_rows_in_db = DataAllegation.query.count() assert (new_data_file_contents == expected_new_data_file_contents) assert ( match_data_file_contents == expected_match_data_file_contents) assert (entry_from_db is not None) assert (number_of_rows_in_db == 150) local_upload_dir = LocalStorage().local_upload_directory os.remove( os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'match_data.csv')) os.remove( os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50", 'new_data.csv')) os.rmdir( os.path.join(local_upload_dir, "COPA_SCRAPE-2019-03-25_05-30-50"))
def client(): app = make_app("test") app.app_context().push() app.test_client_class = TestClient client = app.test_client() with app.app_context(): db.create_all() seed.main() yield client db.session.remove() db.drop_all()
def setUp(self): self.app = app # initialize the test client self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() email = '*****@*****.**' self.user = { 'username': '******', 'password': '******', 'email': email } self.user1 = { 'username': '******', 'password': '******', 'email': email } self.client.post('/api/auth/logout', content_type='application/json') self.client.post('/api/auth/register', data=json.dumps(self.user), content_type='application/json') self.user.pop('email') res = self.client.post('/api/auth/login', data=json.dumps(self.user), content_type='application/json') data = json.loads(res.data.decode()) self.token = data['token'] self.business = { 'name': 'business', 'location': 'kampala', 'category': 'technology', 'description': 'service providers for repair of computers' } self.business1 = { 'name': 'business1', 'location': 'mukono', 'category': 'bio-tech', 'description': 'service providers of bio-tech chemicals' }
def setUp(self): password = generate_password_hash('password') test_user = User('testuser', password, '*****@*****.**', 'matchuptesting', challonge_api_key) self.test_user = test_user db.drop_all() db.create_all() db.session.add(self.test_user) db.session.commit() valid_credentials = base64.b64encode(b'testuser:password').decode( 'utf-8') response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(response.data) self.tk_valid_user = returned['token'] self.headers = { 'Content-Type': 'application/json', 'x-access-token': self.tk_valid_user } challonge.set_credentials( self.test_user.challonge_username, xor_crypt_string(self.test_user.api_key, decode=True)) challonge.tournaments.reset(bracket_1_id) challonge.tournaments.start(bracket_1_id) event_data = { 'event_name': 'Test Event', 'brackets': [{ 'bracket_id': bracket_1_id, 'number_of_setups': 0 }, { 'bracket_id': bracket_2_id, 'number_of_setups': 0 }] } response = self.client.post(EVENT_URL, json=event_data, headers=self.headers) self.event = Event.query.get(json.loads(response.data)['id']) self.matches_available_bracket_1 = challonge.matches.index( bracket_1_id, state='open') self.match_to_test = self.matches_available_bracket_1[0]
def setUp(self): self.app=app # initialize the test client self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() self.user = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } self.user_login = { 'username': '******', 'password': '******', } self.business={ 'name':'business', 'location':'kampala', 'category':'technology', 'description':'service providers for repair of computers' } self.client.post('/api/auth/logout', content_type='application/json') self.client.post('/api/auth/register', data=json.dumps(self.user), content_type='application/json') res = self.client.post('/api/auth/login', data=json.dumps(self.user_login), content_type='application/json') data = json.loads(res.data.decode()) self.token = data['token'] self.client.post('/api/businesses', data=json.dumps(self.business), headers={'x-access-token': self.token}) self.review= { 'review': 'test review for a business' } # db.session.close()
def setUp(self): self.app=app # initialize the test client self.client = self.app.test_client() with self.app.app_context(): db.drop_all() db.create_all() self.client.post('api/') email = '*****@*****.**' self.user = { 'username': '******', 'password': '******', 'email': email } self.user1 = { 'username': '******', 'password': '******', 'email': email }
def setUp(self): db.drop_all() db.create_all() password = generate_password_hash('password') test_user = User('testuser', password, '*****@*****.**', 'matchuptesting', challonge_api_key) self.test_user = test_user db.session.add(self.test_user) db.session.commit() valid_credentials = base64.b64encode(b'testuser:password').decode( 'utf-8') response = self.client.post( LOGIN_URL, headers={'Authorization': 'Basic ' + valid_credentials}) returned = json.loads(response.data) self.tk_valid_user = returned['token'] self.headers = { 'Content-Type': 'application/json', 'x-access-token': self.tk_valid_user } event_data = { 'event_name': 'Test Event', 'brackets': [{ 'bracket_id': bracket_1_id, 'number_of_setups': 4 }, { 'bracket_id': bracket_2_id, 'number_of_setups': 5 }] } response = self.client.post(CREATE_EVENT_URL, json=event_data, headers=self.headers) self.test_event = Event.query.get(json.loads(response.data)['id'])
def get_db(self): db.session.close() db.drop_all() db.create_all(bind=COPA_DB_BIND_KEY) yield db
def tearDown(self): db.session.remove() db.drop_all(app=self.app_t)
def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop()
def tearDown(self): db.session.remove() db.drop_all()
def set_up(self): db.session.close() db.drop_all() db.create_all(bind=COPA_DB_BIND_KEY)
def tearDown(self): """Drops the db.""" db.session.remove() db.drop_all()
def tearDown(self): """teardown all initialized variables.""" with self.app.app_context(): # drop all tables db.session.remove() db.drop_all()
def tearDown(self): db.session.remove() db.drop_all() challonge.tournaments.reset(bracket_1_id) challonge.tournaments.start(bracket_1_id)
def set_up(self): db.session.close() db.drop_all() db.create_all()
def tearDown(self): db.session.close() db.drop_all()
#!/usr/bin/env python # coding=utf-8 from manage import db import app.domain.model db.drop_all() db.create_all()