def test_init_fetch(self): ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) self.assertTrue(fake_git.FakeRepo.checkout) self.assertTrue(fake_git.FakeRepo.cloned) fake_git.FakeRepo.reset(self.default_index) ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) self.assertFalse(fake_git.FakeRepo.cloned) self.assertFalse(fake_git.FakeRepo.pulled) fake_git.FakeRepo.reset(self.default_index, head="1") ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) self.assertFalse(fake_git.FakeRepo.cloned) self.assertTrue(fake_git.FakeRepo.pulled)
def test_upload_delete(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) git_index.upload("delete", {"model": "a", "uuid": "b"}) self.assertTrue(fake_git.FakeRepo.added) self.assertEqual(fake_git.FakeRepo.message, "Delete a/b") self.assertTrue(fake_git.FakeRepo.pushed)
def test_upload_add(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) git_index.upload("add", {"model": "a", "uuid": "b"}) self.assertTrue(fake_git.FakeRepo.added) self.assertIn("Add a/b\n\nSigned-off-by:", fake_git.FakeRepo.message) self.assertTrue(fake_git.FakeRepo.pushed)
def test_upload_dco(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path, signoff=True) git_index.upload("reset", {}) self.assertIn("Initialize a new Modelforge index\n\nSigned-off-by: ", fake_git.FakeRepo.message)
def test_add(self): template_path = os.path.join(self.templates_dir, "template_model.md.jinja2") git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) template = git_index.load_template(template_path) meta = { "default": { "default": "92609e70-f79c-46b5-8419-55726e873cfc", "code": "readme_code %s", "description": "readme_description" }, "model": { "code": "model_code %s", "description": "model_description", "size": "4 Bytes", "references": [["ref_name", "ref_url"]], "extra": { "ext": "data" }, "license": ["license", "link"], "dependencies": [], "url": "http://xxx", "created_at": "13:42", "version": [1, 0, 3] } } git_index.add_model("docfreq", "92609e70-f79c-46b5-8419-55726e873cfc", meta, template) self.assertEqual( git_index.models["docfreq"] ["92609e70-f79c-46b5-8419-55726e873cfc"], meta["model"]) self.assertNotEqual(git_index.meta["docfreq"]["default"], "92609e70-f79c-46b5-8419-55726e873cfc") model_path = os.path.join(self.repo_path, "docfreq", "92609e70-f79c-46b5-8419-55726e873cfc.md") self.assertTrue(os.path.exists(model_path)) with open(model_path) as _in: model = _in.read() with open(os.path.join(os.path.dirname(__file__), "model.md")) as _in: real_model = _in.read() self.assertEqual(model, real_model) git_index.add_model("docfreq", "92609e70-f79c-46b5-8419-55726e873cfc", meta, template, update_default=True) self.assertDictEqual(git_index.meta["docfreq"], meta["default"]) git_index.add_model("other", "92609e70-f79c-46b5-8419-55726e873cfc", meta, template) self.assertEqual( git_index.models["other"]["92609e70-f79c-46b5-8419-55726e873cfc"], meta["model"]) self.assertDictEqual(git_index.meta["other"], meta["default"]) self.assertTrue( os.path.exists( os.path.join(self.repo_path, "other", "92609e70-f79c-46b5-8419-55726e873cfc.md"))) self.assertDictEqual(git_index.meta["other"], meta["default"])
def test_upload_init(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) git_index.upload("initilialize", {}) self.assertTrue(fake_git.FakeRepo.added) self.assertEqual(fake_git.FakeRepo.message, "Initialize a new Modelforge index") self.assertTrue(fake_git.FakeRepo.pushed)
def test_upload_add(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path, signoff=False) git_index.upload("add", {"model": "a", "uuid": "b"}) self.assertTrue(fake_git.FakeRepo.added) self.assertEqual(fake_git.FakeRepo.message, "Add a/b") self.assertTrue(fake_git.FakeRepo.pushed)
def test_remove(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) with self.assertRaises(ValueError): git_index.remove_model("fake_uuid") git_index.remove_model("1e3da42a-28b6-4b33-94a2-a5671f4102f4") self.assertNotIn("1e3da42a-28b6-4b33-94a2-a5671f4102f4", git_index.models["docfreq"]) self.assertIn("12345678-9abc-def0-1234-56789abcdef0", git_index.models["docfreq"]) self.assertEqual(git_index.meta["docfreq"]["default"], "12345678-9abc-def0-1234-56789abcdef0") self.assertFalse( os.path.exists( os.path.join(self.repo_path, "docfreq", "1e3da42a-28b6-4b33-94a2-a5671f4102f4.md"))) self.assertTrue( os.path.exists( os.path.join(self.repo_path, "docfreq", "12345678-9abc-def0-1234-56789abcdef0.md"))) git_index.remove_model("12345678-9abc-def0-1234-56789abcdef0") self.assertNotIn("docfreq", git_index.models) self.assertNotIn("docfreq", git_index.meta) self.assertFalse( os.path.exists( os.path.join(self.repo_path, "docfreq", "12345678-9abc-def0-1234-56789abcdef0"))) self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) git_index.remove_model("12345678-9abc-def0-1234-56789abcdef0") self.assertTrue( os.path.exists( os.path.join(self.repo_path, "docfreq", "1e3da42a-28b6-4b33-94a2-a5671f4102f4.md"))) self.assertFalse( os.path.exists( os.path.join(self.repo_path, "docfreq", "12345678-9abc-def0-1234-56789abcdef0.md"))) self.assertIn("1e3da42a-28b6-4b33-94a2-a5671f4102f4", git_index.models["docfreq"]) self.assertNotIn("12345678-9abc-def0-1234-56789abcdef0", git_index.models["docfreq"]) self.assertEqual(git_index.meta["docfreq"]["default"], "")
def test_upload_init(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path, signoff=False) git_index.upload("reset", {}) self.assertTrue(fake_git.FakeRepo.added) self.assertEqual(fake_git.FakeRepo.message, "Initialize a new Modelforge index") self.assertTrue(fake_git.FakeRepo.pushed)
def test_remove(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) success = False try: git_index.remove_model("fake_uuid") except ValueError: success = True self.assertTrue(success) git_index.remove_model("1e3da42a-28b6-4b33-94a2-a5671f4102f4") self.assertNotIn("1e3da42a-28b6-4b33-94a2-a5671f4102f4", git_index.models["docfreq"]) self.assertIn("12345678-9abc-def0-1234-56789abcdef0", git_index.models["docfreq"]) self.assertEqual(git_index.meta["docfreq"]["default"], "12345678-9abc-def0-1234-56789abcdef0") self.assertFalse( os.path.exists("/tmp/modelforge-test-cache/src-d/models/docfreq/" "1e3da42a-28b6-4b33-94a2-a5671f4102f4.md")) self.assertTrue( os.path.exists("/tmp/modelforge-test-cache/src-d/models/docfreq/" "12345678-9abc-def0-1234-56789abcdef0.md")) git_index.remove_model("12345678-9abc-def0-1234-56789abcdef0") self.assertNotIn("docfreq", git_index.models) self.assertNotIn("docfreq", git_index.meta) self.assertFalse( os.path.exists("/tmp/modelforge-test-cache/src-d/models/docfreq/")) self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) git_index.remove_model("12345678-9abc-def0-1234-56789abcdef0") self.assertTrue( os.path.exists("/tmp/modelforge-test-cache/src-d/models/docfreq/" "1e3da42a-28b6-4b33-94a2-a5671f4102f4.md")) self.assertFalse( os.path.exists("/tmp/modelforge-test-cache/src-d/models/docfreq/" "12345678-9abc-def0-1234-56789abcdef0.md")) self.assertIn("1e3da42a-28b6-4b33-94a2-a5671f4102f4", git_index.models["docfreq"]) self.assertNotIn("12345678-9abc-def0-1234-56789abcdef0", git_index.models["docfreq"]) self.assertEqual(git_index.meta["docfreq"]["default"], "")
def test_upload_bug(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) fake_git.FakeRepo.reset(self.default_index, head="1") succeeded = True try: git_index.upload("initilialize", {}) succeeded = False except ValueError: pass self.assertTrue(succeeded)
def test_initialize(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) with open(os.path.join(git_index.cached_repo, ".gitignore"), "w") as _out: _out.write("nothing") git_index.reset() empty_index = {"models": {}, "meta": {}} self.assertDictEqual(empty_index, git_index.contents) self.assertTrue(os.path.exists(git_index.cached_repo)) self.assertListEqual(os.listdir(git_index.cached_repo), [".gitignore"])
def test_template(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) with self.assertRaises(ValueError): git_index.load_template("fake.jinj4") with self.assertRaises(ValueError): git_index.load_template("fake.jinja2") template_path = os.path.join(self.templates_dir, "readme.md.jinja2") template = git_index.load_template(template_path) self.assertEqual( template.render(meta={}, models={}, links={}), "source{d} MLonCode models\n=========================\n")
def test_readme(self): self.maxDiff = None git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) template_path = os.path.join(self.templates_dir, "readme.md.jinja2") template = git_index.load_template(template_path) git_index.update_readme(template) readme_path = os.path.join(self.cached_path, "src-d/models/README.md") self.assertTrue(os.path.exists(readme_path)) with open(readme_path) as _in: readme = _in.read() with open(os.path.join(os.path.dirname(__file__), "readme.md")) as _in: real_readme = _in.read() self.assertEqual(readme, real_readme)
def test_create_backend_noexc(self): backup = back.config.BACKEND_ARGS back.config.BACKEND_ARGS = "lalala" logger = logging.getLogger() git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) try: self.assertIsNone( back.create_backend_noexc(logger, name="Bar", git_index=git_index)) finally: back.config.BACKEND_ARGS = backup self.assertIsNone( back.create_backend_noexc(logger, name="Bar!!!", git_index=git_index))
def test_create_backend_invalid_args(self): backup = back.config.BACKEND_ARGS back.config.BACKEND_ARGS = "lalala" with self.assertRaises(ValueError): back.create_backend("Bar") back.config.BACKEND_ARGS = backup backup = back.config.BACKEND_ARGS back.config.BACKEND_ARGS = "" class Bar(back.StorageBackend): NAME = "Bar" back.register_backend(Bar) git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) try: self.assertIsInstance(back.create_backend("Bar", git_index), Bar) finally: back.config.BACKEND_ARGS = backup
def test_init_base(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://github.com/src-d/models") self.assertEqual(git_index.repo, "src-d/models") self.assertEqual(git_index.cached_repo, self.repo_path) self.assertTrue( os.path.exists(os.path.join(self.repo_path, "index.json"))) self.assertTrue(os.path.exists(os.path.join(self.repo_path, "docfreq"))) self.assertListEqual( sorted(os.listdir(os.path.join(self.repo_path, "docfreq"))), [ "12345678-9abc-def0-1234-56789abcdef0.md", "1e3da42a-28b6-4b33-94a2-a5671f4102f4.md" ]) self.assertEqual(git_index.contents, self.default_index) self.assertEqual(git_index.models, self.default_index["models"]) self.assertEqual(git_index.meta, self.default_index["meta"]) self.assertTrue(git_index.signoff)
def test_template(self): git_index = ind.GitIndex(index_repo=self.default_url, cache=self.cached_path) succeeded = True try: git_index.load_template("fake.jinj4") succeeded = False except ValueError: pass self.assertTrue(succeeded) try: git_index.load_template("fake.jinja2") succeeded = False except ValueError: pass self.assertTrue(succeeded) template_path = os.path.join(self.templates_dir, "template_readme.md.jinja2") template = git_index.load_template(template_path) self.assertEqual( template.render(meta={}, models={}, links={}), "source{d} MLonCode models\n=========================\n")
def setUp(self): ind.git = fake_git ind.Repo = fake_git.FakeRepo fake_git.FakeRepo.reset(self.default_index) self.backend = create_backend(git_index=ind.GitIndex( remote=self.default_url, cache=self.cached_path))
def test_init_errors(self): succeeded = True try: ind.GitIndex(index_repo="no_protocol", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="badprotocol://github.com", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="http:///nodomain", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="http://nopath.com", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="http://github.com/not-git-repo", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="http://github.com/bad-ssh", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo="http://github.com/bad-credentials", cache=self.cached_path) succeeded = False except ValueError: self.assertTrue(succeeded) try: ind.GitIndex(index_repo=self.default_url, username="******", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded) try: ind.GitIndex(index_repo=self.default_url, password="******", cache=self.cached_path) succeeded = False except ValueError: pass self.assertTrue(succeeded)
def test_init_variants(self): git_index = index.GitIndex(remote="http://github.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "http://github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote="git://github.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "git://github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote="ssh://[email protected]/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "ssh://[email protected]/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex( remote="git+ssh://[email protected]/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "git+ssh://[email protected]/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote=self.default_url, username="******", password="******", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://*****:*****@github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote="https://notgithub.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://notgithub.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex( remote="https://github.com/not/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://github.com/not/src-d/models") self.assertEqual(git_index.repo, "not/src-d/models") self.assertEqual( git_index.cached_repo, os.path.join(self.cached_path, "not", "src-d", "models")) self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex( remote="https://github.com/src-d.git/models.git", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://github.com/src-d.git/models.git") self.assertEqual(git_index.repo, "src-d.git/models") self.assertEqual(git_index.cached_repo, os.path.join(self.cached_path, "src-d.git/models")) self.clear() fake_git.FakeRepo.reset(self.default_index) cached_path = os.path.join(self.cached_path, "cache") git_index = index.GitIndex(remote="https://github.com/src-d/models", cache=cached_path) self.assertEqual(git_index.repo, "src-d/models") self.assertEqual( git_index.cached_repo, os.path.join(self.cached_path, "cache", "src-d", "models")) self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path, signoff=True) self.assertTrue(git_index.signoff)
def test_upload_bug(self): git_index = index.GitIndex(remote=self.default_url, cache=self.cached_path) fake_git.FakeRepo.reset(self.default_index, head="1") with self.assertRaises(ValueError): git_index.upload("reset", {})
def test_init_errors(self): with self.assertRaises(ValueError): index.GitIndex(remote="no_protocol", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="badprotocol://github.com", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http:///nodomain", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://nopath.com", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://github.com/not-git-repo", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://github.com/bad-ssh", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://github.com/bad-credentials", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote=self.default_url, username="******", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote=self.default_url, password="******", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://github.com/no-index", cache=self.cached_path) with self.assertRaises(ValueError): index.GitIndex(remote="http://github.com/json", cache=self.cached_path)
def test_init_variants(self): git_index = ind.GitIndex(index_repo="http://github.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "http://github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex(index_repo="git://github.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "git://github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex( index_repo="ssh://[email protected]/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "ssh://[email protected]/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex( index_repo="git+ssh://[email protected]/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "git+ssh://[email protected]/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex(index_repo=self.default_url, username="******", password="******", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://*****:*****@github.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex( index_repo="https://notgithub.com/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://notgithub.com/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex( index_repo="https://github.com/not/src-d/models", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://github.com/not/src-d/models") self.assertEqual(git_index.repo, "not/src-d/models") self.assertEqual(git_index.cached_repo, "/tmp/modelforge-test-cache/not/src-d/models") self.clear() fake_git.FakeRepo.reset(self.default_index) git_index = ind.GitIndex( index_repo="https://github.com/src-d.git/models.git", cache=self.cached_path) self.assertEqual(git_index.remote_url, "https://github.com/src-d.git/models.git") self.assertEqual(git_index.repo, "src-d.git/models") self.assertEqual(git_index.cached_repo, "/tmp/modelforge-test-cache/src-d.git/models") self.clear() fake_git.FakeRepo.reset(self.default_index) cached_path = "/tmp/modelforge-test-cache/cache" git_index = ind.GitIndex(index_repo="https://github.com/src-d/models", cache=cached_path) self.assertEqual(git_index.repo, "src-d/models") self.assertEqual(git_index.cached_repo, "/tmp/modelforge-test-cache/cache/src-d/models")