def test_serializeML(self): ml = FizzBuzzML(int1=1, int2=2, mlimit=4, str1='un', str2='deux') jsonToCompareTo = { 'int1': 1, 'int2': 2, 'limit': 4, 'str1': 'un', 'str2': 'deux' } jsonSerialized = ml.serialize() self.assertEqual(jsonToCompareTo, jsonSerialized)
def test_InsertUsersRequestDBDown(self): fzapi.db.drop_all() ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='un', str2='deux') fzRq = FizzBuzzRQ() res = fzRq.insertUsersRequest(ml) self.assertTrue('sqlite3.OperationalError' in res) fzapi.db.session.rollback()
def test_InsertUsersRequestALot(self): ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='un', str2='deux') fzRq = FizzBuzzRQ() for i in range(10): fzRq.insertUsersRequest(ml) insList = FizzBuzzML.query.all() self.assertEqual(len(insList), 10)
def test_constructML(self): ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='un', str2='deux') self.assertEqual(ml.int1, 1) self.assertEqual(ml.int2, 2) self.assertEqual(ml.mlimit, 3) self.assertEqual(ml.str1, 'un') self.assertEqual(ml.str2, 'deux')
def test_ctorMLWostr2(self): raised = None try: ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='un') except Exception as e: raised = e self.assertIsInstance(raised, TypeError)
def test_processFizzbuzzOk(self): ml = FizzBuzzML(int1=3, int2=5, mlimit=15, str1='m3', str2='m5') lc = FizzBuzzLC() compareTo = [ 1, 2, 'm3', 4, 'm5', 'm3', 7, 8, 'm3', 'm5', 11, 'm3', 13, 14, 'm3m5' ] self.assertSequenceEqual(lc.processFizzbuzz(ml), compareTo)
def test_InsertUsersRequestStr1(self): ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='un', str2='deux') fzRq = FizzBuzzRQ() fzRq.insertUsersRequest(ml) insList = FizzBuzzML.query.all() self.assertEqual(insList[0].int1, 1) self.assertEqual(insList[0].int2, 2) self.assertEqual(insList[0].mlimit, 3) self.assertEqual(insList[0].str1, 'un') self.assertEqual(insList[0].str2, 'deux')
def test_GetTopUsersRequestsNum(self): self.test_InsertUsersRequestALot() ml = FizzBuzzML(int1=2, int2=4, mlimit=6, str1='trois', str2='six') fzRq = FizzBuzzRQ() for i in range(11): fzRq.insertUsersRequest(ml) insList = FizzBuzzML.query.all() self.assertEqual(len(insList), 21) num, rq = fzRq.getTopUsersRequests() self.assertEqual(num, 11)
def get(self): """ Computes fizzbuzz algorithm Returns the list of fizzbuzz processed """ fzquery = FizzBuzzML(request.args.get('int1', None, int), request.args.get('int2', None, int), request.args.get('limit', None, int), request.args.get('str1', None, str), request.args.get('str2', None, str)) fzlogic = FizzBuzzLC() success, result, errStr = fzlogic.compute(fzquery) if success: #insert into db fzRq = FizzBuzzRQ() fzRq.insertUsersRequest(fzquery) return {'success': success, 'result': result}, 200 else: return {'success': success, 'error': errStr}, 400
def insertUsersRequest(self, request): """ Inserts a new fizzbuzz query Inserts a new row in fizzbuzz table, all required fields must be checked before Args: request: a FizzbuzzML model object that holds all user defined fields """ fzquerydb = FizzBuzzML(int1=request.int1 , int2=request.int2 , mlimit=request.mlimit , str1=request.str1 , str2=request.str2) try: db.session.add(fzquerydb) db.session.commit() except Exception as e: logging.error('Cannot insert into database {0}'.format(str(e))) return str(e) # only used in test
def getTopUsersRequests(self): """ Fetches the most frequent query done by users check the fizzbuzz table to get the most frequent row Returns: A tuple containing, first :how many time the most queryed fizzbuzz had, second : what the most queryed fizzbuzz is """ try: cnt = func.count('*') result = db.session.query(cnt, FizzBuzzML.int1, FizzBuzzML.int2, FizzBuzzML.mlimit, FizzBuzzML.str1, FizzBuzzML.str2).\ group_by(FizzBuzzML.int1, FizzBuzzML.int2, FizzBuzzML.mlimit, FizzBuzzML.str1, FizzBuzzML.str2).\ order_by(cnt.desc()).\ limit(1).all() if len(result) == 1 and len(result[0]) == 6: return result[0][0], FizzBuzzML(result[0][1], result[0][2], result[0][3], result[0][4], result[0][5]) else: return None, None except Exception as e: logging.error('Cannot read from database {0}'.format(str(e))) return None, None
def test_isValidDataKoMlimitLessInt2(self): ml = FizzBuzzML(int1=2, int2=4, mlimit=3, str1='m3', str2='m5') lc = FizzBuzzLC() res, errStr = lc.isValidData(ml) self.assertFalse(res) self.assertIsNotNone(errStr)
def test_isValidDataKoInt2EquInt1(self): ml = FizzBuzzML(int1=1, int2=1, mlimit=1, str1='m3', str2='m5') lc = FizzBuzzLC() res, errStr = lc.isValidData(ml) self.assertFalse(res) self.assertIsNotNone(errStr)
def test_isValidDataOk(self): ml = FizzBuzzML(int1=1, int2=2, mlimit=3, str1='m3', str2='m5') lc = FizzBuzzLC() self.assertTrue(lc.isValidData(ml))
def test_isValidDataKoStr2None(self): ml = FizzBuzzML(int1=2, int2=3, mlimit=10, str1='m3', str2=None) lc = FizzBuzzLC() res, errStr = lc.isValidData(ml) self.assertFalse(res) self.assertIsNotNone(errStr)