def test_with_unknown_field(data_store_path, caplog): """Test `update` when the user has an unknown field.""" caplog.set_level(logging.WARNING) data_set = [{ "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) user = {"phone": "999-999-9999", "foobar": "baz"} data_store.update("Eric Idle", **user) expected_data = { "name": "Eric Idle", "phone": "999-999-9999", "address": "here" } assert expected_data in data_store._users assert expected_data in json.loads(data_store_path.read_text()) for record in caplog.records: if "foobar" in record.getMessage(): break else: pytest.fail( "Unable to find warning message with unknown field 'foobar'")
def test_with_different_name(data_store_path): """Test when updating a user with a different name.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set updated_user = { "name": "Terry Gilliam", "phone": "999-999-9999", "address": "not here", } data_store.update("Eric Idle", **updated_user) assert updated_user in data_store._users assert not [ user for user in data_store._users if user["name"] == "Eric Idle" ] json_data = json.loads(data_store_path.read_text()) assert updated_user in json_data assert not [user for user in json_data if user["name"] == "Eric Idle"]
def test_with_unknown_field(data_store_path, caplog): """Test `create` when the user has an unknown field.""" caplog.set_level(logging.WARNING) data_store = JSONDataStore(file_path=str(data_store_path)) user = { "name": "Eric Idle", "phone": "123-456-7890", "address": "here", "foobar": "baz", } data_store.create(user) expected_data = [{ "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }] assert data_store._users == expected_data assert json.loads(data_store_path.read_text()) == expected_data for record in caplog.records: if "foobar" in record.getMessage(): break else: pytest.fail( "Unable to find warning message with unknown field 'foobar'")
def test_with_duplicate_user(data_store_path): """Test `update` when changing the name and the user already exists.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set updated_user = { "name": "John Cleese", "phone": "999-999-9999", "address": "not here", } with pytest.raises(DuplicateUserError) as error: data_store.update("Eric Idle", **updated_user) assert "John Cleese" in str(error.value)
def test_with_populated_file(data_store_path): """Test creating a user when the data store is not empty.""" data_set = [ { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set data_store.create({ "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }) expected_data = [ { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, ] assert data_store._users == expected_data assert json.loads(data_store_path.read_text()) == expected_data
def test_with_same_name(data_store_path): """Test when updating a user with the same name.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set updated_user = { "name": "Eric Idle", "phone": "999-999-9999", "address": "not here" } data_store.update("Eric Idle", **updated_user) assert updated_user in data_store._users assert updated_user in json.loads(data_store_path.read_text())
def test_with_empty_file_no_filters(data_store_path): """Test reading an empty data store with no filters.""" data_set = [] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read() assert result == data_set
def test_with_missing_user(data_store_path): """Test `get` when the user doesn't exists.""" data_set = [{"name": "Eric Idle", "phone": "123-456-7890", "address": "here"}] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set with pytest.raises(MissingUserError) as error: data_store.get("John Cleese") assert "John Cleese" in str(error.value)
def test_updates_to_data_store(data_store_path): """Test reloading a data store wipes any unsaved changes.""" data_store_path.write_text(json.dumps([])) data_store = JSONDataStore(file_path=str(data_store_path)) data_store._users = [ {"name": "Eric Idle", "phone": "123-456-7890", "address": "here"} ] data_store.reload() assert data_store._users == []
def test_updates_to_file(data_store_path): """Test reloading a data store when the JSON file has changed.""" data_store_path.write_text(json.dumps([])) data_store = JSONDataStore(file_path=str(data_store_path)) # change the file after the class has loaded the empty file into # memory data_set = [{"name": "Eric Idle", "phone": "123-456-7890", "address": "here"}] data_store_path.write_text(json.dumps(data_set)) data_store.reload() assert data_store._users == data_set
def test_with_missing_required_fields(data_store_path, missing_fields): """Test `create` when the user is missing a required field.""" data_store = JSONDataStore(file_path=str(data_store_path)) user = {"name": "Eric Idle", "phone": "123-456-7890", "address": "here"} for missing_field in missing_fields: del user[missing_field] with pytest.raises(InvalidUserError) as error: data_store.create(user) error_msg = str(error.value) for missing_field in missing_fields: assert missing_field in error_msg
def test_with_single_user_no_filters(data_store_path): """Test reading a single-user data store with no filters.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read() assert result == data_set
def test_main_case(data_store_path): """Test getting a user by name.""" data_set = [ {"name": "Eric Idle", "phone": "123-456-7890", "address": "here"}, {"name": "John Cleese", "phone": "111-222-3333", "address": "there"}, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set result = data_store.get("Eric Idle") expected_result = {"name": "Eric Idle", "phone": "123-456-7890", "address": "here"} assert result == expected_result
def test_with_multiple_users(data_store_path): """Test creating a data store when the JSON file has many users.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, { "name": "Terry Gilliam", "phone": "555-555-5555", "address": "not here" }, ] data_store_path.write_text(json.dumps(data_set)) assert data_store_path.exists() data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store assert data_store._users == data_set
def test_with_empty_file(data_store_path): """Test creating a data store when the JSON file is empty.""" data_set = [] data_store_path.write_text(json.dumps(data_set)) assert data_store_path.exists() data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store assert data_store._users == []
def test_with_duplicate_user(data_store_path): """Test `create` when the user already exists.""" data_set = [{ "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set user = { "name": "Eric Idle", "phone": "999-999-9999", "address": "not here" } with pytest.raises(DuplicateUserError) as error: data_store.create(user) assert "Eric Idle" in str(error.value)
def test_main_case(data_store_path): """Test deleting a user.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "there" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store._users == data_set data_store.delete("Eric Idle") assert data_set[0] not in data_store._users assert data_set[0] not in json.loads(data_store_path.read_text())
def test_matching_exact_filter(data_store_path, exact_filters): """Test reading with an exact filter.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "there" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "here" }, { "name": "Terry Gilliam", "phone": "555-555-5555", "address": "not found" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read(filters=dict(exact_filters)) assert result == [data_set[0]]
def test_with_single_user(data_store_path): """Test creating a data store when the JSON file contains one user.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "here" }, ] data_store_path.write_text(json.dumps(data_set)) assert data_store_path.exists() data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store assert data_store._users == data_set
def test_fuzzy_filter_multiple_result(data_store_path, fuzzy_filters): """Test reading with a fuzzy filter with multiple results.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "there" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "here" }, { "name": "Terry Gilliam", "phone": "555-555-5555", "address": "not found" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read(filters=dict(fuzzy_filters)) assert result == [data_set[0], data_set[1]]
def test_without_match(data_store_path, filters): """Test reading when no filters match.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "there" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "here" }, { "name": "Terry Gilliam", "phone": "555-555-5555", "address": "not found" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read(filters=filters) assert result == []
def test_with_multiple_users_no_filters(data_store_path): """Test reading a multiple-user data store with no filters.""" data_set = [ { "name": "Eric Idle", "phone": "123-456-7890", "address": "there" }, { "name": "John Cleese", "phone": "111-222-3333", "address": "here" }, { "name": "Terry Gilliam", "phone": "555-555-5555", "address": "not found" }, ] data_store_path.write_text(json.dumps(data_set)) data_store = JSONDataStore(file_path=str(data_store_path)) result = data_store.read() assert result == data_set
def test_name(data_store_path): """Test the data store has a name defined.""" assert not data_store_path.exists() data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store.NAME == "json"
def test_without_file(data_store_path): """Test creating a data store when the JSON file doesn't exist.""" assert not data_store_path.exists() data_store = JSONDataStore(file_path=str(data_store_path)) assert data_store assert data_store._users == []