Added test to the simple db lib, and depending on config, exclude or not json or memory database.

This commit is contained in:
2017-02-16 14:53:55 +01:00
parent ad3fe2fc7b
commit ae7aeaea30
14 changed files with 543 additions and 241 deletions

View File

@@ -14,6 +14,12 @@
<version type="compiler" max="17.0.0.0"/>
</condition>
</file_rule>
<file_rule>
<exclude>/basic_memory_database\.e$</exclude>
<condition>
<concurrency value="scoop"/>
</condition>
</file_rule>
</cluster>
</target>
</system>

View File

@@ -14,6 +14,12 @@
<version type="compiler" max="17.0.0.0"/>
</condition>
</file_rule>
<file_rule>
<exclude>/basic_memory_database\.e$</exclude>
<condition>
<concurrency value="scoop"/>
</condition>
</file_rule>
</cluster>
</target>
</system>

View File

@@ -38,6 +38,12 @@ feature -- Access
ensure
has_not_item: not has (a_entry_type, a_id)
end
wipe_out
-- Remove all items, and delete the database
deferred
end
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -0,0 +1,177 @@
note
description: "Summary description for {BASIC_FS_DATABASE}."
date: "$Date$"
revision: "$Revision$"
deferred class
BASIC_FS_DATABASE
inherit
BASIC_DATABASE
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
ensure_directory_exists (a_location)
end
feature -- Access
location: PATH
feature {NONE} -- Access
default_extension: detachable STRING_32
-- Default file extension, if any.
deferred
end
feature -- Access
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
ext: like default_extension
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
ext := default_extension
across
d.entries as ic
loop
if
ext = Void or else
attached ic.item.extension as e and then e.is_case_insensitive_equal (ext) then
Result := Result + 1
end
end
end
end
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
do
Result := item_from_location (a_entry_type, entry_path (a_entry_type, a_id))
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
p: PATH
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
p := entry_path (a_entry_type, l_id)
ensure_directory_exists (p.parent)
save_item_to_location (a_entry, p)
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
wipe_out
local
d: DIRECTORY
do
create d.make_with_path (location)
if d.exists then
d.recursive_delete
end
end
feature {NONE} -- Implementation
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
deferred
end
save_item_to_location (a_entry: detachable ANY; p: PATH)
deferred
end
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("json")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -11,7 +11,10 @@ class
BASIC_JSON_FS_DATABASE
inherit
BASIC_DATABASE
BASIC_FS_DATABASE
redefine
make
end
create
make
@@ -19,54 +22,28 @@ create
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
Precursor (a_location)
create serialization
ensure_directory_exists (a_location)
end
feature -- Access serialization
serialization: JSON_SERIALIZATION
feature -- Access
feature {NONE} -- Access
location: PATH
default_extension: STRING_32 = "json"
-- Default file extension, if any.
feature -- Access
feature {NONE} -- Implementation
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
across
d.entries as ic
loop
if attached ic.item.extension as e and then e.is_case_insensitive_equal ("json") then
Result := Result + 1
end
end
end
end
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
local
f: RAW_FILE
s: STRING
do
create f.make_with_path (entry_path (a_entry_type, a_id))
create f.make_with_path (p)
if f.exists then
create s.make (f.count)
f.open_read
@@ -82,95 +59,16 @@ feature -- Access
end
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
save_item_to_location (a_entry: detachable ANY; p: PATH)
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
create f.make_with_path (entry_path (a_entry_type, l_id))
ensure_directory_exists (f.path.parent)
create f.make_with_path (p)
f.open_write
f.put_string (serialization.to_json (a_entry).representation)
f.close
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
feature {NONE} -- Implementation
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("json")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -2,7 +2,7 @@ note
description: "[
Basic database for simple example based on memory.
WARNING: for now, not concurrent compliant.
WARNING: for now, this is a database per instance, this is not shared memory inside the same process.
]"
date: "$Date$"
revision: "$Revision$"
@@ -19,6 +19,8 @@ create
feature {NONE} -- Initialization
make
local
b: SED_MEMORY_READER_WRITER
do
create collections.make (0)
end
@@ -74,6 +76,11 @@ feature -- Access
end
end
wipe_out
do
collections.wipe_out
end
feature {NONE} -- Implementation
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
@@ -81,21 +88,23 @@ feature {NONE} -- Implementation
i: INTEGER
f: RAW_FILE
s: STRING
tb: detachable STRING_TABLE [detachable ANY]
do
if attached collections.item (a_entry_type) as tb then
tb := collections.item (a_entry_type)
if tb /= Void then
i := tb.count
else
i := 0
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
not tb.has_key (Result)
loop
i := i + 1
Result := i.out
end
else
Result := "1"
end
end
note

