Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state: tests: migrate to dir helpers #3096

Merged
merged 1 commit into from
Jan 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions tests/func/test_state.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import os

import mock

from dvc.main import main
from dvc.path_info import PathInfo
from dvc.state import State
from dvc.utils import file_md5


def test_state(dvc_repo, repo_dir):
path = os.path.join(dvc_repo.root_dir, repo_dir.FOO)
def test_state(tmp_dir, dvc):
tmp_dir.gen("foo", "foo content")
path = tmp_dir / "foo"
path_info = PathInfo(path)
md5 = file_md5(path)[0]

state = State(dvc_repo, dvc_repo.config.config)
state = State(dvc, dvc.config.config)

with state:
state.save(path_info, md5)
entry_md5 = state.get(path_info)
assert entry_md5 == md5

os.unlink(path)
with open(path, "a") as fd:
fd.write("1")
path.unlink()
path.write_text("1")

entry_md5 = state.get(path_info)
assert entry_md5 is None
Expand All @@ -34,20 +31,17 @@ def test_state(dvc_repo, repo_dir):
assert entry_md5 == md5


def test_state_overflow(dvc_repo):
def test_state_overflow(tmp_dir, dvc):
# NOTE: trying to add more entries than state can handle,
# to see if it will clean up and vacuum successfully
ret = main(["config", "state.row_limit", "10"])
assert ret == 0
dvc.config.set("state", "row_limit", 10)

dname = "dir"
os.mkdir(dname)
path = tmp_dir / "dir"
path.mkdir()
for i in range(20):
with open(os.path.join(dname, str(i)), "w+") as fobj:
fobj.write(str(i))
(path / str(i)).write_text(str(i))

ret = main(["add", "dir"])
assert ret == 0
dvc.add("dir")


def mock_get_inode(inode):
Expand All @@ -58,34 +52,33 @@ def get_inode_mocked(path):


@mock.patch("dvc.state.get_inode", autospec=True)
def test_get_state_record_for_inode(get_inode_mock, dvc_repo, repo_dir):
state = State(dvc_repo, dvc_repo.config.config)
def test_get_state_record_for_inode(get_inode_mock, tmp_dir, dvc):
tmp_dir.gen("foo", "foo content")

state = State(dvc, dvc.config.config)
inode = state.MAX_INT + 2
assert inode != state._to_sqlite(inode)

path = os.path.join(dvc_repo.root_dir, repo_dir.FOO)
md5 = file_md5(path)[0]
foo = tmp_dir / "foo"
md5 = file_md5(foo)[0]
get_inode_mock.side_effect = mock_get_inode(inode)

with state:
state.save(PathInfo(path), md5)
state.save(PathInfo(foo), md5)
ret = state.get_state_record_for_inode(inode)
assert ret is not None


def test_remove_unused_links(repo_dir, dvc_repo):
stages = dvc_repo.add(repo_dir.FOO)
assert len(stages) == 1

stages = dvc_repo.add(repo_dir.BAR)
assert len(stages) == 1
def test_remove_unused_links(tmp_dir, dvc):
assert len(tmp_dir.dvc_gen("foo", "foo_content")) == 1
assert len(tmp_dir.dvc_gen("bar", "bar_content")) == 1

cmd_count_links = "SELECT count(*) FROM {}".format(State.LINK_STATE_TABLE)
with dvc_repo.state:
result = dvc_repo.state._execute(cmd_count_links).fetchone()[0]
with dvc.state:
result = dvc.state._execute(cmd_count_links).fetchone()[0]
assert result == 2

dvc_repo.state.remove_unused_links([])
dvc.state.remove_unused_links([])

result = dvc_repo.state._execute(cmd_count_links).fetchone()[0]
result = dvc.state._execute(cmd_count_links).fetchone()[0]
assert result == 0