Coverage for test_dirdict.py: 98%

126 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-10-14 16:51 +0000

1#!/usr/bin/env python3 

2 

3from dirdict import DirDict 

4import tempfile 

5import uuid 

6import pathlib 

7import pytest 

8import re 

9from pathlib import Path, PosixPath 

10 

11tmpdir = tempfile.TemporaryDirectory() 

12 

13 

14def test_readme(): 

15 dir = DirDict() 

16 readme = dir["README.md"].decode() 

17 pattern = re.compile("```python[^`]+```") 

18 raw_snippets = pattern.findall(readme) 

19 snippets = [s.removesuffix("```").removeprefix("```python") for s in raw_snippets] 

20 assert len(snippets) > 0 

21 for snippet in snippets: 

22 exec(snippet) 

23 

24 

25@pytest.fixture 

26def dir(): 

27 dir = DirDict(tmpdir.name + "/" + str(uuid.uuid4())) 

28 return dir 

29 

30 

31def test_write_bytes(dir): 

32 dir["testfile"] = b"hello bytes" 

33 assert dir["testfile"] == b"hello bytes" 

34 

35 

36def test_append_bytes(dir): 

37 dir["appendfile"] = b"hello" 

38 dir["appendfile"] += b" world" 

39 assert dir["appendfile"] == b"hello world" 

40 

41 

42def test_create_subfolder(dir): 

43 subdir = dir / "subdir" 

44 assert isinstance(subdir, DirDict) 

45 assert isinstance(subdir / "..", DirDict) 

46 

47 

48def test_equality(dir): 

49 subdir = dir / "subdir" 

50 subdir2 = dir / "subdir" 

51 assert subdir == subdir2 

52 assert subdir / ".." == subdir2 / ".." 

53 

54 

55def test_equality_edgecase(dir): 

56 subdir = dir / "subdir" 

57 assert subdir / ".." == dir 

58 

59 

60def test_inequality(dir): 

61 subdir = dir / "subdir" 

62 assert not (subdir == dir) 

63 assert subdir != dir 

64 assert subdir != None 

65 

66 

67def test_get_subdir(dir): 

68 subdir = dir / "subdir" 

69 subdir2 = dir["subdir"] 

70 assert isinstance(subdir, DirDict) 

71 assert isinstance(subdir2, DirDict) 

72 assert subdir == subdir2 

73 

74 

75def test_missing_error(): 

76 try: 

77 dir = DirDict("/this/dir/does/not/exist") 

78 assert False, "should have raised an EnvironmentError" 

79 except EnvironmentError: 

80 ... 

81 

82 

83def test_file_error(dir): 

84 dir["waddup"] = "🐸".encode() 

85 try: 

86 dir = DirDict(dir.path / "waddup") 

87 assert False, "should have raised an EnvironmentError" 

88 except EnvironmentError: 

89 ... 

90 

91 

92def test_keys(dir): 

93 dir["a"] = b"foo" 

94 dir["b"] = b"bar" 

95 assert set(dir.keys()) == {"a", "b"} 

96 

97 

98def test_values(dir): 

99 dir["a"] = b"foo" 

100 dir["b"] = b"bar" 

101 assert set(dir.values()) == {b"foo", b"bar"} 

102 

103 

104def test_items_files(dir): 

105 dir["a"] = b"foo" 

106 dir["b"] = b"bar" 

107 assert set(dir.items()) == {("a", b"foo"), ("b", b"bar")} 

108 

109 

110def test_items_dirs(dir): 

111 a = dir / "a" 

112 b = dir / "b" 

113 assert set(dir.items()) == {("a", a), ("b", b)} 

114 

115 

116def test_add_filebytes(dir): 

117 dir["a"] = b"foo" 

118 assert dir["a"] + b"bar" == b"foobar" 

119 assert dir["a"] == b"foo" 

120 

121 

122def test_decode_filebytes(dir): 

123 dir["a"] = b"foobar" 

124 assert dir["a"].decode() == "foobar" 

125 

126 

127def test_delete_file(dir): 

128 dir["a"] = b"foobar" 

129 assert (dir.path / "a").exists() 

130 del dir["a"] 

131 assert not (dir.path / "a").exists() 

132 

133 

134def test_delete_directory(dir): 

135 (dir / "dir")["a"] = b"foobar" 

136 del (dir / "dir")["a"] 

137 assert (dir.path / "dir").is_dir() 

138 del dir["dir"] 

139 assert not (dir.path / "dir").exists() 

140 

141 

142def test_iterate(dir): 

143 for i in range(10): 

144 (dir / "dir")[f"{i}"] = b"Hello, World!" 

145 assert set(map(str, range(10))) == {path for path in (dir / "dir")} 

146 

147 

148def test_get(dir): 

149 dir["a"] = b"foobar" 

150 assert dir.get("a") == dir["a"] 

151 assert dir.get("b", "default") == "default" 

152 

153 

154def test_contains(dir): 

155 dir["a"] = b"foobar" 

156 assert "a" in dir 

157 assert "b" not in dir 

158 assert dir.path / "a" in dir 

159 assert dir.path / "b" not in dir 

160 

161 

162def test_items_in_relative_subdir(): 

163 subdir = DirDict(".") / "deleteme" 

164 subdir["subfile1"] = b"content1" 

165 subdir["subfile2"] = b"content2" 

166 items = list(subdir.items()) 

167 assert ("subfile1", b"content1") in items 

168 assert ("subfile2", b"content2") in items 

169 

170 

171def test_values_in_relative_subdir(): 

172 subdir = DirDict(".") / "deleteme" 

173 subdir["subfile1"] = b"content1" 

174 subdir["subfile2"] = b"content2" 

175 values = list(subdir.values()) 

176 assert b"content1" in values 

177 assert b"content2" in values