1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28
29import os, struct
30from suite_subprocess import suite_subprocess
31import wiredtiger, wttest
32
33# test_util09.py
34#    Utilities: wt loadtext
35class test_util09(wttest.WiredTigerTestCase, suite_subprocess):
36    tablename = 'test_util09.a'
37    nentries = 1000
38    session_params = 'key_format=S,value_format=S'
39
40    def populate_file(self, filename, low, high):
41        """
42        Insert some simple key / value lines into the file
43        """
44        keys = {}
45        with open("loadtext.in", "w") as f:
46            for i in range(low, high):
47                key = str(i) + str(i)
48                val = key + key + key
49                f.write(key + '\n')
50                f.write(val + '\n')
51                keys[key] = val
52        #print 'Populated ' + str(len(keys))
53        return keys
54
55    def check_keys(self, tablename, keys):
56        """
57        Check that all the values in the table match the saved dictionary.
58        Values in the dictionary are removed as a side effect.
59        """
60        cursor = self.session.open_cursor('table:' + tablename, None, None)
61        for key, val in cursor:
62            self.assertEqual(keys[key], val)
63            del keys[key]
64        cursor.close()
65        self.assertEqual(len(keys), 0)
66
67    def test_loadtext_empty(self):
68        """
69        Test loadtext in a 'wt' process, using an empty table
70        """
71        self.session.create('table:' + self.tablename, self.session_params)
72        keys = self.populate_file("loadtext.in", 0, 0)
73        self.runWt(["loadtext", "-f", "loadtext.in", "table:" + self.tablename])
74        self.check_keys(self.tablename, keys)
75
76    def test_loadtext_empty_stdin(self):
77        """
78        Test loadtext in a 'wt' process using stdin, using an empty table
79        """
80        self.session.create('table:' + self.tablename, self.session_params)
81        keys = self.populate_file("loadtext.in", 0, 0)
82        self.runWt(["loadtext", "table:" + self.tablename], infilename="loadtext.in")
83        self.check_keys(self.tablename, keys)
84
85    def test_loadtext_populated(self):
86        """
87        Test loadtext in a 'wt' process, creating entries in a table
88        """
89        self.session.create('table:' + self.tablename, self.session_params)
90        keys = self.populate_file("loadtext.in", 1010, 1220)
91        self.runWt(["loadtext", "-f", "loadtext.in", "table:" + self.tablename])
92        self.check_keys(self.tablename, keys)
93
94    def test_loadtext_populated_stdin(self):
95        """
96        Test loadtext in a 'wt' process using stding, creating entries in a table
97        """
98        self.session.create('table:' + self.tablename, self.session_params)
99        keys = self.populate_file("loadtext.in", 200, 300)
100        self.runWt(["loadtext", "table:" + self.tablename], infilename="loadtext.in")
101        self.check_keys(self.tablename, keys)
102
103if __name__ == '__main__':
104    wttest.run()
105