def test_create_competition(self): company = CompanyModel('GOL') self.assertEqual( company.name, 'GOL', "The name of the company after creation does not equal the constructor argument." )
def test_delete_company(self): with self.app() as c: with self.app_context(): CompanyModel('Azul').save_to_db() r = c.delete('/company/Azul') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Company deleted'}, d2=json.loads(r.data.decode('utf-8')))
def test_competition_json(self): company = CompanyModel('GOL') expected = { 'id': company.id, 'name': company.name, } self.assertEqual( company.json(), expected, "The JSON export of the company is incorrect. Received {}, expected {}." .format(company.json(), expected))
def post(self, name): if CompanyModel.find_by_name(name): return {'message': "A store with name '{}' already exists.".format(name)}, 400 company = CompanyModel(name) try: company.save_to_db() except: return {"message": "An error occurred creating the store."}, 500 return company.json(), 201
def test_company_found(self): with self.app() as c: with self.app_context(): CompanyModel('Azul').save_to_db() r = c.get('/company/Azul') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'company': { 'id': 1, 'name': 'Azul' }}, d2=json.loads(r.data.decode('utf-8')))
def post(self): data = self.parser.parse_args() companyName = data['companyName'] if CompanyModel.find_by_name(companyName): return { 'message': "An item with name '{}' already exists.".format(companyName) }, 400 company = CompanyModel(companyName, **data) try: company.save_to_db() except: return { "message": "An error occurred while inserting the item." }, 500 return company.json(), 201
def mutate(root, info, company_data, user_id): name_check = CompanyModel.find_by_name(company_data.name) if name_check: raise Exception("Company already exist!") user = User.find_by_id(user_id) if user.company: raise Exception("User already belongs to a company!") owner_role = EmbeddedRole(name="owner", group=0, priority_level=0) company = CompanyModel(**company_data) company.roles.append(owner_role) company.save() user.role.append(owner_role) user.company = company user.save() return NewCompany(ok=True, company=company)
def test_crud(self): with self.app_context(): competition = CompanyModel('AVIANCA') self.assertIsNone( CompanyModel.find_by_name('AVIANCA').first(), "Found an competition with name 'AVIANCA' before save_to_db") competition.save_to_db() self.assertIsNotNone( CompanyModel.find_by_name('AVIANCA').first(), "Did not find a competition with name 'AVIANCA' after save_to_db" ) competition.delete_from_db() self.assertIsNone( CompanyModel.find_by_name('AVIANCA').first(), "Found an competition with name 'AVIANCA' after delete_from_db" )
def post(self, us_employer_id): if CompanyModel.find_by_us_employer_id(us_employer_id): return { "message": "A company already exists with the same employer ID, use a different one!" }, 400 data = Company.parser.parse_args() is_valid, error_message = CompanyModel.check_if_data_has_valid_format( us_employer_id, **data) if is_valid: new_company = CompanyModel(us_employer_id, **data) try: format_company_to_json(new_company) new_company.save_to_db() return format_company_to_json(new_company), 201 except: return { "message": "An error occurred while creating the user!" }, 500 else: return {"message": error_message}, 400
def post(self): data = Company.parser.parse_args() company_model = CompanyModel(data['name']) result = company_model.insert() return result