def get_all_logs(self): load = self.file_connector.read_json_file() article_logs = list() logs = list() for i in load: obj = ArticleLogs(i['id'], i['logs']) for j in obj.logs: log = Log(j['data'], j['text']) logs.append(log) article_logs.append(ArticleLogs(obj.id, logs)) logs = [] return article_logs
def get_logs_by_id(self, id): load = self.file_connector.read_json_file() logs = list() article_logs = list() for i in load: obj = ArticleLogs(i['id'], i['logs']) if obj.id == id: for j in obj.logs: log = Log(j['data'], j['text']) logs.append(log) return ArticleLogs(obj.id, logs) return NullArticleLogs()
def test_get_logs_by_id(self): # Given logs = [{ 'id': '1', 'logs': [{ "data": "08-05-2020", "text": "Added" }] }, { 'id': '2', 'logs': [{ "data": "08-05-2020", "text": "Added" }, { "data": "07-05-2020", "text": "Deleted" }] }] with open(self.config_file_name, "w") as f: json.dump(logs, f) config_manager = ConfigManager() config_manager.logger_path = self.config_file_name logger = LoggerConnector(LoggerFileConnector(config_manager)) article_id = '1' expected = ArticleLogs('1', [Log("08-05-2020", "Added")]) # When logs = logger.get_logs_by_id(article_id) # Then self.assertEqual(expected, logs)
def test_add_log_2(self): # Given logs = [{'id': '1', 'logs': [{"data": "08-05-2020", "text": "Added"}]}] with open(self.config_file_name, "w") as f: json.dump(logs, f) config_manager = ConfigManager() config_manager.logger_path = self.config_file_name logger = LoggerConnector(LoggerFileConnector(config_manager)) expected = [ ArticleLogs('1', [Log("08-05-2020", "Added")]), ArticleLogs('99', [Log("20-05-2020", "Added")]) ] # When logger.add_log('99', Log("20-05-2020", "Added")) # Then self.assertListEqual(expected, logger.get_all_logs())
def add_log(self, id, log): article_logs = self.get_all_logs() def add_log_input(article_log): if article_log.id == id: article_log.logs.append(log) return article_log else: return article_log if any(x.id == id for x in article_logs): article_logs = list(map(add_log_input, article_logs)) else: article_logs.append(ArticleLogs(id, [log])) self.file_connector.save_json_file(article_logs)
def test_article_serialization(self): # Given obj = ArticleLogs('1', [Log("08-05-2020", "Added")]) expected = ''' { "id": "1", "logs": [ { "data": "08-05-2020", "text": "Added" } ] } ''' # When actual = str(obj) # Then self.assertCountEqual(''.join(expected.split()), ''.join(actual.split()))
def test_article_serialization2(self): # Given obj = ArticleLogs('2', [Log("11-07-2041", "Deleted")]) expected = ''' { "id": "2", "logs": [ { "data": "11-07-2041", "text": "Deleted" } ] } ''' # When actual = str(obj) # Then self.assertCountEqual(''.join(expected.split()), ''.join(actual.split()))