def test_create_myfoodrepo_id_bad_source_or_account(self): # transaction needs to be created each time as the FK violation # disrupts the active transaction with Transaction() as t: template_repo = SurveyTemplateRepo(t) with self.assertRaises(ForeignKeyViolation): template_repo.create_myfoodrepo_entry(str(uuid.uuid4()), TEST2_SOURCE_ID) with Transaction() as t: template_repo = SurveyTemplateRepo(t) with self.assertRaises(ForeignKeyViolation): template_repo.create_myfoodrepo_entry(TEST2_ACCOUNT_ID, str(uuid.uuid4()))
def _remote_survey_url_myfoodrepo(transaction, account_id, source_id, language_tag): # assumes an instance of Transaction is already available st_repo = SurveyTemplateRepo(transaction) # do we already have an id? mfr_id, created = st_repo.get_myfoodrepo_id_if_exists( account_id, source_id) if mfr_id is None: # we need an ID so let's try and get one if created is None: # we need a slot and an id slot = st_repo.create_myfoodrepo_entry(account_id, source_id) if not slot: # we could not obtain a slot raise NotFound("Sorry, but the annotators are all allocated") mfr_id = myfoodrepo.create_subj() else: # we have a slot but no id mfr_id = myfoodrepo.create_subj() st_repo.set_myfoodrepo_id(account_id, source_id, mfr_id) else: # we already have an ID then just return the URL pass return myfoodrepo.gen_survey_url(mfr_id)
def test_create_myfoodrepo_id(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) obs = template_repo.create_myfoodrepo_entry( TEST2_ACCOUNT_ID, TEST2_SOURCE_ID) self.assertTrue(obs) t.rollback()
def test_create_myfoodrepo_id_no_slots(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) # insert 1 less than the available slots slots = SERVER_CONFIG['myfoodrepo_slots'] cur = t.cursor() cur.execute( """INSERT INTO ag.myfoodrepo_registry (account_id, source_id) SELECT account_id, id as source_id FROM ag.source WHERE id NOT IN %s LIMIT %s""", ((TEST1_SOURCE_ID, TEST2_SOURCE_ID), slots - 1)) # our next insertion should work obs = template_repo.create_myfoodrepo_entry( TEST1_ACCOUNT_ID, TEST1_SOURCE_ID) self.assertTrue(obs) # we should now be at the maximum number of slots, so our final # insertion should fail obs = template_repo.create_myfoodrepo_entry( TEST2_ACCOUNT_ID, TEST2_SOURCE_ID) self.assertFalse(obs) # update some of our creation timestamps cur.execute( """UPDATE ag.myfoodrepo_registry SET creation_timestamp=NOW() - INTERVAL '30 days' WHERE source_id IN ( SELECT source_id FROM ag.myfoodrepo_registry WHERE source_id != %s LIMIT 5 )""", (TEST2_SOURCE_ID, )) # we now have slots, so we should be successful creating an entry obs = template_repo.create_myfoodrepo_entry( TEST2_ACCOUNT_ID, TEST2_SOURCE_ID) self.assertTrue(obs)
def test_set_myfoodrepo_cannot_assign_new_id(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) obs = template_repo.create_myfoodrepo_entry( TEST2_ACCOUNT_ID, TEST2_SOURCE_ID) self.assertTrue(obs) template_repo.set_myfoodrepo_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, "asubject") with self.assertRaises(KeyError): template_repo.set_myfoodrepo_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, "adifferentsubject") t.rollback()
def test_get_myfoodrepo_id_if_exists(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) obs = template_repo.get_myfoodrepo_id_if_exists( TEST1_ACCOUNT_ID, TEST1_SOURCE_ID) self.assertEqual(obs, (None, None)) obs = template_repo.create_myfoodrepo_entry( TEST1_ACCOUNT_ID, TEST1_SOURCE_ID) self.assertTrue(obs) template_repo.set_myfoodrepo_id(TEST1_ACCOUNT_ID, TEST1_SOURCE_ID, "asubject") obs = template_repo.get_myfoodrepo_id_if_exists( TEST1_ACCOUNT_ID, TEST1_SOURCE_ID) self.assertEqual(obs[0], "asubject") self.assertTrue(obs[1] is not None)