Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit caf116b

Browse files
committed
Write test cases to stdout and split into separate Rake tasks
Allow users to redirect output wherever they like rather than hard coding a file name to use.
1 parent 5096467 commit caf116b

2 files changed

Lines changed: 38 additions & 43 deletions

File tree

Rakefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ end
3030

3131
require "./lib/cipherstash/client/ordered_string_test_generator"
3232

33-
desc "Generate test cases for orderable strings to be used by other CipherStash clients"
34-
task :generate_ordered_string_test_cases do
35-
CipherStash::Client::OrderedStringTestGenerator.new.run
33+
desc "Generate test cases for orderise_string to be used by other CipherStash clients"
34+
task :generate_orderise_string_test_cases do
35+
CipherStash::Client::OrderedStringTestGenerator.new.generate_orderise_string_test_cases
36+
end
37+
38+
desc "Generate test cases for string comparison to be used by other CipherStash clients"
39+
task :generate_string_comparison_test_cases do
40+
CipherStash::Client::OrderedStringTestGenerator.new.generate_string_comparison_test_cases
3641
end
3742

3843
require 'yard'

lib/cipherstash/client/ordered_string_test_generator.rb

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,17 @@ class OrderedStringTestGenerator
2727
# beyond what actually gets indexed for ordering but doesn't bloat the test files too much.
2828
MAX_STRING_LENGTH = 200
2929

30-
def run
31-
create_orderise_string_test_cases
32-
create_string_comparison_test_cases
33-
end
34-
35-
private
36-
37-
def random_ascii_string
38-
(0..rand(MAX_STRING_LENGTH - 1)).map { rand(ASCII_CHAR_CODE_MAX + 1).chr }.join
39-
end
40-
41-
def index
42-
id = SecureRandom.uuid
43-
settings = {
44-
"meta" => {
45-
"$indexId" => id,
46-
"$indexName" => "titleSort",
47-
"$prfKey" => SecureRandom.hex(16),
48-
"$prpKey" => SecureRandom.hex(16),
49-
},
50-
"mapping" => {
51-
"kind" => "range",
52-
"field" => "title",
53-
"fieldType"=>"string",
54-
}
55-
}
56-
schema_versions = {:first=>0, :last=>0, :searchable=>true}
57-
58-
CipherStash::Index.generate(id, settings, schema_versions)
59-
end
60-
61-
def write_cases_to_file(filename, test_cases)
62-
File.write(filename, JSON.pretty_generate(test_cases))
63-
puts "Created ./" + filename
64-
end
65-
66-
def create_orderise_string_test_cases
30+
def generate_orderise_string_test_cases
6731
orderise_string_cases = (0..(NUM_TEST_CASES - 1)).map do
6832
str = random_ascii_string
6933
output = index.__send__ :orderise_string, str
7034
{input: str, output: output}
7135
end
7236

73-
write_cases_to_file("orderise_string_test_cases.json", orderise_string_cases)
37+
puts JSON.pretty_generate(orderise_string_cases)
7438
end
7539

76-
def create_string_comparison_test_cases
40+
def generate_string_comparison_test_cases
7741
string_comparison_cases = (0..(NUM_TEST_CASES - 1)).map do
7842
str_a = random_ascii_string
7943
terms_a = index.__send__ :orderise_string, str_a
@@ -93,7 +57,33 @@ def create_string_comparison_test_cases
9357
{input: [str_a, str_b], output: output}
9458
end
9559

96-
write_cases_to_file("string_comparison_test_cases.json", string_comparison_cases)
60+
puts JSON.pretty_generate(string_comparison_cases)
61+
end
62+
63+
private
64+
65+
def random_ascii_string
66+
(0..rand(MAX_STRING_LENGTH - 1)).map { rand(ASCII_CHAR_CODE_MAX + 1).chr }.join
67+
end
68+
69+
def index
70+
id = SecureRandom.uuid
71+
settings = {
72+
"meta" => {
73+
"$indexId" => id,
74+
"$indexName" => "titleSort",
75+
"$prfKey" => SecureRandom.hex(16),
76+
"$prpKey" => SecureRandom.hex(16),
77+
},
78+
"mapping" => {
79+
"kind" => "range",
80+
"field" => "title",
81+
"fieldType"=>"string",
82+
}
83+
}
84+
schema_versions = {:first=>0, :last=>0, :searchable=>true}
85+
86+
CipherStash::Index.generate(id, settings, schema_versions)
9787
end
9888
end
9989
end

0 commit comments

Comments
 (0)