def test_remove(): ipvc = get_environment() ipvc.repo.init() testdir = REPO / 'testdir1' / 'testdir2' testdir.mkdir(parents=True) test_file = testdir / 'test_file.txt' write_file(test_file, 'hello world') changes = ipvc.stage.add(test_file) assert isinstance(changes, list) and len(changes) == 1 and changes[0]['Type'] == 0 assert changes[0]['Path'] == '' ipvc.stage.commit('msg') time.sleep(1) # resolution of modification timestamp is a second write_file(test_file, 'hello world2') changes = ipvc.stage.add(test_file) assert isinstance(changes, list) and len(changes) == 1 and changes[0]['Type'] == 2 assert changes[0]['Path'] == '' changes = ipvc.stage.remove(test_file) assert isinstance(changes, list) and len(changes) == 1 and changes[0]['Type'] == 2 assert changes[0]['Path'] == '' head_stage, stage_workspace = ipvc.stage.status() assert isinstance( head_stage, list) and len(head_stage) == 0 and len(stage_workspace) == 1
def test_create_from(): ipvc = get_environment() ipvc.repo.init() filename1 = REPO / 'test_file.txt' write_file(filename1, 'hello world') ipvc.stage.add() ipvc.stage.commit('msg1') filename2 = REPO / 'test_file2.txt' write_file(filename2, 'hello world2') ipvc.stage.add() ipvc.stage.commit('msg2') ipvc.branch.create('test', from_commit='@head~') commits = ipvc.branch.history() assert len(commits) == 1 filename1.stat() with pytest.raises(FileNotFoundError): filename2.stat() os.remove(filename1) ret = ipvc.diff.run() ipvc.print_ipfs_profile_info()
def test_mv_rm(): ipvc = get_environment() ipvc.repo.init() test_file = REPO / 'test_file.txt' write_file(test_file, 'hello_world') ipvc.stage.add(test_file) with pytest.raises(RuntimeError): ipvc.repo.mv(REPO2, REPO2) with pytest.raises(RuntimeError): ipvc.repo.mv(REPO, REPO) with pytest.raises(RuntimeError): IPVC(Path('/'), NAMESPACE).repo.mv(REPO, None) assert ipvc.repo.mv(REPO, REPO2) == True assert not REPO.exists() assert REPO2.exists() try: h = ipvc.ipfs.files_stat(ipvc.repo.get_mfs_path(REPO2))['Hash'] except: h = None assert h is not None assert ipvc.repo.rm(REPO2) == True assert REPO2.exists() # should still exist on filesystem try: h = ipvc.ipfs.files_stat(ipvc.repo.get_mfs_path(REPO2))['Hash'] except: h = None assert h is None # should not exist on MFS
def test_init_and_ls(): cwd = Path('/current/working/dir') ipvc = get_environment(cwd, mkdirs=False) repos = ipvc.repo.ls() assert len(repos) == 0 ipvc.repo.init() repos = ipvc.repo.ls() assert len(repos) == 1 assert repos[0][2] == str(cwd) assert repos[0][0] == None branch_name = ipvc.ipfs.files_read( ipvc.repo.get_mfs_path(cwd, repo_info='active_branch_name')).decode('utf-8') assert branch_name == 'master' with pytest.raises(RuntimeError): ipvc.repo.init() with pytest.raises(RuntimeError): ipvc.set_cwd(cwd / 'test') ipvc.repo.init() cwd2 = Path('/somewhere/else') ipvc.set_cwd(cwd2) assert ipvc.repo.init(name='my_repo') == True repos = ipvc.repo.ls() assert len(repos) == 2 assert repos[1][0] == 'my_repo' assert repos[0][2] == str(cwd) assert repos[1][2] == str(cwd2)
def test_create_and_checkout(): ipvc = get_environment() ipvc.repo.init() write_file(REPO / 'test_file.txt', 'hello world') ipvc.stage.add() assert ipvc.branch.status(name=True) == 'master' write_file(REPO / 'test_file2.txt', 'hello world2') with pytest.raises(RuntimeError): ipvc.branch.create('master') branches = set(ipvc.branch.ls()) assert branches == set(['master']) ipvc.branch.create('develop') assert ipvc.branch.status(name=True) == 'develop' branches = set(ipvc.branch.ls()) assert branches == set(['master', 'develop']) head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 1 and len(stage_workspace) == 1 test_file3 = REPO / 'test_file3.txt' write_file(test_file3, 'hello world3') t1 = test_file3.stat().st_mtime_ns head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 1 and len(stage_workspace) == 2 ipvc.branch.checkout('master') head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 1 and len(stage_workspace) == 1 with pytest.raises(FileNotFoundError): test_file3.stat() ipvc.branch.checkout('develop') head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 1 and len(stage_workspace) == 2 # Test that timestamps are checked out correctly t2 = test_file3.stat().st_mtime_ns assert t1 == t2 ipvc.print_ipfs_profile_info()
def test_clone(): ipvc = get_environment() ipvc.repo.init(name='myrepo') id1 = ipvc.id.create(key='id1', use=True) test_file = REPO / 'test_file.txt' write_file(test_file, 'hello world') ipvc.stage.add(test_file) ipvc.stage.commit(message='msg') ipvc.repo.publish() ipvc.repo.rm() write_file(test_file, 'other text') ipvc.repo.clone(f'{id1}/myrepo') with open(test_file, 'r') as f: assert f.read() == 'hello world'
def test_status(): ipvc = get_environment() ipvc.repo.init() test_file = REPO / 'test_file.txt' write_file(test_file, 'hello world') head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 0 assert len(stage_workspace) == 1 and stage_workspace[0]['Type'] == 0 ipvc.stage.add() head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 1 and head_stage[0]['Type'] == 0 assert len(stage_workspace) == 0
def test_history(): ipvc = get_environment() ipvc.repo.init() write_file(REPO / 'test_file.txt', 'hello world') ipvc.stage.add() ipvc.stage.commit('commit message') try: ipvc.ipfs.files_stat( ipvc.repo.get_mfs_path(REPO, 'master', branch_info='head/data/bundle')) except: assert False try: ipvc.ipfs.files_stat( ipvc.repo.get_mfs_path(REPO, 'master', branch_info='head/data/parent')) except: assert False try: metadata = ipvc.repo.mfs_read_json( ipvc.repo.get_mfs_path(REPO, 'master', branch_info='head/data/commit_metadata')) except: assert False commits = ipvc.branch.history() assert len(commits) == 1 assert ipvc.branch.show(Path('@head')) == 'test_file.txt' assert ipvc.branch.show(Path('@head/test_file.txt')) == 'hello world' ipvc.print_ipfs_profile_info()
def test_stage_diff(): ipvc = get_environment() ipvc.repo.init() test_file = REPO / 'test_file.txt' write_file(test_file, 'hello world') ipvc.stage.add(test_file) assert len(ipvc.stage.diff()) == 1 ipvc.stage.commit('my commit') assert len(ipvc.stage.diff()) == 0 time.sleep(1) # sleep to make sure we get another time stamp write_file(test_file, 'hello world2') assert len(ipvc.stage.diff()) == 0 assert len(ipvc.diff.run(files=True)) == 1 changes = ipvc.stage.add(test_file) assert len(changes) == 1 assert len(ipvc.stage.diff()) == 1 diff = ipvc.diff.run(files=True) assert len(diff) == 0
def test_merge_replay(): ipvc = get_environment() ipvc.repo.init() test_file_content = 'line1\nline2\nline3\nline4' write_file(REPO / 'test_file.txt', test_file_content) write_file(REPO / 'test_file3.txt', test_file_content) ipvc.stage.add() ipvc.stage.commit('msg1') ipvc.branch.create('other', no_checkout=True) time.sleep(1) # resolution of modification timestamp is a second write_file(REPO / 'test_file.txt', 'line1\nother\nline3\nline4') write_file(REPO / 'test_file3.txt', 'line1\nline2\nline3\nline4\nappended') write_file(REPO / 'other_file.txt', 'hello world') ipvc.stage.add() ipvc.stage.commit('msg2') ipvc.branch.checkout('other') time.sleep(1) # resolution of modification timestamp is a second write_file(REPO / 'test_file.txt', 'line1\nline2\nblerg\nline4') write_file(REPO / 'test_file3.txt', 'prepended\nline1\nline2\nline3\nline4') with pytest.raises(RuntimeError): ipvc.branch.merge('master') ipvc.stage.add() with pytest.raises(RuntimeError): ipvc.branch.merge('master') ipvc.stage.commit('msg2other') _, merged_files, conflict_files = ipvc.branch.replay('master') assert conflict_files == set(['test_file.txt']) assert merged_files == set(['test_file3.txt']) ipvc.branch.replay(abort=True) pulled_files, merged_files, conflict_files = ipvc.branch.merge('master') assert conflict_files == set(['test_file.txt']) assert pulled_files == set(['other_file.txt']) assert merged_files == set(['test_file3.txt']) conflict_lines = [ 'line1', '>>>>>>> other (ours)', 'line2', 'blerg', '======= master (theirs)', 'other', 'line3', '<<<<<<<', 'line4' ] pulled_lines = ['hello world'] merged_lines = [ 'prepended', 'line1', 'line2', 'line3', 'line4', 'appended' ] assert_list_equals( open(REPO / 'test_file.txt', 'r').read().splitlines(), conflict_lines) assert_list_equals( open(REPO / 'other_file.txt', 'r').read().splitlines(), pulled_lines) assert_list_equals( open(REPO / 'test_file3.txt', 'r').read().splitlines(), merged_lines) head_stage, stage_workspace = ipvc.stage.status() # Two files should be staged (other_file.txt and test_file3.txt) # and one file (test_file.txt) has a conflict and should not be staged assert len(head_stage) == 2 and len(stage_workspace) == 1 ipvc.branch.merge(abort=True) head_stage, stage_workspace = ipvc.stage.status() assert len(head_stage) == 0 and len(stage_workspace) == 0 with pytest.raises(FileNotFoundError): (REPO / 'other_file.txt').stat() assert open(REPO / 'test_file.txt', 'r').read() == 'line1\nline2\nblerg\nline4' assert open(REPO / 'test_file3.txt', 'r').read() == 'prepended\nline1\nline2\nline3\nline4' # Pull again and fix the merge and commit this time ipvc.branch.merge('master') with pytest.raises(RuntimeError): # Should raise because conflict markers are still there ipvc.branch.merge(resolve='msg') # "Fix" write_file(REPO / 'test_file.txt', '\n'.join(merged_lines)) ipvc.branch.merge(resolve='msg') # Latest commit must have a merge parent assert ipvc.branch.history()[0][-1] is not None # Test fast-forward merges write_file(REPO / 'ff_file.txt', 'hello world') ipvc.stage.add(REPO / 'ff_file.txt') ff_hash = ipvc.stage.commit('ff') ipvc.branch.checkout('master') ipvc.branch.merge('other') history = ipvc.branch.history() assert history[0][0] == ff_hash # Doesn't have a merge parent assert history[0][-1] == None ipvc.print_ipfs_profile_info()
def test_add_commit(): ipvc = get_environment() ipvc.repo.init() with pytest.raises(RuntimeError): ipvc.stage.commit('asd') with pytest.raises(ValueError): ipvc.stage.add('/notrepo') testdir = REPO / 'testdir1' / 'testdir2' testdir.mkdir(parents=True) test_file = testdir / 'test_file.txt' write_file(test_file, 'hello world') changes = ipvc.stage.add(test_file) assert isinstance(changes, list) and len(changes) == 1 and changes[0]['Type'] == 0 assert changes[0]['Path'] == '' test_file = REPO / 'test_file.txt' write_file(test_file, 'hello world') changes = ipvc.stage.add(test_file) assert isinstance(changes, list) and len(changes) == 1 and changes[0]['Type'] == 0 assert changes[0]['Path'] == '' test_file1 = testdir / 'test_file1' write_file(test_file1, 'hello world1') test_file2 = testdir / 'test_file2' write_file(test_file2, 'hello world2') changes = ipvc.stage.add() assert isinstance(changes, list) and len(changes) == 2 assert changes[0]['Type'] == 0 and changes[1]['Type'] == 0 assert changes[0]['Path'] == 'testdir1/testdir2/test_file1' assert changes[1]['Path'] == 'testdir1/testdir2/test_file2' test_file3 = testdir / 'test_file3' test_file4 = testdir / 'test_file4' write_file(test_file3, 'hello world3') write_file(test_file4, 'hello world4') changes = ipvc.stage.add(testdir) assert isinstance(changes, list) and len(changes) == 2 assert changes[0]['Type'] == 0 assert changes[0]['Path'] == 'test_file3' assert changes[1]['Type'] == 0 assert changes[1]['Path'] == 'test_file4' os.remove(test_file2) changes = ipvc.stage.add(REPO / 'notafolder/') assert isinstance(changes, list) and len(changes) == 0 changes = ipvc.stage.add(test_file2) assert isinstance(changes, list) and len(changes) == 1 assert changes[0]['Type'] == 1 assert changes[0]['Path'] == '' time.sleep(1) # resolution of modification timestamp is a second write_file(test_file4, 'hello world5') changes = ipvc.stage.add(testdir) assert isinstance(changes, list) and len(changes) == 1 assert changes[0]['Type'] == 2 assert changes[0]['Path'] == 'test_file4'
import datetime from mongokit import Document from mongokit import Connection from helpers import get_environment settings = get_environment() connection = Connection(settings.get('MONGODB_URI')) pagging = settings.get('PAGINATION_ROW_COUNT') @connection.register class UserVisit(Document): __database__ = 'highload' __collection__ = 'user_visits' structure = { 'time': datetime.datetime, 'user_id': int, 'url': basestring } required_fields = ['user_id', 'url'] default_values = { 'time': datetime.datetime.now } def get_page(page): queryset = connection[UserVisit.__database__][UserVisit.__collection__].group( ['url'], {}, {'count': 1}, "function(current, result){ result.count++ }"