aboutsummaryrefslogtreecommitdiffstats
path: root/spec/stores_spec.moon
blob: 4ba838e1c7e3d75c79de126e39e0fbe229308e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import toseq, toseq2 from require 'spec.test_util'

test_store = (ts) ->
  randomize false

  it "starts out empty", ->
    assert.are.same {}, toseq ts\list_fileders_in!
    assert.are.same {}, toseq ts\list_all_fileders!

  it "can't create fileders without missing parents", ->
    assert.has_error ->
      ts\create_fileder '/hello', 'world'

  it "can create root fileders", ->
    assert.are.same '/hello', ts\create_fileder '', 'hello'
    assert.are.same {'/hello'}, toseq ts\list_all_fileders!

  it "can create and list child fileders recursively", ->
    assert.are.same '/hello/world',
                    ts\create_fileder '/hello', 'world'
    assert.are.same '/hello/world/again',
                    ts\create_fileder '/hello/world', 'again'

    assert.are.same {'/hello', '/hello/world', '/hello/world/again'},
                    toseq ts\list_all_fileders!

  it "can list immediate children", ->
    assert.are.same {"/hello"}, toseq ts\list_fileders_in!
    assert.are.same {"/hello/world"}, toseq ts\list_fileders_in "/hello"
    assert.are.same {"/hello/world/again"}, toseq ts\list_fileders_in "/hello/world"

  describe "can create and list facets", ->
    ts\create_facet '/hello', 'name', 'alpha', 'hello'
    ts\create_facet '/hello/world', 'name', 'alpha', 'world'
    ts\create_facet '/hello/world', '', 'text/markdown', '# Helau World!'

    it "but can't create facet for nonexistant fileders", ->
      assert.has_error -> ts\create_facet '/hello/orldw', 'name', 'alpha', 'foo'

    it "but can't create facet without value", ->
      assert.has_error -> ts\create_facet '/hello/world', 'other', 'alpha', nil

    it "but can't create facet for duplicate keys", ->
      assert.has_error -> ts\create_facet '/hello/world', 'name', 'alpha', 'foo'

    assert.are.same {{'name', 'alpha'}}, toseq2 ts\list_facets '/hello'
    assert.are.same {{'', 'text/markdown'}, {'name', 'alpha'}},
                    toseq2 ts\list_facets '/hello/world'

  describe "can get indexes", ->
    hello_index = {
      path: '/hello'
      children: {
        '/hello/world'
      }
      facets: {
        { name: 'name', type: 'alpha' }
      }
    }

    it "for a single level", ->
      assert.are.same hello_index, ts\get_index '/hello'


    root_index = {
      path: ''
      children: {
        hello_index
      }
      facets: {}
    }
    it "for a limited number of levels", ->
      assert.are.same root_index, ts\get_index '', 2

    it "recursively", ->
      hello_index.children[1] = {
        path: '/hello/world'
        children: {
          {
            path: '/hello/world/again'
            children: {}
            facets: {}
          }
        }
        facets: {
          { name: '', type: 'text/markdown' }
          { name: 'name', type: 'alpha' }
        }
      }

      assert.are.same root_index, ts\get_index '', -1

  it "can load facets", ->
    assert.are.equal 'hello', ts\load_facet '/hello', 'name', 'alpha'
    assert.are.equal 'world', ts\load_facet '/hello/world', 'name', 'alpha'
    assert.are.equal '# Helau World!', ts\load_facet '/hello/world', '', 'text/markdown'
    assert.is_falsy ts\load_facet '/hello', 'nonexistant', 'facet'

  it "can rename facets", ->
    ts\rename_facet '/hello/world', 'name', 'alpha', 'gnome'
    assert.are.same {{'', 'text/markdown'}, {'gnome', 'alpha'}},
                    toseq2 ts\list_facets '/hello/world'
    assert.are.equal 'world', ts\load_facet '/hello/world', 'gnome', 'alpha'

  it "can update facets", ->
    ts\update_facet '/hello/world', '', 'text/markdown', '# Hello World!'
    assert.are.same {{'', 'text/markdown'}, {'gnome', 'alpha'}},
                    toseq2 ts\list_facets '/hello/world'
    assert.are.equal '# Hello World!', ts\load_facet '/hello/world', '', 'text/markdown'

  it "can remove facets", ->
    ts\remove_facet '/hello/world', 'gnome', 'alpha'
    assert.are.same {{'', 'text/markdown'}}, toseq2 ts\list_facets '/hello/world'

    assert.has_error -> ts\remove_facet '/hello/world', 'gnome', 'alpha'

  it "can delete fileders", ->
    ts\remove_fileder '/hello/world'
    assert.is_falsy ts\load_facet '/hello/world', 'gnome', 'alpha'
    assert.are.same {'/hello'}, toseq ts\list_all_fileders!

    ts\remove_fileder '/hello'
    assert.are.same {}, toseq ts\list_all_fileders!

  it "can be closed", ->
    ts\close!

describe "SQL store", ->
  import SQLStore from require 'mmm.mmmfs.stores.sql'

  test_store SQLStore memory: true

describe "FS store", ->
  import FSStore from require 'mmm.mmmfs.stores.fs'

  lfs = require 'lfs'

  root = os.tmpname!

  setup ->
    assert os.remove root
    assert lfs.mkdir root

  test_store FSStore :root

  teardown ->
    assert lfs.rmdir root