Project

General

Profile

« Previous | Next » 

Revision 5c58b3d6

Added by koszko over 1 year ago

facilitate querying IndexedDB for script files of resource and its dependencies

View differences:

test/unit/utils.py
42 42
        'contents': contents
43 43
    }
44 44

  
45
sample_files = {
46
    'report.spdx':              sample_file('<!-- dummy report -->'),
47
    'LICENSES/somelicense.txt': sample_file('Permission is granted...'),
48
    'LICENSES/CC0-1.0.txt':     sample_file('Dummy Commons...'),
49
    'hello.js':                 sample_file('console.log("uńićódę hello!");\n'),
50
    'bye.js':                   sample_file('console.log("bye!");\n'),
51
    'combined.js':              sample_file('console.log("hello!\\nbye!");\n'),
52
    'README.md':                sample_file('# Python Frobnicator\n...')
53
}
54

  
55
sample_files_by_hash = dict([[file['hash_key'], file['contents']]
56
                             for file in sample_files.values()])
57

  
58
def sample_file_ref(file_name):
59
    return {'file': file_name, 'hash_key': sample_files[file_name]['hash_key']}
60

  
61
def make_sample_mapping():
45
def make_sample_files(names_contents):
46
    """
47
    Take a dict mapping file names to file contents. Return, as a tuple, dicts
48
    mapping file names to file objects (dicts) and file hash keys to file
49
    contents.
50
    """
51
    sample_files = dict([(name, sample_file(contents))
52
                         for name, contents in names_contents.items()])
53

  
54
    sample_files_by_hash = dict([[file['hash_key'], file['contents']]
55
                                 for file in sample_files.values()])
56

  
57
    return sample_files, sample_files_by_hash
58

  
59
sample_files, sample_files_by_hash = make_sample_files({
60
    'report.spdx':              '<!-- dummy report -->',
61
    'LICENSES/somelicense.txt': 'Permission is granted...',
62
    'LICENSES/CC0-1.0.txt':     'Dummy Commons...',
63
    'hello.js':                 'console.log("uńićódę hello!");\n',
64
    'bye.js':                   'console.log("bye!");\n',
65
    'combined.js':              'console.log("hello!\\nbye!");\n',
66
    'README.md':                '# Python Frobnicator\n...'
67
})
68

  
69
def sample_file_ref(file_name, sample_files_dict=sample_files):
70
    """
71
    Return a dictionary suitable for using as file reference in resource/mapping
72
    definition.
73
    """
74
    return {
75
        'file': file_name,
76
        'hash_key': sample_files_dict[file_name]['hash_key']
77
    }
78

  
79
def make_sample_mapping(with_files=True):
80
    """
81
    Procude a sample mapping definition that can be dumped to JSON and put into
82
    Haketilo's IndexedDB.
83
    """
62 84
    return {
63 85
        'source_name': 'example-org-fixes-new',
64 86
        'source_copyright': [
65 87
            sample_file_ref('report.spdx'),
66 88
            sample_file_ref('LICENSES/CC0-1.0.txt')
67
        ],
89
        ] if with_files else [],
68 90
        'type': 'mapping',
69 91
        'identifier': 'example-org-minimal',
70 92
        'long_name': 'Example.org Minimal',
......
81 103
        }
82 104
    }
83 105

  
84
def make_sample_resource():
106
def make_sample_resource(with_files=True):
107
    """
108
    Procude a sample resource definition that can be dumped to JSON and put into
109
    Haketilo's IndexedDB.
110
    """
85 111
    return {
86 112
        'source_name': 'hello',
87 113
        'source_copyright': [
88 114
            sample_file_ref('report.spdx'),
89 115
            sample_file_ref('LICENSES/CC0-1.0.txt')
90
        ],
116
        ] if with_files else [],
91 117
        'type': 'resource',
92 118
        'identifier': 'helloapple',
93 119
        'long_name': 'Hello Apple',
......
99 125
        'scripts': [
100 126
            sample_file_ref('hello.js'),
101 127
            sample_file_ref('bye.js')
102
        ]
128
        ] if with_files else []
103 129
    }
104 130

  
105 131
def item_version_string(definition, include_revision=False):
......
113 139

  
114 140
def sample_data_dict(items):
115 141
    """
116
    Some indexeddb functions expect saved items to be provided in a nested dict
142
    Some IndexedDB functions expect saved items to be provided in a nested dict
117 143
    that makes them queryable by identifier by version. This function converts
118 144
    items list to such dict.
119 145
    """
......
202 228
            ''',
203 229
            nonce)
204 230

  
231
def mock_broadcast(execute_in_page):
232
    """
233
    Make all broadcast operations no-ops (broadcast must be imported).
234
    """
235
    execute_in_page(
236
        'Object.keys(broadcast).forEach(k => broadcast[k] = () => {});'
237
    )
238

  
205 239
def mock_cacher(execute_in_page):
206 240
    """
207 241
    Some parts of code depend on content/repo_query_cacher.js and

Also available in: Unified diff