def test_claim_pot_unclaimed(self): maker = PotMaker.create(name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2) State.create(state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=5, weight=10) State.create(state="FULL_TEAPOT", timestamp=datetime(2017, 1, 1, 12, 0, 0), num_of_cups=2, weight=5) result = self.app.post('/claimPot', data=json.dumps({ 'potMaker': 'bob', })) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data['submitMessage'], 'Pot claimed, thanks, bob') updated_pot_maker = PotMaker.get_single_pot_maker('bob') self.assertEqual(updated_pot_maker.number_of_pots_made, 2) self.assertEqual(updated_pot_maker.total_weight_made, 17) self.assertEqual(updated_pot_maker.number_of_cups_made, 7) self.assertEqual(updated_pot_maker.largest_single_pot, 5) updated_state = State.get_latest_full_teapot() self.assertEqual(updated_state.claimed_by, maker)
def test_tea_ready_claimed(self, mock_slack): maker = PotMaker.create( name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2 ) State.create( state="FULL_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=3, claimed_by=maker ) result = self.app.post("/teaReady") self.assertEqual(result.status_code, 200) mock_slack.post_message_to_room.assert_any_call( "The Teapot :teapot: is ready with 3 cups, thanks to bob" ) reaction_message = \ "Want a cup of tea from the next teapot ? " + \ "React to this message to let everyone know!" mock_slack.post_message_to_room.assert_any_call( reaction_message, True) self.assertEqual(mock_slack.post_message_to_room.call_count, 2)
def test_teapot_age(self, mock_time): mock_time.return_value = datetime(2016, 1, 1, 12, 5, 0) State.create(state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2) result = self.app.get("/teapotAge", ) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data["teapotAge"], 5)
def test_claim_pot_no_pot_maker(self): State.create(state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2, claimed_by=None) result = self.app.post('/claimPot', data=json.dumps({})) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data['submitMessage'], 'You need to select a pot maker')
def test_tea_webhook_data(self): State.create(state="TEAPOT FULL", timestamp=datetime.now().isoformat(), num_of_cups=3) State.create(state="TEAPOT EMPTY", timestamp=datetime.now() - timedelta(weeks=1), num_of_cups=2) result = self.app.post("/teabotWebhook") self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data["text"], "There are 3 cups left")
def test_claim_pot_no_pot_maker(self): State.create( state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2, claimed_by=None ) result = self.app.post('/claimPot', data=json.dumps({})) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual( data['submitMessage'], 'You need to select a pot maker')
def test_teapot_age(self, mock_time): mock_time.return_value = datetime(2016, 1, 1, 12, 5, 0) State.create( state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2 ) result = self.app.get( "/teapotAge", ) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data["teapotAge"], 5)
def test_claim_pot_already_claimed(self): maker = PotMaker.create(name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2) State.create(state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2, claimed_by=maker) result = self.app.post('/claimPot') self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data['submitMessage'], 'Pot has already been claimed')
def test_get_latest_state(self): State.create( state="TEAPOT_FULL", timestamp=datetime.now().isoformat(), num_of_cups=3 ) State.create( state="TEAPOT_EMPTY", timestamp=datetime.now() - timedelta(weeks=1), num_of_cups=0 ) result = State.get_newest_state() self.assertEqual(result.state, "TEAPOT_FULL") self.assertEqual(result.num_of_cups, 3)
def test_tea_ready_no_claim(self, mock_slack): State.create(state="FULL_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=3) result = self.app.post("/teaReady") self.assertEqual(result.status_code, 200) mock_slack.post_message_to_room.assert_any_call( "The Teapot :teapot: is ready with 3 cups") reaction_message = \ "Want a cup of tea from the next teapot ? " + \ "React to this message to let everyone know!" mock_slack.post_message_to_room.assert_any_call(reaction_message, True) self.assertEqual(mock_slack.post_message_to_room.call_count, 2)
def test_tea_webhook_data(self): State.create( state="TEAPOT FULL", timestamp=datetime.now().isoformat(), num_of_cups=3 ) State.create( state="TEAPOT EMPTY", timestamp=datetime.now() - timedelta(weeks=1), num_of_cups=2 ) result = self.app.post("/teabotWebhook") self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data["text"], "There are 3 cups left")
def test_human_teapot_state_good_tea_many_cups(self): state = State.create( state="TEAPOT_FULL", timestamp=datetime.now().isoformat(), num_of_cups=2 ) result = _human_teapot_state(state) self.assertEqual(result, "There are 2 cups left")
def test_human_teapot_state_cold_tea_one_cup(self): state = State.create( state="COLD_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=1 ) result = _human_teapot_state(state) self.assertEqual(result, "There is 1 cup left but the teas cold :(")
def test_tea_ready_no_claim(self, mock_slack): State.create( state="FULL_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=3 ) result = self.app.post("/teaReady") self.assertEqual(result.status_code, 200) mock_slack.post_message_to_room.assert_any_call( "The Teapot :teapot: is ready with 3 cups" ) reaction_message = \ "Want a cup of tea from the next teapot ? " + \ "React to this message to let everyone know!" mock_slack.post_message_to_room.assert_any_call( reaction_message, True) self.assertEqual(mock_slack.post_message_to_room.call_count, 2)
def test_claim_pot_already_claimed(self): maker = PotMaker.create( name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2 ) State.create( state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=2, claimed_by=maker ) result = self.app.post('/claimPot') self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data['submitMessage'], 'Pot has already been claimed')
def test_store_state(self): database_entries = [d for d in State.select()] self.assertEqual(len(database_entries), 0) now = datetime.now() result = self.app.post("/storeState", data=json.dumps({ 'num_of_cups': 3, 'timestamp': now.isoformat(), 'state': 'TEAPOT FULL' })) self.assertEqual(result.status_code, 200) database_entries = [d for d in State.select()] self.assertEqual(len(database_entries), 1) db_entry = database_entries[0] self.assertEqual(type(db_entry.timestamp), datetime) self.assertEqual(db_entry.num_of_cups, 3) self.assertEqual(db_entry.timestamp, now) self.assertEqual(db_entry.state, 'TEAPOT FULL')
def test_tea_ready_claimed(self, mock_slack): maker = PotMaker.create(name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2) State.create(state="FULL_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=3, claimed_by=maker) result = self.app.post("/teaReady") self.assertEqual(result.status_code, 200) mock_slack.post_message_to_room.assert_any_call( "The Teapot :teapot: is ready with 3 cups, thanks to bob") reaction_message = \ "Want a cup of tea from the next teapot ? " + \ "React to this message to let everyone know!" mock_slack.post_message_to_room.assert_any_call(reaction_message, True) self.assertEqual(mock_slack.post_message_to_room.call_count, 2)
def test_store_state(self): database_entries = [d for d in State.select()] self.assertEqual(len(database_entries), 0) now = datetime.now() result = self.app.post( "/storeState", data=json.dumps({ 'num_of_cups': 3, 'timestamp': now.isoformat(), 'state': 'TEAPOT FULL' }) ) self.assertEqual(result.status_code, 200) database_entries = [d for d in State.select()] self.assertEqual(len(database_entries), 1) db_entry = database_entries[0] self.assertEqual(type(db_entry.timestamp), datetime) self.assertEqual(db_entry.num_of_cups, 3) self.assertEqual(db_entry.timestamp, now) self.assertEqual(db_entry.state, 'TEAPOT FULL')
def test_claim_pot_unclaimed(self): maker = PotMaker.create( name='bob', number_of_pots_made=1, total_weight_made=12, number_of_cups_made=5, largest_single_pot=2 ) State.create( state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1, 12, 0, 0), num_of_cups=5, weight=10 ) State.create( state="FULL_TEAPOT", timestamp=datetime(2017, 1, 1, 12, 0, 0), num_of_cups=2, weight=5 ) result = self.app.post( '/claimPot', data=json.dumps({ 'potMaker': 'bob', }) ) self.assertEqual(result.status_code, 200) data = json.loads(result.data) self.assertEqual(data['submitMessage'], 'Pot claimed, thanks, bob') updated_pot_maker = PotMaker.get_single_pot_maker( 'bob' ) self.assertEqual(updated_pot_maker.number_of_pots_made, 2) self.assertEqual(updated_pot_maker.total_weight_made, 17) self.assertEqual(updated_pot_maker.number_of_cups_made, 7) self.assertEqual(updated_pot_maker.largest_single_pot, 5) updated_state = State.get_latest_full_teapot() self.assertEqual(updated_state.claimed_by, maker)
def test_get_number_of_new_teapots(self): State.create( state="FULL_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=3 ) State.create( state="FULL_TEAPOT", timestamp=datetime.now() - timedelta(weeks=1), num_of_cups=0 ) State.create( state="EMPTY_TEAPOT", timestamp=datetime.now() - timedelta(weeks=1), num_of_cups=0 ) result = State.get_number_of_new_teapots() self.assertEqual(result, 2)
def test_latest_full_teapot(self): State.create( state="FULL_TEAPOT", timestamp=datetime(2015, 1, 1).isoformat(), num_of_cups=3 ) State.create( state="FULL_TEAPOT", timestamp=datetime(2016, 1, 1).isoformat(), num_of_cups=4 ) State.create( state="FULL_TEAPOT", timestamp=datetime(2017, 1, 1).isoformat(), num_of_cups=5 ) result = State.get_latest_full_teapot() self.assertEqual(result.num_of_cups, 5)
def test_get_latest_state_none(self): result = State.get_newest_state() self.assertIsNone(result)
def test_human_teapot_state_cold_tea_one_cup(self): state = State.create(state="COLD_TEAPOT", timestamp=datetime.now().isoformat(), num_of_cups=1) result = _human_teapot_state(state) self.assertEqual(result, "There is 1 cup left but the teas cold :(")
def test_human_teapot_state_good_tea_many_cups(self): state = State.create(state="TEAPOT_FULL", timestamp=datetime.now().isoformat(), num_of_cups=2) result = _human_teapot_state(state) self.assertEqual(result, "There are 2 cups left")