|
| 1 | +# Copyright 2024 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Unit tests for nbfmt.""" |
| 15 | +import pathlib |
| 16 | +import unittest |
| 17 | +from nbformat import notebooknode |
| 18 | +from tensorflow_docs.tools.nbfmt import __main__ as nbfmt |
| 19 | + |
| 20 | + |
| 21 | +class NotebookFormatTest(unittest.TestCase): |
| 22 | + |
| 23 | + def test_metadata_cleansing(self): |
| 24 | + subject_notebook = notebooknode.NotebookNode({ |
| 25 | + "cells": [], |
| 26 | + "metadata": { |
| 27 | + "unknown": ["delete", "me"], |
| 28 | + "accelerator": "GPU", |
| 29 | + "colab": { |
| 30 | + "name": "/this/is/clobbered.ipynb", |
| 31 | + "collapsed_sections": [], |
| 32 | + "deleteme": "pls", |
| 33 | + }, |
| 34 | + "kernelspec": { |
| 35 | + "display_name": "Python 2 foreverrrr", |
| 36 | + "name": "python2", |
| 37 | + "deleteme": "deldeldel", |
| 38 | + }, |
| 39 | + "google": { |
| 40 | + "keywords": ["one", "two"], |
| 41 | + "image_path": "/foo/img.png", |
| 42 | + "more_stuff": "delete me", |
| 43 | + } |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + expected_notebook = notebooknode.NotebookNode({ |
| 48 | + "cells": [], |
| 49 | + "metadata": { |
| 50 | + "accelerator": "GPU", |
| 51 | + "colab": { |
| 52 | + "name": "test.ipynb", |
| 53 | + "collapsed_sections": [], |
| 54 | + "toc_visible": True, |
| 55 | + }, |
| 56 | + "kernelspec": { |
| 57 | + "display_name": "Python 3", |
| 58 | + "name": "python3", |
| 59 | + }, |
| 60 | + "google": { |
| 61 | + "keywords": ["one", "two"], |
| 62 | + "image_path": "/foo/img.png", |
| 63 | + } |
| 64 | + }, |
| 65 | + 'nbformat': 4, |
| 66 | + 'nbformat_minor': 0, |
| 67 | + }) |
| 68 | + |
| 69 | + nbfmt.clean_root(subject_notebook, pathlib.Path('/path/test.ipynb')) |
| 70 | + self.assertEqual(subject_notebook, expected_notebook) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + unittest.main() |
0 commit comments