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

Commit f0eb42c

Browse files
committed
Move generate_ordered_string_test_cases task logic into separate class
1 parent 1945212 commit f0eb42c

2 files changed

Lines changed: 101 additions & 60 deletions

File tree

Rakefile

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,68 +28,11 @@ task :release do
2828
sh "git release"
2929
end
3030

31-
require "securerandom"
32-
require "./lib/cipherstash/index"
33-
require "json"
31+
require "./lib/cipherstash/ordered_string_test_generator"
3432

33+
desc "Generate test cases for orderable strings to be used by other CipherStash clients"
3534
task :generate_ordered_string_test_cases do
36-
id = SecureRandom.uuid
37-
settings = {
38-
"meta" => {
39-
"$indexId" => id,
40-
"$indexName" => "titleSort",
41-
"$prfKey" => SecureRandom.hex(16),
42-
"$prpKey" => SecureRandom.hex(16),
43-
},
44-
"mapping" => {
45-
"kind" => "range",
46-
"field" => "title",
47-
"fieldType"=>"string",
48-
}
49-
}
50-
schema_versions = {:first=>0, :last=>0, :searchable=>true}
51-
52-
index = CipherStash::Index.generate(id, settings, schema_versions)
53-
54-
random_ascii_string = -> () do
55-
unicode_char_max = 127
56-
max_string_length = 200
57-
(0..rand(max_string_length -1)).map { rand(unicode_char_max + 1).chr }.join
58-
end
59-
60-
# This number is arbitrary. 100 seems good because GitHub is willing to display
61-
# the output files in diffs in PRs, but will require pulling branches down for
62-
# larger numbers of test cases.
63-
num_test_cases = 100
64-
65-
orderise_string_cases = (0..(num_test_cases - 1)).map do
66-
str = random_ascii_string.call
67-
output = index.__send__ :orderise_string, str
68-
{input: str, output: output}
69-
end
70-
71-
File.write("orderise_string_test_cases.json", JSON.pretty_generate(orderise_string_cases))
72-
73-
string_comparison_cases = (0..(num_test_cases - 1)).map do
74-
str_a = random_ascii_string.call
75-
terms_a = index.__send__ :orderise_string, str_a
76-
77-
str_b = random_ascii_string.call
78-
terms_b = index.__send__ :orderise_string, str_b
79-
80-
output = case terms_a <=> terms_b
81-
when -1
82-
"<"
83-
when 0
84-
"=="
85-
when 1
86-
">"
87-
end
88-
89-
{input: [str_a, str_b], output: output}
90-
end
91-
92-
File.write("string_comparison_test_cases.json", JSON.pretty_generate(string_comparison_cases))
35+
CipherStash::OrderedStringTestGenerator.new.run
9336
end
9437

9538
require 'yard'
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require "securerandom"
2+
require "json"
3+
4+
require_relative "./index"
5+
6+
module CipherStash
7+
# Generates test cases for orderable strings that can be used in automated tests in other
8+
# clients (e.g. StashRS). Test cases are written to JSON files.
9+
#
10+
# Example usage in StashRS:
11+
# https://github.com/cipherstash/cipherstash-rs/blob/00ff66d712e6f36a89acfa731b680a69789647cf/packages/cipherstash-client/src/indexer/mapping_indexer.rs#L537-L570
12+
#
13+
# @private
14+
class OrderedStringTestGenerator
15+
# The number of test cases to generate per test file. This number is arbitrary. 100 seems good
16+
# because GitHub is willing to display the output files in diffs in PRs, but will require
17+
# pulling branches down for larger numbers of test cases.
18+
NUM_TEST_CASES = 100
19+
20+
# Max character code for ASCII characters in randomly generated strings in test data.
21+
ASCII_CHAR_CODE_MAX = 127
22+
23+
# The max length of randomly generated strings in test data. This number is also somewhat
24+
# arbitrary. The first 80 ASCII characters are considered for ordering, so we want some
25+
# strings with a length of at least 80. 200 seems good because this will also test lengths
26+
# beyond what actually gets indexed for ordering but doesn't bloat the test files too much.
27+
MAX_STRING_LENGTH = 200
28+
29+
def run
30+
create_orderise_string_test_cases
31+
create_string_comparison_test_cases
32+
end
33+
34+
private
35+
36+
def random_ascii_string
37+
(0..rand(MAX_STRING_LENGTH - 1)).map { rand(ASCII_CHAR_CODE_MAX + 1).chr }.join
38+
end
39+
40+
def index
41+
id = SecureRandom.uuid
42+
settings = {
43+
"meta" => {
44+
"$indexId" => id,
45+
"$indexName" => "titleSort",
46+
"$prfKey" => SecureRandom.hex(16),
47+
"$prpKey" => SecureRandom.hex(16),
48+
},
49+
"mapping" => {
50+
"kind" => "range",
51+
"field" => "title",
52+
"fieldType"=>"string",
53+
}
54+
}
55+
schema_versions = {:first=>0, :last=>0, :searchable=>true}
56+
57+
CipherStash::Index.generate(id, settings, schema_versions)
58+
end
59+
60+
def write_cases_to_file(filename, test_cases)
61+
File.write(filename, JSON.pretty_generate(test_cases))
62+
puts "Created ./" + filename
63+
end
64+
65+
def create_orderise_string_test_cases
66+
orderise_string_cases = (0..(NUM_TEST_CASES - 1)).map do
67+
str = random_ascii_string
68+
output = index.__send__ :orderise_string, str
69+
{input: str, output: output}
70+
end
71+
72+
write_cases_to_file("orderise_string_test_cases.json", orderise_string_cases)
73+
end
74+
75+
def create_string_comparison_test_cases
76+
string_comparison_cases = (0..(NUM_TEST_CASES - 1)).map do
77+
str_a = random_ascii_string
78+
terms_a = index.__send__ :orderise_string, str_a
79+
80+
str_b = random_ascii_string
81+
terms_b = index.__send__ :orderise_string, str_b
82+
83+
output = case terms_a <=> terms_b
84+
when -1
85+
"<"
86+
when 0
87+
"=="
88+
when 1
89+
">"
90+
end
91+
92+
{input: [str_a, str_b], output: output}
93+
end
94+
95+
write_cases_to_file("string_comparison_test_cases.json", string_comparison_cases)
96+
end
97+
end
98+
end

0 commit comments

Comments
 (0)