def test_insert_metadata_in_file(tmpdir): file_content = dedent(""" 2016-02-26 * "Uncle Boons" "Eating out alone" Liabilities:US:Chase:Slate -24.84 USD Expenses:Food:Restaurant 24.84 USD """) samplefile = tmpdir.mkdir('fava_util_file').join('example.beancount') samplefile.write(file_content) assert samplefile.read() == dedent(file_content) assert len(tmpdir.listdir()) == 1 insert_metadata_in_file(str(samplefile), 1, 'metadata', 'test1') assert samplefile.read() == dedent(""" 2016-02-26 * "Uncle Boons" "Eating out alone" metadata: "test1" Liabilities:US:Chase:Slate -24.84 USD Expenses:Food:Restaurant 24.84 USD """) insert_metadata_in_file(str(samplefile), 1, 'metadata', 'test2') assert samplefile.read() == dedent(""" 2016-02-26 * "Uncle Boons" "Eating out alone" metadata: "test2" metadata: "test1" Liabilities:US:Chase:Slate -24.84 USD Expenses:Food:Restaurant 24.84 USD """)
def insert_metadata(self, filename, lineno, basekey, value): """Insert metadata into a file at lineno. Also, prevent duplicate keys. """ entry = entry_at_lineno(self.entries, filename, lineno, Transaction) key = next_key(basekey, entry.meta) insert_metadata_in_file(filename, lineno - 1, key, value)