Fix tests.
[dcpomatic.git] / test / uploader_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/uploader.h"
21 #include <boost/bind.hpp>
22 #include <boost/test/unit_test.hpp>
23 #include <fstream>
24
25 using std::vector;
26 using std::make_pair;
27 using std::pair;
28 using std::string;
29 using std::ofstream;
30 using boost::shared_ptr;
31 using boost::bind;
32
33 static void
34 set_status (string)
35 {
36
37 }
38
39 static void
40 set_progress (float)
41 {
42
43 }
44
45 class TestUploader : public Uploader
46 {
47 public:
48         TestUploader ()
49                 : Uploader (bind (&set_status, _1), bind (&set_progress, _1))
50         {
51                 _directories.push_back ("uploader");
52                 _directories.push_back ("uploader/c");
53                 _directories.push_back ("uploader/b");
54                 _directories.push_back ("uploader/b/e");
55                 _directories.push_back ("uploader/a");
56                 _next_directory = _directories.begin ();
57
58                 _files.push_back (make_pair ("test/data/uploader/b/e/f", "uploader/b/e/f"));
59                 _files.push_back (make_pair ("test/data/uploader/a/d", "uploader/a/d"));
60                 _next_file = _files.begin ();
61         }
62
63 protected:
64         void create_directory (boost::filesystem::path directory) {
65                 std::cout << directory << "\n";
66                 BOOST_CHECK (directory == *_next_directory);
67                 ++_next_directory;
68         }
69
70         void upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t &, boost::uintmax_t) {
71                 BOOST_CHECK (make_pair (from, to) == *_next_file);
72                 ++_next_file;
73         }
74
75 private:
76         vector<boost::filesystem::path> _directories;
77         vector<boost::filesystem::path>::iterator _next_directory;
78         vector<std::pair<boost::filesystem::path, boost::filesystem::path> > _files;
79         vector<std::pair<boost::filesystem::path, boost::filesystem::path> >::iterator _next_file;
80 };
81
82 BOOST_AUTO_TEST_CASE (uploader_test)
83 {
84         boost::filesystem::remove_all ("test/data/uploader");
85         boost::filesystem::create_directories ("test/data/uploader/a");
86         boost::filesystem::create_directories ("test/data/uploader/b");
87         boost::filesystem::create_directories ("test/data/uploader/c");
88         boost::filesystem::create_directories ("test/data/uploader/b/e");
89
90         ofstream f ("test/data/uploader/a/d");
91         f.close ();
92
93         ofstream g ("test/data/uploader/b/e/f");
94         g.close ();
95
96         TestUploader uploader;
97         uploader.upload ("test/data/uploader");
98 }