def simian() -> str: service = Dna() try: Validator.request(request.data) dna = json.loads(request.data).get("dna", []) item = service.store(dna) return make_response("", 200 if item.get('is_simian', False) else 403) except Exception as exc: return make_response({'status': "error", 'message': str(exc)}, 500)
def testGenerateStats(self, boto3_resource: MagicMock): boto3_resource.return_value.Table.return_value.scan.return_value = { 'Count': 40, 'ScannedCount': 140 } obj = Dna() expected = { '_id': "1", 'simians': 40, 'humans': 100, 'ratio': Decimal("0.4") } self.assertEqual(expected, asyncio.run(obj._Dna__generateStats()))
def testStore(self, simian_analyzer: MagicMock, hashlib_md5: MagicMock, validator_check: MagicMock, boto3_resource: MagicMock): dna = ["CTGAGA", "CTAACC", "TCACGT", "ATACTT", "CCTTGT", "TCTTTT"] validator_check.return_value = True boto3_resource.return_value.Table.return_value.put_item.return_value = True boto3_resource.return_value.Table.return_value.scan.return_value = { 'Count': 40, 'ScannedCount': 140 } hash_md5 = "XXX-123-HASH-789-XXX" hashlib_md5.return_value.hexdigest.return_value = hash_md5 simian_analyzer.return_value = True obj = Dna() expected = {'_id': hash_md5, 'dna': dna, 'is_simian': True} self.assertEqual(expected, obj.store(dna))
def testStats(self, boto3_resource: MagicMock): simians = "40" humans = "100" ratio = "0.4" boto3_resource.return_value.Table.return_value.scan.return_value = { 'Items': [{ 'simians': Decimal(simians), 'humans': Decimal(humans), 'ratio': Decimal(ratio) }] } obj = Dna() expected = { 'count_mutant_dna': simians, 'count_human_dna': humans, 'ratio': ratio } self.assertEqual(expected, obj.stats())
def testIsSimian(self, simian_analyzer: MagicMock): dna = ["CTGAGA", "CTAACC", "TCACGT", "ATACTT", "CCTTGT", "TCTTTT"] simian_analyzer.return_value = True obj = Dna() self.assertTrue(obj.isSimian(dna))
def stats() -> str: service = Dna() return make_response(service.stats())