# Copyright 2019 Mike Shoup # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path from humulus.couch import put_doc, get_doc, update_doc, put_designs, get_view def test_put_doc(app): with app.app_context(): data = {"foo": "bar"} response = put_doc(data) assert "_id" in response assert "created" in response response = put_doc({"name": "test"}) assert response["_id"] == "test" response = put_doc({"name": "test"}) assert response["_id"] == "test-1" response = put_doc({"name": "test"}) assert response["_id"] == "test-2" def test_update_doc(app): with app.app_context(): doc = get_doc("awesome-lager") rev = doc["_rev"] doc["test"] = "update" update_doc(doc) updated_doc = get_doc("awesome-lager") assert doc["_id"] == updated_doc["_id"] assert rev < updated_doc["_rev"] assert updated_doc["test"] == "update" assert "updated" in updated_doc def test_build_couch_command(runner, monkeypatch): class Recorder(object): called = False def fake_build_couch(): Recorder.called = True monkeypatch.setattr("humulus.couch.build_couch", fake_build_couch) result = runner.invoke(args=["build-couch"]) assert "Built a couch. Please have a seat." in result.output assert Recorder.called def test_get_doc(app): with app.app_context(): assert get_doc("foobar")["data"] == "test" def test_put_designs(app, monkeypatch): # Always return the path to our test assets testpath = Path(__file__).parent with app.app_context(): # Test initial load of designs monkeypatch.setattr(Path, "parent", testpath / "assets/initial") put_designs() recipes = get_doc("_design/recipes") assert "language" in recipes rev = recipes["_rev"] # Try again, make sure nothing changed. put_designs() recipes = get_doc("_design/recipes") assert recipes["_rev"] == rev # Test that changes can be loaded monkeypatch.setattr(Path, "parent", testpath / "assets/changed") put_designs() recipes = get_doc("_design/recipes") assert "by-date" in recipes["views"] def test_get_view(app): with app.app_context(): view = get_view("_design/recipes", "by-date") assert view()["total_rows"] > 0