Fix for a significant error in naming conventions, with tests.
This commit is contained in:
		
							parent
							
								
									1860790027
								
							
						
					
					
						commit
						27044d6405
					
				
					 2 changed files with 93 additions and 21 deletions
				
			
		| 
						 | 
					@ -261,7 +261,7 @@
 | 
				
			||||||
      " "
 | 
					      " "
 | 
				
			||||||
      (map
 | 
					      (map
 | 
				
			||||||
        #(apply str (cons (Character/toUpperCase (first %)) (rest %)))
 | 
					        #(apply str (cons (Character/toUpperCase (first %)) (rest %)))
 | 
				
			||||||
        (s/split s #"[ \t\r\n]+")))
 | 
					        (s/split s #"[^a-zA-Z0-9]+")))
 | 
				
			||||||
    s))
 | 
					    s))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -272,7 +272,8 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn safe-name
 | 
					(defn safe-name
 | 
				
			||||||
  "Return a safe name for the object `o`, given the specified `convention`.
 | 
					  "Return a safe name for the object `o`, given the specified `convention`.
 | 
				
			||||||
  `o` is expected to be either a string or an element."
 | 
					  `o` is expected to be either a string or an element. Recognised values for
 | 
				
			||||||
 | 
					  `convention` are: #{:c :c-sharp :java :sql}"
 | 
				
			||||||
  ([o]
 | 
					  ([o]
 | 
				
			||||||
   (cond
 | 
					   (cond
 | 
				
			||||||
     (element? o)
 | 
					     (element? o)
 | 
				
			||||||
| 
						 | 
					@ -285,15 +286,20 @@
 | 
				
			||||||
     ;; if it's an entity, it's permitted to have a different table name
 | 
					     ;; if it's an entity, it's permitted to have a different table name
 | 
				
			||||||
     ;; from its entity name. This isn't actually likely, but...
 | 
					     ;; from its entity name. This isn't actually likely, but...
 | 
				
			||||||
     (safe-name (or (-> o :attrs :table) (-> o :attrs :name)) :sql)
 | 
					     (safe-name (or (-> o :attrs :table) (-> o :attrs :name)) :sql)
 | 
				
			||||||
 | 
					     (and (property? o) (= convention :sql))
 | 
				
			||||||
 | 
					     ;; if it's a property, it's entitle to have a different column name
 | 
				
			||||||
 | 
					     ;; from its property name.
 | 
				
			||||||
 | 
					     (safe-name (or (-> o :attrs :column) (-> o :attrs :name)) :sql)
 | 
				
			||||||
     (element? o)
 | 
					     (element? o)
 | 
				
			||||||
     (safe-name (:name (:attrs o)) convention)
 | 
					     (safe-name (:name (:attrs o)) convention)
 | 
				
			||||||
     true
 | 
					     true
 | 
				
			||||||
    (let [string (str o)]
 | 
					     (let [string (str o)
 | 
				
			||||||
 | 
					           capitalised (capitalise string)]
 | 
				
			||||||
       (case convention
 | 
					       (case convention
 | 
				
			||||||
         (:sql :c) (s/replace string #"[^a-zA-Z0-9_]" "_")
 | 
					         (:sql :c) (s/replace string #"[^a-zA-Z0-9_]" "_")
 | 
				
			||||||
        :c-sharp (s/replace (capitalise string) #"[^a-zA-Z0-9]" "")
 | 
					         :c-sharp (s/replace capitalised #"[^a-zA-Z0-9]" "")
 | 
				
			||||||
         :java (let
 | 
					         :java (let
 | 
				
			||||||
                [camel (s/replace (capitalise string) #"[^a-zA-Z0-9]" "")]
 | 
					                 [camel (s/replace capitalised #"[^a-zA-Z0-9]" "")]
 | 
				
			||||||
                 (apply str (cons (Character/toLowerCase (first camel)) (rest camel))))
 | 
					                 (apply str (cons (Character/toLowerCase (first camel)) (rest camel))))
 | 
				
			||||||
         (safe-name string))))))
 | 
					         (safe-name string))))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -506,3 +506,69 @@
 | 
				
			||||||
      (is (= false (unique-link? e1 e2)) "There are two logical links, three link properties, between e1 and e2")
 | 
					      (is (= false (unique-link? e1 e2)) "There are two logical links, three link properties, between e1 and e2")
 | 
				
			||||||
      (is (= true (unique-link? e2 e3)) "There is only one link between e2 and e3"))))
 | 
					      (is (= true (unique-link? e2 e3)) "There is only one link between e2 and e3"))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(deftest capitalise-tests
 | 
				
			||||||
 | 
					  (testing "capitalise"
 | 
				
			||||||
 | 
					    (is (= (capitalise "the quick brown fox jumped over the lazy dog") "The Quick Brown Fox Jumped Over The Lazy Dog"))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(deftest safe-name-tests
 | 
				
			||||||
 | 
					  (testing "safe-name"
 | 
				
			||||||
 | 
					     (let [e1 {:tag :entity
 | 
				
			||||||
 | 
					              :attrs {:name "canvass-teams" :table "team"}
 | 
				
			||||||
 | 
					              :content [{:tag :key
 | 
				
			||||||
 | 
					                         :content [{:tag :property
 | 
				
			||||||
 | 
					                                    :attrs {:name "id" :type "integer" :distinct "system"}}]}
 | 
				
			||||||
 | 
					                        {:tag :property
 | 
				
			||||||
 | 
					                         :attrs {:name "members" :type "link" :entity "canvassers"}}
 | 
				
			||||||
 | 
					                        {:tag :property
 | 
				
			||||||
 | 
					                         :attrs {:name "organisers" :type "link" :entity "canvassers"}}]}
 | 
				
			||||||
 | 
					           p1 {:tag :property
 | 
				
			||||||
 | 
					                                    :attrs {:name "id" :type "integer" :distinct "system"}}
 | 
				
			||||||
 | 
					           p2 {:tag :property
 | 
				
			||||||
 | 
					               :attrs {:name "with_underscore" :column "with-hyphen" :type "integer"}}]
 | 
				
			||||||
 | 
					       (is
 | 
				
			||||||
 | 
					         (= (safe-name "the quick brown fox jumped over the lazy dog")
 | 
				
			||||||
 | 
					            "thequickbrownfoxjumpedoverthelazydog")
 | 
				
			||||||
 | 
					         "absent a convention, spaces are suppressed")
 | 
				
			||||||
 | 
					       (is
 | 
				
			||||||
 | 
					         (= (safe-name "the quick brown fox jumped over the lazy dog" :c)
 | 
				
			||||||
 | 
					            "the_quick_brown_fox_jumped_over_the_lazy_dog")
 | 
				
			||||||
 | 
					         "in :c convention, spaces are replaced with underscores")
 | 
				
			||||||
 | 
					       (is
 | 
				
			||||||
 | 
					         (= (safe-name "the quick brown fox jumped over the lazy dog" :c-sharp)
 | 
				
			||||||
 | 
					            "TheQuickBrownFoxJumpedOverTheLazyDog")
 | 
				
			||||||
 | 
					         "in :c-sharp convention spaces are suppressed and all words camel cased")
 | 
				
			||||||
 | 
					       (is
 | 
				
			||||||
 | 
					         (= (safe-name "the quick brown fox jumped over the lazy dog" :java)
 | 
				
			||||||
 | 
					            "theQuickBrownFoxJumpedOverTheLazyDog")
 | 
				
			||||||
 | 
					         "in :java convention spaces are suppressed and embedded words camel cased")
 | 
				
			||||||
 | 
					       (is
 | 
				
			||||||
 | 
					         (= (safe-name "the quick brown fox jumped over the lazy dog" :sql)
 | 
				
			||||||
 | 
					            "the_quick_brown_fox_jumped_over_the_lazy_dog")
 | 
				
			||||||
 | 
					         "in :sql convention, spaces are replaced with underscores")
 | 
				
			||||||
 | 
					       (is (= (safe-name e1) "canvass-teams"))
 | 
				
			||||||
 | 
					       (is (= (safe-name e1 :c) "canvass_teams")
 | 
				
			||||||
 | 
					           "In :c convention, hyphen is replaced by underscore")
 | 
				
			||||||
 | 
					       (is (= (safe-name e1 :c-sharp) "CanvassTeams")
 | 
				
			||||||
 | 
					           "In :c-sharp convention, hyphen is suppressed and words capitalised")
 | 
				
			||||||
 | 
					       (is (= (safe-name e1 :java) "canvassTeams")
 | 
				
			||||||
 | 
					           "In :java convention, hyphen is suppressed and embedded words capitalised")
 | 
				
			||||||
 | 
					       (is (= (safe-name e1 :sql) "team")
 | 
				
			||||||
 | 
					           "In :sql convention, the :table attribute is preferred")
 | 
				
			||||||
 | 
					       (is (= (safe-name p1) "id"))
 | 
				
			||||||
 | 
					       (is (= (safe-name p1 :c) "id"))
 | 
				
			||||||
 | 
					       (is (= (safe-name p1 :c-sharp) "Id"))
 | 
				
			||||||
 | 
					       (is (= (safe-name p1 :java) "id"))
 | 
				
			||||||
 | 
					       (is (= (safe-name p1 :sql) "id"))
 | 
				
			||||||
 | 
					       (is (= (safe-name p2) "withunderscore")
 | 
				
			||||||
 | 
					           "Absent a convention, underscore is not considered safe")
 | 
				
			||||||
 | 
					       (is (= (safe-name p2 :c) "with_underscore")
 | 
				
			||||||
 | 
					           "In :c convention, underscore is considered safe")
 | 
				
			||||||
 | 
					       (is (= (safe-name p2 :c-sharp) "WithUnderscore")
 | 
				
			||||||
 | 
					           "In :c-sharp convention, initial letters are capialised and underscore is suppressed")
 | 
				
			||||||
 | 
					       (is (= (safe-name p2 :java) "withUnderscore")
 | 
				
			||||||
 | 
					           "In :java convention, underscore is suppressed and embedded words capitalised")
 | 
				
			||||||
 | 
					       (is (= (safe-name p2 :sql) "with_hyphen")
 | 
				
			||||||
 | 
					           "In :sql convention, the column-name variant is preferred, and hyphens replaced with underscores"))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue