async def repo(self, src_url: str): db = self.parent.db repo = Repo(src_url) # Get features features = await db.execute( "SELECT " + ", ".join(self.parent.FEATURE_COLS) + " " "FROM features WHERE src_url=?", (repo.src_url,), ) features = await features.fetchone() if features is not None: repo.evaluated(features) # Get prediction prediction = await db.execute( "SELECT * FROM prediction WHERE " "src_url=?", (repo.src_url,) ) prediction = await prediction.fetchone() if prediction is not None: repo.predicted( prediction["classification"], prediction["confidence"] ) # Get classification classification = await db.execute( "SELECT * FROM classification WHERE " "src_url=?", (repo.src_url,) ) classification = await classification.fetchone() if classification is not None: repo.classify(classification["classification"]) return repo
async def repo(self, src_url: str): repo = Repo(src_url) db = self.conn # Get features await db.execute("SELECT json FROM ml_data WHERE src_url=%s", (src_url, )) dump = await db.fetchone() if dump is not None and dump[0] is not None: repo.merge(Repo(src_url, data=json.loads(dump[0]))) await db.execute("SELECT maintained FROM `status` WHERE src_url=%s", (src_url, )) classification = await db.fetchone() if classification is not None and classification[0] is not None: repo.classify(str(classification[0])) return repo
class TestRepo(unittest.TestCase): def setUp(self): self.null = Repo("null") self.full = Repo( "full", data=dict( features=dict(dead="beef"), extra=dict(extra="read all about it"), ), extra=dict(half=True), ) def test_dict(self): data = self.full.dict() self.assertIn("extra", data) def test_repr(self): repr(self.full) def test_str(self): self.full.prediction = RepoPrediction() self.assertIn("Undetermined", str(self.full)) self.full.data.prediction = RepoPrediction(classification="Good") self.assertIn("Good", str(self.full)) self.full.data.classification = "Great" self.assertIn("Great", str(self.full)) self.full.extra.update(dict(hi=5)) self.assertIn("5", str(self.full)) self.full.extra = dict() self.assertNotIn("5", str(self.full)) def test_merge(self): null = Repo("null") null.merge(self.full) self.assertIn("half", null.extra) self.assertTrue(null.extra["half"]) def test_src_url(self): return self.full.data.src_url def test_evaluated(self): old_last_updated = self.full.data.last_updated results = {"new": "feature"} self.full.evaluated({"feed": "face"}) self.assertIn("feed", self.full.data.features) self.assertEqual("face", self.full.data.features["feed"]) self.full.evaluated(results, overwrite=True) self.assertEqual(self.full.data.features, results) self.assertNotEqual(old_last_updated, self.full.data.last_updated) def test_features(self): self.assertIn("dead", self.full.features()) self.assertIn("dead", self.full.features(["dead"])) self.assertFalse(self.full.features(["dead", "beaf"])) def test_predicted(self): old_prediction = self.full.data.prediction old_last_updated = self.full.data.last_updated self.full.predicted("feed", 1.00) self.assertNotEqual(old_prediction, self.full.data.prediction) self.assertNotEqual(old_last_updated, self.full.data.last_updated) def test_prediction(self): self.full.predicted("feed", 1.00) self.assertTrue(self.full.prediction()) def test_classify(self): self.full.classify("face") self.assertEqual(self.full.data.classification, "face") def test_classified(self): self.full.classify("") self.assertFalse(self.full.classified()) self.full.classify(True) self.assertTrue(self.full.classified()) def test_classification(self): self.full.classify(True) self.assertTrue(self.full.classification()) self.full.classify("") with self.assertRaisesRegex(ValueError, "Unclassified"): self.full.classification()
class TestRepo(unittest.TestCase): def setUp(self): self.null = Repo('null') self.full = Repo('full', data=dict(features=dict(dead='beef'), extra=dict(extra='read all about it')), extra=dict(half=True)) def test_dict(self): data = self.full.dict() self.assertIn('extra', data) def test_repr(self): repr(self.full) def test_str(self): self.full.prediction = RepoPrediction() self.assertIn('Undetermined', str(self.full)) self.full.data.prediction = RepoPrediction(classification='Good') self.assertIn('Good', str(self.full)) self.full.data.classification = 'Great' self.assertIn('Great', str(self.full)) self.full.extra.update(dict(hi=5)) self.assertIn('5', str(self.full)) self.full.extra = dict() self.assertNotIn('5', str(self.full)) def test_merge(self): null = Repo('null') null.merge(self.full) self.assertIn('half', null.extra) self.assertTrue(null.extra['half']) def test_src_url(self): return self.full.data.src_url def test_evaluated(self): old_last_updated = self.full.data.last_updated results = {'new': 'feature'} self.full.evaluated({'feed': 'face'}) self.assertIn('feed', self.full.data.features) self.assertEqual('face', self.full.data.features['feed']) self.full.evaluated(results, overwrite=True) self.assertEqual(self.full.data.features, results) self.assertNotEqual(old_last_updated, self.full.data.last_updated) def test_features(self): self.assertIn('dead', self.full.features()) self.assertIn('dead', self.full.features(['dead'])) self.assertFalse(self.full.features(['dead', 'beaf'])) def test_predicted(self): old_prediction = self.full.data.prediction old_last_updated = self.full.data.last_updated self.full.predicted('feed', 1.00) self.assertNotEqual(old_prediction, self.full.data.prediction) self.assertNotEqual(old_last_updated, self.full.data.last_updated) def test_prediction(self): self.full.predicted('feed', 1.00) self.assertTrue(self.full.prediction()) def test_classify(self): self.full.classify('face') self.assertEqual(self.full.data.classification, 'face') def test_classified(self): self.full.classify('') self.assertFalse(self.full.classified()) self.full.classify(True) self.assertTrue(self.full.classified()) def test_classification(self): self.full.classify(True) self.assertTrue(self.full.classification()) self.full.classify('') with self.assertRaisesRegex(ValueError, 'Unclassified'): self.full.classification()