View File

@@ -11,7 +11,10 @@ class
BASIC_SED_FS_DATABASE
inherit
BASIC_DATABASE
BASIC_FS_DATABASE
redefine
make
end
create
make
@@ -19,53 +22,29 @@ create
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
Precursor (a_location)
create serialization
ensure_directory_exists (a_location)
end
feature -- Access
location: PATH
serialization: SED_STORABLE_FACILITIES
feature -- Access
feature {NONE} -- Access
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
across
d.entries as ic
loop
if attached ic.item.extension as e and then e.is_case_insensitive_equal ("sed") then
Result := Result + 1
end
end
end
end
default_extension: STRING_32 = "sed"
-- Default file extension, if any.
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
feature {NONE} -- Implementation
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
local
f: RAW_FILE
s: STRING
l_reader: SED_MEDIUM_READER_WRITER
s: STRING
do
create f.make_with_path (entry_path (a_entry_type, a_id))
create f.make_with_path (p)
if f.exists then
create s.make (f.count)
f.open_read
@@ -75,19 +54,12 @@ feature -- Access
end
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
save_item_to_location (a_entry: detachable ANY; p: PATH)
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
l_writer: SED_MEDIUM_READER_WRITER
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
create f.make_with_path (entry_path (a_entry_type, l_id))
ensure_directory_exists (f.path.parent)
create f.make_with_path (p)
f.open_write
if a_entry /= Void then
create l_writer.make_for_writing (f)
@@ -96,78 +68,6 @@ feature -- Access
f.close
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
feature {NONE} -- Implementation
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("sed")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -0,0 +1,38 @@
note
description: "Summary description for {A}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
A
create
make_with_name
feature {NONE} -- Initialization
make_with_name (n: STRING)
do
name := n
create items.make (0)
end
feature -- Access
name: STRING
count: INTEGER
items: ARRAYED_LIST [B]
feature -- Element change
extend (b: B)
do
items.extend (b)
count := items.count
end
end

View File

@@ -0,0 +1,30 @@
note
description: "Summary description for {B}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
B
create
make
feature
make (k: STRING; b: BOOLEAN; v: STRING)
do
key := k
value := v
state := b
end
feature -- Access
key: STRING
value: STRING
state: BOOLEAN
end

View File

@@ -0,0 +1,53 @@
note
description: "Summary description for {TEST_BASIC_DB_I}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
TEST_BASIC_DB_I
inherit
EQA_TEST_SET
feature -- Factory
new_object: ANY
local
a: A
b: B
do
create a.make_with_name ("Test")
create b.make ("foo", True, "test-foo")
a.extend (b)
create b.make ("bar", True, "test-bar")
a.extend (b)
Result := a
end
same_object (a,b: like new_object): BOOLEAN
local
b1,b2: B
do
if attached {A} a as l_a1 and attached {A} b as l_a2 then
Result := l_a1.name.same_string (l_a2.name) and
l_a1.count = l_a2.count
if Result then
from
l_a1.items.start
l_a2.items.start
until
not Result or l_a1.items.after or l_a2.items.after
loop
b1 := l_a1.items.item
b2 := l_a1.items.item
l_a1.items.forth
l_a2.items.forth
end
Result := Result and l_a1.items.after = l_a2.items.after
end
end
end
end

View File

