1import os
2import subprocess
3from includes import *
4from common import *
5from RLTest import Env
6
7
8REDISEARCH_CACHE_DIR = '/tmp/'
9BASE_RDBS_URL = 'https://s3.amazonaws.com/redismodules/redisearch-enterprise/rdbs/'
10
11RDBS = [
12    'redisearch_1.2.0.rdb',
13    'redisearch_1.4.0.rdb',
14    'redisearch_1.4.6.rdb',
15    'redisearch_1.4.11.rdb',
16    'redisearch_1.6.13.rdb',
17    'redisearch_1.6.13_with_synonyms.rdb',
18    'redisearch_1.8.1.rdb',
19    'redisearch_2.0.9.rdb'
20]
21
22def downloadFiles():
23    if not os.path.exists(REDISEARCH_CACHE_DIR):
24        os.makedirs(REDISEARCH_CACHE_DIR)
25    for f in RDBS:
26        path = os.path.join(REDISEARCH_CACHE_DIR, f)
27        if not os.path.exists(path):
28            subprocess.call(['wget', '-q', BASE_RDBS_URL + f, '-O', path])
29        if not os.path.exists(path):
30            return False
31    return True
32
33@unstable
34def testRDBCompatibility(env):
35    # temp skip for out-of-index
36
37    env = Env(moduleArgs='UPGRADE_INDEX idx; PREFIX 1 tt; LANGUAGE french; LANGUAGE_FIELD MyLang; SCORE 0.5; SCORE_FIELD MyScore; PAYLOAD_FIELD MyPayload; UPGRADE_INDEX idx1')
38    # env = Env(moduleArgs=['UPGRADE_INDEX idx', 'PREFIX 1 tt', 'LANGUAGE french', 'LANGUAGE_FIELD MyLang', 'SCORE 0.5', 'SCORE_FIELD MyScore', 'PAYLOAD_FIELD MyPayload', 'UPGRADE_INDEX idx1'])
39    # env = Env(moduleArgs=['UPGRADE_INDEX idx; PREFIX 1 tt; LANGUAGE french', 'LANGUAGE_FIELD MyLang', 'SCORE 0.5', 'SCORE_FIELD MyScore', 'PAYLOAD_FIELD MyPayload', 'UPGRADE_INDEX idx1'])
40
41    env.skipOnCluster()
42    skipOnExistingEnv(env)
43    dbFileName = env.cmd('config', 'get', 'dbfilename')[1]
44    dbDir = env.cmd('config', 'get', 'dir')[1]
45    rdbFilePath = os.path.join(dbDir, dbFileName)
46    if not downloadFiles():
47        if os.environ.get('CI'):
48            env.assertTrue(False)  ## we could not download rdbs and we are running on CI, let fail the test
49        else:
50            env.skip()
51            return
52
53    for fileName in RDBS:
54        env.stop()
55        filePath = os.path.join(REDISEARCH_CACHE_DIR, fileName)
56        try:
57            os.unlink(rdbFilePath)
58        except OSError:
59            pass
60        os.symlink(filePath, rdbFilePath)
61        env.start()
62        waitForIndex(env, 'idx')
63        env.expect('FT.SEARCH idx * LIMIT 0 0').equal([1000])
64        env.expect('DBSIZE').equal(1000)
65        res = env.cmd('FT.INFO idx')
66        res = {res[i]: res[i + 1] for i in range(0, len(res), 2)}
67        env.assertEqual(res['index_definition'], ['key_type', 'HASH', 'prefixes', ['tt'], 'default_language', 'french', 'language_field', 'MyLang', 'default_score', '0.5', 'score_field', 'MyScore', 'payload_field', 'MyPayload'])
68        env.assertEqual(res['num_docs'], '1000')
69        env.expect('FT.SEARCH', 'idx', 'Short', 'LIMIT', '0', '0').equal([943])
70        if fileName == 'redisearch_1.6.13_with_synonyms.rdb':
71            res = env.cmd('FT.SYNDUMP idx')
72            res = {res[i]: res[i + 1] for i in range(0, len(res), 2)}
73            env.assertEqual(res, {'term2': ['0'], 'term1': ['0']})
74        env.cmd('flushall')
75        env.assertTrue(env.checkExitCode())
76
77if __name__ == "__main__":
78    if not downloadFiles():
79        raise Exception("Couldn't download RDB files")
80    print("RDB Files ready for testing!")
81