当前位置: 首页>>代码示例>>Python>>正文


Python TopicTagDB.save_to_json_index方法代码示例

本文整理汇总了Python中awscli.topictags.TopicTagDB.save_to_json_index方法的典型用法代码示例。如果您正苦于以下问题:Python TopicTagDB.save_to_json_index方法的具体用法?Python TopicTagDB.save_to_json_index怎么用?Python TopicTagDB.save_to_json_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在awscli.topictags.TopicTagDB的用法示例。


在下文中一共展示了TopicTagDB.save_to_json_index方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestTopicDBScan

# 需要导入模块: from awscli.topictags import TopicTagDB [as 别名]
# 或者: from awscli.topictags.TopicTagDB import save_to_json_index [as 别名]
class TestTopicDBScan(TestTopicTagDB):
    def create_topic_src_file(self, topic_name, tags):
        """Create a topic source file from a list of tags and topic name"""
        content = '\n'.join(tags)
        topic_name = topic_name + '.rst'
        topic_filepath = self.file_creator.create_file(topic_name, content)
        return topic_filepath

    def assert_json_index(self, file_paths, reference_tag_dict):
        """Asserts the scanned tags by checking the saved JSON index"""
        json_index = self.file_creator.create_file('index.json', '')
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.scan(file_paths)
        self.topic_tag_db.save_to_json_index()
        with open(json_index, 'r') as f:
            saved_index = json.loads(f.read())
            self.assertEqual(saved_index, reference_tag_dict)

    def test_scan_all_valid_tags(self):
        tags = [
            ':description: This is a description',
            ':title: Title',
            ':category: Foo',
            ':related topic: Bar',
            ':related command: ec2'
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'description': ['This is a description'],
                'title': ['Title'],
                'category': ['Foo'],
                'related topic': ['Bar'],
                'related command': ['ec2']
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_invalid_tag(self):
        tags = [
            ':description: This is a description',
            ':title: Title',
            ':category: Foo',
            ':related_topic: Bar',
        ]
        topic_name = 'my-topic'

        topic_filepath = self.create_topic_src_file(topic_name, tags)
        with self.assertRaises(ValueError):
            self.topic_tag_db.scan([topic_filepath])

    def test_scan_no_tags(self):
        tags = []
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {}
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_values(self):
        tags = [
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_single_and_multi_values(self):
        tags = [
            ':title: Title',
            ':category: Foo, Bar',
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
            topic_name: {
                'title': ['Title'],
                'category': ['Foo', 'Bar'],
            }
        }
        topic_filepath = self.create_topic_src_file(topic_name, tags)
        self.assert_json_index([topic_filepath], reference_tag_dict)

    def test_scan_tags_with_multi_duplicate_values(self):
        tags = [
            ':category: Foo, Foo, Bar'
        ]
        topic_name = 'my-topic'

        reference_tag_dict = {
#.........这里部分代码省略.........
开发者ID:Avtarsingh127,项目名称:aws-cli,代码行数:103,代码来源:test_topictags.py

示例2: TestTopicTagDBGeneral

# 需要导入模块: from awscli.topictags import TopicTagDB [as 别名]
# 或者: from awscli.topictags.TopicTagDB import save_to_json_index [as 别名]

#.........这里部分代码省略.........
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', 'related topic')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_tag(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value(topic_name, ':foo:')
        self.assertEqual(value, None)

    def test_get_tag_no_exist_use_default(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'related topic': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_value('no-exist', ':foo:', [])
        self.assertEqual(value, [])

    def test_get_tag_single_value(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')
        self.assertEqual(value, 'foo')

    def test_get_tag_single_value_exception(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo', 'bar']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        with self.assertRaises(ValueError):
            self.topic_tag_db.get_tag_single_value('topic-name-1', 'title')

    def test_get_tag_single_value_no_exists(self):
        topic_name = 'topic-name-1'
        tag_dict = {
            topic_name: {
                'title': ['foo']
            }
        }
        self.topic_tag_db = TopicTagDB(tag_dict)
        value = self.topic_tag_db.get_tag_single_value(
            'topic-name-1', ':title:')
        self.assertEqual(value, None)

    def test_load_and_save_json_index(self):
        tag_dict = {
            'topic-name-1': {
                'title': ['My First Topic Title'],
                'description': ['This describes my first topic'],
                'category': ['General Topics', 'S3'],
                'related command': ['aws s3'],
                'related topic': ['topic-name-2']
            },
            'topic-name-2': {
                'title': ['My Second Topic Title'],
                'description': ['This describes my second topic'],
                'category': ['General Topics'],
                'related topic': ['topic-name-1']
            }
        }

        json_index = self.file_creator.create_file('index.json', '')

        # Create a JSON index to be loaded.
        tag_json = json.dumps(tag_dict, indent=4, sort_keys=True)
        with open(json_index, 'w') as f:
            f.write(tag_json)

        # Load the JSON index.
        self.topic_tag_db = TopicTagDB(index_file=json_index)
        self.topic_tag_db.load_json_index()

        # Write the loaded json to disk and ensure it is as expected.
        saved_json_index = self.file_creator.create_file('index2.json', '')
        self.topic_tag_db.index_file = saved_json_index

        self.topic_tag_db.save_to_json_index()
        with open(saved_json_index, 'r') as f:
            self.assertEqual(f.read(), tag_json)
开发者ID:Avtarsingh127,项目名称:aws-cli,代码行数:104,代码来源:test_topictags.py


注:本文中的awscli.topictags.TopicTagDB.save_to_json_index方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。