@@ -0,0 +1,47 @@
note
description: "Summary description for {TEST_JSON_DB}."
date: "$Date$"
revision: "$Revision$"
class
TEST_JSON_DB
inherit
EQA_TEST_SET
TEST_BASIC_DB_I
feature -- Test routines
test_json_db
-- New test routine
local
db: BASIC_JSON_FS_DATABASE
obj: like new_object
cl: CELL [detachable READABLE_STRING_GENERAL]
l_type: TYPE [detachable ANY]
l_id: detachable READABLE_STRING_GENERAL
do
create db.make (create {PATH}.make_from_string ("json-db"))
db.serialization.set_pretty_printing
db.serialization.register_default (create{JSON_REFLECTOR_SERIALIZATION})
obj := new_object
l_type := obj.generating_type
create cl.put ("0")
db.save (l_type, obj, cl)
l_id := cl.item
assert ("new id", l_id /= Void and then not l_id.is_whitespace)
if l_id /= Void then
assert ("has previous entry", db.has (l_type, l_id))
if attached db.item (l_type, l_id) as l_stored_obj then
assert ("same object", same_object (l_stored_obj, obj))
else
assert ("found", False)
end
end
db.wipe_out
end
end

View File

@@ -0,0 +1,47 @@
note
description: "Summary description for {TEST_MEMORY_DB}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
TEST_MEMORY_DB
inherit
EQA_TEST_SET
TEST_BASIC_DB_I
feature -- Test routines
test_memory_db
-- New test routine
local
db: BASIC_MEMORY_DATABASE
obj: like new_object
cl: CELL [detachable READABLE_STRING_GENERAL]
l_type: TYPE [detachable ANY]
l_id: detachable READABLE_STRING_GENERAL
do
create db.make
obj := new_object
l_type := obj.generating_type
create cl.put ("0")
db.save (l_type, obj, cl)
l_id := cl.item
assert ("new id", l_id /= Void and then not l_id.is_whitespace)
if l_id /= Void then
assert ("has previous entry", db.has (l_type, l_id))
if attached db.item (l_type, l_id) as l_stored_obj then
assert ("same object", l_stored_obj.is_deep_equal (obj))
else
assert ("found", False)
end
end
db.wipe_out
end
end

View File

@@ -0,0 +1,49 @@
note
description: "[
Eiffel tests that can be executed by testing tool.
]"
author: "EiffelStudio test wizard"
date: "$Date$"
revision: "$Revision$"
testing: "type/manual"
class
TEST_SED_DB
inherit
EQA_TEST_SET
TEST_BASIC_DB_I
feature -- Test routines
test_sed_db
-- New test routine
local
db: BASIC_SED_FS_DATABASE
obj: like new_object
cl: CELL [detachable READABLE_STRING_GENERAL]
l_type: TYPE [detachable ANY]
l_id: detachable READABLE_STRING_GENERAL
do
create db.make (create {PATH}.make_from_string ("sed-db"))
obj := new_object
l_type := obj.generating_type
create cl.put ("0")
db.save (l_type, obj, cl)
l_id := cl.item
assert ("new id", l_id /= Void and then not l_id.is_whitespace)
if l_id /= Void then
assert ("has previous entry", db.has (l_type, l_id))
if attached db.item (l_type, l_id) as l_stored_obj then
assert ("same object", l_stored_obj.is_deep_equal (obj))
else
assert ("found", False)
end
end
db.wipe_out
end
end

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="test_simple_db" uuid="2A955961-2B7F-4075-BD05-F75225D8A711">
<target name="test_simple_db">
<file_rule>
<exclude>/\.svn$</exclude>
<exclude>/\.git$</exclude>
<exclude>/EIFGENs$</exclude>
</file_rule>
<root class="ANY" feature="default_create"/>
<option warning="true" void_safety="all">
</option>
<setting name="concurrency" value="scoop"/>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf"/>
<library name="simple_db" location="..\simple_db-safe.ecf" readonly="False"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<cluster name="tests" location=".\" recursive="true">
<file_rule>
<exclude>/.*json.*\.e$</exclude>
<condition>
<version type="compiler" max="17.0.0.0"/>
</condition>
</file_rule>
<file_rule>
<exclude>/.*memory.*\.e$</exclude>
<condition>
<concurrency value="scoop"/>
</condition>
</file_rule>
</cluster>
</target>
<target name="test_simple_db_st" extends="test_simple_db">
<setting name="concurrency" value="none"/>
</target>
</system>