-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
92 lines (79 loc) · 2.89 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest
import json
from picloudsync import PiCloudSync
import os
from os import listdir
CFG = {}
CFG['BASE_DIR']='tests/data/discs'
CFG['INFO_FILE_NAME'] = 'picloud.json'
CFG['SYNC_FILE_NAME'] = 'pisync.json'
class PiCloudTest(unittest.TestCase):
def getMasterSyncDate(self):
f = open(self.cfg['BASE_DIR'] + '/master/' + self.cfg['INFO_FILE_NAME'])
info = json.load(f)
return info['last_synced']
def commonSetUp(self):
self.sync = PiCloudSync(self.cfg)
self.slaveDir = self.cfg['BASE_DIR'] + '/' + 'slave'
self.masterDir = self.cfg['BASE_DIR'] + '/' + 'master'
self.beforeSListDir = listdir(self.slaveDir)
self.beforeMListDir = listdir(self.masterDir)
self.beforeMSyncDate = self.getMasterSyncDate()
class TestsNoMaster(PiCloudTest):
"""
TC1
Checks that if there's no master, nothing happens
"""
cfg = CFG.copy()
def setUp(self):
self.cfg['BASE_DIR'] = 'tests/data/discs_tc1'
self.commonSetUp()
def runTest(self):
self.sync.run()
newListDir = listdir(self.slaveDir)
self.assertEqual(len(self.beforeSListDir), len(newListDir), 'The two dirs have different lengths'),
self.assertEqual(self.beforeMSyncDate, self.getMasterSyncDate(), 'The sync date might have been updated on master')
self.assertListEqual(newListDir, self.beforeSListDir, 'The two lists are different')
class TestsNoSlave(PiCloudTest):
"""
TC2
Checks that if there's no slave, nothing happens
"""
cfg = CFG.copy()
def setUp(self):
self.cfg['BASE_DIR'] = 'tests/data/discs_tc2'
self.commonSetUp()
def runTest(self):
self.sync.run()
newListDir = listdir(self.slaveDir)
self.assertEqual(len(self.beforeSListDir), len(newListDir), 'The two dirs have different lengths'),
self.assertEqual(self.beforeMSyncDate, self.getMasterSyncDate(), 'The sync date might have been updated on master')
self.assertListEqual(newListDir, self.beforeSListDir, 'The two lists are different')
class TestsGoodSync(PiCloudTest):
"""
TC3
Checks that if there's both, master and slave they're synced
"""
cfg = CFG.copy()
def setUp(self):
self.cfg['BASE_DIR'] = 'tests/data/discs_tc3'
command = 'rm -rf {0}'.format(self.cfg['BASE_DIR'] + '/slave/*.txt')
print command
os.system(command)
self.commonSetUp()
def runTest(self):
self.sync.run()
newListDir = listdir(self.slaveDir)
self.assertEqual(len(newListDir), len(self.beforeMListDir), 'The two dirs have different lengths'),
self.assertNotEqual(self.beforeMSyncDate, self.getMasterSyncDate(), 'The sync date might have been updated on master')
self.assertListEqual(self.beforeMListDir, newListDir, 'The two lists are different')
def suite():
t_suite = unittest.TestSuite()
t_suite.addTest(unittest.makeSuite(TestsNoMaster))
t_suite.addTest(unittest.makeSuite(TestsNoSlave))
t_suite.addTest(unittest.makeSuite(TestsGoodSync))
return t_suite
if __name__=='__main__':
runner = unittest.TextTestRunner(verbosity=2)
test_suite = suite()
runner.run(test_suite)