def test_insert_news(self): name = "test suitcase" Topics_model.db_insert_one_topic(name) topic_obj = Topics_model.db_get_one_topic_by_name(name) c_date = timezone.now() News_model.db_insert_one_news(_title="test news title", _news_date=c_date, _summary="testing_news_summary_16", _news_url="wwww.google", _topic=topic_obj, _image_url="https://pbs.twimg.com/profile_images/626307178563960833/lKjVudoW.jpg")
def db2_get_n_recent_hot_topic_rest(topics_num, topics_start): topic_set = Topics_model.db_get_n_recent_hot_topics_rest( topics_num, topics_start) if topic_set is not None: return topic_set else: return None
def cluster_test_flow(): try: test_topic_name = "White House Press Secretary" test_topic = Topics_model.db_get_one_topic_by_name(test_topic_name) if test_topic is None: return None one_topic_news_dynamic_cluster_flow(test_topic) except Exception as e: logger.error("cluster_test_flow() failed: " + str(e))
def generate_news(topic): topic_obj = Topics_model.db_get_one_topic_by_name(topic) news_date = timezone.now() news_url = "https://www.google.ie/" img_url = "http://lorempixel.com/400/250/" if topic_obj is not None: for i in range(3): news_title = topic + " - news - " + str(i) summary = topic + " - news summary - " + str(i) News_model.db_insert_one_news(news_title, news_date, summary, news_url, topic_obj, img_url)
def db2_add_one_filter_news(news_date, news_title, news_summary, news_topic_id, news_url, image_rul): if len(news_summary) < 200: return None if len(image_rul) < 3: return None flag = -1 topic = Topics_model.db_get_one_topic_by_id(news_topic_id) if topic is not None: news = News_model.db_insert_one_news(news_title, news_date, news_summary, news_url, topic, image_rul) if news is not None: flag = 1 topic.latest_filter_time = timezone.now() topic2 = Topics_model.db_update_one_topic(topic) else: news = None topic = None return flag, news, topic
def db2_get_all_topics(start_id, amount): topic_list = [] count = 0 last_id = None for i in range(start_id, start_id + amount): topic = Topics_model.db_get_one_topic_by_id(i) if topic is not None: topic_list.append(topic) count = count + 1 last_id = topic.id return topic_list, amount, count, last_id
def db2_get_all_old_topics(start_id, amount, date_input): return_list = [] count = 0 last_id = None topic_list = Topics_model.db_get_all_topics_created_before_date( date_input) for topic in topic_list: if topic.id in range(start_id, start_id + amount): return_list.append(topic) count = count + 1 last_id = topic.id return return_list, amount, count, last_id
def latest_cluster_time_mark_clean(): try: test_topic_name = "White House Press Secretary" test_topic = Topics_model.db_get_one_topic_by_name(test_topic_name) if test_topic is None: return None test_topic.latest_filter_time = None test_topic.latest_cluster_time = None test_topic.save() Model_Interfaces.db2_get_filter_news_of_one_topic( test_topic.id).delete() except Exception as e: logger.error("latest_cluster_time_mark_clean() failed: " + str(e))
def db2_add_new_topics(topic_name_list): flag = -1 add_list = [] fail_list = [] if topic_name_list is not None: try: for name in topic_name_list: topic = Topics_model.db_insert_one_topic(name) if topic == None: fail_list.append(name) flag = 0 else: add_list.append(name) if add_list == topic_name_list: flag = 1 for e_name in fail_list: topic = Topics_model.db_get_one_topic_by_name(e_name) if topic is not None: topic.db_update_recent_hot_date() except Exception as e: logger.error("db2_add_new_topics failed: " + str(e)) return flag, add_list, fail_list return flag, add_list, fail_list
class TopicUpdateToday(generics.ListAPIView): queryset = Topics_model.db_today_updated_topics() serializer_class = TopicsSerializer # # api/topics_latest/N => return N topics order by id descending # # class Topics_Latest_N(APIView): # # # Get top 'N' hottest topics. # # def get(self, request, topics_num, format=None): # # n_topics = Model_Interfaces.db2_get_n_recent_hot_topic(topics_num) # # serializer = TopicsSerializer(n_topics, many=True) # # if len(serializer.data)>0: # # return Response(serializer.data) # # return Response(status=status.HTTP_204_NO_CONTENT)
def generate_ori_news(topic): topic_obj = Topics_model.db_get_one_topic_by_name(topic) current_date = timezone.now() news_url = "https://www.google.ie/" img_url = "http://lorempixel.com/400/250/" if topic_obj is not None: for day in range(1, 5): news_date = current_date - timedelta(days=day) for i in range(randint(0, 6)): news_title = topic + " - day - " + str( day) + " - news - " + str(i) summary = topic + " - day - " + str( day) + " - news summary - " + str(i) Ori_News_model.db_insert_one_news(news_title, news_date, summary, news_url, topic_obj, img_url)
def test_get_topic_name(self): name = self.topic.topic_name topic_check = Topics_model.db_get_one_topic_by_name(name) self.assertEqual(name, topic_check.topic_name)
def generate_news(topic): topic_obj = Topics_model.db_get_one_topic_by_name(topic) news_date = timezone.now() news_url = "https://www.google.ie/" img_url = "http://lorempixel.com/400/250/" if topic_obj is not None: for i in range(3): news_title = topic + " - news - " + str(i) summary = topic + " - news summary - " + str(i) News_model.db_insert_one_news(news_title, news_date, summary, news_url, topic_obj, img_url) for name in topic_name: Topics_model.db_insert_one_topic(name) generate_news(name) def generate_ori_news(topic): topic_obj = Topics_model.db_get_one_topic_by_name(topic) current_date = timezone.now() news_url = "https://www.google.ie/" img_url = "http://lorempixel.com/400/250/" if topic_obj is not None: for day in range(1, 5): news_date = current_date - timedelta(days=day) for i in range(randint(0, 6)): news_title = topic + " - day - " + str( day) + " - news - " + str(i) summary = topic + " - day - " + str(
def db2_delete_all_topics_without_news(): Topics_model.db_delete_all_topics_without_news()
def generate_topics(names): Topics_model.db_insert_one_topic(names)
def db2_get_all_topicsv2(): topics_list = Topics_model.db_get_all_topics() if topics_list is not None: return topics_list else: return None
def setUp(self): self.topic = Topics_model.db_insert_one_topic("Test topic")
def test_insert_duplicated_topic(self): d_topic = Topics_model.db_insert_one_topic("Test topic") #print("test2", "topic2 type",type(d_topic)) self.assertIsNone(d_topic)
def db2_get_all_latest_topics(date_input): topic_list = Topics_model.db_get_all_recent_hot_topics(date_input) return topic_list
def db2_topics_search(inputValue): topic_set = Topics_model.db_search_topics(inputValue) # return topic_set if len(topic_set) > 0 else None return topic_set
def test_get_topic_name_by_unexist_id(self): id = 100 name = self.topic.topic_name topic_check = Topics_model.db_get_one_topic_by_id(id) self.assertIsNone(topic_check)
def test_get_topic_name_by_id(self): id = self.topic.id name = self.topic.topic_name topic_check = Topics_model.db_get_one_topic_by_id(id) # self.assertIsNotNone(topic_check) self.assertEqual(name, topic_check.topic_name)
def db2_topic_update_latest_cluster_time(topic_id, _latest_cluster_time): topic = Topics_model.db_update_latest_cluster_time( topic_id, _latest_cluster_time) return topic
def test_get_topic_unexist_name(self): name = "Test topic 0" topic_check = Topics_model.db_get_one_topic_by_name(name) self.assertIsNone(topic_check)
def db2_get_only_latest_topics(date_input): topic_list = Topics_model.db_get_all_topics_created_before_date( date_input) return topic_list
def test_get_all_recent_hot_topics(self): today = datetime.date.today() topic_check = Topics_model.db_get_all_recent_hot_topics(today) self.assertIsNotNone(topic_check)
def test_get_all_recent_hot_topics_failed(self): today = datetime.date(2000,1,1) topic_check = Topics_model.db_get_all_recent_hot_topics(today) self.assertEqual([],topic_check)
def test_get_recent_hot_topics(self): topic_check = Topics_model.db_get_recent_hot_topics() self.assertEqual([],topic_check)
def db2_get_recent_hot_topic_rest(): topic_set = Topics_model.db_get_recent_hot_topics_rest() if topic_set is not None: return topic_set else: return None
import django django.setup() import datetime # inmport models from your model application from dataCollector.models import Topics_model, News_model, Medium_model from dataCollector.model_interfaces import Model_Interfaces # test for Topic_model # test 1 -- test 4.1.2.1 # Test Name: Insert one new topic # Expect Output: topic's str print("\ntest 1 -- test 4.1.2.1") print("Test Name: Insert one new topic") print("Expect Output: topic's id, name, date") topic = Topics_model.db_insert_one_topic("Technology") temp_id = topic.id temp_name = topic.topic_name print("Actual Output:") print(topic.id) print(topic.topic_name) print(topic.create_date) topic.delete() Topics_model.db_insert_one_topic("Science") Topics_model.db_insert_one_topic("Mathematic") Topics_model.db_insert_one_topic("Trump election") Topics_model.db_insert_one_topic("Presentent Putin") Topics_model.db_insert_one_topic("ISIS") Topics_model.db_insert_one_topic("Sourth China Sea") Topics_model.db_insert_one_topic("North Korea Nuclear Weapon")